in_place_partition.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Université de Bordeaux 1
  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 <starpu.h>
  17. #include <starpu_opencl.h>
  18. #include "../helper.h"
  19. void scal_func_cpu(void *buffers[], void *cl_arg)
  20. {
  21. unsigned i;
  22. struct starpu_vector_interface *vector = (struct starpu_vector_interface *) buffers[0];
  23. unsigned *val = (unsigned *) STARPU_VECTOR_GET_PTR(vector);
  24. unsigned n = STARPU_VECTOR_GET_NX(vector);
  25. /* scale the vector */
  26. for (i = 0; i < n; i++)
  27. val[i] *= 2;
  28. }
  29. #ifdef STARPU_USE_CUDA
  30. extern void scal_func_cuda(void *buffers[], void *cl_arg);
  31. #endif
  32. #ifdef STARPU_USE_OPENCL
  33. static struct starpu_opencl_program opencl_program;
  34. void scal_func_opencl(void *buffers[], void *_args)
  35. {
  36. int id, devid;
  37. cl_int err;
  38. cl_kernel kernel;
  39. cl_command_queue queue;
  40. cl_event event;
  41. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  42. cl_mem val = (cl_mem)STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  43. unsigned offset = STARPU_VECTOR_GET_OFFSET(buffers[0]);
  44. id = starpu_worker_get_id();
  45. devid = starpu_worker_get_devid(id);
  46. err = starpu_opencl_load_kernel(&kernel, &queue, &opencl_program, "vector_mult_opencl", devid);
  47. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  48. err = clSetKernelArg(kernel, 0, sizeof(val), &val);
  49. err |= clSetKernelArg(kernel, 1, sizeof(offset), &offset);
  50. err |= clSetKernelArg(kernel, 2, sizeof(n), &n);
  51. if (err) STARPU_OPENCL_REPORT_ERROR(err);
  52. {
  53. size_t global=n;
  54. size_t local;
  55. size_t s;
  56. cl_device_id device;
  57. starpu_opencl_get_device(devid, &device);
  58. err = clGetKernelWorkGroupInfo (kernel, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(local), &local, &s);
  59. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  60. if (local > global) local=global;
  61. err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, &local, 0, NULL, &event);
  62. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  63. }
  64. clFinish(queue);
  65. starpu_opencl_collect_stats(event);
  66. clReleaseEvent(event);
  67. starpu_opencl_release_kernel(kernel);
  68. }
  69. #endif
  70. static struct starpu_codelet codelet =
  71. {
  72. .where = STARPU_CPU
  73. #ifdef STARPU_USE_CUDA
  74. | STARPU_CUDA
  75. #endif
  76. #ifdef STARPU_USE_OPENCL
  77. | STARPU_OPENCL
  78. #endif
  79. ,
  80. .cpu_funcs = { scal_func_cpu, NULL },
  81. #ifdef STARPU_USE_OPENCL
  82. .opencl_funcs = { scal_func_opencl, NULL },
  83. #endif
  84. #ifdef STARPU_USE_CUDA
  85. .cuda_funcs = { scal_func_cuda, NULL },
  86. #endif
  87. .modes = { STARPU_RW },
  88. .model = NULL,
  89. .nbuffers = 1
  90. };
  91. int main(int argc, char **argv)
  92. {
  93. unsigned *foo;
  94. starpu_data_handle_t handle;
  95. int ret;
  96. int n, i, size;
  97. ret = starpu_init(NULL);
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  99. #ifdef STARPU_USE_OPENCL
  100. starpu_opencl_load_opencl_from_file("tests/datawizard/scal_opencl.cl", &opencl_program, NULL);
  101. #endif
  102. n = starpu_worker_get_count();
  103. size = 10 * n;
  104. foo = calloc(size, sizeof(*foo));
  105. for (i = 0; i < size; i++)
  106. foo[i] = i;
  107. starpu_vector_data_register(&handle, 0, (uintptr_t)foo, size, sizeof(*foo));
  108. /* Broadcast the data to force in-place partitioning */
  109. for (i = 0; i < n; i++)
  110. starpu_data_prefetch_on_node(handle, starpu_worker_get_memory_node(i), 0);
  111. struct starpu_data_filter f =
  112. {
  113. .filter_func = starpu_block_filter_func_vector,
  114. .nchildren = n > 1 ? n : 2,
  115. };
  116. starpu_data_partition(handle, &f);
  117. for (i = 0; i < n; i++) {
  118. struct starpu_task *task = starpu_task_create();
  119. task->handles[0] = starpu_data_get_sub_data(handle, 1, i);
  120. task->cl = &codelet;
  121. task->execute_on_a_specific_worker = 1;
  122. task->workerid = i;
  123. ret = starpu_task_submit(task);
  124. if (ret == -ENODEV) goto enodev;
  125. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  126. }
  127. ret = starpu_task_wait_for_all();
  128. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  129. starpu_data_unpartition(handle, 0);
  130. starpu_data_unregister(handle);
  131. starpu_shutdown();
  132. for (i = 0; i < size; i++) {
  133. if (foo[i] != i*2) {
  134. fprintf(stderr,"value %d is %d instead of %d\n", i, foo[i], 2*i);
  135. return EXIT_FAILURE;
  136. }
  137. }
  138. return EXIT_SUCCESS;
  139. enodev:
  140. starpu_data_unregister(handle);
  141. fprintf(stderr, "WARNING: No one can execute this task\n");
  142. /* yes, we do not perform the computation but we did detect that no one
  143. * could perform the kernel, so this is not an error from StarPU */
  144. starpu_shutdown();
  145. return STARPU_TEST_SKIPPED;
  146. }