increment_redux.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #ifdef STARPU_USE_CUDA
  18. #include <cuda.h>
  19. #endif
  20. static unsigned var = 0;
  21. static starpu_data_handle handle;
  22. /*
  23. * Reduction methods
  24. */
  25. #ifdef STARPU_USE_CUDA
  26. static void redux_cuda_kernel(void *descr[], void *arg)
  27. {
  28. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  29. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  30. unsigned host_dst, host_src;
  31. /* This is a dummy technique of course */
  32. cudaMemcpy(&host_src, src, sizeof(unsigned), cudaMemcpyDeviceToHost);
  33. cudaMemcpy(&host_dst, dst, sizeof(unsigned), cudaMemcpyDeviceToHost);
  34. cudaThreadSynchronize();
  35. host_dst += host_src;
  36. cudaMemcpy(src, &host_src, sizeof(unsigned), cudaMemcpyHostToDevice);
  37. cudaMemcpy(dst, &host_dst, sizeof(unsigned), cudaMemcpyHostToDevice);
  38. cudaThreadSynchronize();
  39. }
  40. static void neutral_cuda_kernel(void *descr[], void *arg)
  41. {
  42. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  43. /* This is a dummy technique of course */
  44. unsigned host_dst = 0;
  45. cudaMemcpy(dst, &host_dst, sizeof(unsigned), cudaMemcpyHostToDevice);
  46. cudaThreadSynchronize();
  47. }
  48. #endif
  49. static void redux_cpu_kernel(void *descr[], void *arg)
  50. {
  51. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  52. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  53. *dst = *dst + *src;
  54. }
  55. static void neutral_cpu_kernel(void *descr[], void *arg)
  56. {
  57. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  58. *dst = 0;
  59. }
  60. static starpu_codelet redux_cl = {
  61. .where = STARPU_CPU|STARPU_CUDA,
  62. #ifdef STARPU_USE_CUDA
  63. .cuda_func = redux_cuda_kernel,
  64. #endif
  65. .cpu_func = redux_cpu_kernel,
  66. .nbuffers = 2
  67. };
  68. static starpu_codelet neutral_cl = {
  69. .where = STARPU_CPU|STARPU_CUDA,
  70. #ifdef STARPU_USE_CUDA
  71. .cuda_func = neutral_cuda_kernel,
  72. #endif
  73. .cpu_func = neutral_cpu_kernel,
  74. .nbuffers = 1
  75. };
  76. /*
  77. * Increment codelet
  78. */
  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. cudaMemcpy(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost);
  86. cudaThreadSynchronize();
  87. host_token++;
  88. cudaMemcpy(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice);
  89. cudaThreadSynchronize();
  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 starpu_codelet increment_cl = {
  98. .where = STARPU_CPU|STARPU_CUDA,
  99. #ifdef STARPU_USE_CUDA
  100. .cuda_func = increment_cuda_kernel,
  101. #endif
  102. .cpu_func = increment_cpu_kernel,
  103. .nbuffers = 1
  104. };
  105. int main(int argc, char **argv)
  106. {
  107. starpu_init(NULL);
  108. starpu_variable_data_register(&handle, 0, (uintptr_t)&var, sizeof(unsigned));
  109. starpu_data_set_reduction_methods(handle, &redux_cl, &neutral_cl);
  110. unsigned ntasks = 1024;
  111. unsigned nloops = 16;
  112. unsigned loop;
  113. unsigned t;
  114. for (loop = 0; loop < nloops; loop++)
  115. {
  116. for (t = 0; t < ntasks; t++)
  117. {
  118. struct starpu_task *task = starpu_task_create();
  119. task->cl = &increment_cl;
  120. task->buffers[0].mode = STARPU_REDUX;
  121. task->buffers[0].handle = handle;
  122. int ret = starpu_task_submit(task);
  123. STARPU_ASSERT(!ret);
  124. }
  125. starpu_data_acquire(handle, STARPU_R);
  126. STARPU_ASSERT(var == ntasks*(loop + 1));
  127. starpu_data_release(handle);
  128. }
  129. starpu_data_unregister(handle);
  130. STARPU_ASSERT(var == ntasks*nloops);
  131. starpu_shutdown();
  132. return 0;
  133. }