sync_and_notify_data.c 5.6 KB

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