sched_ctx_hierarchy.c 5.5 KB

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