increment_init.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 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. 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. unsigned h_dst = 0;
  44. cl_mem d_dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  45. cl_command_queue queue;
  46. starpu_opencl_get_current_queue(&queue);
  47. clEnqueueWriteBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  48. clFinish(queue);
  49. }
  50. #endif
  51. void neutral_cpu_kernel(void *descr[], void *arg)
  52. {
  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 STARPU_ATTRIBUTE_UNUSED)
  75. {
  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. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  90. unsigned host_token;
  91. /* This is a dummy technique of course */
  92. cudaMemcpyAsync(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  93. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  94. host_token++;
  95. cudaMemcpyAsync(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  96. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  97. }
  98. #endif
  99. void increment_cpu_kernel(void *descr[], void *arg)
  100. {
  101. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  102. *tokenptr = *tokenptr + 1;
  103. }
  104. static struct starpu_codelet increment_cl =
  105. {
  106. #ifdef STARPU_USE_CUDA
  107. .cuda_funcs = {increment_cuda_kernel},
  108. #endif
  109. #ifdef STARPU_USE_OPENCL
  110. .opencl_funcs = {increment_opencl_kernel},
  111. #endif
  112. .cpu_funcs = {increment_cpu_kernel},
  113. .cpu_funcs_name = {"increment_cpu_kernel"},
  114. .nbuffers = 1,
  115. .modes = {STARPU_RW}
  116. };
  117. int main(int argc, char **argv)
  118. {
  119. unsigned *pvar = NULL;
  120. int ret;
  121. ret = starpu_init(NULL);
  122. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  123. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  124. starpu_variable_data_register(&handle, -1, 0, sizeof(unsigned));
  125. starpu_data_set_reduction_methods(handle, NULL, &neutral_cl);
  126. #ifdef STARPU_QUICK_CHECK
  127. unsigned ntasks = 32;
  128. unsigned nloops = 4;
  129. #else
  130. unsigned ntasks = 1024;
  131. unsigned nloops = 16;
  132. #endif
  133. unsigned loop;
  134. unsigned t;
  135. for (loop = 0; loop < nloops; loop++)
  136. {
  137. for (t = 0; t < ntasks; t++)
  138. {
  139. struct starpu_task *task = starpu_task_create();
  140. task->cl = &increment_cl;
  141. task->handles[0] = handle;
  142. ret = starpu_task_submit(task);
  143. if (ret == -ENODEV) goto enodev;
  144. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  145. }
  146. ret = starpu_data_acquire(handle, STARPU_R);
  147. pvar = starpu_data_handle_to_pointer(handle, 0);
  148. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  149. if (*pvar != ntasks)
  150. {
  151. FPRINTF(stderr, "[end of loop] Value %u != Expected value %u\n", *pvar, ntasks * (loop+1));
  152. starpu_data_release(handle);
  153. starpu_data_unregister(handle);
  154. goto err;
  155. }
  156. starpu_data_release(handle);
  157. starpu_data_invalidate(handle);
  158. }
  159. starpu_data_unregister(handle);
  160. starpu_shutdown();
  161. return EXIT_SUCCESS;
  162. enodev:
  163. starpu_data_unregister(handle);
  164. fprintf(stderr, "WARNING: No one can execute this task\n");
  165. /* yes, we do not perform the computation but we did detect that no one
  166. * could perform the kernel, so this is not an error from StarPU */
  167. starpu_shutdown();
  168. return STARPU_TEST_SKIPPED;
  169. err:
  170. starpu_shutdown();
  171. return EXIT_FAILURE;
  172. }