embb_mtapi_opencl.c 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (c) 2014, Siemens AG. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

27 28
#include <CL/cl.h>
#include <string.h>
29
#include <assert.h>
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

#include <embb/base/c/memory_allocation.h>
#include <embb/mtapi/c/mtapi_ext.h>
#include <embb/base/c/internal/unused.h>

#include <embb/mtapi/c/mtapi_opencl.h>

#include <embb_mtapi_opencl_runtimelinker.h>

#include <embb_mtapi_task_t.h>
#include <embb_mtapi_action_t.h>
#include <embb_mtapi_node_t.h>
#include <mtapi_status_t.h>

struct embb_mtapi_opencl_plugin_struct {
  cl_platform_id platform_id;
  cl_device_id device_id;
  cl_context context;
  cl_command_queue command_queue;
49 50
  size_t work_group_size;
  size_t work_item_sizes[3];
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
};

typedef struct embb_mtapi_opencl_plugin_struct embb_mtapi_opencl_plugin_t;

static embb_mtapi_opencl_plugin_t embb_mtapi_opencl_plugin;

struct embb_mtapi_opencl_action_struct {
  cl_program program;
  cl_kernel kernel;
  cl_mem node_local_data;
  int node_local_data_size;
  size_t local_work_size;
  size_t element_size;
};

typedef struct embb_mtapi_opencl_action_struct embb_mtapi_opencl_action_t;

struct embb_mtapi_opencl_task_struct {
  cl_mem arguments;
  int arguments_size;
  cl_mem result_buffer;
  int result_buffer_size;
  cl_event kernel_finish_event;
  mtapi_task_hndl_t task;
};

typedef struct embb_mtapi_opencl_task_struct embb_mtapi_opencl_task_t;

79 80
static size_t round_up(size_t group_size, size_t global_size) {
  size_t r = global_size % group_size;
81 82
  if (r == 0) {
    return global_size;
83
  } else {
84 85 86 87
    return global_size + group_size - r;
  }
}

88 89
static void CL_API_CALL opencl_task_complete(
  cl_event ev, cl_int status, void * data) {
90 91 92 93
  EMBB_UNUSED(ev);
  EMBB_UNUSED(status);

  cl_int err;
94
  EMBB_UNUSED_IN_RELEASE(err);
95 96 97 98 99
  embb_mtapi_opencl_task_t * opencl_task = (embb_mtapi_opencl_task_t*)data;

  if (embb_mtapi_node_is_initialized()) {
    embb_mtapi_node_t * node = embb_mtapi_node_get_instance();

100 101
    if (embb_mtapi_task_pool_is_handle_valid(
      node->task_pool, opencl_task->task)) {
102
      embb_mtapi_task_t * local_task =
103 104
        embb_mtapi_task_pool_get_storage_for_handle(
          node->task_pool, opencl_task->task);
105 106

      err = clWaitForEvents(1, &opencl_task->kernel_finish_event);
107
      assert(CL_SUCCESS == err);
108 109 110

      if (NULL != opencl_task->result_buffer) {
        err = clReleaseMemObject(opencl_task->result_buffer);
111
        assert(CL_SUCCESS == err);
112 113 114
      }
      if (NULL != opencl_task->arguments) {
        err = clReleaseMemObject(opencl_task->arguments);
115
        assert(CL_SUCCESS == err);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
      }

      embb_mtapi_task_set_state(local_task, MTAPI_TASK_COMPLETED);
    }
  }
}

static void opencl_task_start(
  MTAPI_IN mtapi_task_hndl_t task,
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  cl_int err;

  if (embb_mtapi_node_is_initialized()) {
    embb_mtapi_node_t * node = embb_mtapi_node_get_instance();

    if (embb_mtapi_task_pool_is_handle_valid(node->task_pool, task)) {
      embb_mtapi_task_t * local_task =
        embb_mtapi_task_pool_get_storage_for_handle(node->task_pool, task);

136 137
      if (embb_mtapi_action_pool_is_handle_valid(
        node->action_pool, local_task->action)) {
138
        embb_mtapi_action_t * local_action =
139 140
          embb_mtapi_action_pool_get_storage_for_handle(
          node->action_pool, local_task->action);
141 142

        embb_mtapi_opencl_plugin_t * plugin = &embb_mtapi_opencl_plugin;
143 144 145 146 147 148 149 150
        embb_mtapi_opencl_action_t * opencl_action =
          (embb_mtapi_opencl_action_t*)local_action->plugin_data;
        embb_mtapi_opencl_task_t * opencl_task =
          (embb_mtapi_opencl_task_t*)embb_alloc(
            sizeof(embb_mtapi_opencl_task_t));

        size_t elements = local_task->result_size /
          opencl_action->element_size;
151 152 153 154
        size_t global_work_size;

        if (0 == elements)
          elements = 1;
155
        global_work_size =
156
          round_up(opencl_action->local_work_size, elements);
157 158 159

        opencl_task->task = task;

160
        opencl_task->arguments_size = (int)local_task->arguments_size;
161
        if (0 < local_task->arguments_size) {
162 163
          opencl_task->arguments = clCreateBuffer(plugin->context,
            CL_MEM_READ_ONLY, local_task->arguments_size, NULL, &err);
164 165 166
        } else {
          opencl_task->arguments = NULL;
        }
167
        opencl_task->result_buffer_size = (int)local_task->result_size;
168
        if (0 < local_task->result_size) {
169 170
          opencl_task->result_buffer = clCreateBuffer(plugin->context,
            CL_MEM_WRITE_ONLY, local_task->result_size, NULL, &err);
171 172 173 174
        } else {
          opencl_task->result_buffer = NULL;
        }

175 176
        err = clSetKernelArg(opencl_action->kernel, 0, sizeof(cl_mem),
          (const void*)&opencl_task->arguments);
177
        err |= clSetKernelArg(opencl_action->kernel, 1, sizeof(cl_int),
178 179
          (const void*)&opencl_task->arguments_size);

180
        err |= clSetKernelArg(opencl_action->kernel, 2, sizeof(cl_mem),
181
          (const void*)&opencl_task->result_buffer);
182
        err |= clSetKernelArg(opencl_action->kernel, 3, sizeof(cl_int),
183 184
          (const void*)&opencl_task->result_buffer_size);

185
        err |= clEnqueueWriteBuffer(plugin->command_queue,
186
          opencl_task->arguments, CL_FALSE, 0,
Marcus Winter committed
187 188
          (size_t)opencl_task->arguments_size, local_task->arguments,
          0, NULL, NULL);
189 190

        if (CL_SUCCESS == err) {
191 192
          embb_mtapi_task_set_state(local_task, MTAPI_TASK_RUNNING);

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
          err |= clEnqueueNDRangeKernel(plugin->command_queue,
            opencl_action->kernel, 1, NULL,
            &global_work_size, &opencl_action->local_work_size, 0, NULL, NULL);
          err |= clEnqueueReadBuffer(plugin->command_queue,
            opencl_task->result_buffer, CL_FALSE, 0,
            (size_t)opencl_task->result_buffer_size, local_task->result_buffer,
            0, NULL, &opencl_task->kernel_finish_event);
          err |= clSetEventCallback(opencl_task->kernel_finish_event,
            CL_COMPLETE, opencl_task_complete, opencl_task);
        }

        err |= clFlush(plugin->command_queue);
        if (CL_SUCCESS != err) {
          embb_mtapi_task_set_state(local_task, MTAPI_TASK_ERROR);
          local_status = MTAPI_ERR_ACTION_FAILED;
        } else {
          local_status = MTAPI_SUCCESS;
        }
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
      }
    }
  }

  mtapi_status_set(status, local_status);
}

static void opencl_task_cancel(
  MTAPI_IN mtapi_task_hndl_t task,
  MTAPI_OUT mtapi_status_t* status
  ) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;

  EMBB_UNUSED(task);

  mtapi_status_set(status, local_status);
}

static void opencl_action_finalize(
  MTAPI_IN mtapi_action_hndl_t action,
  MTAPI_OUT mtapi_status_t* status
  ) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  cl_int err;
235
  EMBB_UNUSED_IN_RELEASE(err);
236 237 238

  if (embb_mtapi_node_is_initialized()) {
    embb_mtapi_node_t * node = embb_mtapi_node_get_instance();
239
    if (embb_mtapi_action_pool_is_handle_valid(node->action_pool, action)) {
240
      embb_mtapi_action_t * local_action =
241 242
        embb_mtapi_action_pool_get_storage_for_handle(
        node->action_pool, action);
243 244 245 246
      embb_mtapi_opencl_action_t * opencl_action =
        (embb_mtapi_opencl_action_t *)local_action->plugin_data;
      if (NULL != opencl_action->node_local_data) {
        err = clReleaseMemObject(opencl_action->node_local_data);
247
        assert(CL_SUCCESS == err);
248 249 250
      }

      err = clReleaseKernel(opencl_action->kernel);
251
      assert(CL_SUCCESS == err);
252
      err = clReleaseProgram(opencl_action->program);
253
      assert(CL_SUCCESS == err);
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

      embb_free(opencl_action);
      local_status = MTAPI_SUCCESS;
    }
  }

  mtapi_status_set(status, local_status);
}

char buffer[1024];

void mtapi_opencl_plugin_initialize(
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  cl_int err;
  embb_mtapi_opencl_plugin_t * plugin = &embb_mtapi_opencl_plugin;

271
  err = embb_mtapi_opencl_link_at_runtime();
272 273 274 275 276
  if (err <= 0) {
    // OpenCL not available, or wrong version
    local_status = MTAPI_ERR_FUNC_NOT_IMPLEMENTED;
  } else {
    // all good, go ahead
277 278 279 280 281 282 283 284 285 286 287
    err = clGetPlatformIDs(1, &plugin->platform_id, NULL);
    if (CL_SUCCESS == err) {
      err = clGetDeviceIDs(plugin->platform_id, CL_DEVICE_TYPE_DEFAULT,
        1, &plugin->device_id, NULL);
    }
    if (CL_SUCCESS == err) {
      plugin->context = clCreateContext(NULL, 1, &plugin->device_id,
        NULL, NULL, &err);
    }
    if (CL_SUCCESS == err) {
      err = clGetDeviceInfo(plugin->device_id, CL_DEVICE_MAX_WORK_GROUP_SIZE,
288
        sizeof(size_t), &plugin->work_group_size, NULL);
289 290 291
    }
    if (CL_SUCCESS == err) {
      err = clGetDeviceInfo(plugin->device_id, CL_DEVICE_MAX_WORK_ITEM_SIZES,
292
        3 * sizeof(size_t), &plugin->work_item_sizes[0], NULL);
293 294 295 296 297 298 299 300
    }
    if (CL_SUCCESS == err) {
      plugin->command_queue = clCreateCommandQueue(plugin->context,
        plugin->device_id, 0, &err);
    }
    if (CL_SUCCESS == err) {
      local_status = MTAPI_SUCCESS;
    }
301
  }
302 303 304 305 306 307 308 309 310

  mtapi_status_set(status, local_status);
}

void mtapi_opencl_plugin_finalize(
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;

  cl_int err;
311
  EMBB_UNUSED_IN_RELEASE(err);
312 313 314 315
  embb_mtapi_opencl_plugin_t * plugin = &embb_mtapi_opencl_plugin;

  /* finalization */
  err = clReleaseCommandQueue(plugin->command_queue);
316
  assert(CL_SUCCESS == err);
317
  err = clReleaseContext(plugin->context);
318
  assert(CL_SUCCESS == err);
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

  local_status = MTAPI_SUCCESS;
  mtapi_status_set(status, local_status);
}

mtapi_action_hndl_t mtapi_opencl_action_create(
  MTAPI_IN mtapi_job_id_t job_id,
  MTAPI_IN char* kernel_source,
  MTAPI_IN char* kernel_name,
  MTAPI_IN mtapi_size_t local_work_size,
  MTAPI_IN mtapi_size_t element_size,
  MTAPI_IN void* node_local_data,
  MTAPI_IN mtapi_size_t node_local_data_size,
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;

  cl_int err;
  embb_mtapi_opencl_plugin_t * plugin = &embb_mtapi_opencl_plugin;
337 338 339
  embb_mtapi_opencl_action_t * action =
    (embb_mtapi_opencl_action_t*)embb_alloc(
      sizeof(embb_mtapi_opencl_action_t));
340
  mtapi_action_hndl_t action_hndl = { 0, 0 }; // invalid handle
341
  size_t kernel_length = strlen(kernel_source);
342 343 344
  mtapi_boolean_t free_program_on_error = MTAPI_FALSE;
  mtapi_boolean_t free_kernel_on_error = MTAPI_FALSE;
  mtapi_boolean_t free_node_local_data_on_error = MTAPI_FALSE;
345 346 347 348 349

  action->local_work_size = local_work_size;
  action->element_size = element_size;

  /* initialization */
350 351
  action->program = clCreateProgramWithSource(plugin->context,
    1, &kernel_source, &kernel_length, &err);
352 353 354 355 356
  if (CL_SUCCESS == err) {
    free_program_on_error = MTAPI_TRUE;
    err = clBuildProgram(action->program, 1, &plugin->device_id,
      NULL, NULL, NULL);
  } else {
357 358
    err = clGetProgramBuildInfo(action->program, plugin->device_id,
      CL_PROGRAM_BUILD_LOG, 1024, buffer, NULL);
359
  }
360 361 362 363 364 365 366

  if (CL_SUCCESS == err) {
    action->kernel = clCreateKernel(action->program, kernel_name, &err);
    if (CL_SUCCESS == err) {
      free_kernel_on_error = MTAPI_TRUE;
    }
  }
367 368

  if (0 < node_local_data_size) {
369 370
    action->node_local_data = clCreateBuffer(plugin->context, CL_MEM_READ_ONLY,
      node_local_data_size, NULL, &err);
371 372 373
    if (CL_SUCCESS == err) {
      free_node_local_data_on_error = MTAPI_TRUE;
    }
374
    action->node_local_data_size = (int)node_local_data_size;
375 376 377
    if (CL_SUCCESS == err) {
      err = clEnqueueWriteBuffer(plugin->command_queue,
        action->node_local_data, CL_TRUE, 0,
378
        (size_t)action->node_local_data_size, node_local_data, 0, NULL, NULL);
379
    }
380 381 382 383 384
  } else {
    action->node_local_data = NULL;
    action->node_local_data_size = 0;
  }

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
  if (CL_SUCCESS == err) {
    err = clSetKernelArg(action->kernel, 4, sizeof(cl_mem),
      (const void*)&action->node_local_data);
  }
  if (CL_SUCCESS == err) {
    err = clSetKernelArg(action->kernel, 5, sizeof(cl_int),
      (const void*)&action->node_local_data_size);
  }

  if (CL_SUCCESS == err) {
    action_hndl = mtapi_ext_plugin_action_create(
      job_id,
      opencl_task_start,
      opencl_task_cancel,
      opencl_action_finalize,
      action,
      node_local_data,
      node_local_data_size,
      MTAPI_NULL,
      &local_status);
  } else {
    if (free_node_local_data_on_error) {
      clReleaseMemObject(action->node_local_data);
    }
    if (free_kernel_on_error) {
      clReleaseKernel(action->kernel);
    }
    if (free_program_on_error) {
      clReleaseProgram(action->program);
    }
    embb_free(action);
  }
417 418 419 420 421

  mtapi_status_set(status, local_status);

  return action_hndl;
}