cl_buildprogram.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010,2011 Université de 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. {
  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. sprintf(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_ENTRY cl_int CL_API_CALL
  65. soclBuildProgram(cl_program program,
  66. cl_uint num_devices,
  67. const cl_device_id * device_list,
  68. const char * options,
  69. void (*pfn_notify)(cl_program program, void * user_data),
  70. void * user_data) CL_API_SUFFIX__VERSION_1_0
  71. {
  72. struct bp_data *data;
  73. program->options = options != NULL ? strdup(options) : NULL;
  74. program->options_size = options != NULL ? strlen(options)+1 : 0;
  75. data = (struct bp_data*)malloc(sizeof(struct bp_data));
  76. gc_entity_store(&data->program, program);
  77. data->options = (char*)options;
  78. /* If the device list is empty, we compile for every device in the context associated to the program */
  79. if (device_list == NULL)
  80. {
  81. num_devices = program->context->num_devices;
  82. device_list = program->context->devices;
  83. }
  84. data->num_devices = num_devices;
  85. data->device_list = device_list;
  86. /*FIXME: starpu_execute_on_specific_workers is synchronous.
  87. * However pfn_notify is useful only because build is supposed to be asynchronous
  88. */
  89. unsigned workers[num_devices];
  90. unsigned i;
  91. for (i=0; i<num_devices; i++)
  92. {
  93. workers[i] = device_list[i]->worker_id;
  94. }
  95. starpu_execute_on_specific_workers(soclBuildProgram_task, data, num_devices, workers, "SOCL_BUILD_PROGRAM");
  96. if (pfn_notify != NULL)
  97. pfn_notify(program, user_data);
  98. gc_entity_unstore(&data->program);
  99. free(data);
  100. return CL_SUCCESS;
  101. }