parallel_code.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2014, 2016-2017 Université de Bordeaux
  4. * Copyright (C) 2010-2017 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. #ifdef STARPU_USE_CPU
  19. #include <omp.h>
  20. #ifdef STARPU_QUICK_CHECK
  21. #define NTASKS 4
  22. #else
  23. #define NTASKS 10
  24. #endif
  25. int parallel_code(unsigned *sched_ctx)
  26. {
  27. int i;
  28. int t = 0;
  29. int *cpuids = NULL;
  30. int ncpuids = 0;
  31. starpu_sched_ctx_get_available_cpuids(*sched_ctx, &cpuids, &ncpuids);
  32. /* printf("execute task of %d threads \n", ncpuids); */
  33. omp_set_num_threads(ncpuids);
  34. #pragma omp parallel
  35. {
  36. starpu_sched_ctx_bind_current_thread_to_cpuid(cpuids[omp_get_thread_num()]);
  37. /* printf("cpu = %d ctx%d nth = %d\n", sched_getcpu(), *sched_ctx, omp_get_num_threads()); */
  38. #pragma omp for
  39. for(i = 0; i < NTASKS; i++)
  40. {
  41. #pragma omp atomic
  42. t++;
  43. }
  44. }
  45. free(cpuids);
  46. return t;
  47. }
  48. void *th(void* p)
  49. {
  50. unsigned* sched_ctx = (unsigned*)p;
  51. void* ret;
  52. ret = starpu_sched_ctx_exec_parallel_code((void*)parallel_code, p, *sched_ctx);
  53. pthread_exit(ret);
  54. }
  55. int main(int argc, char **argv)
  56. {
  57. int ret;
  58. void* tasks_executed;
  59. ret = starpu_init(NULL);
  60. if (ret == -ENODEV)
  61. return 77;
  62. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  63. int nprocs1;
  64. int *procs1;
  65. unsigned ncpus = starpu_cpu_worker_get_count();
  66. procs1 = (int*)malloc(ncpus*sizeof(int));
  67. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs1, ncpus);
  68. nprocs1 = ncpus;
  69. unsigned sched_ctx1 = starpu_sched_ctx_create(procs1, nprocs1, "ctx1", STARPU_SCHED_CTX_POLICY_NAME, "dmda", 0);
  70. /* This is the interesting part, we can launch a code to hijack the context and
  71. use its cores to do something else entirely thanks to this */
  72. pthread_t mp;
  73. pthread_create(&mp, NULL, th, &sched_ctx1);
  74. pthread_join(mp, &tasks_executed);
  75. /* Finished, delete the context and print the amount of executed tasks */
  76. starpu_sched_ctx_delete(sched_ctx1);
  77. printf("ctx%u: tasks starpu executed %ld out of %d\n", sched_ctx1, (intptr_t)tasks_executed, NTASKS);
  78. starpu_shutdown();
  79. free(procs1);
  80. return 0;
  81. }
  82. #else /* STARPU_USE_CPU */
  83. int main(int argc, char **argv)
  84. {
  85. /* starpu_sched_ctx_exec_parallel_code() requires a CPU worker has parallel region master */
  86. return 77; /* STARPU_TEST_SKIPPED */
  87. }
  88. #endif /* STARPU_USE_CPU */