sched_ctx.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #define NTASKS 1000
  20. int tasks_executed = 0;
  21. pthread_mutex_t mut;
  22. static void sched_ctx_func(void *descr[] __attribute__ ((unused)), void *arg __attribute__ ((unused)))
  23. {
  24. pthread_mutex_lock(&mut);
  25. tasks_executed++;
  26. pthread_mutex_unlock(&mut);
  27. }
  28. static struct starpu_codelet sched_ctx_codelet =
  29. {
  30. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  31. .cpu_funcs = {sched_ctx_func, NULL},
  32. .cuda_funcs = {sched_ctx_func, NULL},
  33. .opencl_funcs = {sched_ctx_func, NULL},
  34. .model = NULL,
  35. .nbuffers = 0
  36. };
  37. int main(int argc, char **argv)
  38. {
  39. int ntasks = NTASKS;
  40. int ret;
  41. struct starpu_conf conf;
  42. ret = starpu_init(NULL);
  43. if (ret == -ENODEV)
  44. return 77;
  45. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  46. #ifdef STARPU_QUICK_CHECK
  47. ntasks /= 100;
  48. #endif
  49. pthread_mutex_init(&mut, NULL);
  50. unsigned ncpus = starpu_cpu_worker_get_count();
  51. unsigned ncuda = starpu_cuda_worker_get_count();
  52. int cpus[ncpus];
  53. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, cpus, ncpus);
  54. int cudadevs[ncuda];
  55. starpu_worker_get_ids_by_type(STARPU_CUDA_WORKER, cudadevs, ncuda);
  56. int nprocs1 = ncpus;
  57. int nprocs2 = ncuda;
  58. int procs1[nprocs1];
  59. int procs2[nprocs2];
  60. int k;
  61. for(k = 0; k < nprocs1; k++)
  62. {
  63. if(k < ncpus)
  64. procs1[k] = cpus[k];
  65. }
  66. for(k = 0; k < nprocs2; k++)
  67. {
  68. procs2[k] = cudadevs[k];
  69. }
  70. unsigned sched_ctx1 = starpu_create_sched_ctx("heft", procs1, nprocs1, "ctx1");
  71. unsigned sched_ctx2 = starpu_create_sched_ctx("heft", procs2, nprocs2, "ctx2");
  72. starpu_sched_ctx_set_inheritor(sched_ctx2, sched_ctx1);
  73. unsigned i;
  74. for (i = 0; i < ntasks/2; i++)
  75. {
  76. struct starpu_task *task = starpu_task_create();
  77. task->cl = &sched_ctx_codelet;
  78. task->cl_arg = NULL;
  79. ret = starpu_task_submit_to_ctx(task,sched_ctx1);
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  81. }
  82. starpu_sched_ctx_finished_submit(sched_ctx1);
  83. for (i = 0; i < ntasks/2; i++)
  84. {
  85. struct starpu_task *task = starpu_task_create();
  86. task->cl = &sched_ctx_codelet;
  87. task->cl_arg = NULL;
  88. ret = starpu_task_submit_to_ctx(task,sched_ctx2);
  89. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  90. }
  91. starpu_sched_ctx_finished_submit(sched_ctx2);
  92. starpu_task_wait_for_all();
  93. printf("tasks executed %d out of %d\n", tasks_executed, ntasks);
  94. starpu_shutdown();
  95. return 0;
  96. }