sync_and_notify_data.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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 <stdio.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <starpu.h>
  21. #ifdef STARPU_USE_GORDON
  22. #include <gordon.h>
  23. #endif
  24. #include "../common/helper.h"
  25. #define N 100
  26. #define K 256
  27. /*
  28. * In this test, we maintain a vector v = (a,b,c).
  29. *
  30. * Each iteration consists of:
  31. * - increment a n times
  32. * - sync v in ram
  33. * - incrementer b
  34. * - notify the modification of v
  35. * - incrementer c n times
  36. * - sync v
  37. *
  38. * At the end, we have to make sure that if we did k iterations,
  39. * v == (kn, k, kn)
  40. */
  41. #ifdef STARPU_USE_CUDA
  42. void cuda_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args);
  43. void cuda_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args);
  44. #endif
  45. #ifdef STARPU_USE_OPENCL
  46. #include <starpu_opencl.h>
  47. void opencl_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args);
  48. void opencl_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args);
  49. struct starpu_opencl_program opencl_code;
  50. #endif
  51. #define VECTORSIZE 16
  52. starpu_data_handle v_handle;
  53. static unsigned v[VECTORSIZE] __attribute__((aligned(128))) = {0, 0, 0, 0};
  54. void cpu_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args)
  55. {
  56. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  57. val[0]++;
  58. }
  59. void cpu_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args)
  60. {
  61. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  62. val[2]++;
  63. }
  64. int main(int argc, char **argv)
  65. {
  66. int ret;
  67. ret = starpu_init(NULL);
  68. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  69. #ifdef STARPU_USE_GORDON
  70. unsigned elf_id = gordon_register_elf_plugin("./datawizard/sync_and_notify_data_gordon_kernels.spuelf");
  71. gordon_load_plugin_on_all_spu(elf_id);
  72. unsigned kernel_incA_id = gordon_register_kernel(elf_id, "incA");
  73. gordon_load_kernel_on_all_spu(kernel_incA_id);
  74. unsigned kernel_incC_id = gordon_register_kernel(elf_id, "incC");
  75. gordon_load_kernel_on_all_spu(kernel_incC_id);
  76. FPRINTF(stderr, "kernel incA %d incC %d elf %d\n", kernel_incA_id, kernel_incC_id, elf_id);
  77. #endif
  78. #ifdef STARPU_USE_OPENCL
  79. starpu_opencl_load_opencl_from_file("tests/datawizard/sync_and_notify_data_opencl_codelet.cl", &opencl_code, NULL);
  80. #endif
  81. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  82. unsigned iter;
  83. for (iter = 0; iter < K; iter++)
  84. {
  85. int ret;
  86. unsigned ind;
  87. for (ind = 0; ind < N; ind++)
  88. {
  89. /* increment a = v[0] */
  90. starpu_codelet cl_inc_a = {
  91. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_GORDON,
  92. .cpu_func = cpu_codelet_incA,
  93. #ifdef STARPU_USE_CUDA
  94. .cuda_func = cuda_codelet_incA,
  95. #endif
  96. #ifdef STARPU_USE_OPENCL
  97. .opencl_func = opencl_codelet_incA,
  98. #endif
  99. #ifdef STARPU_USE_GORDON
  100. .gordon_func = kernel_incA_id,
  101. #endif
  102. .nbuffers = 1
  103. };
  104. struct starpu_task *task = starpu_task_create();
  105. task->cl = &cl_inc_a;
  106. task->buffers[0].handle = v_handle;
  107. task->buffers[0].mode = STARPU_RW;
  108. task->synchronous = 1;
  109. ret = starpu_task_submit(task);
  110. if (ret == -ENODEV) goto enodev;
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  112. }
  113. /* synchronize v in RAM */
  114. ret = starpu_data_acquire(v_handle, STARPU_RW);
  115. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  116. /* increment b */
  117. v[1]++;
  118. starpu_data_release(v_handle);
  119. for (ind = 0; ind < N; ind++)
  120. {
  121. /* increment c = v[2] */
  122. starpu_codelet cl_inc_c = {
  123. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_GORDON,
  124. .cpu_func = cpu_codelet_incC,
  125. #ifdef STARPU_USE_CUDA
  126. .cuda_func = cuda_codelet_incC,
  127. #endif
  128. #ifdef STARPU_USE_OPENCL
  129. .opencl_func = opencl_codelet_incC,
  130. #endif
  131. #ifdef STARPU_USE_GORDON
  132. .gordon_func = kernel_incC_id,
  133. #endif
  134. .nbuffers = 1
  135. };
  136. struct starpu_task *task = starpu_task_create();
  137. task->cl = &cl_inc_c;
  138. task->buffers[0].handle = v_handle;
  139. task->buffers[0].mode = STARPU_RW;
  140. task->synchronous = 1;
  141. ret = starpu_task_submit(task);
  142. if (ret == -ENODEV) goto enodev;
  143. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  144. }
  145. }
  146. ret = starpu_data_acquire(v_handle, STARPU_RW);
  147. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  148. FPRINTF(stderr, "V = {%u, %u, %u}\n", v[0], v[1], v[2]);
  149. starpu_data_release(v_handle);
  150. starpu_data_unregister(v_handle);
  151. starpu_shutdown();
  152. if ((v[0] != N*K) || (v[1] != K) || (v[2] != N*K))
  153. return -1;
  154. return 0;
  155. enodev:
  156. starpu_data_unregister(v_handle);
  157. starpu_shutdown();
  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. return 77;
  162. }