sync_and_notify_data.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <starpu.h>
  19. #ifdef USE_GORDON
  20. #include <cell/gordon/gordon.h>
  21. #endif
  22. #define N 100
  23. #define K 256
  24. /*
  25. * In this test, we maintain a vector v = (a,b,c).
  26. *
  27. * Each iteration consists of:
  28. * - increment a n times
  29. * - sync v in ram
  30. * - incrementer b
  31. * - notify the modification of v
  32. * - incrementer c n times
  33. * - sync v
  34. *
  35. * At the end, we have to make sure that if we did k iterations,
  36. * v == (kn, k, kn)
  37. */
  38. #ifdef USE_CUDA
  39. void cuda_codelet_incA(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args);
  40. void cuda_codelet_incC(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args);
  41. #endif
  42. #define VECTORSIZE 16
  43. starpu_data_handle v_handle;
  44. static unsigned v[VECTORSIZE] __attribute__((aligned(128))) = {0, 0, 0, 0};
  45. void core_codelet_incA(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args)
  46. {
  47. unsigned *val = (unsigned *)buffers[0].vector.ptr;
  48. val[0]++;
  49. }
  50. void core_codelet_incC(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args)
  51. {
  52. unsigned *val = (unsigned *)buffers[0].vector.ptr;
  53. val[2]++;
  54. }
  55. int main(int argc, char **argv)
  56. {
  57. starpu_init(NULL);
  58. #ifdef USE_GORDON
  59. unsigned elf_id = gordon_register_elf_plugin("./microbenchs/sync_and_notify_data_gordon_kernels.spuelf");
  60. gordon_load_plugin_on_all_spu(elf_id);
  61. unsigned kernel_incA_id = gordon_register_kernel(elf_id, "incA");
  62. gordon_load_kernel_on_all_spu(kernel_incA_id);
  63. unsigned kernel_incC_id = gordon_register_kernel(elf_id, "incC");
  64. gordon_load_kernel_on_all_spu(kernel_incC_id);
  65. fprintf(stderr, "kernel incA %d incC %d elf %d\n", kernel_incA_id, kernel_incC_id, elf_id);
  66. #endif
  67. starpu_monitor_vector_data(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  68. unsigned iter;
  69. for (iter = 0; iter < K; iter++)
  70. {
  71. int ret;
  72. unsigned ind;
  73. for (ind = 0; ind < N; ind++)
  74. {
  75. /* increment a = v[0] */
  76. starpu_codelet cl_inc_a = {
  77. .where = CORE|CUBLAS|GORDON,
  78. .core_func = core_codelet_incA,
  79. #ifdef USE_CUDA
  80. .cublas_func = cuda_codelet_incA,
  81. #endif
  82. #ifdef USE_GORDON
  83. .gordon_func = kernel_incA_id,
  84. #endif
  85. .nbuffers = 1
  86. };
  87. struct starpu_task *task = starpu_task_create();
  88. task->cl = &cl_inc_a;
  89. task->buffers[0].state = v_handle;
  90. task->buffers[0].mode = RW;
  91. task->synchronous = 1;
  92. ret = starpu_submit_task(task);
  93. if (ret == -ENODEV)
  94. goto enodev;
  95. }
  96. /* synchronize v in RAM */
  97. starpu_sync_data_with_mem(v_handle);
  98. /* increment b */
  99. v[1]++;
  100. /* inform StarPU that v was modified by the apps */
  101. starpu_notify_data_modification(v_handle, 0);
  102. for (ind = 0; ind < N; ind++)
  103. {
  104. /* increment c = v[2] */
  105. starpu_codelet cl_inc_c = {
  106. .where = CORE|CUBLAS|GORDON,
  107. .core_func = core_codelet_incC,
  108. #ifdef USE_CUDA
  109. .cublas_func = cuda_codelet_incC,
  110. #endif
  111. #ifdef USE_GORDON
  112. .gordon_func = kernel_incC_id,
  113. #endif
  114. .nbuffers = 1
  115. };
  116. struct starpu_task *task = starpu_task_create();
  117. task->cl = &cl_inc_c;
  118. task->buffers[0].state = v_handle;
  119. task->buffers[0].mode = RW;
  120. task->synchronous = 1;
  121. ret = starpu_submit_task(task);
  122. if (ret == -ENODEV)
  123. goto enodev;
  124. }
  125. }
  126. starpu_sync_data_with_mem(v_handle);
  127. fprintf(stderr, "V = { %d, %d, %d }\n", v[0], v[1], v[2]);
  128. starpu_shutdown();
  129. if ((v[0] != N*K) || (v[1] != K) || (v[2] != N*K))
  130. return -1;
  131. return 0;
  132. enodev:
  133. fprintf(stderr, "WARNING: No one can execute this task\n");
  134. /* yes, we do not perform the computation but we did detect that no one
  135. * could perform the kernel, so this is not an error from StarPU */
  136. return 0;
  137. }