Merge pull request #1998 from ilya-lavrenov:tapi_gemm
authorAndrey Pavlenko <andrey.pavlenko@itseez.com>
Tue, 17 Dec 2013 12:41:58 +0000 (16:41 +0400)
committerOpenCV Buildbot <buildbot@opencv.org>
Tue, 17 Dec 2013 12:41:59 +0000 (16:41 +0400)
1  2 
modules/core/src/ocl.cpp

diff --combined modules/core/src/ocl.cpp
@@@ -42,6 -42,8 +42,8 @@@
  #include "precomp.hpp"
  #include <map>
  
+ #include "opencv2/core/opencl/runtime/opencl_clamdblas.hpp"
  #ifdef HAVE_OPENCL
  #include "opencv2/core/opencl/runtime/opencl_core.hpp"
  #else
@@@ -1309,36 -1311,30 +1311,30 @@@ inline bool operator < (const HashKey& 
      return h1.a < h2.a || (h1.a == h2.a && h1.b < h2.b);
  }
  
- static bool g_isInitialized = false;
+ static bool g_isOpenCLInitialized = false;
  static bool g_isOpenCLAvailable = false;
  bool haveOpenCL()
  {
-     if (!g_isInitialized)
+     if (!g_isOpenCLInitialized)
      {
-         if (!g_isInitialized)
+         try
          {
-             try
-             {
-                 cl_uint n = 0;
-                 cl_int err = ::clGetPlatformIDs(0, NULL, &n);
-                 if (err != CL_SUCCESS)
-                     g_isOpenCLAvailable = false;
-                 else
-                     g_isOpenCLAvailable = true;
-             }
-             catch (...)
-             {
-                 g_isOpenCLAvailable = false;
-             }
-             g_isInitialized = true;
+             cl_uint n = 0;
+             g_isOpenCLAvailable = ::clGetPlatformIDs(0, NULL, &n) == CL_SUCCESS;
+         }
+         catch (...)
+         {
+             g_isOpenCLAvailable = false;
          }
+         g_isOpenCLInitialized = true;
      }
      return g_isOpenCLAvailable;
  }
  
  bool useOpenCL()
  {
 -    TLSData* data = TLSData::get();
 +    CoreTLSData* data = coreTlsData.get();
      if( data->useOpenCL < 0 )
          data->useOpenCL = (int)haveOpenCL();
      return data->useOpenCL > 0;
@@@ -1348,11 -1344,85 +1344,85 @@@ void setUseOpenCL(bool flag
  {
      if( haveOpenCL() )
      {
 -        TLSData* data = TLSData::get();
 +        CoreTLSData* data = coreTlsData.get();
          data->useOpenCL = flag ? 1 : 0;
      }
  }
  
+ #ifdef HAVE_CLAMDBLAS
+ class AmdBlasHelper
+ {
+ public:
+     static AmdBlasHelper & getInstance()
+     {
+         static AmdBlasHelper amdBlas;
+         return amdBlas;
+     }
+     bool isAvailable() const
+     {
+         return g_isAmdBlasAvailable;
+     }
+     ~AmdBlasHelper()
+     {
+         try
+         {
+             clAmdBlasTeardown();
+         }
+         catch (...) { }
+     }
+ protected:
+     AmdBlasHelper()
+     {
+         if (!g_isAmdBlasInitialized)
+         {
+             AutoLock lock(m);
+             if (!g_isAmdBlasInitialized && haveOpenCL())
+             {
+                 try
+                 {
+                     g_isAmdBlasAvailable = clAmdBlasSetup() == clAmdBlasSuccess;
+                 }
+                 catch (...)
+                 {
+                     g_isAmdBlasAvailable = false;
+                 }
+             }
+             else
+                 g_isAmdBlasAvailable = false;
+             g_isAmdBlasInitialized = true;
+         }
+     }
+ private:
+     static Mutex m;
+     static bool g_isAmdBlasInitialized;
+     static bool g_isAmdBlasAvailable;
+ };
+ bool AmdBlasHelper::g_isAmdBlasAvailable = false;
+ bool AmdBlasHelper::g_isAmdBlasInitialized = false;
+ Mutex AmdBlasHelper::m;
+ bool haveAmdBlas()
+ {
+     return AmdBlasHelper::getInstance().isAvailable();
+ }
+ #else
+ bool haveAmdBlas()
+ {
+     return false;
+ }
+ #endif
  void finish2()
  {
      Queue::getDefault().finish();
@@@ -1765,7 -1835,7 +1835,7 @@@ size_t Device::profilingTimerResolution
  const Device& Device::getDefault()
  {
      const Context2& ctx = Context2::getDefault();
 -    int idx = TLSData::get()->device;
 +    int idx = coreTlsData.get()->device;
      return ctx.device(idx);
  }
  
@@@ -2059,7 -2129,7 +2129,7 @@@ void* Queue::ptr() cons
  
  Queue& Queue::getDefault()
  {
 -    Queue& q = TLSData::get()->oclQueue;
 +    Queue& q = coreTlsData.get()->oclQueue;
      if( !q.p && haveOpenCL() )
          q.create(Context2::getDefault());
      return q;