testCommute.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015 INRIA
  4. *
  5. * StarPU 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. * StarPU 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. /*
  17. * for i from 0 to nbA
  18. * insert task handles[i] in STARPU_RW|STARPU_COMMUTE
  19. * for j from 0 to nbA
  20. * if i != j
  21. insert task handles[i] in STARPU_RW|STARPU_COMMUTE, and handles[j] in STARPU_RW|STARPU_COMMUTE
  22. */
  23. // @FUSE_STARPU
  24. #ifdef STARPU_QUICK_CHECK
  25. #define SLEEP_SLOW 6000
  26. #define SLEEP_FAST 1000
  27. #else
  28. #define SLEEP_SLOW 600000
  29. #define SLEEP_FAST 100000
  30. #endif
  31. #include <starpu.h>
  32. #include "../helper.h"
  33. #include <vector>
  34. #include <unistd.h>
  35. static unsigned nb, nb_slow;
  36. void callback(void * /*buffers*/[], void * /*cl_arg*/)
  37. {
  38. unsigned val;
  39. val = STARPU_ATOMIC_ADD(&nb, 1);
  40. FPRINTF(stdout,"callback in (%d)\n", val); fflush(stdout);
  41. usleep(SLEEP_FAST);
  42. val = STARPU_ATOMIC_ADD(&nb, -1);
  43. FPRINTF(stdout,"callback out (%d)\n", val); fflush(stdout);
  44. }
  45. void callback_slow(void * /*buffers*/[], void * /*cl_arg*/)
  46. {
  47. unsigned val;
  48. val = STARPU_ATOMIC_ADD(&nb_slow, 1);
  49. FPRINTF(stdout,"callback_slow in (%d)\n", val); fflush(stdout);
  50. usleep(SLEEP_SLOW);
  51. val = STARPU_ATOMIC_ADD(&nb_slow, -1);
  52. FPRINTF(stdout,"callback_slow out (%d)\n", val); fflush(stdout);
  53. }
  54. int main(int /*argc*/, char** /*argv*/)
  55. {
  56. int ret;
  57. struct starpu_conf conf;
  58. ret = starpu_conf_init(&conf);
  59. STARPU_ASSERT(ret == 0);
  60. //conf.ncpus = 1;//// 4
  61. ret = starpu_init(&conf);
  62. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  63. STARPU_ASSERT(ret == 0);
  64. FPRINTF(stdout, "Max Thread %d\n", starpu_worker_get_count());
  65. //////////////////////////////////////////////////////
  66. starpu_codelet normalCodelete;
  67. {
  68. memset(&normalCodelete, 0, sizeof(normalCodelete));
  69. normalCodelete.where = STARPU_CPU;
  70. normalCodelete.cpu_funcs[0] = callback;
  71. normalCodelete.nbuffers = 2;
  72. normalCodelete.modes[0] = starpu_data_access_mode(STARPU_RW|STARPU_COMMUTE);
  73. normalCodelete.modes[1] = starpu_data_access_mode(STARPU_RW|STARPU_COMMUTE);
  74. normalCodelete.name = "normalCodelete";
  75. }
  76. starpu_codelet slowCodelete;
  77. {
  78. memset(&slowCodelete, 0, sizeof(slowCodelete));
  79. slowCodelete.where = STARPU_CPU;
  80. slowCodelete.cpu_funcs[0] = callback_slow;
  81. slowCodelete.nbuffers = 1;
  82. slowCodelete.modes[0] = starpu_data_access_mode (STARPU_RW|STARPU_COMMUTE);
  83. slowCodelete.name = "slowCodelete";
  84. }
  85. //////////////////////////////////////////////////////
  86. //////////////////////////////////////////////////////
  87. ///const int nbA = 3;
  88. const int nbA = 10;
  89. FPRINTF(stdout, "Nb A = %d\n", nbA);
  90. std::vector<starpu_data_handle_t> handleA(nbA);
  91. std::vector<int> dataA(nbA);
  92. for(int idx = 0 ; idx < nbA ; ++idx)
  93. {
  94. dataA[idx] = idx;
  95. }
  96. for(int idxHandle = 0 ; idxHandle < nbA ; ++idxHandle)
  97. {
  98. starpu_variable_data_register(&handleA[idxHandle], 0, (uintptr_t)&dataA[idxHandle], sizeof(dataA[idxHandle]));
  99. }
  100. //////////////////////////////////////////////////////
  101. //////////////////////////////////////////////////////
  102. FPRINTF(stdout,"Submit tasks\n");
  103. for(int idxHandleA1 = 0 ; idxHandleA1 < nbA ; ++idxHandleA1)
  104. {
  105. ret = starpu_task_insert(&slowCodelete,
  106. (STARPU_RW|STARPU_COMMUTE), handleA[idxHandleA1],
  107. 0);
  108. if (ret == -ENODEV) goto out;
  109. for(int idxHandleA2 = 0 ; idxHandleA2 < nbA ; ++idxHandleA2)
  110. {
  111. if(idxHandleA1 != idxHandleA2)
  112. {
  113. ret = starpu_task_insert(&normalCodelete,
  114. (STARPU_RW|STARPU_COMMUTE), handleA[idxHandleA1],
  115. (STARPU_RW|STARPU_COMMUTE), handleA[idxHandleA2],
  116. 0);
  117. if (ret == -ENODEV) goto out;
  118. }
  119. }
  120. }
  121. //////////////////////////////////////////////////////
  122. FPRINTF(stdout,"Wait task\n");
  123. out:
  124. starpu_task_wait_for_all();
  125. //////////////////////////////////////////////////////
  126. FPRINTF(stdout,"Release data\n");
  127. for(int idxHandle = 0 ; idxHandle < nbA ; ++idxHandle)
  128. {
  129. starpu_data_unregister(handleA[idxHandle]);
  130. }
  131. //////////////////////////////////////////////////////
  132. FPRINTF(stdout,"Shutdown\n");
  133. starpu_shutdown();
  134. return 0;
  135. }