two_cpu_contexts.c 3.4 KB

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