[arrayfire] 229/284: Functions to get opencl device type and platforms

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Sun Feb 7 18:59:36 UTC 2016


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

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

commit 5fba37c972d1a1bf92bc8bf5870a6442a1a6db32
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Mon Jan 18 17:36:14 2016 -0500

    Functions to get opencl device type and platforms
    
    - Also use this mechanism for checking for particular device type / platform
---
 include/af/opencl.h                | 77 ++++++++++++++++++++++++++++++++++++--
 src/backend/opencl/magma/getrs.cpp |  4 +-
 src/backend/opencl/platform.cpp    | 44 ++++++++++++++++++++++
 src/backend/opencl/platform.hpp    |  8 ++++
 src/backend/opencl/solve.cpp       |  8 ++--
 test/ocl_ext_context.cpp           | 19 ++++++++++
 6 files changed, 150 insertions(+), 10 deletions(-)

diff --git a/include/af/opencl.h b/include/af/opencl.h
index 88e47d2..16b85d7 100644
--- a/include/af/opencl.h
+++ b/include/af/opencl.h
@@ -7,6 +7,7 @@
  * http://arrayfire.com/licenses/BSD-3-Clause
  ********************************************************/
 
+#pragma once
 #if defined(__APPLE__) || defined(__MACOSX)
 #include <OpenCL/cl.h>
 #else
@@ -19,6 +20,29 @@
 extern "C" {
 #endif
 
+#if AF_API_VERSION >= 33
+typedef enum
+{
+    AFCL_DEVICE_TYPE_CPU     = CL_DEVICE_TYPE_CPU,
+    AFCL_DEVICE_TYPE_GPU     = CL_DEVICE_TYPE_GPU,
+    AFCL_DEVICE_TYPE_ACC     = CL_DEVICE_TYPE_ACCELERATOR,
+    AFCL_DEVICE_TYPE_UNKNOWN = -1
+} afcl_device_type;
+#endif
+
+#if AF_API_VERSION >= 33
+typedef enum
+{
+    AFCL_PLATFORM_AMD     = 0,
+    AFCL_PLATFORM_APPLE   = 1,
+    AFCL_PLATFORM_INTEL   = 2,
+    AFCL_PLATFORM_NVIDIA  = 3,
+    AFCL_PLATFORM_BEIGNET = 4,
+    AFCL_PLATFORM_POCL    = 5,
+    AFCL_PLATFORM_UNKNOWN = -1
+} afcl_platform;
+#endif
+
 /**
     \ingroup opencl_mat
     @{
@@ -110,6 +134,20 @@ AFAPI af_err afcl_set_device_context(cl_device_id dev, cl_context ctx);
 AFAPI af_err afcl_delete_device_context(cl_device_id dev, cl_context ctx);
 #endif
 
+#if AF_API_VERSION >= 33
+/**
+   Get the type of the current device
+*/
+AFAPI af_err afcl_get_device_type(afcl_device_type *res);
+#endif
+
+#if AF_API_VERSION >= 33
+/**
+   Get the platform of the current device
+*/
+AFAPI af_err afcl_get_platform(afcl_platform *res);
+#endif
+
 /**
   @}
 */
@@ -253,6 +291,38 @@ static inline void deleteDevice(cl_device_id dev, cl_context ctx)
 }
 #endif
 
+
+#if AF_API_VERSION >= 33
+ typedef afcl_device_type deviceType;
+ typedef afcl_platform platform;
+#endif
+
+#if AF_API_VERSION >= 33
+/**
+   Get the type of the current device
+*/
+static inline deviceType getDeviceType()
+{
+    afcl_device_type res = AFCL_DEVICE_TYPE_UNKNOWN;
+    af_err err = afcl_get_device_type(&res);
+    if (err!=AF_SUCCESS) throw af::exception("Failed to get OpenCL device type");
+    return res;
+}
+#endif
+
+#if AF_API_VERSION >= 33
+/**
+   Get the type of the current device
+*/
+static inline platform getPlatform()
+{
+    afcl_platform res = AFCL_PLATFORM_UNKNOWN;
+    af_err err = afcl_get_platform(&res);
+    if (err!=AF_SUCCESS) throw af::exception("Failed to get OpenCL platform");
+    return res;
+}
+#endif
+
  /**
  Create an af::array object from an OpenCL cl_mem buffer
 
@@ -369,15 +439,15 @@ static inline void deleteDevice(cl_device_id dev, cl_context ctx)
      return afcl::array(af::dim4(dim0, dim1, dim2, dim3), buf, type, retain);
  }
 
- /**
+/**
    @}
- */
-
+*/
 }
 
 namespace af
 {
 
+#if !defined(AF_OPENCL)
 template<> AFAPI cl_mem *array::device() const
 {
     cl_mem *mem = new cl_mem;
@@ -385,6 +455,7 @@ template<> AFAPI cl_mem *array::device() const
     if (err != AF_SUCCESS) throw af::exception("Failed to get cl_mem from array object");
     return mem;
 }
+#endif
 
 }
 
diff --git a/src/backend/opencl/magma/getrs.cpp b/src/backend/opencl/magma/getrs.cpp
index 1dc106c..eb28a51 100644
--- a/src/backend/opencl/magma/getrs.cpp
+++ b/src/backend/opencl/magma/getrs.cpp
@@ -61,6 +61,7 @@
 #include <platform.hpp>
 #include <algorithm>
 #include <string>
+#include <af/opencl.h>
 
 template<typename Ty>  magma_int_t
 magma_getrs_gpu(magma_trans_t trans, magma_int_t n, magma_int_t nrhs,
@@ -168,8 +169,7 @@ magma_getrs_gpu(magma_trans_t trans, magma_int_t n, magma_int_t nrhs,
     clblasTranspose cltrans =(trans == MagmaNoTrans) ? clblasNoTrans :
         (trans == MagmaTrans ? clblasTrans : clblasConjTrans);
 
-    std::string pName = opencl::getPlatformName(opencl::getDevice());
-    bool cond = pName.find("NVIDIA") != std::string::npos;
+    bool cond = opencl::getActivePlatform() == AFCL_PLATFORM_NVIDIA;
     cl_mem dAT = 0;
     if (nrhs > 1 && cond) {
         magma_malloc<Ty>(&dAT, n * n);
diff --git a/src/backend/opencl/platform.cpp b/src/backend/opencl/platform.cpp
index d7c3e1c..884dca1 100644
--- a/src/backend/opencl/platform.cpp
+++ b/src/backend/opencl/platform.cpp
@@ -198,6 +198,25 @@ static inline bool compare_default(const Device *ldev, const Device *rdev)
     return l_mem >= r_mem;
 }
 
+static afcl::deviceType getDeviceTypeEnum(cl::Device dev)
+{
+    return (afcl::deviceType)dev.getInfo<CL_DEVICE_TYPE>();
+}
+
+
+static afcl::platform getPlatformEnum(cl::Device dev)
+{
+    std::string pname = getPlatformName(dev);
+    if (verify_present(pname, "AMD")) return AFCL_PLATFORM_AMD;
+    if (verify_present(pname, "NVIDIA")) return AFCL_PLATFORM_NVIDIA;
+    if (verify_present(pname, "INTEL")) return AFCL_PLATFORM_INTEL;
+    if (verify_present(pname, "APPLE")) return AFCL_PLATFORM_APPLE;
+    if (verify_present(pname, "BEIGNET")) return AFCL_PLATFORM_BEIGNET;
+    if (verify_present(pname, "POCL")) return AFCL_PLATFORM_POCL;
+    return AFCL_PLATFORM_UNKNOWN;
+}
+
+
 DeviceManager::DeviceManager()
     : mUserDeviceOffset(0), mActiveCtxId(0), mActiveQId(0)
 {
@@ -260,6 +279,8 @@ DeviceManager::DeviceManager()
             mContexts.push_back(ctx);
             mQueues.push_back(cq);
             mIsGLSharingOn.push_back(false);
+            mDeviceTypes.push_back(getDeviceTypeEnum(*mDevices[i]));
+            mPlatforms.push_back(getPlatformEnum(*mDevices[i]));
         }
 
         bool default_device_set = false;
@@ -437,6 +458,17 @@ int getDeviceIdFromNativeId(cl_device_id id)
     return devId;
 }
 
+int getActiveDeviceType()
+{
+    DeviceManager &instance = DeviceManager::getInstance();
+    return instance.mDeviceTypes[instance.mActiveQId];
+}
+
+int getActivePlatform()
+{
+    DeviceManager &instance = DeviceManager::getInstance();
+    return instance.mPlatforms[instance.mActiveQId];
+}
 const Context& getContext()
 {
     DeviceManager& devMngr = DeviceManager::getInstance();
@@ -731,6 +763,18 @@ bool synchronize_calls() {
 
 using namespace opencl;
 
+af_err afcl_get_device_type(afcl_device_type *res)
+{
+    *res = (afcl_device_type)getActiveDeviceType();
+    return AF_SUCCESS;
+}
+
+af_err afcl_get_platform(afcl_platform *res)
+{
+    *res = (afcl_platform)getActivePlatform();
+    return AF_SUCCESS;
+}
+
 af_err afcl_get_context(cl_context *ctx, const bool retain)
 {
     *ctx = getContext()();
diff --git a/src/backend/opencl/platform.hpp b/src/backend/opencl/platform.hpp
index 85c533f..d4f9f0e 100644
--- a/src/backend/opencl/platform.hpp
+++ b/src/backend/opencl/platform.hpp
@@ -49,6 +49,9 @@ class DeviceManager
 
     friend void removeDeviceContext(cl_device_id dev, cl_context ctx);
 
+    friend int getActiveDeviceType();
+    friend int getActivePlatform();
+
     public:
         static const unsigned MAX_DEVICES = 32;
 
@@ -77,6 +80,8 @@ class DeviceManager
         std::vector<cl::Context*>     mContexts;
         std::vector<cl::CommandQueue*>  mQueues;
         std::vector<bool>        mIsGLSharingOn;
+        std::vector<int>         mDeviceTypes;
+        std::vector<int>         mPlatforms;
         unsigned mUserDeviceOffset;
 
         unsigned mActiveCtxId;
@@ -123,4 +128,7 @@ void sync(int device);
 
 bool synchronize_calls();
 
+int getActiveDeviceType();
+int getActivePlatform();
+
 }
diff --git a/src/backend/opencl/solve.cpp b/src/backend/opencl/solve.cpp
index 4fede07..9317675 100644
--- a/src/backend/opencl/solve.cpp
+++ b/src/backend/opencl/solve.cpp
@@ -21,6 +21,7 @@
 #include <blas.hpp>
 #include <transpose.hpp>
 #include <math.hpp>
+#include <af/opencl.h>
 
 #include <algorithm>
 #include <string>
@@ -226,9 +227,7 @@ Array<T> leastSquares(const Array<T> &a, const Array<T> &b)
                               (*dT)(), tmp.getOffset() + NB * MN,
                               NB, 0, queue);
 
-
-        std::string pName = getPlatformName(getDevice());
-        if(pName.find("NVIDIA") != std::string::npos)
+        if(getActivePlatform() == AFCL_PLATFORM_NVIDIA)
         {
             Array<T> AT = transpose<T>(A, true);
             cl::Buffer* AT_buf = AT.get();
@@ -268,8 +267,7 @@ Array<T> triangleSolve(const Array<T> &A, const Array<T> &b, const af_mat_prop o
     cl_event event = 0;
     cl_command_queue queue = getQueue()();
 
-    std::string pName = getPlatformName(getDevice());
-    if(pName.find("NVIDIA") != std::string::npos && (options & AF_MAT_UPPER))
+    if(getActivePlatform() == AFCL_PLATFORM_NVIDIA && (options & AF_MAT_UPPER))
     {
         Array<T> AT = transpose<T>(A, true);
 
diff --git a/test/ocl_ext_context.cpp b/test/ocl_ext_context.cpp
index 0d4f89b..e711c63 100644
--- a/test/ocl_ext_context.cpp
+++ b/test/ocl_ext_context.cpp
@@ -105,6 +105,25 @@ TEST(OCLExtContext, pop)
     printf("%d devices after afcl::deleteDevice\n", af::getDeviceCount());
     af::info();
 }
+
+TEST(OCLCheck, DeviceType)
+{
+    afcl::deviceType devType = afcl::getDeviceType();
+    cl_device_type type = -100;
+    clGetDeviceInfo(afcl::getDeviceId(),
+                    CL_DEVICE_TYPE,
+                    sizeof(cl_device_type),
+                    &type,
+                    NULL);
+    ASSERT_EQ(type, (cl_device_type)devType);
+}
+
+TEST(OCLCheck, DevicePlatform)
+{
+    afcl::platform platform = afcl::getPlatform();
+    ASSERT_NE(platform, AFCL_PLATFORM_UNKNOWN);
+}
+
 #else
 TEST(OCLExtContext, NoopCPU)
 {

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