sched_ctx_without_sched_policy_awake.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2014 Université de Bordeaux
  4. * Copyright (C) 2010-2014, 2016 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <omp.h>
  19. #ifdef STARPU_QUICK_CHECK
  20. #define NTASKS 64
  21. #else
  22. #define NTASKS 100
  23. #endif
  24. int tasks_executed[2][STARPU_NMAXWORKERS];
  25. int parallel_code(int sched_ctx)
  26. {
  27. int i;
  28. int t = 0;
  29. int workerid = starpu_worker_get_id();
  30. for(i = 0; i < NTASKS; i++)
  31. t++;
  32. tasks_executed[sched_ctx-1][workerid] = t;
  33. // printf("executed %d tasks on worker %d of sched_ctx %d \n", t, workerid, sched_ctx);
  34. return t;
  35. }
  36. static void sched_ctx_func(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  37. {
  38. unsigned sched_ctx = (uintptr_t)arg;
  39. parallel_code(sched_ctx);
  40. }
  41. static struct starpu_codelet sched_ctx_codelet =
  42. {
  43. .cpu_funcs = {sched_ctx_func},
  44. .model = NULL,
  45. .nbuffers = 0,
  46. .name = "sched_ctx"
  47. };
  48. int main(int argc, char **argv)
  49. {
  50. int i;
  51. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  52. {
  53. tasks_executed[0][i] = 0;
  54. tasks_executed[1][i] = 0;
  55. }
  56. int ntasks = NTASKS;
  57. int ret, j, k;
  58. unsigned ncpus = 0;
  59. ret = starpu_init(NULL);
  60. if (ret == -ENODEV)
  61. return 77;
  62. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  63. int nprocs1 = 1;
  64. int nprocs2 = 1;
  65. int *procs1, *procs2;
  66. #ifdef STARPU_USE_CPU
  67. ncpus = starpu_cpu_worker_get_count();
  68. procs1 = (int*)malloc(ncpus*sizeof(int));
  69. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs1, ncpus);
  70. if(ncpus > 1)
  71. {
  72. nprocs1 = ncpus/2;
  73. nprocs2 = ncpus-nprocs1;
  74. k = 0;
  75. procs2 = (int*)malloc(nprocs2*sizeof(int));
  76. for(j = nprocs1; j < nprocs1+nprocs2; j++)
  77. procs2[k++] = procs1[j];
  78. }
  79. else
  80. {
  81. procs2 = (int*)malloc(nprocs2*sizeof(int));
  82. procs2[0] = procs1[0];
  83. }
  84. #endif
  85. if (ncpus == 0) goto enodev;
  86. /*create contexts however you want*/
  87. unsigned sched_ctx1 = starpu_sched_ctx_create(procs1, nprocs1, "ctx1", STARPU_SCHED_CTX_AWAKE_WORKERS, 0);
  88. unsigned sched_ctx2 = starpu_sched_ctx_create(procs2, nprocs2, "ctx2", STARPU_SCHED_CTX_AWAKE_WORKERS, 0);
  89. for (i = 0; i < ntasks; i++)
  90. {
  91. struct starpu_task *task = starpu_task_create();
  92. task->cl = &sched_ctx_codelet;
  93. task->cl_arg = (void*)(uintptr_t) sched_ctx1;
  94. /*submit tasks to context*/
  95. ret = starpu_task_submit_to_ctx(task,sched_ctx1);
  96. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  97. }
  98. for (i = 0; i < ntasks; i++)
  99. {
  100. struct starpu_task *task = starpu_task_create();
  101. task->cl = &sched_ctx_codelet;
  102. task->cl_arg = (void*)(uintptr_t) sched_ctx2;
  103. /*submit tasks to context*/
  104. ret = starpu_task_submit_to_ctx(task,sched_ctx2);
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  106. }
  107. /* tell starpu when you finished submitting tasks to this context
  108. in order to allow moving resources from this context to the inheritor one
  109. when its corresponding tasks finished executing */
  110. /* wait for all tasks at the end*/
  111. starpu_task_wait_for_all();
  112. starpu_sched_ctx_delete(sched_ctx1);
  113. starpu_sched_ctx_delete(sched_ctx2);
  114. int tasks_per_ctx[2];
  115. tasks_per_ctx[0] = 0;
  116. tasks_per_ctx[1] = 0;
  117. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  118. {
  119. tasks_per_ctx[0] += tasks_executed[0][i];
  120. tasks_per_ctx[1] += tasks_executed[1][i];
  121. }
  122. printf("ctx%u: tasks starpu executed %d out of %d\n", sched_ctx1, tasks_per_ctx[0]/nprocs1, NTASKS);
  123. printf("ctx%u: tasks starpu executed %d out of %d\n", sched_ctx2, tasks_per_ctx[1]/nprocs2, NTASKS);
  124. enodev:
  125. #ifdef STARPU_USE_CPU
  126. free(procs1);
  127. free(procs2);
  128. #endif
  129. starpu_shutdown();
  130. return ncpus == 0 ? 77 : 0;
  131. }