sync_and_notify_data_implicit.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. //#define N 1
  26. //#define K 1
  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. void opencl_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args);
  47. void opencl_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args);
  48. #endif
  49. #define VECTORSIZE 16
  50. starpu_data_handle v_handle;
  51. static unsigned v[VECTORSIZE] __attribute__((aligned(128))) = {0, 0, 0, 0};
  52. void cpu_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args)
  53. {
  54. unsigned *val = (unsigned *)STARPU_GET_VECTOR_PTR(descr[0]);
  55. val[0]++;
  56. }
  57. void cpu_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args)
  58. {
  59. unsigned *val = (unsigned *)STARPU_GET_VECTOR_PTR(descr[0]);
  60. val[2]++;
  61. }
  62. /* increment a = v[0] */
  63. static starpu_codelet cl_inc_a = {
  64. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_GORDON,
  65. .cpu_func = cpu_codelet_incA,
  66. #ifdef STARPU_USE_CUDA
  67. .cuda_func = cuda_codelet_incA,
  68. #endif
  69. #ifdef STARPU_USE_OPENCL
  70. .opencl_func = opencl_codelet_incA,
  71. #endif
  72. #ifdef STARPU_USE_GORDON
  73. .gordon_func = kernel_incA_id,
  74. #endif
  75. .nbuffers = 1
  76. };
  77. /* increment c = v[2] */
  78. starpu_codelet cl_inc_c = {
  79. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_GORDON,
  80. .cpu_func = cpu_codelet_incC,
  81. #ifdef STARPU_USE_CUDA
  82. .cuda_func = cuda_codelet_incC,
  83. #endif
  84. #ifdef STARPU_USE_OPENCL
  85. .opencl_func = opencl_codelet_incC,
  86. #endif
  87. #ifdef STARPU_USE_GORDON
  88. .gordon_func = kernel_incC_id,
  89. #endif
  90. .nbuffers = 1
  91. };
  92. int main(int argc, char **argv)
  93. {
  94. starpu_init(NULL);
  95. #ifdef STARPU_USE_GORDON
  96. unsigned elf_id = gordon_register_elf_plugin("./datawizard/sync_and_notify_data_gordon_kernels.spuelf");
  97. gordon_load_plugin_on_all_spu(elf_id);
  98. unsigned kernel_incA_id = gordon_register_kernel(elf_id, "incA");
  99. gordon_load_kernel_on_all_spu(kernel_incA_id);
  100. unsigned kernel_incC_id = gordon_register_kernel(elf_id, "incC");
  101. gordon_load_kernel_on_all_spu(kernel_incC_id);
  102. fprintf(stderr, "kernel incA %d incC %d elf %d\n", kernel_incA_id, kernel_incC_id, elf_id);
  103. #endif
  104. #ifdef STARPU_USE_OPENCL
  105. _starpu_opencl_compile_source_to_opencl("tests/datawizard/sync_and_notify_data_opencl_codelet.cl");
  106. #endif
  107. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  108. unsigned iter;
  109. for (iter = 0; iter < K; iter++)
  110. {
  111. int ret;
  112. unsigned ind;
  113. for (ind = 0; ind < N; ind++)
  114. {
  115. struct starpu_task *task = starpu_task_create();
  116. task->cl = &cl_inc_a;
  117. task->buffers[0].handle = v_handle;
  118. task->buffers[0].mode = STARPU_RW;
  119. ret = starpu_task_submit(task);
  120. if (ret == -ENODEV)
  121. goto enodev;
  122. }
  123. /* synchronize v in RAM */
  124. starpu_data_sync_with_mem(v_handle, STARPU_RW);
  125. /* increment b */
  126. v[1]++;
  127. starpu_data_release_from_mem(v_handle);
  128. for (ind = 0; ind < N; ind++)
  129. {
  130. struct starpu_task *task = starpu_task_create();
  131. task->cl = &cl_inc_c;
  132. task->buffers[0].handle = v_handle;
  133. task->buffers[0].mode = STARPU_RW;
  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. }