increment_init.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 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 <starpu.h>
  17. #include "../helper.h"
  18. /*
  19. * Check that the initializer passed to starpu_data_set_reduction_methods
  20. * is used to initialize a handle when it is registered from NULL, and when
  21. * starpu_data_invalidate is called
  22. */
  23. /*
  24. * Reduction methods
  25. */
  26. #ifdef STARPU_USE_CUDA
  27. static void neutral_cuda_kernel(void *descr[], void *arg)
  28. {
  29. (void)arg;
  30. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  31. /* This is a dummy technique of course */
  32. unsigned host_dst = 0;
  33. cudaMemcpyAsync(dst, &host_dst, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  34. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  35. }
  36. #endif
  37. #ifdef STARPU_USE_OPENCL
  38. static void neutral_opencl_kernel(void *descr[], void *arg)
  39. {
  40. (void)arg;
  41. unsigned h_dst = 0;
  42. cl_mem d_dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  43. cl_command_queue queue;
  44. starpu_opencl_get_current_queue(&queue);
  45. clEnqueueWriteBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  46. clFinish(queue);
  47. }
  48. #endif
  49. void neutral_cpu_kernel(void *descr[], void *arg)
  50. {
  51. (void)arg;
  52. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  53. *dst = 0;
  54. }
  55. static struct starpu_codelet neutral_cl =
  56. {
  57. #ifdef STARPU_USE_CUDA
  58. .cuda_funcs = {neutral_cuda_kernel},
  59. #endif
  60. #ifdef STARPU_USE_OPENCL
  61. .opencl_funcs = {neutral_opencl_kernel},
  62. #endif
  63. .cpu_funcs = {neutral_cpu_kernel},
  64. .cpu_funcs_name = {"neutral_cpu_kernel"},
  65. .modes = {STARPU_W},
  66. .nbuffers = 1
  67. };
  68. /*
  69. * Increment codelet
  70. */
  71. #ifdef STARPU_USE_OPENCL
  72. /* dummy OpenCL implementation */
  73. static void increment_opencl_kernel(void *descr[], void *cl_arg)
  74. {
  75. (void)cl_arg;
  76. cl_mem d_token = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  77. unsigned h_token;
  78. cl_command_queue queue;
  79. starpu_opencl_get_current_queue(&queue);
  80. clEnqueueReadBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  81. h_token++;
  82. clEnqueueWriteBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  83. clFinish(queue);
  84. }
  85. #endif
  86. #ifdef STARPU_USE_CUDA
  87. static void increment_cuda_kernel(void *descr[], void *arg)
  88. {
  89. (void)arg;
  90. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  91. unsigned host_token;
  92. /* This is a dummy technique of course */
  93. cudaMemcpyAsync(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  94. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  95. host_token++;
  96. cudaMemcpyAsync(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  97. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  98. }
  99. #endif
  100. void increment_cpu_kernel(void *descr[], void *arg)
  101. {
  102. (void)arg;
  103. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  104. *tokenptr = *tokenptr + 1;
  105. }
  106. static struct starpu_codelet increment_cl =
  107. {
  108. #ifdef STARPU_USE_CUDA
  109. .cuda_funcs = {increment_cuda_kernel},
  110. #endif
  111. #ifdef STARPU_USE_OPENCL
  112. .opencl_funcs = {increment_opencl_kernel},
  113. #endif
  114. .cpu_funcs = {increment_cpu_kernel},
  115. .cpu_funcs_name = {"increment_cpu_kernel"},
  116. .nbuffers = 1,
  117. .modes = {STARPU_RW}
  118. };
  119. int main(void)
  120. {
  121. unsigned *pvar = NULL;
  122. int ret;
  123. starpu_data_handle_t handle;
  124. ret = starpu_init(NULL);
  125. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  126. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  127. starpu_variable_data_register(&handle, -1, 0, sizeof(unsigned));
  128. starpu_data_set_reduction_methods(handle, NULL, &neutral_cl);
  129. #ifdef STARPU_QUICK_CHECK
  130. unsigned ntasks = 32;
  131. unsigned nloops = 4;
  132. #else
  133. unsigned ntasks = 1024;
  134. unsigned nloops = 16;
  135. #endif
  136. unsigned loop;
  137. unsigned t;
  138. for (loop = 0; loop < nloops; loop++)
  139. {
  140. for (t = 0; t < ntasks; t++)
  141. {
  142. struct starpu_task *task = starpu_task_create();
  143. task->cl = &increment_cl;
  144. task->handles[0] = handle;
  145. ret = starpu_task_submit(task);
  146. if (ret == -ENODEV) goto enodev;
  147. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  148. }
  149. ret = starpu_data_acquire(handle, STARPU_R);
  150. pvar = starpu_data_handle_to_pointer(handle, 0);
  151. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  152. if (*pvar != ntasks)
  153. {
  154. FPRINTF(stderr, "[end of loop] Value %u != Expected value %u\n", *pvar, ntasks * (loop+1));
  155. starpu_data_release(handle);
  156. starpu_data_unregister(handle);
  157. goto err;
  158. }
  159. starpu_data_release(handle);
  160. starpu_data_invalidate(handle);
  161. }
  162. starpu_data_unregister(handle);
  163. starpu_shutdown();
  164. return EXIT_SUCCESS;
  165. enodev:
  166. starpu_data_unregister(handle);
  167. fprintf(stderr, "WARNING: No one can execute this task\n");
  168. /* yes, we do not perform the computation but we did detect that no one
  169. * could perform the kernel, so this is not an error from StarPU */
  170. starpu_shutdown();
  171. return STARPU_TEST_SKIPPED;
  172. err:
  173. starpu_shutdown();
  174. return EXIT_FAILURE;
  175. }