sched_ctx.c 3.4 KB

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