[arrayfire] 219/248: Encode backend info into ArrayInfo::devId
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Tue Nov 17 15:54:29 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 7ad7ce0082f622e18108a4a497d6f35b021d9501
Author: Shehzan Mohammed <shehzan at arrayfire.com>
Date: Tue Nov 10 12:03:56 2015 -0500
Encode backend info into ArrayInfo::devId
* See ArrayInfo.hpp more more details
* Added getBackend function into all backends instead of using macros
---
src/api/c/device.cpp | 18 ++----------------
src/backend/ArrayInfo.cpp | 37 +++++++++++++++++++++++++++++++++++++
src/backend/ArrayInfo.hpp | 37 ++++++++++++++++++++++++++++++++-----
src/backend/cpu/Array.cpp | 2 +-
src/backend/cpu/platform.cpp | 5 +++++
src/backend/cpu/platform.hpp | 2 ++
src/backend/cuda/Array.cpp | 2 +-
src/backend/cuda/platform.cpp | 5 +++++
src/backend/cuda/platform.hpp | 2 ++
src/backend/opencl/Array.cpp | 2 +-
src/backend/opencl/platform.cpp | 5 +++++
src/backend/opencl/platform.hpp | 2 ++
12 files changed, 95 insertions(+), 24 deletions(-)
diff --git a/src/api/c/device.cpp b/src/api/c/device.cpp
index 80b8733..f72a6cd 100644
--- a/src/api/c/device.cpp
+++ b/src/api/c/device.cpp
@@ -23,15 +23,7 @@ using namespace detail;
af_err af_set_backend(const af_backend bknd)
{
try {
-#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
+ ARG_ASSERT(0, bknd==getBackend());
}
CATCHALL;
@@ -46,13 +38,7 @@ af_err af_get_backend_count(unsigned* num_backends)
af_err af_get_available_backends(int* result)
{
-#if defined(AF_CPU)
- *result = AF_BACKEND_CPU;
-#elif defined(AF_CUDA)
- *result = AF_BACKEND_CUDA;
-#elif defined(AF_OPENCL)
- *result = AF_BACKEND_OPENCL;
-#endif
+ *result = getBackend();
return AF_SUCCESS;
}
diff --git a/src/backend/ArrayInfo.cpp b/src/backend/ArrayInfo.cpp
index 8aea983..219bc19 100644
--- a/src/backend/ArrayInfo.cpp
+++ b/src/backend/ArrayInfo.cpp
@@ -13,6 +13,9 @@
#include <functional>
#include <err_common.hpp>
+#include <backend.hpp>
+#include <platform.hpp>
+
using af::dim4;
dim_t
@@ -57,6 +60,40 @@ dim4 calcStrides(const dim4 &parentDim)
return out;
}
+int ArrayInfo::getDevId() const
+{
+ // The actual device ID is only stored in the first 4 bits of devId
+ // See ArrayInfo.hpp for more
+ return devId & 0xf;
+}
+
+void ArrayInfo::setId(int id) const
+{
+ // 1 << (backendId + 3) sets the 4th, 5th or 6th bit of devId to 1
+ // for CPU, CUDA and OpenCL respectively
+ // See ArrayInfo.hpp for more
+ int backendId = detail::getBackend() >> 1; // Convert enums 1, 2, 4 to ints 0, 1, 2
+ const_cast<ArrayInfo *>(this)->setId(id | 1 << (backendId + 3));
+}
+
+void ArrayInfo::setId(int id)
+{
+ // 1 << (backendId + 3) sets the 4th, 5th or 6th bit of devId to 1
+ // for CPU, CUDA and OpenCL respectively
+ // See ArrayInfo.hpp for more
+ int backendId = detail::getBackend() >> 1; // Convert enums 1, 2, 4 to ints 0, 1, 2
+ devId = id | 1 << (backendId + 3);
+}
+
+af_backend ArrayInfo::getBackendId() const
+{
+ // devId >> 3 converts the backend info to 1, 2, 4 which are enums
+ // for CPU, CUDA and OpenCL respectively
+ // See ArrayInfo.hpp for more
+ int backendId = devId >> 3;
+ return (af_backend)backendId;
+}
+
void ArrayInfo::modStrides(const dim4 &newStrides)
{
dim_strides = newStrides;
diff --git a/src/backend/ArrayInfo.hpp b/src/backend/ArrayInfo.hpp
index f6d2663..ca6fcd3 100644
--- a/src/backend/ArrayInfo.hpp
+++ b/src/backend/ArrayInfo.hpp
@@ -14,6 +14,7 @@
#include <af/dim4.hpp>
#include <af/device.h>
#include <vector>
+#include <cstddef>
dim_t
calcOffset(const af::dim4 &strides, const af::dim4 &offsets);
@@ -30,6 +31,20 @@ af::dim4 getOutDims(const af::dim4 &ldims, const af::dim4 &rdims, bool batchMode
class ArrayInfo
{
private:
+ // The devId variable stores information about the deviceId as well as the backend.
+ // The 4 LSBs (0-3) are used to store the device ID.
+ // The 4th LSB is set to 1 if backend is CPU
+ // The 5th LSB is set to 1 if backend is CUDA
+ // The 6th LSB is set to 1 if backend is OpenCL
+ // This information can be retrieved directly from an af_array by doing
+ // int* devId = reinterpret_cast<int*>(a); // a is an af_array
+ // af_backend backendID = *devId >> 3; // Returns 1, 2, 4 for CPU, CUDA or OpenCL respectively
+ // int deviceID = *devId & 0xf; // Returns devices ID between 0-15
+ // This is possible by doing a static_assert on devId
+ //
+ // This can be changed in the future if the need arises for more devices as this
+ // implementation is internal. Make sure to change the bit shift ops when
+ // such a change is being made
int devId;
af_dtype type;
af::dim4 dim_size;
@@ -42,7 +57,16 @@ public:
dim_size(size),
dim_offsets(offset),
dim_strides(stride)
- { af_init(); }
+ {
+ af_init();
+ setId(id);
+#if __cplusplus > 199711l
+ static_assert(offsetof(ArrayInfo, devId) == 0,
+ "ArrayInfo::devId must be the first member variable of ArrayInfo. \
+ devId is used to encode the backend into the integer. \
+ This is then used in the unified backend to check mismatched arrays.");
+#endif
+ }
#if __cplusplus > 199711L
//Copy constructors are deprecated if there is a
@@ -55,16 +79,19 @@ public:
const af::dim4& offsets() const { return dim_offsets; }
- const af::dim4& strides() const { return dim_strides; }
+ const af::dim4& strides() const { return dim_strides; }
size_t elements() const { return dim_size.elements(); }
size_t ndims() const { return dim_size.ndims(); }
const af::dim4& dims() const { return dim_size; }
- int getDevId() const { return devId; }
+ int getDevId() const;
+
+ void setId(int id) const;
+
+ void setId(int id);
- void setId(int id) const { const_cast<ArrayInfo *>(this)->setId(id); }
- void setId(int id) { devId = id; }
+ af_backend getBackendId() const;
void resetInfo(const af::dim4& dims)
{
diff --git a/src/backend/cpu/Array.cpp b/src/backend/cpu/Array.cpp
index 096d75f..5321137 100644
--- a/src/backend/cpu/Array.cpp
+++ b/src/backend/cpu/Array.cpp
@@ -49,7 +49,7 @@ namespace cpu
template<typename T>
Array<T>::Array(af::dim4 dims, TNJ::Node_ptr n) :
- info(-1, dims, af::dim4(0,0,0,0), calcStrides(dims), (af_dtype)dtype_traits<T>::af_type),
+ info(getActiveDeviceId(), dims, af::dim4(0,0,0,0), calcStrides(dims), (af_dtype)dtype_traits<T>::af_type),
data(), data_dims(dims),
node(n), offset(0), ready(false), owner(true)
{
diff --git a/src/backend/cpu/platform.cpp b/src/backend/cpu/platform.cpp
index ac8ec54..fc782ea 100644
--- a/src/backend/cpu/platform.cpp
+++ b/src/backend/cpu/platform.cpp
@@ -172,6 +172,11 @@ CPUInfo::CPUInfo()
namespace cpu
{
+int getBackend()
+{
+ return AF_BACKEND_CPU;
+}
+
static const std::string get_system(void)
{
std::string arch = (sizeof(void *) == 4) ? "32-bit " : "64-bit ";
diff --git a/src/backend/cpu/platform.hpp b/src/backend/cpu/platform.hpp
index e899837..2e52cd1 100644
--- a/src/backend/cpu/platform.hpp
+++ b/src/backend/cpu/platform.hpp
@@ -10,6 +10,8 @@
#include <string>
namespace cpu {
+ int getBackend();
+
std::string getInfo();
bool isDoubleSupported(int device);
diff --git a/src/backend/cuda/Array.cpp b/src/backend/cuda/Array.cpp
index 23a7512..d7dbec5 100644
--- a/src/backend/cuda/Array.cpp
+++ b/src/backend/cuda/Array.cpp
@@ -75,7 +75,7 @@ namespace cuda
template<typename T>
Array<T>::Array(af::dim4 dims, JIT::Node_ptr n) :
- info(-1, dims, af::dim4(0,0,0,0), calcStrides(dims), (af_dtype)dtype_traits<T>::af_type),
+ info(getActiveDeviceId(), dims, af::dim4(0,0,0,0), calcStrides(dims), (af_dtype)dtype_traits<T>::af_type),
data(), data_dims(dims),
node(n), offset(0), ready(false), owner(true)
{
diff --git a/src/backend/cuda/platform.cpp b/src/backend/cuda/platform.cpp
index 704f937..c154a7e 100644
--- a/src/backend/cuda/platform.cpp
+++ b/src/backend/cuda/platform.cpp
@@ -141,6 +141,11 @@ static inline string toString(T val)
///////////////////////////////////////////////////////////////////////////
// Wrapper Functions
///////////////////////////////////////////////////////////////////////////
+int getBackend()
+{
+ return AF_BACKEND_CUDA;
+}
+
string getInfo()
{
ostringstream info;
diff --git a/src/backend/cuda/platform.hpp b/src/backend/cuda/platform.hpp
index b07ee97..7b64968 100644
--- a/src/backend/cuda/platform.hpp
+++ b/src/backend/cuda/platform.hpp
@@ -20,6 +20,8 @@
namespace cuda
{
+int getBackend();
+
std::string getInfo();
std::string getDeviceInfo(int device);
diff --git a/src/backend/opencl/Array.cpp b/src/backend/opencl/Array.cpp
index 4498f07..466666f 100644
--- a/src/backend/opencl/Array.cpp
+++ b/src/backend/opencl/Array.cpp
@@ -38,7 +38,7 @@ namespace opencl
template<typename T>
Array<T>::Array(af::dim4 dims, JIT::Node_ptr n) :
- info(-1, dims, af::dim4(0,0,0,0), calcStrides(dims), (af_dtype)dtype_traits<T>::af_type),
+ info(getActiveDeviceId(), dims, af::dim4(0,0,0,0), calcStrides(dims), (af_dtype)dtype_traits<T>::af_type),
data(),
data_dims(dims),
node(n), offset(0), ready(false), owner(true)
diff --git a/src/backend/opencl/platform.cpp b/src/backend/opencl/platform.cpp
index ebce873..85364c4 100644
--- a/src/backend/opencl/platform.cpp
+++ b/src/backend/opencl/platform.cpp
@@ -74,6 +74,11 @@ static const std::string get_system(void)
#endif
}
+int getBackend()
+{
+ return AF_BACKEND_OPENCL;
+}
+
DeviceManager& DeviceManager::getInstance()
{
static DeviceManager my_instance;
diff --git a/src/backend/opencl/platform.hpp b/src/backend/opencl/platform.hpp
index d59852e..90f57ae 100644
--- a/src/backend/opencl/platform.hpp
+++ b/src/backend/opencl/platform.hpp
@@ -78,6 +78,8 @@ class DeviceManager
unsigned mActiveQId;
};
+int getBackend();
+
std::string getInfo();
int getDeviceCount();
--
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