1 /* ************************************************************************
2  * Copyright 2014 Advanced Micro Devices, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  * ************************************************************************/
16 
17 #include <stdio.h>
18 #include <fstream>
19 #include <iostream>
20 #include <ios>
21 
22 #include <../functor/include/functor.h>
23 #include <clblas-internal.h>
24 
25 #include <vector>
26 #include <set>
27 
28 
29 
30 // ==================================================
31 // == clblasFunctorCacheBase
32 // ==================================================
33 
34 typedef std::set<clblasFunctorCacheBase*> clblasFunctorCacheSet ;
35 
36 // Provide the set of all existing functor cache
37 //
38 // Remark: Since the set is typically populated by the constructors
39 //         of global objects, we use the "construct on first use"
40 //         idiom, to avoid the infamous "static initialization order fiasco".
41 //         See for example  http://www.parashift.com/c++-faq/static-init-order.html
42 //
43 // Remark: The current implementation is not thread-safe but that should
44 //         be fine since the cache is supposed to be populated at startup
45 //         (assuming that all functor caches are global objects) and
46 //
getFunctorCacheSet()47 static clblasFunctorCacheSet & getFunctorCacheSet()
48 {
49   static clblasFunctorCacheSet * all = new clblasFunctorCacheSet ;
50   return * all ;
51 }
52 
53 //
54 // This function is supposed to be called from clblasTearDown to empty all caches
55 //
cleanFunctorCaches(void)56 extern "C" void cleanFunctorCaches(void)
57 {
58   // Ask each registered cache to clean itself.
59   clblasFunctorCacheSet & all = getFunctorCacheSet() ;
60   for (clblasFunctorCacheSet::iterator it= all.begin(); it!=all.end(); ++it)
61     {
62       clblasFunctorCacheBase * cache = *it ;
63       cache->discardAll() ;
64     }
65 }
66 
clblasFunctorCacheBase()67 clblasFunctorCacheBase::clblasFunctorCacheBase()
68 {
69   //  if ( _cleanFunctorCachesHook == 0 )
70   //   _cleanFunctorCachesHook = cleanFunctorCaches ; // Install the hook to call cleanFunctorCaches
71 
72   clblasFunctorCacheSet & all = getFunctorCacheSet() ;
73   all.insert(this) ;
74 }
75 
~clblasFunctorCacheBase()76 clblasFunctorCacheBase::~clblasFunctorCacheBase()
77 {
78   clblasFunctorCacheSet & all = getFunctorCacheSet() ;
79   all.erase(this) ;
80 }
81