parallel_code.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010-2013 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. #ifdef STARPU_QUICK_CHECK
  19. #define NTASKS 64
  20. #else
  21. #define NTASKS 1000
  22. #endif
  23. int tasks_executed = 0;
  24. starpu_pthread_mutex_t mut;
  25. static void sched_ctx_func(void *descr[] __attribute__ ((unused)), void *arg __attribute__ ((unused)))
  26. {
  27. starpu_pthread_mutex_lock(&mut);
  28. tasks_executed++;
  29. starpu_pthread_mutex_unlock(&mut);
  30. }
  31. static struct starpu_codelet sched_ctx_codelet =
  32. {
  33. .cpu_funcs = {sched_ctx_func, NULL},
  34. .cuda_funcs = {sched_ctx_func, NULL},
  35. .opencl_funcs = {sched_ctx_func, NULL},
  36. .model = NULL,
  37. .nbuffers = 0,
  38. .name = "sched_ctx"
  39. };
  40. int parallel_code(int nprocs)
  41. {
  42. int i;
  43. int tasks = 0;
  44. #pragma omp parallel for num_threads(nprocs)
  45. for (i = 0; i < NTASKS; i++)
  46. tasks++;
  47. return tasks;
  48. }
  49. int main(int argc, char **argv)
  50. {
  51. int ntasks = NTASKS;
  52. int ret;
  53. ret = starpu_init(NULL);
  54. if (ret == -ENODEV)
  55. return 77;
  56. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  57. starpu_pthread_mutex_init(&mut, NULL);
  58. int nprocs1 = 1;
  59. int nprocs2 = 1;
  60. int procs1[20], procs2[20];
  61. procs1[0] = 0;
  62. procs2[0] = 0;
  63. #ifdef STARPU_USE_CPU
  64. unsigned ncpus = starpu_cpu_worker_get_count();
  65. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs1, ncpus);
  66. nprocs1 = ncpus/2;
  67. nprocs2 = nprocs1;
  68. int j, k = 0;
  69. for(j = nprocs1; j < nprocs1+nprocs2; j++)
  70. procs2[k++] = j;
  71. #endif
  72. /*create contexts however you want*/
  73. unsigned sched_ctx1 = starpu_sched_ctx_create("dmda", procs1, nprocs1, "ctx1");
  74. unsigned sched_ctx2 = starpu_sched_ctx_create("dmda", procs2, nprocs2, "ctx2");
  75. /*indicate what to do with the resources when context 2 finishes (it depends on your application)*/
  76. starpu_sched_ctx_set_inheritor(sched_ctx2, sched_ctx1);
  77. int i;
  78. for (i = 0; i < ntasks; i++)
  79. {
  80. struct starpu_task *task = starpu_task_create();
  81. task->cl = &sched_ctx_codelet;
  82. task->cl_arg = NULL;
  83. /*submit tasks to context*/
  84. ret = starpu_task_submit_to_ctx(task,sched_ctx1);
  85. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  86. }
  87. /* tell starpu when you finished submitting tasks to this context
  88. in order to allow moving resources from this context to the inheritor one
  89. when its corresponding tasks finished executing */
  90. starpu_sched_ctx_finished_submit(sched_ctx1);
  91. /* execute an openmp code */
  92. int ret_ntasks = (int)starpu_sched_ctx_exec_parallel_code((void*)parallel_code, (void*)nprocs2, sched_ctx2);
  93. starpu_sched_ctx_finished_submit(sched_ctx2);
  94. /* wait for all tasks at the end*/
  95. starpu_task_wait_for_all();
  96. starpu_sched_ctx_delete(sched_ctx1);
  97. starpu_sched_ctx_delete(sched_ctx2);
  98. printf("ctx%d: tasks starpu executed %d out of %d\n", sched_ctx1, tasks_executed, ntasks);
  99. printf("ctx%d: tasks openmp executed %d out of %d\n", sched_ctx2, ret_ntasks, NTASKS);
  100. starpu_shutdown();
  101. return 0;
  102. }