parallel_code.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2015,2017 Inria
  4. * Copyright (C) 2010-2017 CNRS
  5. * Copyright (C) 2010-2014,2016-2017 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #ifdef STARPU_USE_CPU
  20. #include <omp.h>
  21. #ifdef STARPU_QUICK_CHECK
  22. #define NTASKS 4
  23. #else
  24. #define NTASKS 10
  25. #endif
  26. int parallel_code(unsigned *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. omp_set_num_threads(ncpuids);
  35. #pragma omp parallel
  36. {
  37. starpu_sched_ctx_bind_current_thread_to_cpuid(cpuids[omp_get_thread_num()]);
  38. /* printf("cpu = %d ctx%d nth = %d\n", sched_getcpu(), *sched_ctx, omp_get_num_threads()); */
  39. #pragma omp for
  40. for(i = 0; i < NTASKS; i++)
  41. {
  42. #pragma omp atomic
  43. t++;
  44. }
  45. }
  46. free(cpuids);
  47. return t;
  48. }
  49. void *th(void* p)
  50. {
  51. unsigned* sched_ctx = (unsigned*)p;
  52. void* ret;
  53. ret = starpu_sched_ctx_exec_parallel_code((void*)parallel_code, p, *sched_ctx);
  54. pthread_exit(ret);
  55. }
  56. int main(void)
  57. {
  58. int ret;
  59. void* tasks_executed;
  60. ret = starpu_init(NULL);
  61. if (ret == -ENODEV)
  62. return 77;
  63. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  64. int nprocs1;
  65. int *procs1;
  66. unsigned ncpus = starpu_cpu_worker_get_count();
  67. procs1 = (int*)malloc(ncpus*sizeof(int));
  68. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs1, ncpus);
  69. nprocs1 = ncpus;
  70. unsigned sched_ctx1 = starpu_sched_ctx_create(procs1, nprocs1, "ctx1", STARPU_SCHED_CTX_POLICY_NAME, "dmda", 0);
  71. /* This is the interesting part, we can launch a code to hijack the context and
  72. use its cores to do something else entirely thanks to this */
  73. pthread_t mp;
  74. pthread_create(&mp, NULL, th, &sched_ctx1);
  75. pthread_join(mp, &tasks_executed);
  76. /* Finished, delete the context and print the amount of executed tasks */
  77. starpu_sched_ctx_delete(sched_ctx1);
  78. printf("ctx%u: tasks starpu executed %ld out of %d\n", sched_ctx1, (intptr_t)tasks_executed, NTASKS);
  79. starpu_shutdown();
  80. free(procs1);
  81. return 0;
  82. }
  83. #else /* STARPU_USE_CPU */
  84. int main(int argc, char **argv)
  85. {
  86. /* starpu_sched_ctx_exec_parallel_code() requires a CPU worker has parallel region master */
  87. return 77; /* STARPU_TEST_SKIPPED */
  88. }
  89. #endif /* STARPU_USE_CPU */