summaryrefslogtreecommitdiff
path: root/src/boring_parts.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/boring_parts.cc')
-rw-r--r--src/boring_parts.cc101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/boring_parts.cc b/src/boring_parts.cc
new file mode 100644
index 0000000..3affae9
--- /dev/null
+++ b/src/boring_parts.cc
@@ -0,0 +1,101 @@
+#include "boring_parts.h"
+
+#define RETURN_IF_FAIL(expr) do { \
+ int res=(expr); \
+ if ( res != 0 ) return res; \
+} while(0)
+
+// TODO : print streamsdk::getOpenCLErrorCodeStr(res)
+#define CL_RETURN_VAL_IF_FAIL(val, expr) do { \
+ cl_int res=(expr); \
+ if ( res != CL_SUCCESS ) { \
+ std::cerr << "file " << __FILE__ << ": line " << __LINE__ << " (" << __PRETTY_FUNCTION__ \
+ << "): '" << "expr" << "' failed (return code : " << res << ")" << std::endl; \
+ return val; \
+ } \
+} while(0)
+
+int initLibs() {
+
+ // FIXME : unused
+ RETURN_IF_FAIL( initOpenCL() );
+
+ return 0;
+}
+
+int initOpenCL() {
+ cl_uint id, numPlatforms, numDevices;
+ char pbuf[100];
+ std::string dTypeStr;
+ cl_platform_id *platforms, platform;
+ cl_device_id *devices, device;
+
+ // Get platform count
+ CL_RETURN_VAL_IF_FAIL(1,
+ clGetPlatformIDs(0, NULL, &numPlatforms)
+ );
+
+ std::cout << "Detected " << numPlatforms << " platform(s)" << std::endl;
+ if ( ! ( numPlatforms > 0 ) ) return 2;
+
+ // Allocate room for all platform IDs
+ platforms = new cl_platform_id[numPlatforms];
+
+ // Get platform IDs
+ CL_RETURN_VAL_IF_FAIL(3,
+ clGetPlatformIDs(numPlatforms, platforms, &numPlatforms)
+ );
+
+ // Enumerate platforms and grab informations
+ for(id=0;id<numPlatforms;id++) {
+ CL_RETURN_VAL_IF_FAIL(4,
+ clGetPlatformInfo(platforms[id], CL_PLATFORM_VENDOR, sizeof(pbuf), pbuf, NULL)
+ );
+ std::cout << "Platform " << id << " : " << pbuf << std::endl;
+ }
+
+ // Take the first one FIXME : do more !
+ platform = platforms[0];
+
+ // Get device count
+ CL_RETURN_VAL_IF_FAIL(11,
+ clGetDeviceIDs(platform,CL_DEVICE_TYPE_ALL,0,NULL,&numDevices)
+ );
+
+ std::cout << "\tDetected " << numDevices << " device(s) for platform " << platform << std::endl;
+ if ( ! ( numDevices > 0 ) ) return 12;
+
+ // Allocate room for all devices IDs
+ devices = new cl_device_id[numDevices];
+
+ // Get devices IDs
+ CL_RETURN_VAL_IF_FAIL(13,
+ clGetDeviceIDs(platform,CL_DEVICE_TYPE_ALL,numDevices,devices,&numDevices)
+ );
+
+ // Enumerate devices and grab informations
+ for(id=0;id<numDevices;id++) {
+ cl_device_type dType;
+
+ CL_RETURN_VAL_IF_FAIL(14,
+ clGetDeviceInfo(devices[id], CL_DEVICE_TYPE, sizeof(dType), &dType, NULL)
+ );
+
+ switch(dType) {
+ case CL_DEVICE_TYPE_GPU: dTypeStr="gpu"; break;
+ case CL_DEVICE_TYPE_CPU: dTypeStr="cpu"; break;
+ default: dTypeStr="(not supported)"; break;
+ }
+ std::cout << "\tDevice " << id << " type is " << dTypeStr << std::endl;
+ }
+
+ // Take the first one FIXME : do more !
+ device=devices[0];
+ std::cout << "\tWill using device 0 (" << device << ")" << std::endl;
+
+ delete [] devices;
+ delete [] platforms;
+
+ return 0;
+}
+