increment_init.c 5.4 KB

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