sync_and_notify_data.c 4.8 KB

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