cl_getkernelworkgroupinfo.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "getinfo.h"
  18. struct gkwgi_data {
  19. cl_kernel_work_group_info param_name;
  20. cl_kernel kernel;
  21. union {
  22. size_t work_group_size;
  23. size_t compile_work_group_size[3];
  24. cl_ulong local_mem_size;
  25. };
  26. };
  27. static void gkwgi_task(void *data) {
  28. cl_int err;
  29. struct gkwgi_data *d = (struct gkwgi_data*)data;
  30. int wid = starpu_worker_get_id();
  31. int range = starpu_worker_get_range();
  32. cl_device_id device;
  33. starpu_opencl_get_device(wid, &device);
  34. size_t value;
  35. size_t oldval;
  36. err = clGetKernelWorkGroupInfo(d->kernel->cl_kernels[range], device, d->param_name, sizeof(value), &value, NULL);
  37. if (err != CL_SUCCESS) {
  38. DEBUG_MSG("Worker [%d] failed\n", wid);
  39. DEBUG_CL("clGetKernelWorkGroupInfo", err);
  40. }
  41. switch (d->param_name) {
  42. case CL_KERNEL_WORK_GROUP_SIZE: {
  43. //Get the smallest work group size
  44. do {
  45. oldval = d->work_group_size;
  46. } while (value < oldval && !(__sync_bool_compare_and_swap(&d->work_group_size, oldval, value)));
  47. }
  48. break;
  49. case CL_KERNEL_LOCAL_MEM_SIZE: {
  50. //Get the biggest local mem size
  51. do {
  52. oldval = d->local_mem_size;
  53. } while (value > oldval && !(__sync_bool_compare_and_swap(&d->local_mem_size, oldval, value)));
  54. }
  55. break;
  56. }
  57. }
  58. static void gkwgi_task2(void **UNUSED(desc), void *data) {
  59. cl_int err;
  60. struct gkwgi_data *d = (struct gkwgi_data*)data;
  61. int wid = starpu_worker_get_id();
  62. int range = starpu_worker_get_range();
  63. cl_device_id device;
  64. starpu_opencl_get_device(wid, &device);
  65. err = clGetKernelWorkGroupInfo(d->kernel->cl_kernels[range], device, d->param_name, sizeof(d->compile_work_group_size), &d->compile_work_group_size, NULL);
  66. if (err != CL_SUCCESS) {
  67. DEBUG_MSG("Worker [%d] failed\n", wid);
  68. DEBUG_CL("clGetKernelWorkGroupInfo", err);
  69. }
  70. }
  71. static struct starpu_codelet gkwgi_codelet = {
  72. .where = STARPU_OPENCL,
  73. .opencl_funcs = { gkwgi_task2, NULL },
  74. .nbuffers = 0,
  75. .model = NULL
  76. };
  77. CL_API_ENTRY cl_int CL_API_CALL
  78. soclGetKernelWorkGroupInfo(cl_kernel kernel,
  79. cl_device_id UNUSED(device),
  80. cl_kernel_work_group_info param_name,
  81. size_t param_value_size,
  82. void * param_value,
  83. size_t * param_value_size_ret) CL_API_SUFFIX__VERSION_1_0
  84. {
  85. if (kernel == NULL)
  86. return CL_INVALID_KERNEL;
  87. struct gkwgi_data data;
  88. data.param_name = param_name;
  89. data.kernel = kernel;
  90. switch (param_name) {
  91. case CL_KERNEL_WORK_GROUP_SIZE:
  92. /* We take the smallest value to be sure the kernel can be executed on any available device */
  93. data.work_group_size = SIZE_MAX;
  94. starpu_execute_on_each_worker(gkwgi_task, &data, STARPU_OPENCL);
  95. INFO_CASE_EX2(data.work_group_size);
  96. case CL_KERNEL_COMPILE_WORK_GROUP_SIZE: {
  97. struct starpu_task *task;
  98. task = starpu_task_create();
  99. task->cl = &gkwgi_codelet;
  100. task->cl_arg = &data;
  101. task->cl_arg_size = sizeof(data);
  102. task->synchronous = 1;
  103. starpu_task_submit(task);
  104. INFO_CASE_EX2(data.compile_work_group_size);
  105. }
  106. case CL_KERNEL_LOCAL_MEM_SIZE:
  107. /* We take the biggest value to be sure the kernel can be executed on any available device */
  108. data.local_mem_size = 0;
  109. starpu_execute_on_each_worker(gkwgi_task, &data, STARPU_OPENCL);
  110. INFO_CASE_EX2(data.local_mem_size);
  111. default:
  112. return CL_INVALID_OPERATION;
  113. }
  114. return CL_SUCCESS;
  115. }