sched_ctx_without_sched_policy_awake.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014 Inria
  4. * Copyright (C) 2010-2017 CNRS
  5. * Copyright (C) 2010-2014 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 <omp.h>
  20. #ifdef STARPU_QUICK_CHECK
  21. #define NTASKS 64
  22. #else
  23. #define NTASKS 100
  24. #endif
  25. int tasks_executed[2][STARPU_NMAXWORKERS];
  26. int parallel_code(int sched_ctx)
  27. {
  28. int i;
  29. int t = 0;
  30. int workerid = starpu_worker_get_id();
  31. for(i = 0; i < NTASKS; i++)
  32. t++;
  33. tasks_executed[sched_ctx-1][workerid] = t;
  34. // printf("executed %d tasks on worker %d of sched_ctx %d \n", t, workerid, sched_ctx);
  35. return t;
  36. }
  37. static void sched_ctx_func(void *descr[], void *arg)
  38. {
  39. (void)descr;
  40. unsigned sched_ctx = (uintptr_t)arg;
  41. parallel_code(sched_ctx);
  42. }
  43. static struct starpu_codelet sched_ctx_codelet =
  44. {
  45. .cpu_funcs = {sched_ctx_func},
  46. .model = NULL,
  47. .nbuffers = 0,
  48. .name = "sched_ctx"
  49. };
  50. int main(void)
  51. {
  52. int i;
  53. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  54. {
  55. tasks_executed[0][i] = 0;
  56. tasks_executed[1][i] = 0;
  57. }
  58. int ntasks = NTASKS;
  59. int ret, j, k;
  60. unsigned ncpus = 0;
  61. ret = starpu_init(NULL);
  62. if (ret == -ENODEV)
  63. return 77;
  64. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  65. int nprocs1 = 1;
  66. int nprocs2 = 1;
  67. int *procs1, *procs2;
  68. #ifdef STARPU_USE_CPU
  69. ncpus = starpu_cpu_worker_get_count();
  70. procs1 = (int*)malloc(ncpus*sizeof(int));
  71. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs1, ncpus);
  72. if(ncpus > 1)
  73. {
  74. nprocs1 = ncpus/2;
  75. nprocs2 = ncpus-nprocs1;
  76. k = 0;
  77. procs2 = (int*)malloc(nprocs2*sizeof(int));
  78. for(j = nprocs1; j < nprocs1+nprocs2; j++)
  79. procs2[k++] = procs1[j];
  80. }
  81. else
  82. {
  83. procs2 = (int*)malloc(nprocs2*sizeof(int));
  84. procs2[0] = procs1[0];
  85. }
  86. #endif
  87. if (ncpus == 0) goto enodev;
  88. /*create contexts however you want*/
  89. unsigned sched_ctx1 = starpu_sched_ctx_create(procs1, nprocs1, "ctx1", STARPU_SCHED_CTX_AWAKE_WORKERS, 0);
  90. unsigned sched_ctx2 = starpu_sched_ctx_create(procs2, nprocs2, "ctx2", STARPU_SCHED_CTX_AWAKE_WORKERS, 0);
  91. for (i = 0; i < ntasks; i++)
  92. {
  93. struct starpu_task *task = starpu_task_create();
  94. task->cl = &sched_ctx_codelet;
  95. task->cl_arg = (void*)(uintptr_t) sched_ctx1;
  96. /*submit tasks to context*/
  97. ret = starpu_task_submit_to_ctx(task,sched_ctx1);
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  99. }
  100. for (i = 0; i < ntasks; i++)
  101. {
  102. struct starpu_task *task = starpu_task_create();
  103. task->cl = &sched_ctx_codelet;
  104. task->cl_arg = (void*)(uintptr_t) sched_ctx2;
  105. /*submit tasks to context*/
  106. ret = starpu_task_submit_to_ctx(task,sched_ctx2);
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  108. }
  109. /* tell starpu when you finished submitting tasks to this context
  110. in order to allow moving resources from this context to the inheritor one
  111. when its corresponding tasks finished executing */
  112. /* wait for all tasks at the end*/
  113. starpu_task_wait_for_all();
  114. starpu_sched_ctx_delete(sched_ctx1);
  115. starpu_sched_ctx_delete(sched_ctx2);
  116. int tasks_per_ctx[2];
  117. tasks_per_ctx[0] = 0;
  118. tasks_per_ctx[1] = 0;
  119. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  120. {
  121. tasks_per_ctx[0] += tasks_executed[0][i];
  122. tasks_per_ctx[1] += tasks_executed[1][i];
  123. }
  124. printf("ctx%u: tasks starpu executed %d out of %d\n", sched_ctx1, tasks_per_ctx[0]/nprocs1, NTASKS);
  125. printf("ctx%u: tasks starpu executed %d out of %d\n", sched_ctx2, tasks_per_ctx[1]/nprocs2, NTASKS);
  126. enodev:
  127. #ifdef STARPU_USE_CPU
  128. free(procs1);
  129. free(procs2);
  130. #endif
  131. starpu_shutdown();
  132. return ncpus == 0 ? 77 : 0;
  133. }