parallel_tasks_with_cluster_api.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015,2017,2019 CNRS
  4. * Copyright (C) 2015,2017 Inria
  5. * Copyright (C) 2015,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. #include <omp.h>
  20. #if !defined(STARPU_CLUSTER)
  21. int main(void)
  22. {
  23. return 77;
  24. }
  25. #else
  26. #ifdef STARPU_QUICK_CHECK
  27. #define NTASKS 8
  28. #else
  29. #define NTASKS 32
  30. #endif
  31. #define SIZE 4000
  32. /* Codelet SUM */
  33. static void sum_cpu(void * descr[], void *cl_arg)
  34. {
  35. double * v_dst = (double *) STARPU_VECTOR_GET_PTR(descr[0]);
  36. double * v_src0 = (double *) STARPU_VECTOR_GET_PTR(descr[1]);
  37. double * v_src1 = (double *) STARPU_VECTOR_GET_PTR(descr[1]);
  38. int size;
  39. starpu_codelet_unpack_args(cl_arg, &size);
  40. fprintf(stderr, "sum_cpu\n");
  41. int i, k;
  42. #pragma omp parallel
  43. fprintf(stderr, "hello from the task %d\n", omp_get_thread_num());
  44. for (k=0;k<10;k++)
  45. {
  46. #pragma omp parallel for
  47. for (i=0; i<size; i++)
  48. {
  49. v_dst[i]+=v_src0[i]+v_src1[i];
  50. }
  51. }
  52. }
  53. static struct starpu_codelet sum_cl =
  54. {
  55. .cpu_funcs = {sum_cpu, NULL},
  56. .nbuffers = 3,
  57. .modes={STARPU_RW,STARPU_R, STARPU_R}
  58. };
  59. int main(void)
  60. {
  61. int ntasks = NTASKS;
  62. int ret, i;
  63. struct starpu_cluster_machine *clusters;
  64. setenv("STARPU_NMIC","0",1);
  65. setenv("STARPU_NMPI_MS","0",1);
  66. ret = starpu_init(NULL);
  67. if (ret == -ENODEV)
  68. return 77;
  69. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  70. /* We regroup resources under each sockets into a cluster. We express a partition
  71. * of one socket to create two internal clusters */
  72. clusters = starpu_cluster_machine(HWLOC_OBJ_SOCKET,
  73. STARPU_CLUSTER_PARTITION_ONE,
  74. STARPU_CLUSTER_NEW,
  75. // STARPU_CLUSTER_TYPE, STARPU_CLUSTER_OPENMP,
  76. // STARPU_CLUSTER_TYPE, STARPU_CLUSTER_INTEL_OPENMP_MKL,
  77. STARPU_CLUSTER_NB, 2,
  78. STARPU_CLUSTER_NCORES, 1,
  79. 0);
  80. starpu_cluster_print(clusters);
  81. /* Data preparation */
  82. double array1[SIZE];
  83. double array2[SIZE];
  84. memset(array1, 0, sizeof(double));
  85. for (i=0;i<SIZE;i++)
  86. {
  87. array2[i]=i*2;
  88. }
  89. starpu_data_handle_t handle1;
  90. starpu_data_handle_t handle2;
  91. starpu_vector_data_register(&handle1, 0, (uintptr_t)array1, SIZE, sizeof(double));
  92. starpu_vector_data_register(&handle2, 0, (uintptr_t)array2, SIZE, sizeof(double));
  93. int size = SIZE;
  94. for (i = 0; i < ntasks; i++)
  95. {
  96. ret = starpu_task_insert(&sum_cl,
  97. STARPU_RW, handle1,
  98. STARPU_R, handle2,
  99. STARPU_R, handle1,
  100. STARPU_VALUE, &size, sizeof(int),
  101. /* For two tasks, try out the case when the task isn't parallel and expect
  102. the configuration to be sequential due to this, then automatically changed
  103. back to the parallel one */
  104. STARPU_POSSIBLY_PARALLEL, (i<=4 || i > 6) ? 1 : 0,
  105. /* Note that this mode requires that you put a prologue callback managing
  106. this on all tasks to be taken into account. */
  107. STARPU_PROLOGUE_CALLBACK_POP, &starpu_openmp_prologue,
  108. 0);
  109. if (ret == -ENODEV)
  110. goto out;
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  112. }
  113. out:
  114. /* wait for all tasks at the end*/
  115. starpu_task_wait_for_all();
  116. starpu_data_unregister(handle1);
  117. starpu_data_unregister(handle2);
  118. starpu_uncluster_machine(clusters);
  119. starpu_shutdown();
  120. return 0;
  121. }
  122. #endif