increment_init.c 5.2 KB

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