|
| 1 | +S. Chauveau |
| 2 | +CAPS Entreprise |
| 3 | +April 30, 2014 |
| 4 | + |
| 5 | +The Functor concept was introduced in clBLAS to simplify the creation |
| 6 | +of specialized versions for dedicated architectures. |
| 7 | + |
| 8 | +The original system, referred as the 'Solver' system in this document, |
| 9 | +is very centralized and not flexible enough to insert customized kernels. |
| 10 | + |
| 11 | +The Functor |
| 12 | +=========== |
| 13 | + |
| 14 | +A functor is simply a C++ object that provides an implementation of |
| 15 | +a function. In the current case, that function is one of the BLAS calls |
| 16 | +implemented in OpenCL. |
| 17 | + |
| 18 | +The base class of all functors is clblasFunctor |
| 19 | + - see src/library/blas/functor/include/functor.h |
| 20 | + - see src/library/blas/functor/functor.cc |
| 21 | + |
| 22 | +That class does not provide much by itself but it is supposed to be derived |
| 23 | +once for each BLAS function to be implemented. |
| 24 | + |
| 25 | +For instance the clblasSgemmFunctor class will be the base class of all |
| 26 | +functors providing a generic or specific implementation of SGEMM. |
| 27 | + |
| 28 | +A generic functor is one that is applicable to all possible arguments of the |
| 29 | +function it implements. In most cases, there will be at least one generic |
| 30 | +functor that will simply call the existing Solver-based implementation of the |
| 31 | +function. For SGEMM, that is the class clblasSgemmFunctorFallback. |
| 32 | + |
| 33 | +A specific functor is one that is applicable to only a subset of the possible |
| 34 | +arguments of the function it implements. For instance, a SGEMM functor could |
| 35 | +only implement it for matrices of a given block size or only for square |
| 36 | +matrices or only for a specific device architecture (e.g. AMD Hawai) etc |
| 37 | + |
| 38 | +The Functor Selector |
| 39 | +==================== |
| 40 | + |
| 41 | +Multiple generic and specific functors may be available to implement each |
| 42 | +clBLAS call. The selection of the proper functor is delegated to the class |
| 43 | +clblasFunctorSelector whose default implementation typically returns the |
| 44 | +fallback functors. |
| 45 | + |
| 46 | + - see src/library/blas/functor/include/functor_selector.h |
| 47 | + - see src/library/blas/functor/functor_selector.cc |
| 48 | + |
| 49 | +So clblasFunctorSelector provides a large set of virtual selection methods. |
| 50 | +Typically, a method to select a specific functor will be provided for each |
| 51 | +supported BLAS function. Another method may be provided to select a generic |
| 52 | +functor but that is not mandatory. |
| 53 | + |
| 54 | +The default implementation of clblasFunctorSelector is typically that the |
| 55 | +specific selector is redirected to the generic one returning the fallback |
| 56 | +functor (so using the existing Solver-based implementation). |
| 57 | + |
| 58 | + |
| 59 | +The class clblasFunctorSelector is supposed to be derived once for each |
| 60 | +supported architecture (e.g. Hawai, Tahiti, ...) and a single global instance |
| 61 | +of each of those derived classes shall be created. This is important because |
| 62 | +those instances register themselves in a global data structure that is later |
| 63 | +used to find the proper clblasFunctorSelector according to the architecture |
| 64 | +(see clblasFunctorSelector::find() ) |
| 65 | + |
| 66 | + |
| 67 | +Functor Management & Cache |
| 68 | +========================== |
| 69 | + |
| 70 | +Each functor contains a reference counter that, when it reaches zero, causes |
| 71 | +the functor destruction. See the members clblasFunctor::retain() and |
| 72 | +clblasFunctor::release(). |
| 73 | + |
| 74 | +Of course, to be efficient, functors must be reusable between BLAS calls so |
| 75 | +some mechanisms must be implemented to manage the functors. |
| 76 | + |
| 77 | +Some functors, such as the fallback functors, are independent of the |
| 78 | +arguments and of the opencl context & device. Those can typically be |
| 79 | +implemented using a single global instance that will never be destroyed. |
| 80 | + |
| 81 | +Other functors, such as those that manage a cl_program internally, are |
| 82 | +dependent of the opencl context & device and sometimes of some arguments. |
| 83 | +They need to be stored in caches using some information as keys. |
| 84 | + |
| 85 | +In the current implementation, we propose that each functor class shall |
| 86 | +implement its own private cache. Such functors shall not be created directly |
| 87 | +using its constructor but via a dedicated 'provide' function (the name 'provide' |
| 88 | +is not mandatory) that will take care of managing the internal cache. |
| 89 | + |
| 90 | +The template class clblasFunctorCache<F> is provided as a simple |
| 91 | +implementation of a cache of functors of type F. Use of that cache is not a |
| 92 | +mandatory part of the functor design. Another strategies could be to keep a |
| 93 | +single instance of the functor and implement a cache for the cl_program or to |
| 94 | +implement a global cache shared by multiple functor classes. |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
0 commit comments