Let clAmdBlas library initialize once during program lifetime.
authorpeng xiao <hisenxpress@gmail.com>
Mon, 2 Sep 2013 02:06:01 +0000 (10:06 +0800)
committerpeng xiao <hisenxpress@gmail.com>
Mon, 2 Sep 2013 02:06:01 +0000 (10:06 +0800)
modules/ocl/src/gemm.cpp
modules/ocl/src/initialization.cpp

index 44f23da69c2742acf372358f8f9c60381166f62a..6e04baca4b069417c8cd443a0b2228e0031da890 100644 (file)
 #include <iomanip>
 #include "precomp.hpp"
 
+namespace cv { namespace ocl {
+
+// used for clAmdBlas library to avoid redundant setup/teardown
+void clBlasSetup();
+void clBlasTeardown();
+
+}} /* namespace cv { namespace ocl */
+
+
 #if !defined HAVE_CLAMDBLAS
 void cv::ocl::gemm(const oclMat&, const oclMat&, double,
                    const oclMat&, double, oclMat&, int)
 {
     CV_Error(CV_StsNotImplemented, "OpenCL BLAS is not implemented");
 }
+
+void cv::ocl::clBlasSetup()
+{
+    CV_Error(CV_StsNotImplemented, "OpenCL BLAS is not implemented");
+}
+
+void cv::ocl::clBlasTeardown()
+{
+    CV_Error(CV_StsNotImplemented, "OpenCL BLAS is not implemented");
+}
+
 #else
 #include "clAmdBlas.h"
 using namespace cv;
 
+static bool clBlasInitialized = false;
+static Mutex cs;
+
+void cv::ocl::clBlasSetup()
+{
+    AutoLock al(cs);
+    if(!clBlasInitialized)
+    {
+        openCLSafeCall(clAmdBlasSetup());
+        clBlasInitialized = true;
+    }
+}
+
+void cv::ocl::clBlasTeardown()
+{
+    AutoLock al(cs);
+    if(clBlasInitialized)
+    {
+        clAmdBlasTeardown();
+        clBlasInitialized = false;
+    }
+}
+
 void cv::ocl::gemm(const oclMat &src1, const oclMat &src2, double alpha,
                    const oclMat &src3, double beta, oclMat &dst, int flags)
 {
@@ -71,7 +114,8 @@ void cv::ocl::gemm(const oclMat &src1, const oclMat &src2, double alpha,
         dst.create(src1.rows, src2.cols, src1.type());
         dst.setTo(Scalar::all(0));
     }
-    openCLSafeCall( clAmdBlasSetup() );
+
+    clBlasSetup();
 
     const clAmdBlasTranspose transA = (cv::GEMM_1_T & flags) ? clAmdBlasTrans : clAmdBlasNoTrans;
     const clAmdBlasTranspose transB = (cv::GEMM_2_T & flags) ? clAmdBlasTrans : clAmdBlasNoTrans;
@@ -156,6 +200,5 @@ void cv::ocl::gemm(const oclMat &src1, const oclMat &src2, double alpha,
     }
     break;
     }
-    clAmdBlasTeardown();
 }
 #endif
index b990e09fe0b968142a80019f155376d144b7cc23..564b4035728726b6ec336b59932bad9bdcdb03ba 100644 (file)
@@ -68,6 +68,7 @@ namespace cv
     namespace ocl
     {
         extern void fft_teardown();
+        extern void clBlasTeardown();
         /*
          * The binary caching system to eliminate redundant program source compilation.
          * Strictly, this is not a cache because we do not implement evictions right now.
@@ -1050,6 +1051,7 @@ namespace cv
         void Info::release()
         {
             fft_teardown();
+            clBlasTeardown();
             impl->release();
             impl = new Impl;
             DeviceName.clear();
@@ -1058,6 +1060,7 @@ namespace cv
         Info::~Info()
         {
             fft_teardown();
+            clBlasTeardown();
             impl->release();
         }