execute_on_a_specific_worker.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <pthread.h>
  23. #include "../helper.h"
  24. #include <common/thread.h>
  25. #ifdef STARPU_QUICK_CHECK
  26. #define N 100
  27. #else
  28. #define N 1000
  29. #endif
  30. #define VECTORSIZE 1024
  31. static _starpu_pthread_mutex_t mutex = _STARPU_PTHREAD_MUTEX_INITIALIZER;
  32. static _starpu_pthread_cond_t cond = _STARPU_PTHREAD_COND_INITIALIZER;
  33. static unsigned finished = 0;
  34. static unsigned cnt;
  35. starpu_data_handle_t v_handle;
  36. static unsigned *v;
  37. static void callback(void *arg)
  38. {
  39. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  40. if (res == 0)
  41. {
  42. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  43. finished = 1;
  44. _STARPU_PTHREAD_COND_SIGNAL(&cond);
  45. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  46. }
  47. }
  48. static void codelet_null(void *descr[], __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. .nbuffers = 1,
  59. .modes = {STARPU_R}
  60. };
  61. static struct starpu_codelet cl_w =
  62. {
  63. .cpu_funcs = {codelet_null, NULL},
  64. .cuda_funcs = {codelet_null, NULL},
  65. .opencl_funcs = {codelet_null, NULL},
  66. .nbuffers = 1,
  67. .modes = {STARPU_W}
  68. };
  69. static struct starpu_codelet cl_rw =
  70. {
  71. .cpu_funcs = {codelet_null, NULL},
  72. .cuda_funcs = {codelet_null, NULL},
  73. .opencl_funcs = {codelet_null, NULL},
  74. .nbuffers = 1,
  75. .modes = {STARPU_RW}
  76. };
  77. static struct starpu_codelet *select_codelet_with_random_mode(void)
  78. {
  79. int r = rand();
  80. switch (r % 3)
  81. {
  82. case 0:
  83. return &cl_r;
  84. case 1:
  85. return &cl_w;
  86. case 2:
  87. return &cl_rw;
  88. };
  89. return &cl_rw;
  90. }
  91. int main(int argc, char **argv)
  92. {
  93. int ret;
  94. ret = starpu_init(NULL);
  95. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  96. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  97. ret = starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  99. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  100. unsigned nworker = starpu_worker_get_count();
  101. cnt = nworker*N;
  102. unsigned iter, worker;
  103. for (iter = 0; iter < N; iter++)
  104. {
  105. for (worker = 0; worker < nworker; worker++)
  106. {
  107. /* execute a task on that worker */
  108. struct starpu_task *task = starpu_task_create();
  109. task->handles[0] = v_handle;
  110. task->cl = select_codelet_with_random_mode();
  111. task->callback_func = callback;
  112. task->callback_arg = NULL;
  113. task->execute_on_a_specific_worker = 1;
  114. task->workerid = worker;
  115. ret = starpu_task_submit(task);
  116. if (ret == -ENODEV) goto enodev;
  117. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  118. }
  119. }
  120. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  121. while (!finished)
  122. _STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  123. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  124. starpu_data_unregister(v_handle);
  125. starpu_free(v);
  126. starpu_shutdown();
  127. return EXIT_SUCCESS;
  128. enodev:
  129. starpu_data_unregister(v_handle);
  130. starpu_free(v);
  131. starpu_shutdown();
  132. fprintf(stderr, "WARNING: No one can execute this task\n");
  133. /* yes, we do not perform the computation but we did detect that no one
  134. * could perform the kernel, so this is not an error from StarPU */
  135. return STARPU_TEST_SKIPPED;
  136. }