[arrayfire] 288/408: Cleaning up unwrap code in OpenCL by using cache store

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Sep 21 19:12:15 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/sid
in repository arrayfire.

commit aca30fac276ad02327d1e5e7f9ef4c93ba8f1ffa
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Wed Aug 19 03:30:22 2015 -0400

    Cleaning up unwrap code in OpenCL by using cache store
---
 src/backend/opencl/cache.hpp         |  2 +
 src/backend/opencl/kernel/unwrap.hpp | 80 ++++++++++++++++++++----------------
 src/backend/opencl/unwrap.cpp        |  7 +---
 test/unwrap.cpp                      |  9 ++--
 4 files changed, 52 insertions(+), 46 deletions(-)

diff --git a/src/backend/opencl/cache.hpp b/src/backend/opencl/cache.hpp
index 76e4f9d..937d3d8 100644
--- a/src/backend/opencl/cache.hpp
+++ b/src/backend/opencl/cache.hpp
@@ -9,6 +9,8 @@
 
 #pragma once
 #include <program.hpp>
+#include <map>
+#include <string>
 
 namespace opencl
 {
diff --git a/src/backend/opencl/kernel/unwrap.hpp b/src/backend/opencl/kernel/unwrap.hpp
index 180dc8a..7e2b571 100644
--- a/src/backend/opencl/kernel/unwrap.hpp
+++ b/src/backend/opencl/kernel/unwrap.hpp
@@ -10,6 +10,7 @@
 #pragma once
 #include <kernel_headers/unwrap.hpp>
 #include <program.hpp>
+#include <cache.hpp>
 #include <traits.hpp>
 #include <string>
 #include <map>
@@ -33,47 +34,47 @@ namespace opencl
 {
     namespace kernel
     {
-        template<typename T, bool is_column>
-        void unwrap(Param out, const Param in, const dim_t wx, const dim_t wy,
-                    const dim_t sx, const dim_t sy, const dim_t px, const dim_t py, const dim_t nx)
+        template<typename T>
+        void unwrap(Param out, const Param in,
+                    const dim_t wx, const dim_t wy,
+                    const dim_t sx, const dim_t sy,
+                    const dim_t px, const dim_t py,
+                    const dim_t nx, const bool is_column)
         {
             try {
-                static std::once_flag compileFlags[DeviceManager::MAX_DEVICES];
-                static std::map<int, Program*>   unwrapProgs;
-                static std::map<int, Kernel *> unwrapKernels;
+                std::string ref_name =
+                    std::string("unwrap_") +
+                    std::string(dtype_traits<T>::getName()) +
+                    std::string("_") +
+                    std::to_string(is_column);
 
                 int device = getActiveDeviceId();
+                kc_t::iterator idx = kernelCaches[device].find(ref_name);
 
-                std::call_once( compileFlags[device], [device] () {
-
-                        ToNum<T> toNum;
-                        std::ostringstream options;
-                        options << " -D is_column=" << is_column
-                                << " -D ZERO=" << toNum(scalar<T>(0))
-                                << " -D T="    << dtype_traits<T>::getName();
-
-                        if((af_dtype) dtype_traits<T>::af_type == c32 ||
-                           (af_dtype) dtype_traits<T>::af_type == c64) {
-                            options << " -D CPLX=1";
-                        } else {
-                            options << " -D CPLX=0";
-                        }
-
-                        if (std::is_same<T, double>::value ||
-                            std::is_same<T, cdouble>::value) {
-                            options << " -D USE_DOUBLE";
-                        }
-
-                        Program prog;
-                        buildProgram(prog, unwrap_cl, unwrap_cl_len, options.str());
-                        unwrapProgs[device] = new Program(prog);
-                        unwrapKernels[device] = new Kernel(*unwrapProgs[device], "unwrap_kernel");
-                    });
-
-                auto unwrapOp = make_kernel<Buffer, const KParam, const Buffer, const KParam,
-                                      const dim_t, const dim_t, const dim_t, const dim_t,
-                                      const dim_t, const dim_t, const dim_t, const dim_t>
-                                      (*unwrapKernels[device]);
+                kc_entry_t entry;
+                if (idx == kernelCaches[device].end()) {
+
+                    ToNum<T> toNum;
+                    std::ostringstream options;
+                    options << " -D is_column=" << is_column
+                            << " -D ZERO=" << toNum(scalar<T>(0))
+                            << " -D T="    << dtype_traits<T>::getName();
+
+                    if (std::is_same<T, double>::value ||
+                        std::is_same<T, cdouble>::value) {
+                        options << " -D USE_DOUBLE";
+                    }
+
+                    Program prog;
+                    buildProgram(prog, unwrap_cl, unwrap_cl_len, options.str());
+
+                    entry.prog = new Program(prog);
+                    entry.ker = new Kernel(*entry.prog, "unwrap_kernel");
+
+                    kernelCaches[device][ref_name] = entry;
+                } else {
+                    entry = idx->second;
+                }
 
                 dim_t TX = 1, TY = 1;
                 dim_t BX = 1;
@@ -96,6 +97,13 @@ namespace opencl
                 NDRange global(local[0] * BX,
                                local[1] * BY);
 
+                auto unwrapOp = make_kernel<Buffer, const KParam,
+                                            const Buffer, const KParam,
+                                            const dim_t, const dim_t,
+                                            const dim_t, const dim_t,
+                                            const dim_t, const dim_t,
+                                            const dim_t, const dim_t> (*entry.ker);
+
                 unwrapOp(EnqueueArgs(getQueue(), global, local),
                        *out.data, out.info, *in.data, in.info, wx, wy, sx, sy, px, py, nx, reps);
 
diff --git a/src/backend/opencl/unwrap.cpp b/src/backend/opencl/unwrap.cpp
index e0676ca..4fc91a7 100644
--- a/src/backend/opencl/unwrap.cpp
+++ b/src/backend/opencl/unwrap.cpp
@@ -32,12 +32,7 @@ namespace opencl
 
         // Create output placeholder
         Array<T> outArray = createEmptyArray<T>(odims);
-
-        if (is_column) {
-            kernel::unwrap<T, true >(outArray, in, wx, wy, sx, sy, px, py, nx);
-        } else {
-            kernel::unwrap<T, false>(outArray, in, wx, wy, sx, sy, px, py, nx);
-        }
+        kernel::unwrap<T>(outArray, in, wx, wy, sx, sy, px, py, nx, is_column);
 
         return outArray;
     }
diff --git a/test/unwrap.cpp b/test/unwrap.cpp
index a36f14e..28ec1c0 100644
--- a/test/unwrap.cpp
+++ b/test/unwrap.cpp
@@ -84,10 +84,11 @@ void unwrapTest(string pTestFile, const unsigned resultIdx,
     if(outArray2 != 0) af_release_array(outArray2);
 }
 
-#define UNWRAP_INIT(desc, file, resultIdx, wx, wy, sx, sy, px,py)                                           \
-    TYPED_TEST(Unwrap, desc)                                                                                \
-    {                                                                                                       \
-        unwrapTest<TypeParam>(string(TEST_DIR"/unwrap/"#file".test"), resultIdx, wx, wy, sx, sy, px, py);   \
+#define UNWRAP_INIT(desc, file, resultIdx, wx, wy, sx, sy, px,py)       \
+    TYPED_TEST(Unwrap, desc)                                            \
+    {                                                                   \
+        unwrapTest<TypeParam>(string(TEST_DIR"/unwrap/"#file".test"),   \
+                              resultIdx, wx, wy, sx, sy, px, py);       \
     }
 
     UNWRAP_INIT(UnwrapSmall00, unwrap_small,  0,  3,  3,  1,  1,  0,  0);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/arrayfire.git



More information about the debian-science-commits mailing list