increment_init.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 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. static starpu_data_handle_t handle;
  24. /*
  25. * Reduction methods
  26. */
  27. #ifdef STARPU_USE_CUDA
  28. static void neutral_cuda_kernel(void *descr[], void *arg)
  29. {
  30. (void)arg;
  31. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  32. /* This is a dummy technique of course */
  33. unsigned host_dst = 0;
  34. cudaMemcpyAsync(dst, &host_dst, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  35. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  36. }
  37. #endif
  38. #ifdef STARPU_USE_OPENCL
  39. static void neutral_opencl_kernel(void *descr[], void *arg)
  40. {
  41. (void)arg;
  42. unsigned h_dst = 0;
  43. cl_mem d_dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  44. cl_command_queue queue;
  45. starpu_opencl_get_current_queue(&queue);
  46. clEnqueueWriteBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  47. clFinish(queue);
  48. }
  49. #endif
  50. void neutral_cpu_kernel(void *descr[], void *arg)
  51. {
  52. (void)arg;
  53. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  54. *dst = 0;
  55. }
  56. static struct starpu_codelet neutral_cl =
  57. {
  58. #ifdef STARPU_USE_CUDA
  59. .cuda_funcs = {neutral_cuda_kernel},
  60. #endif
  61. #ifdef STARPU_USE_OPENCL
  62. .opencl_funcs = {neutral_opencl_kernel},
  63. #endif
  64. .cpu_funcs = {neutral_cpu_kernel},
  65. .cpu_funcs_name = {"neutral_cpu_kernel"},
  66. .modes = {STARPU_W},
  67. .nbuffers = 1
  68. };
  69. /*
  70. * Increment codelet
  71. */
  72. #ifdef STARPU_USE_OPENCL
  73. /* dummy OpenCL implementation */
  74. static void increment_opencl_kernel(void *descr[], void *cl_arg)
  75. {
  76. (void)cl_arg;
  77. cl_mem d_token = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  78. unsigned h_token;
  79. cl_command_queue queue;
  80. starpu_opencl_get_current_queue(&queue);
  81. clEnqueueReadBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  82. h_token++;
  83. clEnqueueWriteBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  84. clFinish(queue);
  85. }
  86. #endif
  87. #ifdef STARPU_USE_CUDA
  88. static void increment_cuda_kernel(void *descr[], void *arg)
  89. {
  90. (void)arg;
  91. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  92. unsigned host_token;
  93. /* This is a dummy technique of course */
  94. cudaMemcpyAsync(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  95. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  96. host_token++;
  97. cudaMemcpyAsync(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  98. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  99. }
  100. #endif
  101. void increment_cpu_kernel(void *descr[], void *arg)
  102. {
  103. (void)arg;
  104. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  105. *tokenptr = *tokenptr + 1;
  106. }
  107. static struct starpu_codelet increment_cl =
  108. {
  109. #ifdef STARPU_USE_CUDA
  110. .cuda_funcs = {increment_cuda_kernel},
  111. #endif
  112. #ifdef STARPU_USE_OPENCL
  113. .opencl_funcs = {increment_opencl_kernel},
  114. #endif
  115. .cpu_funcs = {increment_cpu_kernel},
  116. .cpu_funcs_name = {"increment_cpu_kernel"},
  117. .nbuffers = 1,
  118. .modes = {STARPU_RW}
  119. };
  120. int main(void)
  121. {
  122. unsigned *pvar = NULL;
  123. int ret;
  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. }