increment_init.c 5.5 KB

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