two_cpu_contexts.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2016-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 <stdlib.h>
  18. /* This example case follows the same pattern its native Fortran version nf_sched_ctx.f90 */
  19. static void sched_ctx_cpu_func(void *descr[], void *cl_args)
  20. {
  21. (void)descr;
  22. int task_id;
  23. starpu_codelet_unpack_args(cl_args, &task_id);
  24. printf("task: %d, workerid: %d\n", task_id, starpu_worker_get_id());
  25. }
  26. static struct starpu_codelet sched_ctx_codelet =
  27. {
  28. .cpu_funcs = {sched_ctx_cpu_func},
  29. .model = NULL,
  30. .nbuffers = 0,
  31. .name = "sched_ctx"
  32. };
  33. int main(void)
  34. {
  35. int ncpu;
  36. int nprocs1;
  37. int nprocs2;
  38. int *procs = NULL;
  39. int *procs1 = NULL;
  40. int *procs2 = NULL;
  41. int i;
  42. int n = 20;
  43. int ret = starpu_init(NULL);
  44. if (ret == -ENODEV)
  45. return 77;
  46. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  47. ncpu = starpu_cpu_worker_get_count();
  48. /* actually we really need at least 2 CPU workers such to allocate 2
  49. * non overlapping contexts */
  50. if (ncpu < 2)
  51. return 77;
  52. procs = calloc(ncpu, sizeof(int));
  53. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs, ncpu);
  54. nprocs1 = ncpu / 2;
  55. procs1 = calloc(nprocs1, sizeof(int));
  56. for (i=0; i<nprocs1; i++)
  57. {
  58. procs1[i] = procs[i];
  59. }
  60. nprocs2 = ncpu - nprocs1;
  61. procs2 = calloc(nprocs2, sizeof(int));
  62. for (i=0; i<nprocs2; i++)
  63. {
  64. procs2[i] = procs[i+nprocs1];
  65. }
  66. /* create sched context 1 with default policy, by giving a empty policy name */
  67. unsigned sched_ctx1 = starpu_sched_ctx_create(procs1, nprocs1, "ctx1", STARPU_SCHED_CTX_POLICY_NAME, "", 0);
  68. /* create sched context 2 with a user selected policy name */
  69. unsigned sched_ctx2 = starpu_sched_ctx_create(procs2, nprocs2, "ctx2", STARPU_SCHED_CTX_POLICY_NAME, "eager", 0);
  70. starpu_sched_ctx_set_inheritor(sched_ctx2, sched_ctx1);
  71. starpu_sched_ctx_display_workers(sched_ctx1, stderr);
  72. starpu_sched_ctx_display_workers(sched_ctx2, stderr);
  73. for (i=0; i < n; i++)
  74. {
  75. int arg_id = 1*1000 + i;
  76. ret = starpu_task_insert(&sched_ctx_codelet, STARPU_VALUE, &arg_id, sizeof(int), STARPU_SCHED_CTX, sched_ctx1, 0);
  77. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  78. }
  79. for (i=0; i < n; i++)
  80. {
  81. int arg_id = 2*1000 + i;
  82. ret = starpu_task_insert(&sched_ctx_codelet, STARPU_VALUE, &arg_id, sizeof(int), STARPU_SCHED_CTX, sched_ctx2, 0);
  83. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  84. }
  85. starpu_sched_ctx_finished_submit(sched_ctx2);
  86. for (i=0; i < n; i++)
  87. {
  88. int arg_id = 1*10000 + i;
  89. ret = starpu_task_insert(&sched_ctx_codelet, STARPU_VALUE, &arg_id, sizeof(int), STARPU_SCHED_CTX, sched_ctx1, 0);
  90. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  91. }
  92. starpu_sched_ctx_finished_submit(sched_ctx1);
  93. starpu_task_wait_for_all();
  94. starpu_sched_ctx_add_workers(procs1, nprocs1, sched_ctx2);
  95. starpu_sched_ctx_delete(sched_ctx2);
  96. starpu_sched_ctx_delete(sched_ctx1);
  97. starpu_shutdown();
  98. free(procs);
  99. free(procs1);
  100. free(procs2);
  101. return 0;
  102. }