parallel_code.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #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(int argc, char **argv)
  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. #ifdef STARPU_USE_CPU
  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. #else
  70. nprocs1 = 1;
  71. procs1 = (int*)malloc(nprocs1*sizeof(int));
  72. procs1[0] = 0;
  73. #endif
  74. unsigned sched_ctx1 = starpu_sched_ctx_create(procs1, nprocs1, "ctx1", STARPU_SCHED_CTX_POLICY_NAME, "dmda", 0);
  75. /* This is the interesting part, we can launch a code to hijack the context and
  76. use its cores to do something else entirely thanks to this */
  77. pthread_t mp;
  78. pthread_create(&mp, NULL, th, &sched_ctx1);
  79. pthread_join(mp, &tasks_executed);
  80. /* Finished, delete the context and print the amount of executed tasks */
  81. starpu_sched_ctx_delete(sched_ctx1);
  82. printf("ctx%u: tasks starpu executed %ld out of %d\n", sched_ctx1, (intptr_t)tasks_executed, NTASKS);
  83. starpu_shutdown();
  84. free(procs1);
  85. return 0;
  86. }