cf4ocl (C Framework for OpenCL)  v2.1.0
Object-oriented framework for developing and benchmarking OpenCL projects in C/C++
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ccl_platform_wrapper.c
Go to the documentation of this file.
1 /*
2  * This file is part of cf4ocl (C Framework for OpenCL).
3  *
4  * cf4ocl is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * cf4ocl is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with cf4ocl. If not, see
16  * <http://www.gnu.org/licenses/>.
17  * */
18 
30 #include "ccl_platform_wrapper.h"
31 #include "_ccl_abstract_wrapper.h"
32 #include "_ccl_abstract_dev_container_wrapper.h"
33 #include "_ccl_defs.h"
34 
40 struct ccl_platform {
41 
46  CCLDevContainer base;
47 
48 };
49 
64 static CCLWrapperInfo* ccl_platform_get_cldevices(
65  CCLDevContainer* devcon, CCLErr** err) {
66 
67  /* Make sure err is NULL or it is not set. */
68  g_return_val_if_fail(err == NULL || *err == NULL, NULL);
69 
70  /* Make sure devcon is not NULL. */
71  g_return_val_if_fail(devcon != NULL, NULL);
72 
73  CCLWrapperInfo* info = NULL;
74 
75  cl_int ocl_status;
76 
77  /* Determine number of devices. */
78  ocl_status = clGetDeviceIDs(devcon->base.cl_object,
79  CL_DEVICE_TYPE_ALL, 0, NULL, &devcon->num_devices);
81  CL_SUCCESS != ocl_status, ocl_status, error_handler,
82  "%s: get number of devices (OpenCL error %d: %s).",
83  CCL_STRD, ocl_status, ccl_err(ocl_status));
84 
85  /* Create info object with size in bytes of array of device IDs. */
86  info = ccl_wrapper_info_new(
87  sizeof(cl_device_id) * devcon->num_devices);
88 
89  /* Get existing device IDs. */
90  ocl_status = clGetDeviceIDs(devcon->base.cl_object,
91  CL_DEVICE_TYPE_ALL, devcon->num_devices, info->value, NULL);
93  CL_SUCCESS != ocl_status, ocl_status, error_handler,
94  "%s: get device IDs (OpenCL error %d: %s).",
95  CCL_STRD, ocl_status, ccl_err(ocl_status));
96 
97  /* Add device list to info table, so that it will be
98  * automatically released. Because the cl_platform_id object
99  * doesn't have a CL_PLATFORM_DEVICES parameter, we keep this info
100  * referenced has CL_CONTEXT_DEVICES. */
101  ccl_wrapper_add_info(
102  (CCLWrapper*) devcon, CL_CONTEXT_DEVICES, info);
103 
104  /* If we got here, everything is OK. */
105  g_assert(err == NULL || *err == NULL);
106  goto finish;
107 
108 error_handler:
109  /* If we got here there was an error, verify that it is so. */
110  g_assert(err == NULL || *err != NULL);
111 
112  /* Free info if it was created. */
113  if (info != NULL) ccl_wrapper_info_destroy(info);
114 
115 finish:
116 
117  /* Terminate function. */
118  return info;
119 }
120 
143 CCL_EXPORT
144 CCLPlatform* ccl_platform_new_wrap(cl_platform_id platform) {
145 
146  return (CCLPlatform*) ccl_wrapper_new(
147  CCL_PLATFORM, (void*) platform, sizeof(CCLPlatform));
148 
149 }
150 
163 CCL_EXPORT
165 
166  /* Make sure dev is not NULL. */
167  g_return_val_if_fail(dev != NULL, NULL);
168  /* Make sure err is NULL or it is not set. */
169  g_return_val_if_fail(err == NULL || *err == NULL, NULL);
170 
171  /* The OpenCL platform_id object. */
172  cl_platform_id platform_id;
173  /* The platform wrapper to return. */
174  CCLPlatform* platf = NULL;
175  /* Internal error object. */
176  CCLErr* err_internal = NULL;
177 
178  /* Get OpenCL platform_id object from device. */
179  platform_id = ccl_device_get_info_scalar(
180  dev, CL_DEVICE_PLATFORM, cl_platform_id, &err_internal);
181  ccl_if_err_propagate_goto(err, err_internal, error_handler);
182 
183  /* Create/get the platform wrapper. */
184  platf = ccl_platform_new_wrap(platform_id);
185 
186  /* If we got here, everything is OK. */
187  g_assert(err == NULL || *err == NULL);
188  goto finish;
189 
190 error_handler:
191  /* If we got here there was an error, verify that it is so. */
192  g_assert(err == NULL || *err != NULL);
193 
194 finish:
195 
196  /* Return the device platform wrapper. */
197  return platf;
198 
199 }
200 
209 CCL_EXPORT
211 
212  ccl_wrapper_unref((CCLWrapper*) platf, sizeof(CCLPlatform),
213  (ccl_wrapper_release_fields) ccl_dev_container_release_devices,
214  NULL, NULL);
215 
216 }
217 
236 CCL_EXPORT
238  CCLPlatform* platf, CCLErr** err) {
239 
240  /* Make sure platf is not NULL. */
241  g_return_val_if_fail(platf != NULL, 0);
242  /* Make sure err is NULL or it is not set. */
243  g_return_val_if_fail(err == NULL || *err == NULL, 0);
244 
245  char* ver_str;
246  cl_uint ver = 0;
247 
248  /* Get version string which has the format "OpenCL x.x ..." */
250  platf, CL_PLATFORM_VERSION, err);
251 
252  if (ver_str != NULL) {
253  ver = /* strlen("OpenCL ") == 7 */
254  atoi(ver_str + 7) * 100 + /* Major version. */
255  atoi(ver_str + 9) * 10; /* Minor version. */
256  }
257  return ver;
258 }
259 
276  CCLPlatform* platf, CCLErr** err) {
277 
278  return ccl_dev_container_get_all_devices(
279  (CCLDevContainer*) platf, ccl_platform_get_cldevices, err);
280 }
281 
294 CCL_EXPORT
296  CCLPlatform* platf, cl_uint index, CCLErr** err) {
297 
298  return ccl_dev_container_get_device((CCLDevContainer*) platf,
299  ccl_platform_get_cldevices, index, err);
300 
301 }
302 
314 CCL_EXPORT
316 
317  return ccl_dev_container_get_num_devices((CCLDevContainer*) platf,
318  ccl_platform_get_cldevices, err);
319 
320 }
321 
#define CCL_OCL_ERROR
Resolves to error category identifying string, in this case an error in the OpenCL library...
Definition: ccl_common.h:324
#define ccl_platform_get_info_string(platf, param_name, err)
Helper macro which gets a platform information string.
#define ccl_if_err_create_goto(err, quark, error_condition, error_code, label, msg,...)
If error is detected (error_code != no_error_code), create an error object (CCLErr) and go to the spe...
Definition: _ccl_defs.h:91
Definition of a wrapper class and its methods for OpenCL platform objects.
Useful definitions used internally by cf4ocl.
#define ccl_if_err_propagate_goto(err_dest, err_src, label)
Same as ccl_if_err_goto(), but rethrows error in a source CCLErr object to a new destination CCLErr o...
Definition: _ccl_defs.h:120
const char * ccl_err(int code)
Convert OpenCL error code to a readable string.
Definition: ccl_errors.c:118
Base class for wrappers which contain devices, i.e., CCLPlatform, CCLProgram and CCLContext.
Platform object.
Definition: ccl_common.h:106
Base class for all OpenCL wrappers.
CCLPlatform * ccl_platform_new_from_device(CCLDevice *dev, CCLErr **err)
Get the platform wrapper for the given device wrapper.
CCLPlatform * ccl_platform_new_wrap(cl_platform_id platform)
Get the platform wrapper for the given OpenCL platform.
CCLDevice *const * ccl_platform_get_all_devices(CCLPlatform *platf, CCLErr **err)
Get all device wrappers in platform.
#define ccl_device_get_info_scalar(dev, param_name, param_type, err)
Macro which returns a scalar device information value.
void * value
Object information.
cl_uint ccl_platform_get_num_devices(CCLPlatform *platf, CCLErr **err)
Return number of devices in platform.
void ccl_platform_destroy(CCLPlatform *platf)
Decrements the reference count of the platform wrapper object.
Class which represents information about a wrapped OpenCL object.
GError CCLErr
Error handling class.
Definition: ccl_common.h:291
Device wrapper class.
The platform wrapper class.
CCLDevice * ccl_platform_get_device(CCLPlatform *platf, cl_uint index, CCLErr **err)
Get CCLDevice wrapper at given index.
cl_uint ccl_platform_get_opencl_version(CCLPlatform *platf, CCLErr **err)
Get the OpenCL version of this platform.