sync_and_notify_data.c 3.4 KB

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