execute_on_a_specific_worker.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <starpu.h>
  21. #include <stdlib.h>
  22. #include "../helper.h"
  23. #include <common/thread.h>
  24. #ifdef STARPU_QUICK_CHECK
  25. #define N 100
  26. #else
  27. #define N 1000
  28. #endif
  29. #define VECTORSIZE 1024
  30. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  31. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  32. static unsigned finished = 0;
  33. static unsigned cnt;
  34. starpu_data_handle_t v_handle;
  35. static unsigned *v;
  36. static void callback(void *arg)
  37. {
  38. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  39. if (res == 0)
  40. {
  41. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  42. finished = 1;
  43. STARPU_PTHREAD_COND_SIGNAL(&cond);
  44. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  45. }
  46. }
  47. static
  48. void codelet_null(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  49. {
  50. // int id = starpu_worker_get_id();
  51. // FPRINTF(stderr, "worker #%d\n", id);
  52. }
  53. static struct starpu_codelet cl_r =
  54. {
  55. .cpu_funcs = {codelet_null, NULL},
  56. .cuda_funcs = {codelet_null, NULL},
  57. .opencl_funcs = {codelet_null, NULL},
  58. .cpu_funcs_name = {"codelet_null", NULL},
  59. .nbuffers = 1,
  60. .modes = {STARPU_R}
  61. };
  62. static struct starpu_codelet cl_w =
  63. {
  64. .cpu_funcs = {codelet_null, NULL},
  65. .cuda_funcs = {codelet_null, NULL},
  66. .opencl_funcs = {codelet_null, NULL},
  67. .cpu_funcs_name = {"codelet_null", NULL},
  68. .nbuffers = 1,
  69. .modes = {STARPU_W}
  70. };
  71. static struct starpu_codelet cl_rw =
  72. {
  73. .cpu_funcs = {codelet_null, NULL},
  74. .cuda_funcs = {codelet_null, NULL},
  75. .opencl_funcs = {codelet_null, NULL},
  76. .cpu_funcs_name = {"codelet_null", NULL},
  77. .nbuffers = 1,
  78. .modes = {STARPU_RW}
  79. };
  80. static struct starpu_codelet *select_codelet_with_random_mode(void)
  81. {
  82. int r = rand();
  83. switch (r % 3)
  84. {
  85. case 0:
  86. return &cl_r;
  87. case 1:
  88. return &cl_w;
  89. case 2:
  90. return &cl_rw;
  91. };
  92. return &cl_rw;
  93. }
  94. int main(int argc, char **argv)
  95. {
  96. int ret;
  97. ret = starpu_initialize(NULL, &argc, &argv);
  98. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  100. ret = starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  101. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  102. starpu_vector_data_register(&v_handle, STARPU_MAIN_RAM, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  103. unsigned nworker = starpu_worker_get_count();
  104. cnt = nworker*N;
  105. unsigned iter, worker;
  106. for (iter = 0; iter < N; iter++)
  107. {
  108. for (worker = 0; worker < nworker; worker++)
  109. {
  110. /* execute a task on that worker */
  111. struct starpu_task *task = starpu_task_create();
  112. task->handles[0] = v_handle;
  113. task->cl = select_codelet_with_random_mode();
  114. task->callback_func = callback;
  115. task->callback_arg = NULL;
  116. task->execute_on_a_specific_worker = 1;
  117. task->workerid = worker;
  118. ret = starpu_task_submit(task);
  119. if (ret == -ENODEV) goto enodev;
  120. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  121. }
  122. }
  123. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  124. while (!finished)
  125. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  126. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  127. starpu_data_unregister(v_handle);
  128. starpu_free(v);
  129. starpu_shutdown();
  130. return EXIT_SUCCESS;
  131. enodev:
  132. starpu_data_unregister(v_handle);
  133. starpu_free(v);
  134. starpu_shutdown();
  135. fprintf(stderr, "WARNING: No one can execute this task\n");
  136. /* yes, we do not perform the computation but we did detect that no one
  137. * could perform the kernel, so this is not an error from StarPU */
  138. return STARPU_TEST_SKIPPED;
  139. }