parallel_code.c 2.7 KB

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