parallel_tasks_reuse_handle.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015 INRIA
  4. * Copyright (C) 2015, 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. #define SIZE 40
  22. #define LOOPS 4
  23. #else
  24. #define NTASKS 100
  25. #define SIZE 400
  26. #define LOOPS 10
  27. #endif
  28. struct context
  29. {
  30. int ncpus;
  31. int *cpus;
  32. unsigned id;
  33. };
  34. /* Helper for the task that will initiate everything */
  35. void parallel_task_prologue_init_once_and_for_all(void * sched_ctx_)
  36. {
  37. int sched_ctx = *(int *)sched_ctx_;
  38. int *cpuids = NULL;
  39. int ncpuids = 0;
  40. starpu_sched_ctx_get_available_cpuids(sched_ctx, &cpuids, &ncpuids);
  41. #pragma omp parallel num_threads(ncpuids)
  42. {
  43. starpu_sched_ctx_bind_current_thread_to_cpuid(cpuids[omp_get_thread_num()]);
  44. }
  45. omp_set_num_threads(ncpuids);
  46. free(cpuids);
  47. return;
  48. }
  49. void noop(void * buffers[], void * cl_arg)
  50. {
  51. }
  52. static struct starpu_codelet init_parallel_worker_cl=
  53. {
  54. .cpu_funcs = {noop},
  55. .nbuffers = 0,
  56. .name = "init_parallel_worker"
  57. };
  58. /* function called to initialize the parallel "workers" */
  59. void parallel_task_init_one_context(unsigned * context_id)
  60. {
  61. struct starpu_task * t;
  62. int ret;
  63. t = starpu_task_build(&init_parallel_worker_cl,
  64. STARPU_SCHED_CTX, *context_id,
  65. 0);
  66. t->destroy = 1;
  67. t->prologue_callback_pop_func=parallel_task_prologue_init_once_and_for_all;
  68. if (t->prologue_callback_pop_arg_free)
  69. free(t->prologue_callback_pop_arg);
  70. t->prologue_callback_pop_arg=context_id;
  71. t->prologue_callback_pop_arg_free=0;
  72. ret = starpu_task_submit(t);
  73. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  74. }
  75. struct context main_context;
  76. struct context *contexts;
  77. void parallel_task_init()
  78. {
  79. /* Context creation */
  80. main_context.ncpus = starpu_cpu_worker_get_count();
  81. main_context.cpus = (int *) malloc(main_context.ncpus*sizeof(int));
  82. fprintf(stderr, "ncpus : %d \n",main_context.ncpus);
  83. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, main_context.cpus, main_context.ncpus);
  84. main_context.id = starpu_sched_ctx_create(main_context.cpus,
  85. main_context.ncpus,"main_ctx",
  86. STARPU_SCHED_CTX_POLICY_NAME,"prio",
  87. 0);
  88. /* Initialize nested contexts */
  89. /* WARNING : the number of contexts must be a divisor of the number of available cpus*/
  90. contexts = malloc(sizeof(struct context)*2);
  91. int cpus_per_context = main_context.ncpus/2;
  92. int i;
  93. for(i = 0; i < 2; i++)
  94. {
  95. fprintf(stderr, "ncpus %d for context %d \n",cpus_per_context, i);
  96. contexts[i].ncpus = cpus_per_context;
  97. contexts[i].cpus = main_context.cpus+i*cpus_per_context;
  98. }
  99. for(i = 0; i < 2; i++)
  100. contexts[i].id = starpu_sched_ctx_create(contexts[i].cpus,
  101. contexts[i].ncpus,"nested_ctx",
  102. STARPU_SCHED_CTX_NESTED,main_context.id,
  103. 0);
  104. for (i = 0; i < 2; i++)
  105. {
  106. parallel_task_init_one_context(&contexts[i].id);
  107. }
  108. starpu_task_wait_for_all();
  109. starpu_sched_ctx_set_context(&main_context.id);
  110. }
  111. void parallel_task_deinit()
  112. {
  113. int i;
  114. for (i=0; i<2;i++)
  115. starpu_sched_ctx_delete(contexts[i].id);
  116. free(contexts);
  117. free(main_context.cpus);
  118. }
  119. /* Codelet SUM */
  120. static void sum_cpu(void * descr[], void *cl_arg)
  121. {
  122. double *v_dst = (double *) STARPU_VECTOR_GET_PTR(descr[0]);
  123. double *v_src0 = (double *) STARPU_VECTOR_GET_PTR(descr[1]);
  124. double *v_src1 = (double *) STARPU_VECTOR_GET_PTR(descr[2]);
  125. int size = STARPU_VECTOR_GET_NX(descr[0]);
  126. int i, k;
  127. for (k=0;k<LOOPS;k++)
  128. {
  129. #pragma omp parallel for
  130. for (i=0; i<size; i++)
  131. {
  132. v_dst[i]+=v_src0[i]+v_src1[i];
  133. }
  134. }
  135. }
  136. static struct starpu_codelet sum_cl =
  137. {
  138. .cpu_funcs = {sum_cpu, NULL},
  139. .nbuffers = 3,
  140. .modes={STARPU_RW,STARPU_R, STARPU_R}
  141. };
  142. int main(int argc, char **argv)
  143. {
  144. int ntasks = NTASKS;
  145. int ret, j, k;
  146. unsigned ncpus = 0;
  147. ret = starpu_init(NULL);
  148. if (ret == -ENODEV)
  149. return 77;
  150. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  151. if (starpu_cpu_worker_get_count() < 2)
  152. {
  153. starpu_shutdown();
  154. return 77;
  155. }
  156. parallel_task_init();
  157. /* Data preparation */
  158. double array1[SIZE];
  159. double array2[SIZE];
  160. memset(array1, 0, sizeof(double));
  161. int i;
  162. for (i=0;i<SIZE;i++)
  163. {
  164. array2[i]=i*2;
  165. }
  166. starpu_data_handle_t handle1;
  167. starpu_data_handle_t handle2;
  168. starpu_vector_data_register(&handle1, 0, (uintptr_t)array1, SIZE, sizeof(double));
  169. starpu_vector_data_register(&handle2, 0, (uintptr_t)array2, SIZE, sizeof(double));
  170. for (i = 0; i < ntasks; i++)
  171. {
  172. struct starpu_task * t;
  173. t=starpu_task_build(&sum_cl,
  174. STARPU_RW,handle1,
  175. STARPU_R,handle2,
  176. STARPU_R,handle1,
  177. STARPU_SCHED_CTX, main_context.id,
  178. 0);
  179. t->destroy = 1;
  180. t->possibly_parallel = 1;
  181. ret=starpu_task_submit(t);
  182. if (ret == -ENODEV)
  183. goto out;
  184. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  185. }
  186. out:
  187. /* wait for all tasks at the end*/
  188. starpu_task_wait_for_all();
  189. starpu_data_unregister(handle1);
  190. starpu_data_unregister(handle2);
  191. parallel_task_deinit();
  192. starpu_shutdown();
  193. return 0;
  194. }