increment_init.c 5.5 KB

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