sched_ctx_hierarchy.c 5.1 KB

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