sched_ctx_without_sched_policy.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #ifndef STARPU_QUICK_CHECK
  20. #define NTASKS 64
  21. #else
  22. #define NTASKS 10
  23. #endif
  24. int tasks_executed[2];
  25. int parallel_code(int sched_ctx)
  26. {
  27. int i;
  28. int t = 0;
  29. int *cpuids = NULL;
  30. int ncpuids = 0;
  31. starpu_sched_ctx_get_available_cpuids(sched_ctx, &cpuids, &ncpuids);
  32. // printf("execute task of %d threads \n", ncpuids);
  33. #pragma omp parallel num_threads(ncpuids) reduction(+:t)
  34. {
  35. starpu_sched_ctx_bind_current_thread_to_cpuid(cpuids[omp_get_thread_num()]);
  36. // printf("cpu = %d ctx%d nth = %d\n", sched_getcpu(), sched_ctx, omp_get_num_threads());
  37. #pragma omp for
  38. for(i = 0; i < NTASKS; i++)
  39. t++;
  40. }
  41. free(cpuids);
  42. return t;
  43. }
  44. static void sched_ctx_func(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  45. {
  46. unsigned sched_ctx = (uintptr_t)arg;
  47. tasks_executed[sched_ctx-1] += parallel_code(sched_ctx);
  48. }
  49. static struct starpu_codelet sched_ctx_codelet =
  50. {
  51. .cpu_funcs = {sched_ctx_func},
  52. .model = NULL,
  53. .nbuffers = 0,
  54. .name = "sched_ctx"
  55. };
  56. int main(int argc, char **argv)
  57. {
  58. tasks_executed[0] = 0;
  59. tasks_executed[1] = 0;
  60. int ntasks = NTASKS;
  61. int ret, j, k;
  62. unsigned ncpus = 0;
  63. ret = starpu_init(NULL);
  64. if (ret == -ENODEV)
  65. return 77;
  66. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  67. int nprocs1 = 1;
  68. int nprocs2 = 1;
  69. int ncuda = 0;
  70. int *procs1, *procs2, *procscuda;
  71. #ifdef STARPU_USE_CUDA
  72. ncuda = starpu_cuda_worker_get_count();
  73. procscuda = (int*)malloc(ncuda*sizeof(int));
  74. starpu_worker_get_ids_by_type(STARPU_CUDA_WORKER, procscuda, ncuda);
  75. #endif
  76. #ifdef STARPU_USE_CPU
  77. ncpus = starpu_cpu_worker_get_count();
  78. procs1 = (int*)malloc(ncpus*sizeof(int));
  79. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs1, ncpus);
  80. if(ncpus > 1)
  81. {
  82. nprocs1 = ncpus/2;
  83. nprocs2 = ncpus-nprocs1;
  84. k = 0;
  85. procs2 = (int*)malloc(nprocs2*sizeof(int));
  86. for(j = nprocs1; j < nprocs1+nprocs2; j++)
  87. procs2[k++] = procs1[j];
  88. }
  89. else
  90. {
  91. procs2 = (int*)malloc(nprocs2*sizeof(int));
  92. procs2[0] = procs1[0];
  93. }
  94. #endif
  95. if (ncpus == 0) goto enodev;
  96. #ifdef STARPU_USE_CUDA
  97. if (ncuda > 0 && nprocs1 > 1)
  98. {
  99. procs1[nprocs1-1] = procscuda[0];
  100. }
  101. #endif
  102. /*create contexts however you want*/
  103. unsigned sched_ctx1 = starpu_sched_ctx_create(procs1, nprocs1, "ctx1", 0);
  104. unsigned sched_ctx2 = starpu_sched_ctx_create(procs2, nprocs2, "ctx2", 0);
  105. starpu_sched_ctx_display_workers(sched_ctx1, stderr);
  106. starpu_sched_ctx_display_workers(sched_ctx2, stderr);
  107. int i;
  108. for (i = 0; i < ntasks; i++)
  109. {
  110. struct starpu_task *task = starpu_task_create();
  111. task->cl = &sched_ctx_codelet;
  112. task->cl_arg = (void*)(uintptr_t) sched_ctx1;
  113. /*submit tasks to context*/
  114. ret = starpu_task_submit_to_ctx(task,sched_ctx1);
  115. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  116. }
  117. for (i = 0; i < ntasks; i++)
  118. {
  119. struct starpu_task *task = starpu_task_create();
  120. task->cl = &sched_ctx_codelet;
  121. task->cl_arg = (void*)(uintptr_t) sched_ctx2;
  122. /*submit tasks to context*/
  123. ret = starpu_task_submit_to_ctx(task,sched_ctx2);
  124. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  125. }
  126. /* tell starpu when you finished submitting tasks to this context
  127. in order to allow moving resources from this context to the inheritor one
  128. when its corresponding tasks finished executing */
  129. /* wait for all tasks at the end*/
  130. starpu_task_wait_for_all();
  131. starpu_sched_ctx_delete(sched_ctx1);
  132. starpu_sched_ctx_delete(sched_ctx2);
  133. printf("ctx%d: tasks starpu executed %d out of %d\n", sched_ctx1, tasks_executed[0], NTASKS*NTASKS);
  134. printf("ctx%d: tasks starpu executed %d out of %d\n", sched_ctx2, tasks_executed[1], NTASKS*NTASKS);
  135. enodev:
  136. #ifdef STARPU_USE_CPU
  137. free(procs1);
  138. free(procs2);
  139. #endif
  140. starpu_shutdown();
  141. return ncpus == 0 ? 77 : 0;
  142. }