cl_buildprogram.c 3.9 KB

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