execute_on_a_specific_worker.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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. #define N 1000
  25. #define VECTORSIZE 1024
  26. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  27. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  28. static unsigned finished = 0;
  29. static unsigned cnt;
  30. starpu_data_handle_t v_handle;
  31. static unsigned *v;
  32. static void callback(void *arg)
  33. {
  34. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  35. if (res == 0)
  36. {
  37. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  38. finished = 1;
  39. _STARPU_PTHREAD_COND_SIGNAL(&cond);
  40. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  41. }
  42. }
  43. static void codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  44. {
  45. // int id = starpu_worker_get_id();
  46. // FPRINTF(stderr, "worker #%d\n", id);
  47. }
  48. static struct starpu_codelet cl_r =
  49. {
  50. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  51. .cpu_funcs = {codelet_null, NULL},
  52. .cuda_funcs = {codelet_null, NULL},
  53. .opencl_funcs = {codelet_null, NULL},
  54. .nbuffers = 1,
  55. .modes = {STARPU_R}
  56. };
  57. static struct starpu_codelet cl_w =
  58. {
  59. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  60. .cpu_funcs = {codelet_null, NULL},
  61. .cuda_funcs = {codelet_null, NULL},
  62. .opencl_funcs = {codelet_null, NULL},
  63. .nbuffers = 1,
  64. .modes = {STARPU_W}
  65. };
  66. static struct starpu_codelet cl_rw =
  67. {
  68. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  69. .cpu_funcs = {codelet_null, NULL},
  70. .cuda_funcs = {codelet_null, NULL},
  71. .opencl_funcs = {codelet_null, NULL},
  72. .nbuffers = 1,
  73. .modes = {STARPU_RW}
  74. };
  75. static struct starpu_codelet *select_codelet_with_random_mode(void)
  76. {
  77. int r = rand();
  78. switch (r % 3)
  79. {
  80. case 0:
  81. return &cl_r;
  82. case 1:
  83. return &cl_w;
  84. case 2:
  85. return &cl_rw;
  86. };
  87. return &cl_rw;
  88. }
  89. int main(int argc, char **argv)
  90. {
  91. int ret;
  92. ret = starpu_init(NULL);
  93. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  95. ret = starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  96. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  97. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  98. unsigned nworker = starpu_worker_get_count();
  99. cnt = nworker*N;
  100. unsigned iter, worker;
  101. for (iter = 0; iter < N; iter++)
  102. {
  103. for (worker = 0; worker < nworker; worker++)
  104. {
  105. /* execute a task on that worker */
  106. struct starpu_task *task = starpu_task_create();
  107. task->handles[0] = v_handle;
  108. task->cl = select_codelet_with_random_mode();
  109. task->callback_func = callback;
  110. task->callback_arg = NULL;
  111. task->execute_on_a_specific_worker = 1;
  112. task->workerid = worker;
  113. int ret = starpu_task_submit(task);
  114. if (ret == -ENODEV) goto enodev;
  115. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  116. }
  117. }
  118. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  119. while (!finished)
  120. _STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  121. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  122. starpu_data_unregister(v_handle);
  123. starpu_free(v);
  124. starpu_shutdown();
  125. return EXIT_SUCCESS;
  126. enodev:
  127. starpu_data_unregister(v_handle);
  128. starpu_free(v);
  129. starpu_shutdown();
  130. fprintf(stderr, "WARNING: No one can execute this task\n");
  131. /* yes, we do not perform the computation but we did detect that no one
  132. * could perform the kernel, so this is not an error from StarPU */
  133. return STARPU_TEST_SKIPPED;
  134. }