cl_buildprogram.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 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 bp_data
  18. {
  19. cl_program program;
  20. char * options;
  21. const cl_device_id * device_list;
  22. cl_uint num_devices;
  23. };
  24. static void soclBuildProgram_task(void *data)
  25. {
  26. struct bp_data *d = (struct bp_data*)data;
  27. cl_device_id device;
  28. cl_int err;
  29. unsigned int i;
  30. int wid = starpu_worker_get_id_check();
  31. /* Check if the kernel has to be built for this device */
  32. for (i=0; i <= d->num_devices; i++)
  33. {
  34. if (i == d->num_devices)
  35. return;
  36. if (d->device_list[i]->worker_id == wid)
  37. break;
  38. }
  39. int range = starpu_worker_get_range();
  40. starpu_opencl_get_device(wid, &device);
  41. DEBUG_MSG("[Worker %d] Building program...\n", wid);
  42. cl_device_type dev_type;
  43. clGetDeviceInfo(device, CL_DEVICE_TYPE, sizeof(cl_device_type), &dev_type, NULL);
  44. char * dev_type_str = (dev_type == CL_DEVICE_TYPE_CPU ? "CPU" :
  45. dev_type == CL_DEVICE_TYPE_GPU ? "GPU" :
  46. dev_type == CL_DEVICE_TYPE_ACCELERATOR ? "ACCELERATOR" : "UNKNOWN");
  47. char opts[4096];
  48. snprintf(opts, sizeof(opts), "-DSOCL_DEVICE_TYPE_%s %s",
  49. dev_type_str, (d->options != NULL ? d->options : ""));
  50. err = clBuildProgram(d->program->cl_programs[range], 1, &device, opts, NULL, NULL);
  51. if (err != CL_SUCCESS)
  52. {
  53. size_t len;
  54. clGetProgramBuildInfo(d->program->cl_programs[range], device, CL_PROGRAM_BUILD_LOG, 0, NULL, &len);
  55. char * buffer = malloc(len+1);
  56. buffer[len] = '\0';
  57. clGetProgramBuildInfo(d->program->cl_programs[range], device, CL_PROGRAM_BUILD_LOG, len, buffer, NULL);
  58. DEBUG_CL("clBuildProgram", err);
  59. ERROR_MSG("clBuildProgram: %s\n Aborting.\n", buffer);
  60. free(buffer);
  61. }
  62. DEBUG_MSG("[Worker %d] Done building.\n", wid);
  63. }
  64. CL_API_SUFFIX__VERSION_1_0
  65. CL_API_ENTRY cl_int CL_API_CALL
  66. soclBuildProgram(cl_program program,
  67. cl_uint num_devices,
  68. const cl_device_id * device_list,
  69. const char * options,
  70. void (*pfn_notify)(cl_program program, void * user_data),
  71. void * user_data)
  72. {
  73. struct bp_data *data;
  74. program->options = options != NULL ? strdup(options) : NULL;
  75. program->options_size = options != NULL ? strlen(options)+1 : 0;
  76. data = (struct bp_data*)malloc(sizeof(struct bp_data));
  77. gc_entity_store(&data->program, program);
  78. data->options = (char*)options;
  79. /* If the device list is empty, we compile for every device in the context associated to the program */
  80. if (device_list == NULL)
  81. {
  82. num_devices = program->context->num_devices;
  83. device_list = program->context->devices;
  84. }
  85. data->num_devices = num_devices;
  86. data->device_list = device_list;
  87. /*FIXME: starpu_execute_on_specific_workers is synchronous.
  88. * However pfn_notify is useful only because build is supposed to be asynchronous
  89. */
  90. unsigned workers[num_devices];
  91. unsigned i;
  92. for (i=0; i<num_devices; i++)
  93. {
  94. workers[i] = device_list[i]->worker_id;
  95. }
  96. starpu_execute_on_specific_workers(soclBuildProgram_task, data, num_devices, workers, "SOCL_BUILD_PROGRAM");
  97. if (pfn_notify != NULL)
  98. pfn_notify(program, user_data);
  99. gc_entity_unstore(&data->program);
  100. free(data);
  101. return CL_SUCCESS;
  102. }