cl_createprogramwithsource.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2012 Inria
  4. * Copyright (C) 2012,2016-2017 CNRS
  5. * Copyright (C) 2010-2011,2013, 2018 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include "socl.h"
  19. struct cpws_data
  20. {
  21. struct _cl_program *program;
  22. cl_int *errcodes;
  23. cl_uint count;
  24. char **strings;
  25. size_t *lengths;
  26. };
  27. static void soclCreateProgramWithSource_task(void *data)
  28. {
  29. struct cpws_data *d = (struct cpws_data*)data;
  30. cl_context context;
  31. int wid = starpu_worker_get_id_check();
  32. DEBUG_MSG("Worker id: %d\n", wid);
  33. int range = starpu_worker_get_range();
  34. starpu_opencl_get_context(wid, &context);
  35. d->program->cl_programs[range] = clCreateProgramWithSource(context, d->count, (const char**)d->strings, d->lengths, &d->errcodes[range]);
  36. }
  37. static void release_callback_program(void * e)
  38. {
  39. cl_program program = (cl_program)e;
  40. unsigned int i;
  41. for (i=0; i<socl_device_count; i++)
  42. {
  43. if (program->cl_programs[i] != NULL)
  44. {
  45. cl_int err = clReleaseProgram(program->cl_programs[i]);
  46. if (err != CL_SUCCESS)
  47. DEBUG_CL("clReleaseProgram", err);
  48. }
  49. }
  50. /* Release references */
  51. gc_entity_unstore(&program->context);
  52. free(program->cl_programs);
  53. if (program->options != NULL)
  54. free(program->options);
  55. }
  56. CL_API_SUFFIX__VERSION_1_0
  57. CL_API_ENTRY cl_program CL_API_CALL
  58. soclCreateProgramWithSource(cl_context context,
  59. cl_uint count,
  60. const char ** strings,
  61. const size_t * lengths,
  62. cl_int * errcode_ret)
  63. {
  64. cl_program p;
  65. struct cpws_data *data;
  66. unsigned int i;
  67. if (errcode_ret != NULL)
  68. *errcode_ret = CL_SUCCESS;
  69. /* Check arguments */
  70. if (count == 0 || strings == NULL)
  71. {
  72. if (errcode_ret != NULL)
  73. *errcode_ret = CL_INVALID_VALUE;
  74. return NULL;
  75. }
  76. /* Alloc cl_program structure */
  77. p = (cl_program)gc_entity_alloc(sizeof(struct _cl_program), release_callback_program, "program");
  78. if (p == NULL)
  79. {
  80. if (errcode_ret != NULL)
  81. *errcode_ret = CL_OUT_OF_HOST_MEMORY;
  82. return NULL;
  83. }
  84. gc_entity_store(&p->context, context);
  85. p->options = NULL;
  86. #ifdef DEBUG
  87. static int id = 0;
  88. p->id = id++;
  89. #endif
  90. p->cl_programs = (cl_program*)malloc(sizeof(cl_program) * socl_device_count);
  91. if (p->cl_programs == NULL)
  92. {
  93. if (errcode_ret != NULL)
  94. *errcode_ret = CL_OUT_OF_HOST_MEMORY;
  95. return NULL;
  96. }
  97. {
  98. for (i=0; i<socl_device_count; i++)
  99. p->cl_programs[i] = NULL;
  100. }
  101. /* Construct structure to pass arguments to workers */
  102. data = (struct cpws_data*)malloc(sizeof(struct cpws_data));
  103. if (data == NULL)
  104. {
  105. if (errcode_ret != NULL)
  106. *errcode_ret = CL_OUT_OF_HOST_MEMORY;
  107. return NULL;
  108. }
  109. data->count = count;
  110. data->program = p;
  111. data->strings = (char**)strings;
  112. data->lengths = (size_t*)lengths;
  113. data->errcodes = (cl_int*)malloc(sizeof(cl_int) * socl_device_count);
  114. for (i=0; i<socl_device_count; i++)
  115. {
  116. data->errcodes[i] = CL_SUCCESS;
  117. }
  118. /* Init real cl_program for each OpenCL device */
  119. unsigned workers[context->num_devices];
  120. for (i=0; i<context->num_devices; i++)
  121. {
  122. workers[i] = context->devices[i]->worker_id;
  123. }
  124. starpu_execute_on_specific_workers(soclCreateProgramWithSource_task, data, context->num_devices, workers, "SOCL_CREATE_PROGRAM");
  125. if (errcode_ret != NULL)
  126. {
  127. *errcode_ret = CL_SUCCESS;
  128. for (i=0; i<socl_device_count; i++)
  129. {
  130. if (data->errcodes[i] != CL_SUCCESS)
  131. {
  132. DEBUG_MSG("Worker [%u] failed\n", i);
  133. DEBUG_CL("clCreateProgramWithSource", data->errcodes[i]);
  134. *errcode_ret = data->errcodes[i];
  135. break;
  136. }
  137. }
  138. }
  139. free(data->errcodes);
  140. free(data);
  141. return p;
  142. }