cl_createprogramwithsource.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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. #include "socl.h"
  17. struct cpws_data
  18. {
  19. struct _cl_program *program;
  20. cl_int *errcodes;
  21. cl_uint count;
  22. char **strings;
  23. size_t *lengths;
  24. };
  25. static void soclCreateProgramWithSource_task(void *data)
  26. {
  27. struct cpws_data *d = (struct cpws_data*)data;
  28. cl_context context;
  29. int wid = starpu_worker_get_id_check();
  30. DEBUG_MSG("Worker id: %d\n", wid);
  31. int range = starpu_worker_get_range();
  32. starpu_opencl_get_context(wid, &context);
  33. d->program->cl_programs[range] = clCreateProgramWithSource(context, d->count, (const char**)d->strings, d->lengths, &d->errcodes[range]);
  34. }
  35. static void release_callback_program(void * e)
  36. {
  37. cl_program program = (cl_program)e;
  38. unsigned int i;
  39. for (i=0; i<socl_device_count; i++)
  40. {
  41. if (program->cl_programs[i] != NULL)
  42. {
  43. cl_int err = clReleaseProgram(program->cl_programs[i]);
  44. if (err != CL_SUCCESS)
  45. DEBUG_CL("clReleaseProgram", err);
  46. }
  47. }
  48. /* Release references */
  49. gc_entity_unstore(&program->context);
  50. free(program->cl_programs);
  51. if (program->options != NULL)
  52. free(program->options);
  53. }
  54. CL_API_SUFFIX__VERSION_1_0
  55. CL_API_ENTRY cl_program CL_API_CALL
  56. soclCreateProgramWithSource(cl_context context,
  57. cl_uint count,
  58. const char ** strings,
  59. const size_t * lengths,
  60. cl_int * errcode_ret)
  61. {
  62. cl_program p;
  63. struct cpws_data *data;
  64. unsigned int i;
  65. if (errcode_ret != NULL)
  66. *errcode_ret = CL_SUCCESS;
  67. /* Check arguments */
  68. if (count == 0 || strings == NULL)
  69. {
  70. if (errcode_ret != NULL)
  71. *errcode_ret = CL_INVALID_VALUE;
  72. return NULL;
  73. }
  74. /* Alloc cl_program structure */
  75. p = (cl_program)gc_entity_alloc(sizeof(struct _cl_program), release_callback_program, "program");
  76. if (p == NULL)
  77. {
  78. if (errcode_ret != NULL)
  79. *errcode_ret = CL_OUT_OF_HOST_MEMORY;
  80. return NULL;
  81. }
  82. gc_entity_store(&p->context, context);
  83. p->options = NULL;
  84. #ifdef DEBUG
  85. static int id = 0;
  86. p->id = id++;
  87. #endif
  88. p->cl_programs = (cl_program*)malloc(sizeof(cl_program) * socl_device_count);
  89. if (p->cl_programs == NULL)
  90. {
  91. if (errcode_ret != NULL)
  92. *errcode_ret = CL_OUT_OF_HOST_MEMORY;
  93. return NULL;
  94. }
  95. {
  96. for (i=0; i<socl_device_count; i++)
  97. p->cl_programs[i] = NULL;
  98. }
  99. /* Construct structure to pass arguments to workers */
  100. data = (struct cpws_data*)malloc(sizeof(struct cpws_data));
  101. if (data == NULL)
  102. {
  103. if (errcode_ret != NULL)
  104. *errcode_ret = CL_OUT_OF_HOST_MEMORY;
  105. free(p->cl_programs);
  106. return NULL;
  107. }
  108. data->count = count;
  109. data->program = p;
  110. data->strings = (char**)strings;
  111. data->lengths = (size_t*)lengths;
  112. data->errcodes = (cl_int*)malloc(sizeof(cl_int) * socl_device_count);
  113. for (i=0; i<socl_device_count; i++)
  114. {
  115. data->errcodes[i] = CL_SUCCESS;
  116. }
  117. /* Init real cl_program for each OpenCL device */
  118. unsigned workers[context->num_devices];
  119. for (i=0; i<context->num_devices; i++)
  120. {
  121. workers[i] = context->devices[i]->worker_id;
  122. }
  123. starpu_execute_on_specific_workers(soclCreateProgramWithSource_task, data, context->num_devices, workers, "SOCL_CREATE_PROGRAM");
  124. if (errcode_ret != NULL)
  125. {
  126. *errcode_ret = CL_SUCCESS;
  127. for (i=0; i<socl_device_count; i++)
  128. {
  129. if (data->errcodes[i] != CL_SUCCESS)
  130. {
  131. DEBUG_MSG("Worker [%u] failed\n", i);
  132. DEBUG_CL("clCreateProgramWithSource", data->errcodes[i]);
  133. *errcode_ret = data->errcodes[i];
  134. break;
  135. }
  136. }
  137. }
  138. free(data->errcodes);
  139. free(data);
  140. return p;
  141. }