[arrayfire] 12/248: Windows specific changes to HAPI Symbol Manager

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Tue Nov 17 15:53:49 UTC 2015


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

ghisvail-guest pushed a commit to branch dfsg-clean
in repository arrayfire.

commit b70c2c4a3ce136b083158245934cb2da5b7d360a
Author: Pradeep <pradeep at arrayfire.com>
Date:   Wed Aug 26 16:07:38 2015 -0400

    Windows specific changes to HAPI Symbol Manager
    
    Cleaned up dead code as well
---
 hapi_examples/CMakeLists.txt    |  5 ++++-
 src/api/hapi/data.cpp           | 40 ++++++----------------------------
 src/api/hapi/device.cpp         | 26 +++-------------------
 src/api/hapi/print.cpp          | 16 ++------------
 src/api/hapi/symbol_manager.cpp | 47 ++++++++++++++++++++++++++++++----------
 src/api/hapi/symbol_manager.hpp | 48 ++++++++++++++++++++++++-----------------
 6 files changed, 79 insertions(+), 103 deletions(-)

diff --git a/hapi_examples/CMakeLists.txt b/hapi_examples/CMakeLists.txt
index b7e7aea..ce947d7 100644
--- a/hapi_examples/CMakeLists.txt
+++ b/hapi_examples/CMakeLists.txt
@@ -10,4 +10,7 @@ ENDIF()
 
 ADD_EXECUTABLE(hapi_test test.cpp)
 
-TARGET_LINK_LIBRARIES(hapi_test af dl)
+TARGET_LINK_LIBRARIES(hapi_test af)
+IF(UNIX)
+    TARGET_LINK_LIBRARIES(hapi_test dl)
+ENDIF()
diff --git a/src/api/hapi/data.cpp b/src/api/hapi/data.cpp
index cc042ef..e3edf88 100644
--- a/src/api/hapi/data.cpp
+++ b/src/api/hapi/data.cpp
@@ -9,54 +9,26 @@
 
 #include <af/array.h>
 #include <af/data.h>
-#include <functional>
-#include <stdlib.h>
-#include <dlfcn.h>
-#include <iostream>
 #include "symbol_manager.hpp"
 
 af_err af_create_array(af_array *result, const void * const data,
                        const unsigned ndims, const dim_t * const dims,
                        const af_dtype type)
 {
-    af_err errCode = AF_SUCCESS;
-    try {
-        AFSymbolManager& symbolManager = AFSymbolManager::getInstance();
-        errCode = symbolManager.call("af_create_array", result, data, ndims, dims, type);
-    } catch(std::logic_error &e) {
-        // FIXME: remove std::cerr
-        std::cerr<<e.what()<<std::endl;
-        errCode = AF_ERR_SYM_LOAD;
-    }
-    return errCode;
+    AFSymbolManager& symbolManager = AFSymbolManager::getInstance();
+    return symbolManager.call("af_create_array", result, data, ndims, dims, type);
 }
 
 af_err af_constant(af_array *result, const double value,
                    const unsigned ndims, const dim_t * const dims,
                    const af_dtype type)
 {
-    af_err errCode = AF_SUCCESS;
-    try {
-        AFSymbolManager& symbolManager = AFSymbolManager::getInstance();
-        errCode = symbolManager.call("af_constant", result, value, ndims, dims, type);
-    } catch(std::logic_error &e) {
-        // FIXME: remove std::cerr
-        std::cerr<<e.what()<<std::endl;
-        errCode = AF_ERR_SYM_LOAD;
-    }
-    return errCode;
+    AFSymbolManager& symbolManager = AFSymbolManager::getInstance();
+    return symbolManager.call("af_constant", result, value, ndims, dims, type);
 }
 
 af_err af_release_array(af_array arr)
 {
-    af_err errCode = AF_SUCCESS;
-    try {
-        AFSymbolManager& symbolManager = AFSymbolManager::getInstance();
-        errCode = symbolManager.call("af_release_array", arr);
-    } catch(std::logic_error &e) {
-        // FIXME: remove std::cerr
-        std::cerr<<e.what()<<std::endl;
-        errCode = AF_ERR_SYM_LOAD;
-    }
-    return errCode;
+    AFSymbolManager& symbolManager = AFSymbolManager::getInstance();
+    return symbolManager.call("af_release_array", arr);
 }
diff --git a/src/api/hapi/device.cpp b/src/api/hapi/device.cpp
index dc4de07..b1b419e 100644
--- a/src/api/hapi/device.cpp
+++ b/src/api/hapi/device.cpp
@@ -9,35 +9,15 @@
 
 #include <af/hapi.h>
 #include <af/device.h>
-#include <functional>
-#include <stdlib.h>
-#include <dlfcn.h>
-#include <iostream>
 #include "symbol_manager.hpp"
 
 af_err af_set_backend(const af_backend bknd)
 {
-    af_err errCode = AF_SUCCESS;
-    try {
-        AFSymbolManager::getInstance().setBackend(bknd);
-    } catch(std::logic_error &e) {
-        // FIXME: remove std::cerr
-        std::cerr<<e.what()<<std::endl;
-        errCode = AF_ERR_LOAD_LIB;
-    }
-    return errCode;
+    return AFSymbolManager::getInstance().setBackend(bknd);
 }
 
 af_err af_info()
 {
-    af_err errCode = AF_SUCCESS;
-    try {
-        AFSymbolManager& symbolManager = AFSymbolManager::getInstance();
-        errCode = symbolManager.call("af_info");
-    } catch(std::logic_error &e) {
-        // FIXME: remove std::cerr
-        std::cerr<<e.what()<<std::endl;
-        errCode = AF_ERR_SYM_LOAD;
-    }
-    return errCode;
+    AFSymbolManager& symbolManager = AFSymbolManager::getInstance();
+    return symbolManager.call("af_info");
 }
diff --git a/src/api/hapi/print.cpp b/src/api/hapi/print.cpp
index 3eb28cd..f769dbb 100644
--- a/src/api/hapi/print.cpp
+++ b/src/api/hapi/print.cpp
@@ -8,22 +8,10 @@
  ********************************************************/
 
 #include <af/util.h>
-#include <functional>
-#include <stdlib.h>
-#include <dlfcn.h>
-#include <iostream>
 #include "symbol_manager.hpp"
 
 af_err af_print_array(const af_array arr)
 {
-    af_err errCode = AF_SUCCESS;
-    try {
-        AFSymbolManager& symbolManager = AFSymbolManager::getInstance();
-        errCode = symbolManager.call("af_print_array", arr);
-    } catch(std::logic_error &e) {
-        // FIXME: remove std::cerr
-        std::cerr<<e.what()<<std::endl;
-        errCode = AF_ERR_SYM_LOAD;
-    }
-    return errCode;
+    AFSymbolManager& symbolManager = AFSymbolManager::getInstance();
+    return symbolManager.call("af_print_array", arr);
 }
diff --git a/src/api/hapi/symbol_manager.cpp b/src/api/hapi/symbol_manager.cpp
index ce5bf18..8f10206 100644
--- a/src/api/hapi/symbol_manager.cpp
+++ b/src/api/hapi/symbol_manager.cpp
@@ -15,20 +15,43 @@ AFSymbolManager& AFSymbolManager::getInstance()
     return symbolManager;
 }
 
+/*flag parameter is not used on windows platform */
+LibHandle openDynLibrary(const char* dlName, int flag=RTLD_LAZY)
+{
+#if defined(OS_WIN)
+    HMODULE retVal = LoadLibrary(dlName);
+    if (retVal == NULL) {
+        retVal = LoadLibraryEx(dlName, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
+    }
+    return retVal;
+#else
+    return dlopen(dlName, flag);
+#endif
+}
+
+void closeDynLibrary(LibHandle handle)
+{
+#if defined(OS_WIN)
+    FreeLibrary(handle);
+#else
+    dlclose(handle);
+#endif
+}
+
 AFSymbolManager::AFSymbolManager()
     : isCPULoaded(false), isCUDALoaded(false), isOCLLoaded(false)
 {
-    cpuHandle = dlopen("libafcpu.so", RTLD_LAZY);
+    cpuHandle = openDynLibrary(LIB_AF_CPU_NAME);
     if (cpuHandle) {
         isCPULoaded = true;
         activeHandle = cpuHandle;
     }
-    cudaHandle = dlopen("libafcuda.so", RTLD_LAZY);
+    cudaHandle = openDynLibrary(LIB_AF_CUDA_NAME);
     if (cudaHandle) {
         isCUDALoaded = true;
         activeHandle = cudaHandle;
     }
-    oclHandle = dlopen("libafopencl.so", RTLD_LAZY);
+    oclHandle = openDynLibrary(LIB_AF_OCL_NAME);
     if (oclHandle) {
         isOCLLoaded = true;
         activeHandle = oclHandle;
@@ -38,40 +61,42 @@ AFSymbolManager::AFSymbolManager()
 AFSymbolManager::~AFSymbolManager()
 {
     if (isCPULoaded) {
-        dlclose(cpuHandle);
+        closeDynLibrary(cpuHandle);
         isCPULoaded = false;
     }
     if (isCUDALoaded) {
-        dlclose(cudaHandle);
+        closeDynLibrary(cudaHandle);
         isCUDALoaded = false;
     }
     if (isOCLLoaded) {
-        dlclose(oclHandle);
+        closeDynLibrary(oclHandle);
         isOCLLoaded = false;
     }
 }
 
-void AFSymbolManager::setBackend(af::Backend bknd)
+af_err AFSymbolManager::setBackend(af::Backend bknd)
 {
+    af_err retCode = AF_SUCCESS;
     activeBknd = bknd;
     switch (activeBknd) {
         case af::Backend::AF_BACKEND_CPU:
             if(isCPULoaded)
                 activeHandle = cpuHandle;
             else
-                throw std::logic_error("can't load afcpu library");
+                retCode = AF_ERR_LOAD_LIB;
             break;
         case af::Backend::AF_BACKEND_CUDA:
             if(isCUDALoaded)
                 activeHandle = cudaHandle;
             else
-                throw std::logic_error("can't load afcuda library");
+                retCode = AF_ERR_LOAD_LIB;
             break;
         case af::Backend::AF_BACKEND_OPENCL:
-            if(isOCLLoaded)
+            if (isOCLLoaded)
                 activeHandle = oclHandle;
             else
-                throw std::logic_error("can't load afopencl library");
+                retCode = AF_ERR_LOAD_LIB;
             break;
     }
+    return retCode;
 }
diff --git a/src/api/hapi/symbol_manager.hpp b/src/api/hapi/symbol_manager.hpp
index eea8f38..f51f0b1 100644
--- a/src/api/hapi/symbol_manager.hpp
+++ b/src/api/hapi/symbol_manager.hpp
@@ -8,10 +8,22 @@
  ********************************************************/
 
 #include <af/defines.h>
-#include <functional>
 #include <string>
 #include <stdlib.h>
+#if defined(OS_WIN)
+#include <Windows.h>
+typedef HMODULE LibHandle;
+#define RTLD_LAZY 0
+#define LIB_AF_CPU_NAME "afcpu.dll"
+#define LIB_AF_CUDA_NAME "afcuda.dll"
+#define LIB_AF_OCL_NAME "afopencl.dll"
+#else
 #include <dlfcn.h>
+typedef void* LibHandle;
+#define LIB_AF_CPU_NAME "libafcpu.so"
+#define LIB_AF_CUDA_NAME "libafcuda.so"
+#define LIB_AF_OCL_NAME "libafopencl.so"
+#endif
 
 class AFSymbolManager {
     public:
@@ -19,25 +31,21 @@ class AFSymbolManager {
 
         ~AFSymbolManager();
 
-        void setBackend(af::Backend bnkd);
+        af_err setBackend(af::Backend bnkd);
 
         template<typename... CalleeArgs>
         af_err call(const char* symbolName, CalleeArgs... args) {
-            using std::string;
-            using std::logic_error;
-
-            void* const handle = dlsym(activeHandle, symbolName);
-
-            if (!handle) {
-                char* const error = dlerror();
-                if (error) {
-                    throw logic_error("can't find symbol: "+string(symbolName)+" - "+error);
-                }
+            typedef af_err(*af_func)(CalleeArgs...);
+            af_func funcHandle;
+#if defined(OS_WIN)
+            funcHandle = (af_func)GetProcAddress(activeHandle, symbolName);
+#else
+            funcHandle = (af_func)dlsym(activeHandle, symbolName);
+#endif
+            if (!funcHandle) {
+                return AF_ERR_SYM_LOAD;
             }
-
-            std::function<af_err (CalleeArgs...)> callee = reinterpret_cast<af_err (*)(CalleeArgs...)>(handle);
-
-            return callee(args...);
+            return funcHandle(args...);
         }
 
     protected:
@@ -55,10 +63,10 @@ class AFSymbolManager {
         bool isCUDALoaded;
         bool isOCLLoaded;
 
-        void* cpuHandle;
-        void* cudaHandle;
-        void* oclHandle;
+        LibHandle cpuHandle;
+        LibHandle cudaHandle;
+        LibHandle oclHandle;
 
         af::Backend activeBknd;
-        void* activeHandle;
+        LibHandle activeHandle;
 };

-- 
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