two_cpu_contexts.c 3.6 KB

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