83 *filters = g_ptr_array_new();
89 filter->function =
function;
94 g_ptr_array_add(*filters, filter);
111 static gchar** ccl_get_device_strings_from_array(
115 g_return_val_if_fail(err == NULL || *err == NULL, NULL);
118 g_return_val_if_fail(devices != NULL, NULL);
121 CCLErr* err_internal = NULL;
127 dev_strings = g_new0(gchar*, devices->len + 1);
130 for (guint i = 0; i < devices->len; i++) {
134 devices->pdata[i], CL_DEVICE_NAME,
char*, &err_internal);
139 devices->pdata[i], &err_internal);
144 platf_wrap, CL_PLATFORM_NAME, &err_internal);
148 dev_strings[i] = g_strdup_printf(
149 "%d. %s [%s]", i, name, platf_name);
157 g_assert(err == NULL || *err == NULL);
162 g_assert(err == NULL || *err != NULL);
165 g_strfreev(dev_strings);
187 cl_int selected,
CCLErr** err) {
190 g_return_if_fail(err == NULL || *err == NULL);
193 g_return_if_fail(devices != NULL);
196 CCLErr* err_internal = NULL;
202 dev_strings = ccl_get_device_strings_from_array(
203 devices, &err_internal);
209 for (cl_int i = 0; i < (cl_int) devices->len; i++) {
221 g_printf(
" %s %s\n", sel_str, dev_strings[i]);
226 g_assert(err == NULL || *err == NULL);
231 g_assert(err == NULL || *err != NULL);
236 g_strfreev(dev_strings);
255 g_return_val_if_fail(err == NULL || *err == NULL, -1);
257 g_return_val_if_fail(devices != NULL, -1);
264 CCLErr* err_internal = NULL;
267 g_printf(
"\nList of available OpenCL devices:\n");
268 ccl_devsel_dep_menu_list(devices, -1, &err_internal);
272 if (devices->len == 1) {
277 g_printf(
"\n (?) Select device (0-%d) > ", devices->len - 1);
279 result = scanf(
"%u", &index);
282 do { c = getchar(); }
while (c !=
'\n' && c != EOF);
285 if ((index >= 0) && (index < (cl_int) devices->len))
289 g_printf(
" (!) Invalid choice, please insert a value " \
290 "between 0 and %u.\n", devices->len - 1);
295 g_assert(err == NULL || *err == NULL);
300 g_assert(err == NULL || *err != NULL);
333 g_return_val_if_fail(err == NULL || *err == NULL, NULL);
346 GPtrArray* devices = NULL;
352 CCLErr* err_internal = NULL;
363 devices = g_ptr_array_new_with_free_func(
367 for (guint i = 0; i < num_platfs; i++) {
374 platform, &err_internal);
377 if ((err_internal) && (err_internal->domain ==
CCL_OCL_ERROR) &&
378 (err_internal->code == CL_DEVICE_NOT_FOUND)) {
381 g_clear_error(&err_internal);
389 for (guint j = 0; j < num_devices; j++) {
396 g_ptr_array_add(devices, (gpointer) device);
407 g_assert(err == NULL || *err == NULL);
412 g_assert(err == NULL || *err != NULL);
435 g_ptr_array_free(devices, TRUE);
454 g_return_val_if_fail(err == NULL || *err == NULL, NULL);
457 CCLErr* err_internal = NULL;
463 gchar** dev_strings = NULL;
470 dev_strings = ccl_get_device_strings_from_array(
471 devices, &err_internal);
475 g_assert(err == NULL || *err == NULL);
480 g_assert(err == NULL || *err != NULL);
483 g_strfreev(dev_strings);
489 g_ptr_array_free(devices, CL_TRUE);
507 g_return_if_fail(err == NULL || *err == NULL);
510 CCLErr* err_internal = NULL;
513 gchar** dev_strings = NULL;
520 for (guint i = 0; dev_strings[i] != NULL; ++i) {
521 g_printf(
"%s\n", dev_strings[i]);
525 g_assert(err == NULL || *err == NULL);
531 g_assert(err == NULL || *err != NULL);
536 g_strfreev(dev_strings);
551 ccl_devsel_add_filter(
566 ccl_devsel_add_filter(
587 g_return_val_if_fail(filters != NULL, NULL);
590 g_return_val_if_fail(err == NULL || *err == NULL, NULL);
593 CCLErr* err_internal = NULL;
605 for (guint i = 0; i < (*filters)->len; i++) {
608 if (devices->len == 0)
break;
618 devices, curr_filter->data, &err_internal);
625 for (guint j = 0; j < devices->len; j++) {
629 (
CCLDevice*) g_ptr_array_index(devices, j);
635 curr_device, curr_filter->data, &err_internal);
637 err, err_internal, error_handler);
643 g_ptr_array_remove_index(devices, j);
655 g_assert(err == NULL || *err == NULL);
660 g_assert(err == NULL || *err != NULL);
665 for (guint i = 0; i < (*filters)->len; i++)
669 g_ptr_array_free(*filters, CL_TRUE);
702 g_return_val_if_fail(dev != NULL, CL_FALSE);
704 g_return_val_if_fail(err == NULL || *err == NULL, CL_FALSE);
707 CCLErr* err_internal = NULL;
710 cl_device_type type = CL_DEVICE_TYPE_DEFAULT;
712 cl_device_type type_to_check = CL_DEVICE_TYPE_DEFAULT;
717 "%s: invalid filter data", CCL_STRD);
720 type_to_check = *((cl_device_type*) data);
724 dev, CL_DEVICE_TYPE, cl_device_type, &err_internal);
728 g_assert(err == NULL || *err == NULL);
733 g_assert(err == NULL || *err != NULL);
738 return (cl_bool) (type & type_to_check);
755 cl_device_type type_to_check = CL_DEVICE_TYPE_GPU;
779 cl_device_type type_to_check = CL_DEVICE_TYPE_CPU;
804 cl_device_type type_to_check = CL_DEVICE_TYPE_ACCELERATOR;
829 g_return_val_if_fail(dev != NULL, CL_FALSE);
831 g_return_val_if_fail(err == NULL || *err == NULL, CL_FALSE);
834 CCLErr* err_internal = NULL;
838 cl_bool pass = CL_FALSE;
841 gchar *complt_info, *complt_info_lowr, *part_info;
846 "%s: invalid filter data", CCL_STRD);
849 part_info = g_ascii_strdown((gchar*) data, -1);
853 dev, CL_DEVICE_NAME,
char*, &err_internal);
856 complt_info_lowr = g_ascii_strdown(complt_info, -1);
858 if (!g_strrstr(complt_info_lowr, part_info)) {
862 g_free(complt_info_lowr);
866 dev, CL_DEVICE_VENDOR,
char*, &err_internal);
869 complt_info_lowr = g_ascii_strdown(complt_info, -1);
871 if (!g_strrstr(complt_info_lowr, part_info)) {
875 g_free(complt_info_lowr);
886 platf, CL_PLATFORM_NAME, &err_internal);
889 complt_info_lowr = g_ascii_strdown(complt_info, -1);
892 if (g_strrstr(complt_info_lowr, part_info)) {
907 g_free(complt_info_lowr);
911 g_assert(err == NULL || *err == NULL);
916 g_assert(err == NULL || *err != NULL);
944 g_return_val_if_fail(device != NULL, CL_FALSE);
946 g_return_val_if_fail(err == NULL || *err == NULL, CL_FALSE);
949 cl_platform_id platf;
952 CCLErr* err_internal = NULL;
961 "%s: invalid filter data", CCL_STRD);
965 cl_platform_id, &err_internal);
970 pass = (platf == (cl_platform_id) data) ? CL_TRUE : CL_FALSE;
973 g_assert(err == NULL || *err == NULL);
978 g_assert(err == NULL || *err != NULL);
1011 g_return_val_if_fail(devices != NULL, NULL);
1013 g_return_val_if_fail(err == NULL || *err == NULL, NULL);
1019 cl_platform_id platf_ref, platf_curr;
1022 CCLErr *err_internal = NULL;
1028 dev = (
CCLDevice*) g_ptr_array_index(devices, 0);
1032 cl_platform_id, &err_internal);
1037 for (guint i = 1; i < devices->len; i++) {
1040 dev = (
CCLDevice*) g_ptr_array_index(devices, i);
1044 dev, CL_DEVICE_PLATFORM, cl_platform_id, &err_internal);
1049 if (platf_ref != platf_curr) {
1052 g_ptr_array_remove_index(devices, i);
1063 g_assert(err == NULL || *err == NULL);
1068 g_assert(err == NULL || *err != NULL);
1095 g_return_val_if_fail(devices != NULL, NULL);
1097 g_return_val_if_fail(err == NULL || *err == NULL, NULL);
1100 CCLErr *err_internal = NULL;
1111 index = *((cl_uint*) data);
1113 if ((index >= 0) && (index < (cl_int) devices->len)) {
1116 ccl_devsel_dep_menu_list(devices, index, &err_internal);
1118 }
else if (index >= 0) {
1120 g_printf(
"\n (!) No device at index %d!\n", index);
1130 index = ccl_devsel_dep_menu_query(devices, &err_internal);
1135 sel_dev = g_ptr_array_index(devices, index);
1137 g_ptr_array_remove_range(devices, 0, devices->len);
1138 g_ptr_array_add(devices, sel_dev);
1139 g_assert_cmpuint(1, ==, devices->len);
1142 g_assert(err == NULL || *err == NULL);
1147 g_assert(err == NULL || *err != NULL);
1150 if (devices != NULL) {
1151 g_ptr_array_free(devices, TRUE);
1179 g_return_val_if_fail(devices != NULL, NULL);
1181 g_return_val_if_fail(err == NULL || *err == NULL, NULL);
1193 "%s: The 'data' parameter must not be NULL.", CCL_STRD);
1196 index = *((cl_uint*) data);
1201 "%s: No device found at index %d.", CCL_STRD, index);
1205 sel_dev = g_ptr_array_index(devices, index);
1207 g_ptr_array_remove_range(devices, 0, devices->len);
1208 g_ptr_array_add(devices, sel_dev);
1209 g_assert_cmpuint(1, ==, devices->len);
1212 g_assert(err == NULL || *err == NULL);
1217 g_assert(err == NULL || *err != NULL);
1220 if (devices != NULL) {
1221 g_ptr_array_free(devices, TRUE);
Invalid data passed to a function or returned from function.
Classes and functions for filtering and selecting OpenCL contexts and associated devices.
#define CCL_OCL_ERROR
Resolves to error category identifying string, in this case an error in the OpenCL library...
CCLDevSelDevices ccl_devsel_dep_platform(CCLDevSelDevices devices, void *data, CCLErr **err)
Dependent filter function which only accepts devices of the same platform (the platform to which the ...
#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...
Device filter class, includes a filter function (independent or dependent) and the respective filter ...
CCLDevSelDevices ccl_devsel_dep_menu(CCLDevSelDevices devices, void *data, CCLErr **err)
Dependent filter function which presents a menu to the user allowing him to select the desired device...
Useful definitions used internally by cf4ocl.
void ccl_devsel_devices_destroy(CCLDevSelDevices devices)
Destroy an object containing device wrappers.
cl_bool ccl_devsel_indep_type_cpu(CCLDevice *dev, void *data, CCLErr **err)
Independent filter function which only accepts CPU devices.
#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...
void ccl_platforms_destroy(CCLPlatforms *platforms)
Destroy a CCLPlatforms* object, including all underlying platforms, devices and data.
CCLPlatforms * ccl_platforms_new(CCLErr **err)
Creates a new CCLPlatforms* object, which contains the list of OpenCL platforms available in the syst...
cl_uint ccl_platforms_count(CCLPlatforms *platforms)
Return number of OpenCL platforms found in CCLPlatforms object.
cl_bool(* ccl_devsel_indep)(CCLDevice *device, void *data, CCLErr **err)
Independent filter function: Abstract function for filtering one OpenCL device at a time...
#define CCL_ERROR
Resolves to error category identifying string, in this case an error in cf4ocl.
cl_bool ccl_devsel_indep_type_accel(CCLDevice *dev, void *data, CCLErr **err)
Independent filter function which only accepts accelerator devices.
The requested OpenCL device was not found.
GPtrArray * CCLDevSelDevices
An object containing device wrappers.
#define ccl_device_ref(dev)
Increase the reference count of the device wrapper object.
Independent filter, filters one device at a time.
CCLDevSelDevices ccl_devsel_select(CCLDevSelFilters *filters, CCLErr **err)
Select one or more OpenCL devices based on the provided filters.
void ccl_devsel_add_dep_filter(CCLDevSelFilters *filters, ccl_devsel_dep filter, void *data)
Add a dependent filter to the filter set.
CCLPlatform * ccl_platform_new_from_device(CCLDevice *dev, CCLErr **err)
Get the platform wrapper for the given device wrapper.
void ccl_device_destroy(CCLDevice *dev)
Decrements the reference count of the device wrapper object.
gchar ** ccl_devsel_get_device_strings(CCLErr **err)
Returns a NULL-terminated array of strings, each one containing the name and vendor of each device in...
#define CCL_UNUSED(x)
Macro to avoid warning in unused variables.
void ccl_devsel_add_indep_filter(CCLDevSelFilters *filters, ccl_devsel_indep filter, void *data)
Add an independent filter to the filter set.
#define ccl_device_get_info_array(dev, param_name, param_type, err)
Macro which returns an array device information value.
CCLDevSelDevices ccl_devsel_dep_index(CCLDevSelDevices devices, void *data, CCLErr **err)
Dependent filter function which selects the device at the specified index, failing if no device is fo...
CCLDevSelDevices(* ccl_devsel_dep)(CCLDevSelDevices devices, void *data, CCLErr **err)
Dependent filter function: Abstract function for filtering several OpenCL devices depending on the av...
cl_bool ccl_devsel_indep_string(CCLDevice *dev, void *data, CCLErr **err)
Independent filter which selects devices based on device name, device vendor and/or platform name...
GPtrArray * CCLDevSelFilters
A set of independent and dependent device filters.
cl_bool ccl_devsel_indep_type_gpu(CCLDevice *dev, void *data, CCLErr **err)
Independent filter function which only accepts GPU devices.
Dependent filter, filters devices depending on the currently available device choices.
#define ccl_device_get_info_scalar(dev, param_name, param_type, err)
Macro which returns a scalar device information value.
cl_uint ccl_platform_get_num_devices(CCLPlatform *platf, CCLErr **err)
Return number of devices in platform.
enum ccl_devsel_filter_type CCLDevSelFilterType
Filter type.
void ccl_platform_destroy(CCLPlatform *platf)
Decrements the reference count of the platform wrapper object.
cl_bool ccl_devsel_indep_type(CCLDevice *dev, void *data, CCLErr **err)
Independent filter function which accepts devices of the type given in the data parameter.
void(* ccl_devsel_fp)(void)
Generic filter function pointer.
GError CCLErr
Error handling class.
void ccl_devsel_print_device_strings(CCLErr **err)
Print to stdout a device description string for each device in the system.
CCLDevSelDevices ccl_devsel_devices_new(CCLErr **err)
Create and return an object with device wrappers for all OpenCL devices present in the system...
struct ccl_devsel_filter CCLDevSelFilter
Device filter class, includes a filter function (independent or dependent) and the respective filter ...
CCLPlatform * ccl_platforms_get(CCLPlatforms *platforms, cl_uint index)
Get platform wrapper object at given index.
cl_bool ccl_devsel_indep_platform(CCLDevice *device, void *data, CCLErr **err)
Independent filter function which only accepts devices of a specified platform.
CCLDevice * ccl_platform_get_device(CCLPlatform *platf, cl_uint index, CCLErr **err)
Get CCLDevice wrapper at given index.