sched_ctx_hierarchy.c 5.1 KB

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