comparison opencl/platform.d @ 0:3cea44337083

initial commit
author Trass3r
date Wed, 02 Dec 2009 17:40:59 +0100
parents
children c78ffc9a7434
comparison
equal deleted inserted replaced
-1:000000000000 0:3cea44337083
1 /**
2 *
3 */
4 module opencl.platform;
5
6 public import opencl.c.opencl;
7 import opencl.device;
8 import opencl.error;
9 import opencl.wrapper;
10
11 //! Platform class
12 class CLPlatform : CLWrapper!(cl_platform_id, clGetPlatformInfo)
13 {
14 public:
15 /// constructor
16 this(cl_platform_id platform)
17 {
18 _object = platform;
19 }
20
21 /// get the platform name
22 string name()
23 {
24 return getStringInfo(CL_PLATFORM_NAME);
25 }
26
27 /// get platform vendor
28 string vendor()
29 {
30 return getStringInfo(CL_PLATFORM_VENDOR);
31 }
32
33 /// get platform version
34 string clversion()
35 {
36 return getStringInfo(CL_PLATFORM_VERSION);
37 }
38
39 /// get platform profile
40 string profile()
41 {
42 return getStringInfo(CL_PLATFORM_PROFILE);
43 }
44
45 /// get platform extensions
46 string extensions()
47 {
48 return getStringInfo(CL_PLATFORM_EXTENSIONS);
49 }
50
51 /// returns a list of all devices available on the platform matching deviceType
52 CLDevice[] getDevices(cl_device_type deviceType)
53 {
54 cl_uint numDevices;
55 cl_int res;
56
57 // get number of devices
58 res = clGetDeviceIDs(_object, deviceType, 0, null, &numDevices);
59 switch(res)
60 {
61 case CL_SUCCESS:
62 break;
63 case CL_INVALID_PLATFORM:
64 throw new CLInvalidPlatformException();
65 break;
66 case CL_INVALID_DEVICE_TYPE:
67 throw new CLInvalidDeviceTypeException("There's no such device type");
68 break;
69 case CL_DEVICE_NOT_FOUND:
70 throw new CLDeviceNotFoundException("Couldn't find an OpenCL device matching the given type");
71 break;
72 default:
73 throw new CLException(res, "unexpected error while getting device count");
74 }
75
76 // get device IDs
77 auto deviceIDs = new cl_device_id[numDevices];
78 res = clGetDeviceIDs(_object, deviceType, deviceIDs.length, deviceIDs.ptr, null);
79 if(res != CL_SUCCESS)
80 throw new CLException(res);
81
82 // create CLDevice array
83 auto devices = new CLDevice[numDevices];
84 for(uint i=0; i<numDevices; i++)
85 devices[i] = new CLDevice(this, deviceIDs[i]);
86
87 return devices;
88 }
89
90 /// returns a list of all devices
91 CLDevice[] allDevices() {return getDevices(CL_DEVICE_TYPE_ALL);}
92
93 /// returns a list of all CPU devices
94 CLDevice[] cpuDevices() {return getDevices(CL_DEVICE_TYPE_CPU);}
95
96 /// returns a list of all GPU devices
97 CLDevice[] gpuDevices() {return getDevices(CL_DEVICE_TYPE_GPU);}
98
99 /// returns a list of all accelerator devices
100 CLDevice[] accelDevices() {return getDevices(CL_DEVICE_TYPE_ACCELERATOR);}
101
102 /// get an array of all available platforms
103 static CLPlatform[] getPlatforms()
104 {
105 cl_uint numPlatforms;
106 cl_int res;
107
108 // get number of platforms
109 res = clGetPlatformIDs(0, null, &numPlatforms);
110 if(res != CL_SUCCESS)
111 throw new CLInvalidValueException();
112
113 // get platform IDs
114 auto platformIDs = new cl_platform_id[numPlatforms];
115 res = clGetPlatformIDs(platformIDs.length, platformIDs.ptr, null);
116 if(res != CL_SUCCESS)
117 throw new CLInvalidValueException();
118
119 // create CLPlatform array
120 auto platforms = new CLPlatform[numPlatforms];
121 for(uint i=0; i<numPlatforms; i++)
122 platforms[i] = new CLPlatform(platformIDs[i]);
123
124 return platforms;
125 }
126 }