H2Lib  3.0
opencl.h
Go to the documentation of this file.
1 /* ------------------------------------------------------------
2  This is the file "opencl.h" of the H2Lib package.
3  All rights reserved, Sven Christophersen 2015
4  ------------------------------------------------------------ */
5 
12 #ifndef OPENCL_H_
13 #define OPENCL_H_
14 
15 #ifdef USE_OPENCL
16 
23 /* C STD LIBRARY */
24 #include <stdio.h>
25 #include <assert.h>
26 #include <CL/cl.h>
27 #include <omp.h>
28 /* CORE 0 */
29 #include "basic.h"
30 /* CORE 1 */
31 /* CORE 2 */
32 /* CORE 3 */
33 /* SIMPLE */
34 /* PARTICLES */
35 /* BEM */
36 
45 struct _ocl {
47  cl_uint num_platforms;
49  cl_platform_id *platforms;
53  cl_platform_id *devplatforms;
55  cl_uint num_devices;
57  cl_device_id *devices;
59  cl_uint num_contexts;
61  cl_context *contexts;
66  cl_command_queue *queues;
69 };
70 
75 extern struct _ocl ocl_system;
76 
80 #define CL_CHECK(res) \
81  {if (res != CL_SUCCESS) {fprintf(stderr,"Error \"%s\" (%d) in file %s on line %d\n", \
82  get_error_string(res), res, __FILE__,__LINE__); abort();}}
83 
90 HEADER_PREFIX void get_opencl_devices(cl_device_id **devices, cl_uint *ndevices);
91 
102 HEADER_PREFIX void set_opencl_devices(cl_device_id *devices, cl_uint ndevices,
103  cl_uint queues_per_device);
104 
112 HEADER_PREFIX const char *get_error_string(cl_int error);
113 
125 HEADER_PREFIX void setup_kernels(const char *src_str, const uint num_kernels,
126  const char **kernel_names, cl_kernel **kernels);
127 
132 typedef enum {
142 
146 typedef struct _task task;
147 
151 typedef task *ptask;
152 
161 struct _task {
163  void *data;
166 };
167 
171 typedef struct _taskgroup taskgroup;
172 
177 
204 struct _taskgroup {
208  size_t size;
214  void *data;
215 
218  void (*merge_tasks)(ptaskgroup t, void **data);
220  void (*cleanup_merge)(void *data);
222  void (*distribute_results)(ptaskgroup t, void *data);
224  void (*close_taskgroup)(ptaskgroup tg);
225 
227  void (*callback_cpu)(void *data);
229  void (*callback_gpu)(void *data);
231  size_t (*getsize_task)(void *data);
234  void (*split_task)(void *data, void ***splits, uint *n);
236  void (*cleanup_task)(void *data);
237 };
238 
251 HEADER_PREFIX ptask new_ocltask(void *data, ptask next);
252 
261 
294  void (*merge_tasks)(ptaskgroup tg, void **data),
295  void (*cleanup_merge)(void *data),
296  void (*distribute_results)(ptaskgroup tg, void *data),
297  void (*close_taskgroup)(ptaskgroup tg), void (*callback_cpu)(void *data),
298  void (*callback_gpu)(void *data), size_t (*getsize_task)(void *data),
299  void (*split_task)(void *data, void ***split, uint *n),
300  void (*cleanup_task)(void *data), void *data);
301 
308 
320 HEADER_PREFIX void add_task_taskgroup(ptaskgroup *tg, void *data);
321 
328 
339 HEADER_PREFIX void start_scheduler(uint cpu_threads, uint gpu_threads);
340 
348 
359 #define SCHEDULE_OPENCL(cpu_threads, gpu_threads, func, ...) \
360  _Pragma("omp parallel num_threads(2)") \
361  { \
362  start_scheduler(cpu_threads, gpu_threads); \
363  if (omp_get_thread_num() == 0) { \
364  func(__VA_ARGS__); \
365  } \
366  stop_scheduler(); \
367  }
368 
373 #endif
374 
375 #endif /* OPENCL_H_ */
cl_uint num_platforms
Number of OpenCL platforms that will be used for computations.
Definition: opencl.h:47
ptaskgroup next
Definition: opencl.h:212
task * ptask
Abbreviation for a pointer to a struct _task.
Definition: opencl.h:151
taskgroup * ptaskgroup
Abbreviation for a pointer to a struct _taskgroup.
Definition: opencl.h:176
ptaskgroup new_ocltaskgroup(task_affinity affinity, ptaskgroup next, void(*merge_tasks)(ptaskgroup tg, void **data), void(*cleanup_merge)(void *data), void(*distribute_results)(ptaskgroup tg, void *data), void(*close_taskgroup)(ptaskgroup tg), void(*callback_cpu)(void *data), void(*callback_gpu)(void *data), size_t(*getsize_task)(void *data), void(*split_task)(void *data, void ***split, uint *n), void(*cleanup_task)(void *data), void *data)
Create a new list of tasks with the same code to execute.
void del_taskgroup(ptaskgroup tg)
Delete a taskgroup object.
void get_opencl_devices(cl_device_id **devices, cl_uint *ndevices)
Retrieve an array of available OpenCL devices.
void add_taskgroup_to_scheduler(ptaskgroup tg)
Enqueues a full taskgroup to the execution queue of the scheduler.
void * data
Definition: opencl.h:163
ptask new_ocltask(void *data, ptask next)
Create a new task.
unsigned uint
Unsigned integer type.
Definition: settings.h:70
Simple representation of a task.
Definition: opencl.h:161
ptask tasks
Definition: opencl.h:206
Definition: opencl.h:135
cl_uint num_contexts
Number of OpenCL contexts that will be used.
Definition: opencl.h:59
Definition: opencl.h:139
Definition: opencl.h:133
void set_opencl_devices(cl_device_id *devices, cl_uint ndevices, cl_uint queues_per_device)
Set an array of OpenCL devices to be used for computations.
size_t max_package_size
Definition: opencl.h:68
void setup_kernels(const char *src_str, const uint num_kernels, const char **kernel_names, cl_kernel **kernels)
Reads a file specified by filename and compiles all OpenCL kernels given by the array kernel_names in...
#define HEADER_PREFIX
Prefix for function declarations.
Definition: settings.h:43
ptask next
Definition: opencl.h:165
void del_ocltask(ptaskgroup tg, ptask t)
Delete a task.
Structure that contains basic OpenCL objects for arbitrary computations.
Definition: opencl.h:45
cl_uint num_devices
Number of OpenCL devices the will be used.
Definition: opencl.h:55
const char * get_error_string(cl_int error)
Returns a string corresponding to the error code error.
task_affinity
This enum specifies the affinity of exceution of a taskgroup.
Definition: opencl.h:132
cl_platform_id * platforms
This array contains all OpenCL platform-ids that will be used.
Definition: opencl.h:49
size_t size
Definition: opencl.h:208
cl_platform_id * devplatforms
Just a temporary array of OpenCL platform-ids that is used in between the calls of get_opencl_devices...
Definition: opencl.h:53
void start_scheduler(uint cpu_threads, uint gpu_threads)
Starts the task scheduler with cpu_threads Threads on the CPU and gpu_threads on the GPU...
cl_uint queues_per_device
This values determines the number of queues that will be created per device.
Definition: opencl.h:64
cl_command_queue * queues
Array of all queues for all devices that will be used.
Definition: opencl.h:66
void * data
Definition: opencl.h:214
struct _ocl ocl_system
Global variable that contains basic OpenCL objects for arbitrary computations.
A collection of tasks for the same callback function.
Definition: opencl.h:204
task_affinity affinity
Definition: opencl.h:210
Definition: opencl.h:137
cl_device_id * devices
Array of OpenCl devices that will be used.
Definition: opencl.h:57
cl_context * contexts
Array of OpenCL contexts that will be used.
Definition: opencl.h:61
void stop_scheduler()
Stop the task scheduler.
void add_task_taskgroup(ptaskgroup *tg, void *data)
Adds a new _task to a _taskgroup.