sched_ctx_hierarchy.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2017, 2019 CNRS
  4. * Copyright (C) 2017 Inria
  5. * Copyright (C) 2017 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. void free_codelet(void *arg)
  21. {
  22. // The argument of the function is automatically freed by StarPU
  23. // free(arg);
  24. }
  25. void func_cpu_bis(void *descr[], void *_args)
  26. {
  27. (void)descr;
  28. char msg;
  29. char worker_name[256];
  30. int worker_id = starpu_worker_get_id_check();
  31. int worker_id_expected;
  32. int ntasks;
  33. starpu_worker_get_name(worker_id, worker_name, 256);
  34. starpu_codelet_unpack_args(_args, &msg, &ntasks, &worker_id_expected);
  35. STARPU_ASSERT(worker_id == worker_id_expected);
  36. FPRINTF(stderr, "[msg '%c'] [worker id %d] [worker name %s] [tasks %d]\n", msg, worker_id, worker_name, ntasks);
  37. if (ntasks > 0)
  38. {
  39. struct starpu_codelet *codelet = calloc(1,sizeof(*codelet));
  40. codelet->cpu_funcs[0] = func_cpu_bis;
  41. codelet->cpu_funcs_name[0] = "func_cpu_bis";
  42. int nntasks = ntasks - 1;
  43. starpu_task_insert(codelet,
  44. STARPU_VALUE, &msg, sizeof(msg),
  45. STARPU_VALUE, &nntasks, sizeof(ntasks),
  46. STARPU_VALUE, &worker_id, sizeof(worker_id),
  47. STARPU_CALLBACK_WITH_ARG, free_codelet, codelet,
  48. 0);
  49. }
  50. }
  51. void func_cpu(void *descr[], void *_args)
  52. {
  53. (void)descr;
  54. char msg;
  55. char worker_name[256];
  56. int worker_id = starpu_worker_get_id_check();
  57. int worker_id_expected;
  58. int ntasks;
  59. unsigned sched_ctx_id;
  60. unsigned *sched_ctx_id_p;
  61. starpu_worker_get_name(worker_id, worker_name, 256);
  62. starpu_codelet_unpack_args(_args, &msg, &ntasks, &sched_ctx_id, &worker_id_expected, &sched_ctx_id_p);
  63. STARPU_ASSERT(worker_id == worker_id_expected);
  64. *sched_ctx_id_p = sched_ctx_id;
  65. starpu_sched_ctx_set_context(sched_ctx_id_p);
  66. FPRINTF(stderr, "[msg '%c'] [worker id %d] [worker name %s] [sched_ctx_id %u] [tasks %d] [buffer %p]\n", msg, worker_id, worker_name, sched_ctx_id, ntasks, sched_ctx_id_p);
  67. if (ntasks > 0)
  68. {
  69. struct starpu_codelet *codelet = calloc(1,sizeof(*codelet));
  70. codelet->cpu_funcs[0] = func_cpu_bis;
  71. codelet->cpu_funcs_name[0] = "func_cpu_bis";
  72. int nntasks = ntasks - 1;
  73. starpu_task_insert(codelet,
  74. STARPU_VALUE, &msg, sizeof(msg),
  75. STARPU_VALUE, &nntasks, sizeof(nntasks),
  76. STARPU_VALUE, &worker_id, sizeof(worker_id),
  77. STARPU_CALLBACK_WITH_ARG, free_codelet, codelet,
  78. 0);
  79. }
  80. }
  81. struct starpu_codelet mycodelet =
  82. {
  83. .cpu_funcs = {func_cpu},
  84. .cpu_funcs_name = {"func_cpu"},
  85. };
  86. int main(void)
  87. {
  88. int i, ret;
  89. int nprocs, nprocs_per_context=1;
  90. int procs[STARPU_NMAXWORKERS];
  91. int ntasks=10;
  92. char msg[2] = "ab";
  93. unsigned *buffer[2];
  94. ret = starpu_init(NULL);
  95. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  96. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  97. nprocs = starpu_cpu_worker_get_count();
  98. if (nprocs < 2) goto enodev;
  99. nprocs_per_context = 1;
  100. FPRINTF(stderr, "# Workers = %d -> %d worker for each sched context\n", nprocs, nprocs_per_context);
  101. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs, nprocs);
  102. unsigned sched_ctx_0 = starpu_sched_ctx_create(procs, nprocs_per_context, "ctx_0", 0);
  103. unsigned sched_ctx_1 = starpu_sched_ctx_create(&procs[nprocs_per_context], nprocs_per_context, "ctx_1", 0);
  104. if (!getenv("STARPU_SSILENT"))
  105. {
  106. char name0[256];
  107. char name1[256];
  108. starpu_worker_get_name(procs[0], name0, 256);
  109. starpu_worker_get_name(procs[1], name1, 256);
  110. FPRINTF(stderr, "Creating first sched_ctx with %d worker [id %d name %s]\n", nprocs_per_context, procs[0], name0);
  111. FPRINTF(stderr, "Creating second sched_ctx with %d worker [id %d name %s]\n", nprocs_per_context, procs[1], name1);
  112. starpu_sched_ctx_display_workers(sched_ctx_0, stderr);
  113. starpu_sched_ctx_display_workers(sched_ctx_1, stderr);
  114. }
  115. buffer[0] = malloc(sizeof(unsigned));
  116. buffer[1] = malloc(sizeof(unsigned));
  117. FPRINTF(stderr, "allocating %p and %p\n", buffer[0], buffer[1]);
  118. ret = starpu_task_insert(&mycodelet, STARPU_SCHED_CTX, sched_ctx_0,
  119. STARPU_VALUE, &msg[0], sizeof(msg[0]),
  120. STARPU_VALUE, &ntasks, sizeof(ntasks),
  121. STARPU_VALUE, &sched_ctx_0, sizeof(sched_ctx_0),
  122. STARPU_VALUE, &procs[0], sizeof(procs[0]),
  123. STARPU_VALUE, &buffer[0], sizeof(buffer[0]),
  124. 0);
  125. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  126. ret = starpu_task_insert(&mycodelet, STARPU_SCHED_CTX, sched_ctx_1,
  127. STARPU_VALUE, &msg[1], sizeof(msg[1]),
  128. STARPU_VALUE, &ntasks, sizeof(ntasks),
  129. STARPU_VALUE, &sched_ctx_1, sizeof(sched_ctx_1),
  130. STARPU_VALUE, &procs[1], sizeof(procs[1]),
  131. STARPU_VALUE, &buffer[1], sizeof(buffer[1]),
  132. 0);
  133. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  134. starpu_task_wait_for_all();
  135. starpu_sched_ctx_delete(sched_ctx_0);
  136. starpu_sched_ctx_delete(sched_ctx_1);
  137. starpu_shutdown();
  138. free(buffer[0]);
  139. free(buffer[1]);
  140. return 0;
  141. enodev:
  142. starpu_shutdown();
  143. fprintf(stderr, "WARNING: No one can execute this task\n");
  144. /* yes, we do not perform the computation but we did detect that no one
  145. * could perform the kernel, so this is not an error from StarPU */
  146. return STARPU_TEST_SKIPPED;
  147. }