increment_init.c 5.1 KB

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