include/opencl/openclwrapper.hxx |    1 
 opencl/source/openclwrapper.cxx  |   81 +++++++++++++++++++++++----------------
 2 files changed, 49 insertions(+), 33 deletions(-)

New commits:
commit aac6388b44a55d33222f31e42b5b1e090912792d
Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
Date:   Thu Oct 13 12:55:57 2016 +0200

    tdf#103204 opencl: initialize command queue on demand
    
    Change-Id: Ie3da1d6ec91e951b1ffc15abf376c7af57789e47
    Reviewed-on: https://gerrit.libreoffice.org/29802
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>
    Tested-by: Tomaž Vajngerl <qui...@gmail.com>
    (cherry picked from commit 4eea4af8924e3b1bb00c22cf1f9d21fc4dec6e83)
    Reviewed-on: https://gerrit.libreoffice.org/29995
    Reviewed-by: Jan Holesovsky <ke...@collabora.com>
    Tested-by: Jan Holesovsky <ke...@collabora.com>
    Reviewed-by: Tor Lillqvist <t...@collabora.com>
    Reviewed-by: Michael Meeks <michael.me...@collabora.com>
    Tested-by: Michael Meeks <michael.me...@collabora.com>

diff --git a/include/opencl/openclwrapper.hxx b/include/opencl/openclwrapper.hxx
index dae1192..233412f 100644
--- a/include/opencl/openclwrapper.hxx
+++ b/include/opencl/openclwrapper.hxx
@@ -42,6 +42,7 @@ struct OPENCL_DLLPUBLIC GPUEnv
     cl_context mpContext;
     cl_device_id mpDevID;
     cl_command_queue mpCmdQueue[OPENCL_CMDQUEUE_SIZE];
+    bool mbCommandQueueInitialized;
     cl_program mpArryPrograms[MAX_CLFILE_NUM]; //one program object maps one 
kernel source file
     int mnIsUserCreated; // 1: created , 0:no create and needed to create by 
opencl wrapper
     int mnCmdQueuePos;
diff --git a/opencl/source/openclwrapper.cxx b/opencl/source/openclwrapper.cxx
index d8d16c3..5d40a54 100644
--- a/opencl/source/openclwrapper.cxx
+++ b/opencl/source/openclwrapper.cxx
@@ -103,8 +103,54 @@ OString getCacheFolder()
 
 }
 
+bool initializeCommandQueue(GPUEnv& aGpuEnv)
+{
+    OpenCLZone zone;
+
+    cl_int nState;
+    cl_command_queue command_queue[OPENCL_CMDQUEUE_SIZE];
+
+    for (int i = 0; i < OPENCL_CMDQUEUE_SIZE; ++i)
+    {
+        command_queue[i] = clCreateCommandQueue(aGpuEnv.mpContext, 
aGpuEnv.mpDevID, 0, &nState);
+        if (nState != CL_SUCCESS)
+            SAL_WARN("opencl", "clCreateCommandQueue failed: " << 
errorString(nState));
+
+        if (command_queue[i] == nullptr || nState != CL_SUCCESS)
+        {
+            // Release all command queues created so far.
+            for (int j = 0; j <= i; ++j)
+            {
+                if (command_queue[j])
+                {
+                    clReleaseCommandQueue(command_queue[j]);
+                    command_queue[j] = nullptr;
+                }
+            }
+
+            clReleaseContext(aGpuEnv.mpContext);
+            SAL_WARN("opencl", "failed to set/switch opencl device");
+            return false;
+        }
+
+        SAL_INFO("opencl", "Created command queue " << command_queue[i] << " 
for context " << aGpuEnv.mpContext);
+    }
+
+    for (int i = 0; i < OPENCL_CMDQUEUE_SIZE; ++i)
+    {
+        aGpuEnv.mpCmdQueue[i] = command_queue[i];
+    }
+    aGpuEnv.mbCommandQueueInitialized = true;
+    return true;
+}
+
 void setKernelEnv( KernelEnv *envInfo )
 {
+    if (!gpuEnv.mbCommandQueueInitialized)
+    {
+        initializeCommandQueue(gpuEnv);
+    }
+
     envInfo->mpkContext = gpuEnv.mpContext;
     envInfo->mpkProgram = gpuEnv.mpArryPrograms[0];
 
@@ -265,8 +311,7 @@ bool initOpenCLAttr( OpenCLEnv * env )
 
     gpuEnv.mnIsUserCreated = 1;
 
-    for (int i = 0; i < OPENCL_CMDQUEUE_SIZE; ++i)
-        gpuEnv.mpCmdQueue[i] = env->mpOclCmdQueue[i];
+    gpuEnv.mbCommandQueueInitialized = false;
 
     gpuEnv.mnCmdQueuePos = 0; // default to 0.
 
@@ -765,7 +810,6 @@ bool switchOpenCLDevice(const OUString* pDevice, bool 
bAutoSelect, bool bForceEv
 
     cl_context context;
     cl_platform_id platformId;
-    cl_command_queue command_queue[OPENCL_CMDQUEUE_SIZE];
 
     {
         OpenCLZone zone;
@@ -790,33 +834,6 @@ bool switchOpenCLDevice(const OUString* pDevice, bool 
bAutoSelect, bool bForceEv
         }
         SAL_INFO("opencl", "Created context " << context << " for platform " 
<< platformId << ", device " << pDeviceId);
 
-        for (int i = 0; i < OPENCL_CMDQUEUE_SIZE; ++i)
-        {
-            command_queue[i] = clCreateCommandQueue(
-                context, pDeviceId, 0, &nState);
-            if (nState != CL_SUCCESS)
-                SAL_WARN("opencl", "clCreateCommandQueue failed: " << 
errorString(nState));
-
-            if (command_queue[i] == nullptr || nState != CL_SUCCESS)
-            {
-                // Release all command queues created so far.
-                for (int j = 0; j <= i; ++j)
-                {
-                    if (command_queue[j])
-                    {
-                        clReleaseCommandQueue(command_queue[j]);
-                        command_queue[j] = nullptr;
-                    }
-                }
-
-                clReleaseContext(context);
-                SAL_WARN("opencl", "failed to set/switch opencl device");
-                return false;
-            }
-
-            SAL_INFO("opencl", "Created command queue " << command_queue[i] << 
" for context " << context);
-        }
-
         OString sDeviceID = getDeviceInfoString(pDeviceId, CL_DEVICE_VENDOR) + 
" " + getDeviceInfoString(pDeviceId, CL_DRIVER_VERSION);
         rOutSelectedDeviceVersionIDString = OStringToOUString(sDeviceID, 
RTL_TEXTENCODING_UTF8);
     }
@@ -824,14 +841,12 @@ bool switchOpenCLDevice(const OUString* pDevice, bool 
bAutoSelect, bool bForceEv
     setOpenCLCmdQueuePosition(0); // Call this just to avoid the method being 
deleted from unused function deleter.
 
     releaseOpenCLEnv(&gpuEnv);
+
     OpenCLEnv env;
     env.mpOclPlatformID = platformId;
     env.mpOclContext = context;
     env.mpOclDevsID = pDeviceId;
 
-    for (int i = 0; i < OPENCL_CMDQUEUE_SIZE; ++i)
-        env.mpOclCmdQueue[i] = command_queue[i];
-
     initOpenCLAttr(&env);
 
     return !initOpenCLRunEnv(0);
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to