cl_createprogramwithsource.c 4.3 KB

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