sched_ctx_without_sched_policy.c 4.4 KB

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