increment_init.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <config.h>
  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. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  33. /* This is a dummy technique of course */
  34. unsigned host_dst = 0;
  35. cudaMemcpyAsync(dst, &host_dst, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  36. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  37. }
  38. #endif
  39. #ifdef STARPU_USE_OPENCL
  40. static void neutral_opencl_kernel(void *descr[], void *arg)
  41. {
  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. 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 STARPU_ATTRIBUTE_UNUSED)
  74. {
  75. cl_mem d_token = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  76. unsigned h_token;
  77. cl_command_queue queue;
  78. starpu_opencl_get_current_queue(&queue);
  79. clEnqueueReadBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  80. h_token++;
  81. clEnqueueWriteBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  82. clFinish(queue);
  83. }
  84. #endif
  85. #ifdef STARPU_USE_CUDA
  86. static void increment_cuda_kernel(void *descr[], void *arg)
  87. {
  88. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  89. unsigned host_token;
  90. /* This is a dummy technique of course */
  91. cudaMemcpyAsync(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  92. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  93. host_token++;
  94. cudaMemcpyAsync(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  95. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  96. }
  97. #endif
  98. void increment_cpu_kernel(void *descr[], void *arg)
  99. {
  100. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  101. *tokenptr = *tokenptr + 1;
  102. }
  103. static struct starpu_codelet increment_cl =
  104. {
  105. #ifdef STARPU_USE_CUDA
  106. .cuda_funcs = {increment_cuda_kernel},
  107. #endif
  108. #ifdef STARPU_USE_OPENCL
  109. .opencl_funcs = {increment_opencl_kernel},
  110. #endif
  111. .cpu_funcs = {increment_cpu_kernel},
  112. .cpu_funcs_name = {"increment_cpu_kernel"},
  113. .nbuffers = 1,
  114. .modes = {STARPU_RW}
  115. };
  116. int main(int argc, char **argv)
  117. {
  118. unsigned *pvar = NULL;
  119. int ret;
  120. ret = starpu_init(NULL);
  121. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  122. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  123. starpu_variable_data_register(&handle, -1, 0, sizeof(unsigned));
  124. starpu_data_set_reduction_methods(handle, NULL, &neutral_cl);
  125. #ifdef STARPU_QUICK_CHECK
  126. unsigned ntasks = 32;
  127. unsigned nloops = 4;
  128. #else
  129. unsigned ntasks = 1024;
  130. unsigned nloops = 16;
  131. #endif
  132. unsigned loop;
  133. unsigned t;
  134. for (loop = 0; loop < nloops; loop++)
  135. {
  136. for (t = 0; t < ntasks; t++)
  137. {
  138. struct starpu_task *task = starpu_task_create();
  139. task->cl = &increment_cl;
  140. task->handles[0] = handle;
  141. ret = starpu_task_submit(task);
  142. if (ret == -ENODEV) goto enodev;
  143. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  144. }
  145. ret = starpu_data_acquire(handle, STARPU_R);
  146. pvar = starpu_data_handle_to_pointer(handle, 0);
  147. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  148. if (*pvar != ntasks)
  149. {
  150. FPRINTF(stderr, "[end of loop] Value %u != Expected value %u\n", *pvar, ntasks * (loop+1));
  151. starpu_data_release(handle);
  152. starpu_data_unregister(handle);
  153. goto err;
  154. }
  155. starpu_data_release(handle);
  156. starpu_data_invalidate(handle);
  157. }
  158. starpu_data_unregister(handle);
  159. starpu_shutdown();
  160. return EXIT_SUCCESS;
  161. enodev:
  162. starpu_data_unregister(handle);
  163. fprintf(stderr, "WARNING: No one can execute this task\n");
  164. /* yes, we do not perform the computation but we did detect that no one
  165. * could perform the kernel, so this is not an error from StarPU */
  166. starpu_shutdown();
  167. return STARPU_TEST_SKIPPED;
  168. err:
  169. starpu_shutdown();
  170. return EXIT_FAILURE;
  171. }