[arrayfire] 28/248: set_backend and get_backend_count functions

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Tue Nov 17 15:53:51 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 a9cb8fe1bfc3ecac10aa7495e21285f16c209967
Author: pradeep <pradeep at arrayfire.com>
Date:   Mon Aug 31 16:29:41 2015 -0400

    set_backend and get_backend_count functions
    
    Also cleaned up symbol manager class further
---
 include/af/hapi.h               | 20 +++++++++++++++++++-
 src/api/c/device.cpp            | 20 ++++++++++++++++++++
 src/api/cpp/device.cpp          | 13 +++++++++++++
 src/api/hapi/device.cpp         |  6 ++++++
 src/api/hapi/symbol_manager.cpp | 20 ++++++++++----------
 src/api/hapi/symbol_manager.hpp | 12 ++++--------
 6 files changed, 72 insertions(+), 19 deletions(-)

diff --git a/include/af/hapi.h b/include/af/hapi.h
index 4c74db8..6318f61 100644
--- a/include/af/hapi.h
+++ b/include/af/hapi.h
@@ -13,6 +13,7 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
+
 /**
    Changes the compute backend at run time
 
@@ -21,10 +22,19 @@ extern "C" {
  */
 AFAPI af_err af_set_backend(const af_backend bknd);
 
+/**
+   Gets the number of available backends
+
+   \param[out] num_backends Number of available backends
+   \returns \ref af_err error code
+ */
+AFAPI af_err af_get_backend_count(unsigned* num_backends);
+
 #ifdef __cplusplus
 }
 #endif
 
+#ifdef __cplusplus
 namespace af
 {
 
@@ -33,6 +43,14 @@ namespace af
 
    \param[in] bknd takes one of the values of enum \ref af_backend
  */
-void setBackend(const Backend bknd);
+AFAPI void setBackend(const Backend bknd);
+
+/**
+   Gets the number of available backends
+
+   \returns Number of available backends
+ */
+AFAPI unsigned getBackendCount();
 
 }
+#endif
diff --git a/src/api/c/device.cpp b/src/api/c/device.cpp
index 23fcdd0..0e59bfb 100644
--- a/src/api/c/device.cpp
+++ b/src/api/c/device.cpp
@@ -19,6 +19,26 @@
 
 using namespace detail;
 
+af_err af_set_backend(const af_backend bknd)
+{
+#if defined(AF_CPU)
+    ARG_ASSERT(0, bknd==AF_BACKEND_CPU);
+#endif
+#if defined(AF_CUDA)
+    ARG_ASSERT(0, bknd==AF_BACKEND_CUDA);
+#endif
+#if defined(AF_OPENCL)
+    ARG_ASSERT(0, bknd==AF_BACKEND_OPENCL);
+#endif
+    return AF_SUCCESS;
+}
+
+af_err af_get_backend_count(unsigned* num_backends)
+{
+    *num_backends = 1;
+    return AF_SUCCESS;
+}
+
 af_err af_init()
 {
     try {
diff --git a/src/api/cpp/device.cpp b/src/api/cpp/device.cpp
index 0a39ed2..9641a73 100644
--- a/src/api/cpp/device.cpp
+++ b/src/api/cpp/device.cpp
@@ -10,10 +10,23 @@
 #include <af/device.h>
 #include <af/compatible.h>
 #include <af/traits.hpp>
+#include <af/hapi.h>
 #include "error.hpp"
 
 namespace af
 {
+    void setBackend(const Backend bknd)
+    {
+        AF_THROW(af_set_backend(bknd));
+    }
+
+    unsigned getBackendCount()
+    {
+        unsigned temp = 1;
+        AF_THROW(af_get_backend_count(&temp));
+        return temp;
+    }
+
     void info()
     {
         AF_THROW(af_info());
diff --git a/src/api/hapi/device.cpp b/src/api/hapi/device.cpp
index db59987..cfaa144 100644
--- a/src/api/hapi/device.cpp
+++ b/src/api/hapi/device.cpp
@@ -16,6 +16,12 @@ af_err af_set_backend(const af_backend bknd)
     return AFSymbolManager::getInstance().setBackend(bknd);
 }
 
+af_err af_get_backend_count(unsigned* num_backends)
+{
+    *num_backends = AFSymbolManager::getInstance().getBackendCount();
+    return AF_SUCCESS;
+}
+
 af_err af_info()
 {
     return CALL_NO_PARAMS();
diff --git a/src/api/hapi/symbol_manager.cpp b/src/api/hapi/symbol_manager.cpp
index 8dc7557..38f3399 100644
--- a/src/api/hapi/symbol_manager.cpp
+++ b/src/api/hapi/symbol_manager.cpp
@@ -46,16 +46,15 @@ void closeDynLibrary(LibHandle handle)
 }
 
 AFSymbolManager::AFSymbolManager()
-    : backendBitFlag(NO_BACKEND_LOADED), activeHandle(NULL), defaultHandle(NULL)
+    : activeHandle(NULL), defaultHandle(NULL), numBackends(0)
 {
-    unsigned bkndFlag = CPU_BACKEND_MASK;
+
     for(int i=0; i<NUM_BACKENDS; ++i) {
         bkndHandles[i] = openDynLibrary(LIB_AF_BKND_NAME[i]);
         if (bkndHandles[i]) {
-            backendBitFlag |= bkndFlag;
             activeHandle = bkndHandles[i];
+            numBackends++;
         }
-        bkndFlag = bkndFlag << 1;
     }
     // Keep a copy of default order handle
     // inorder to use it in ::setBackend when
@@ -65,13 +64,15 @@ AFSymbolManager::AFSymbolManager()
 
 AFSymbolManager::~AFSymbolManager()
 {
-    unsigned bkndFlag = CPU_BACKEND_MASK;
     for(int i=0; i<NUM_BACKENDS; ++i) {
-        if (bkndFlag & backendBitFlag)
+        if (bkndHandles[i])
             closeDynLibrary(bkndHandles[i]);
-        bkndFlag = bkndFlag << 1;
     }
-    backendBitFlag = NO_BACKEND_LOADED;
+}
+
+unsigned AFSymbolManager::getBackendCount()
+{
+    return numBackends;
 }
 
 af_err AFSymbolManager::setBackend(af::Backend bknd)
@@ -83,9 +84,8 @@ af_err AFSymbolManager::setBackend(af::Backend bknd)
         } else
             return AF_ERR_LOAD_LIB;
     }
-    unsigned bkndFlag = CPU_BACKEND_MASK;
     unsigned idx = bknd - 1;
-    if((bkndFlag << idx) & backendBitFlag) {
+    if(bkndHandles[idx]) {
         activeHandle = bkndHandles[idx];
         return AF_SUCCESS;
     } else {
diff --git a/src/api/hapi/symbol_manager.hpp b/src/api/hapi/symbol_manager.hpp
index ff6e7df..8eac55b 100644
--- a/src/api/hapi/symbol_manager.hpp
+++ b/src/api/hapi/symbol_manager.hpp
@@ -18,9 +18,7 @@ typedef HMODULE LibHandle;
 typedef void* LibHandle;
 #endif
 
-const unsigned NUM_BACKENDS = 3;
-const unsigned NO_BACKEND_LOADED = 0x0000;
-const unsigned CPU_BACKEND_MASK = 0x0001;
+const int NUM_BACKENDS = 3;
 
 class AFSymbolManager {
     public:
@@ -28,6 +26,8 @@ class AFSymbolManager {
 
         ~AFSymbolManager();
 
+        unsigned getBackendCount();
+
         af_err setBackend(af::Backend bnkd);
 
         template<typename... CalleeArgs>
@@ -56,15 +56,11 @@ class AFSymbolManager {
         void operator=(AFSymbolManager const&);
 
     private:
-        /* The following bit flag represents which
-         * backends are available. 32-bits and 32 backends
-         * LSB - CPU, next one CUDA, next one OpenCL and so on.  */
-        unsigned backendBitFlag;
-
         LibHandle bkndHandles[NUM_BACKENDS];
 
         LibHandle activeHandle;
         LibHandle defaultHandle;
+        unsigned numBackends;
 };
 
 #if defined(OS_WIN)

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