sync_and_notify_data.c 4.7 KB

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