sched_ctx_without_sched_policy.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010-2014 Centre National de la Recherche Scientifique
  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 10
  23. #endif
  24. int tasks_executed[2];
  25. starpu_pthread_mutex_t mut;
  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)
  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[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  46. {
  47. unsigned sched_ctx = (unsigned)arg;
  48. tasks_executed[sched_ctx-1] += parallel_code(sched_ctx);
  49. }
  50. static struct starpu_codelet sched_ctx_codelet =
  51. {
  52. .cpu_funcs = {sched_ctx_func, NULL},
  53. .cuda_funcs = { NULL},
  54. .opencl_funcs = {NULL},
  55. .model = NULL,
  56. .nbuffers = 0,
  57. .name = "sched_ctx"
  58. };
  59. int main(int argc, char **argv)
  60. {
  61. tasks_executed[0] = 0;
  62. tasks_executed[1] = 0;
  63. int ntasks = NTASKS;
  64. int ret, j, k;
  65. ret = starpu_init(NULL);
  66. if (ret == -ENODEV)
  67. return 77;
  68. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  69. starpu_pthread_mutex_init(&mut, NULL);
  70. int nprocs1 = 1;
  71. int nprocs2 = 1;
  72. int *procs1, *procs2;
  73. #ifdef STARPU_USE_CPU
  74. unsigned ncpus = starpu_cpu_worker_get_count();
  75. procs1 = (int*)malloc(ncpus*sizeof(int));
  76. procs2 = (int*)malloc(ncpus*sizeof(int));
  77. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs1, ncpus);
  78. if(ncpus > 1)
  79. {
  80. nprocs1 = ncpus/2;
  81. nprocs2 = ncpus-nprocs1;
  82. k = 0;
  83. for(j = nprocs1; j < nprocs1+nprocs2; j++)
  84. procs2[k++] = j;
  85. }
  86. else
  87. {
  88. procs1 = (int*)malloc(nprocs1*sizeof(int));
  89. procs2 = (int*)malloc(nprocs2*sizeof(int));
  90. procs1[0] = 0;
  91. procs2[0] = 0;
  92. }
  93. #else
  94. procs1 = (int*)malloc(nprocs1*sizeof(int));
  95. procs2 = (int*)malloc(nprocs2*sizeof(int));
  96. procs1[0] = 0;
  97. procs2[0] = 0;
  98. #endif
  99. /*create contexts however you want*/
  100. unsigned sched_ctx1 = starpu_sched_ctx_create(procs1, nprocs1, "ctx1", 0);
  101. unsigned sched_ctx2 = starpu_sched_ctx_create(procs2, nprocs2, "ctx2", 0);
  102. int i;
  103. for (i = 0; i < ntasks; i++)
  104. {
  105. struct starpu_task *task = starpu_task_create();
  106. task->cl = &sched_ctx_codelet;
  107. task->cl_arg = sched_ctx1;
  108. /*submit tasks to context*/
  109. ret = starpu_task_submit_to_ctx(task,sched_ctx1);
  110. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  111. }
  112. for (i = 0; i < ntasks; i++)
  113. {
  114. struct starpu_task *task = starpu_task_create();
  115. task->cl = &sched_ctx_codelet;
  116. task->cl_arg = sched_ctx2;
  117. /*submit tasks to context*/
  118. ret = starpu_task_submit_to_ctx(task,sched_ctx2);
  119. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  120. }
  121. /* tell starpu when you finished submitting tasks to this context
  122. in order to allow moving resources from this context to the inheritor one
  123. when its corresponding tasks finished executing */
  124. /* wait for all tasks at the end*/
  125. starpu_task_wait_for_all();
  126. starpu_sched_ctx_delete(sched_ctx1);
  127. starpu_sched_ctx_delete(sched_ctx2);
  128. printf("ctx%d: tasks starpu executed %d out of %d\n", sched_ctx1, tasks_executed[0], NTASKS*NTASKS);
  129. printf("ctx%d: tasks starpu executed %d out of %d\n", sched_ctx2, tasks_executed[1], NTASKS*NTASKS);
  130. starpu_shutdown();
  131. return 0;
  132. }