cl_createkernel.c.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010,2011 University of Bordeaux
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. static void soclCreateKernel_task(void *data) {
  17. struct _cl_kernel *k = (struct _cl_kernel *)data;
  18. int range = starpu_worker_get_range();
  19. cl_int err;
  20. DEBUG_MSG("[Device %d] Creating kernel...\n", starpu_worker_get_id());
  21. k->cl_kernels[range] = clCreateKernel(k->program->cl_programs[range], k->kernel_name, &err);
  22. if (err != CL_SUCCESS) {
  23. k->errcodes[range] = err;
  24. ERROR_STOP("[Device %d] Unable to create kernel. Aborting.\n", starpu_worker_get_id());
  25. return;
  26. }
  27. /* One worker creates argument structures */
  28. if (__sync_bool_compare_and_swap(&k->arg_count, 0, 666)) {
  29. unsigned int i;
  30. cl_uint arg_count;
  31. err = clGetKernelInfo(k->cl_kernels[range], CL_KERNEL_NUM_ARGS, sizeof(arg_count), &arg_count, NULL);
  32. if (err != CL_SUCCESS) {
  33. DEBUG_CL("clGetKernelInfo", err);
  34. ERROR_STOP("Unable to get kernel argument count. Aborting.\n");
  35. }
  36. k->arg_count = arg_count;
  37. DEBUG_MSG("Kernel has %d arguments\n", arg_count);
  38. k->arg_size = (size_t*)malloc(sizeof(size_t) * arg_count);
  39. k->arg_value = (void**)malloc(sizeof(void*) * arg_count);
  40. k->arg_type = (enum kernel_arg_type*)malloc(sizeof(enum kernel_arg_type) * arg_count);
  41. /* Settings default type to NULL */
  42. for (i=0; i<arg_count; i++) {
  43. k->arg_value[i] = NULL;
  44. k->arg_type[i] = Null;
  45. }
  46. }
  47. }
  48. CL_API_ENTRY cl_kernel CL_API_CALL
  49. soclCreateKernel(cl_program program,
  50. const char * kernel_name,
  51. cl_int * errcode_ret) CL_API_SUFFIX__VERSION_1_0
  52. {
  53. cl_kernel k;
  54. int device_count;
  55. if (program == NULL) {
  56. if (errcode_ret != NULL)
  57. *errcode_ret = CL_INVALID_PROGRAM;
  58. return NULL;
  59. }
  60. //TODO: check programs (see opencl specs)
  61. /* Create Kernel structure */
  62. k = (cl_kernel)gc_entity_alloc(sizeof(struct _cl_kernel), release_callback_kernel);
  63. if (k == NULL) {
  64. if (errcode_ret != NULL)
  65. *errcode_ret = CL_OUT_OF_HOST_MEMORY;
  66. return NULL;
  67. }
  68. gc_entity_store(&k->program, program);
  69. k->kernel_name = strdup(kernel_name);
  70. k->arg_count = 0;
  71. k->arg_value = NULL;
  72. k->arg_size = NULL;
  73. #ifdef DEBUG
  74. static int id = 0;
  75. k->id = id++;
  76. #endif
  77. device_count = starpu_opencl_worker_get_count();
  78. k->cl_kernels = (cl_kernel*)malloc(device_count * sizeof(cl_kernel));
  79. k->errcodes = (cl_int*)malloc(device_count * sizeof(cl_int));
  80. {
  81. int i;
  82. for (i=0; i<device_count; i++) {
  83. k->cl_kernels[i] = NULL;
  84. k->errcodes[i] = -9999;
  85. }
  86. }
  87. /* Create kernel on each device */
  88. DEBUG_MSG("[Kernel %d] Create %d kernels (name \"%s\")\n", k->id, starpu_opencl_worker_get_count(), kernel_name);
  89. starpu_execute_on_each_worker(soclCreateKernel_task, k, STARPU_OPENCL);
  90. if (errcode_ret != NULL) {
  91. int i;
  92. *errcode_ret = CL_SUCCESS;
  93. for (i=0; i<device_count; i++) {
  94. switch (k->errcodes[i]) {
  95. #define CASE_RET(e) case e: *errcode_ret = e; return k;
  96. CASE_RET(CL_INVALID_PROGRAM)
  97. CASE_RET(CL_INVALID_PROGRAM_EXECUTABLE)
  98. CASE_RET(CL_INVALID_KERNEL_NAME)
  99. CASE_RET(CL_INVALID_KERNEL_DEFINITION)
  100. CASE_RET(CL_INVALID_VALUE)
  101. CASE_RET(CL_OUT_OF_RESOURCES)
  102. CASE_RET(CL_OUT_OF_HOST_MEMORY)
  103. #undef CASE_RET
  104. }
  105. }
  106. }
  107. return k;
  108. }