[arrayfire] 43/248: changed unified api to load libraries using prioritized list of paths

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Tue Nov 17 15:53:52 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 d042df9c197a5428ba8cdea9d1c7619b13b67f27
Author: pradeep <pradeep at arrayfire.com>
Date:   Mon Sep 7 14:11:36 2015 -0400

    changed unified api to load libraries using prioritized list of paths
    
    priority of the search path is as follows
    1. search the paths present in <LD_LIBRARY_PATH> / <PATH> environment variables
    2. search in <AF_PATH>/lib/
    3. search in <AF_BUILD_PATH>/src/backend
---
 src/api/unified/symbol_manager.cpp | 86 ++++++++++++++++++++++++++++++++------
 src/api/unified/symbol_manager.hpp |  1 +
 2 files changed, 74 insertions(+), 13 deletions(-)

diff --git a/src/api/unified/symbol_manager.cpp b/src/api/unified/symbol_manager.cpp
index ef4c638..0aa5369 100644
--- a/src/api/unified/symbol_manager.cpp
+++ b/src/api/unified/symbol_manager.cpp
@@ -8,32 +8,87 @@
  ********************************************************/
 
 #include "symbol_manager.hpp"
+#include <algorithm>
+#include <string>
 
+using std::string;
+using std::replace;
+
+static const string LIB_AF_BKND_NAME[NUM_BACKENDS] = {"cpu", "cuda", "opencl"};
 #if defined(OS_WIN)
-static const char* LIB_AF_BKND_NAME[NUM_BACKENDS] = {"afcpu.dll", "afcuda.dll", "afopencl.dll"};
+static const string LIB_AF_BKND_PREFIX = "af";
+static const string LIB_AF_BKND_SUFFIX = ".dll";
 #define RTLD_LAZY 0
 #else
-static const char* LIB_AF_BKND_NAME[NUM_BACKENDS] = {"libafcpu.so", "libafcuda.so", "libafopencl.so"};
+static const string LIB_AF_BKND_PREFIX = "libaf";
+static const string LIB_AF_BKND_SUFFIX = ".so";
 #endif
 
-AFSymbolManager& AFSymbolManager::getInstance()
+static const string LIB_AF_ENVARS[NUM_ENV_VARS] = {"AF_PATH", "AF_BUILD_PATH"};
+static const string LIB_AF_RPATHS[NUM_ENV_VARS] = {"/lib/", "/src/backend/"};
+static const bool LIB_AF_RPATH_SUFFIX[NUM_ENV_VARS] = {false, true};
+
+inline string getBkndLibName(const int backend_index)
 {
-    static AFSymbolManager symbolManager;
-    return symbolManager;
+    int i = backend_index >=0 && backend_index<NUM_BACKENDS ? backend_index : 0;
+    return LIB_AF_BKND_PREFIX + LIB_AF_BKND_NAME[i] + LIB_AF_BKND_SUFFIX;
+}
+
+inline std::string getEnvVar(const std::string &key)
+{
+#if defined(OS_WIN)
+    DWORD bufSize = 32767; // limit according to GetEnvironment Variable documentation
+    string retVal;
+    retVal.resize(bufSize);
+    bufSize = GetEnvironmentVariable(key.c_str(), &retVal[0], bufSize);
+    if (!bufSize) {
+        return string("");
+    } else {
+        retVal.resize(bufSize);
+        return retVal;
+    }
+#else
+    char * str = getenv(key.c_str());
+    return str==NULL ? string("") : string(str);
+#endif
 }
 
 /*flag parameter is not used on windows platform */
-LibHandle openDynLibrary(const char* dlName, int flag=RTLD_LAZY)
+LibHandle openDynLibrary(const int bknd_idx, int flag=RTLD_LAZY)
 {
+    string bkndName = getBkndLibName(bknd_idx);
 #if defined(OS_WIN)
-    HMODULE retVal = LoadLibrary(dlName);
+    HMODULE retVal = LoadLibrary(bkndName.c_str());
+#else
+    LibHandle retVal = dlopen(bkndName.c_str(), flag);
+#endif
+    // default search path is the colon separated list of
+    // paths stored in the environment variable
+    // LD_LIBRARY_PATH(Linux/Unix) or PATH(windows)
+    // in the event that dlopen returns NULL, search for the lib
+    // ub hard coded paths based on the environment variables
+    // defined in the constant string array LIB_AF_PATHS
     if (retVal == NULL) {
-        retVal = LoadLibraryEx(dlName, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
-    }
-    return retVal;
+        for (int i=0; i<NUM_ENV_VARS; ++i) {
+            string abs_path = getEnvVar(LIB_AF_ENVARS[i])
+                                 + LIB_AF_RPATHS[i]
+                                 + (LIB_AF_RPATH_SUFFIX[i] ? LIB_AF_BKND_NAME[bknd_idx]+"/" : "")
+                                 + bkndName;
+#if defined(OS_WIN)
+            replace(abs_path.begin(), abs_path.end(), '/', '\\');
+            retVal = LoadLibrary(abs_path.c_str());
 #else
-    return dlopen(dlName, flag);
+            retVal = dlopen(abs_path.c_str(), flag);
 #endif
+            if (retVal!=NULL) {
+                // if the current absolute path based dlopen
+                // search is a success, then abandon search
+                // and proceed for compute
+                break;
+            }
+        }
+    }
+    return retVal;
 }
 
 void closeDynLibrary(LibHandle handle)
@@ -45,12 +100,17 @@ void closeDynLibrary(LibHandle handle)
 #endif
 }
 
+AFSymbolManager& AFSymbolManager::getInstance()
+{
+    static AFSymbolManager symbolManager;
+    return symbolManager;
+}
+
 AFSymbolManager::AFSymbolManager()
     : activeHandle(NULL), defaultHandle(NULL), numBackends(0)
 {
-
     for(int i=0; i<NUM_BACKENDS; ++i) {
-        bkndHandles[i] = openDynLibrary(LIB_AF_BKND_NAME[i]);
+        bkndHandles[i] = openDynLibrary(i);
         if (bkndHandles[i]) {
             activeHandle = bkndHandles[i];
             numBackends++;
diff --git a/src/api/unified/symbol_manager.hpp b/src/api/unified/symbol_manager.hpp
index 8eac55b..3ce5ec4 100644
--- a/src/api/unified/symbol_manager.hpp
+++ b/src/api/unified/symbol_manager.hpp
@@ -19,6 +19,7 @@ typedef void* LibHandle;
 #endif
 
 const int NUM_BACKENDS = 3;
+const int NUM_ENV_VARS = 2;
 
 class AFSymbolManager {
     public:

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