parallel_tasks_with_cluster_api.c 3.6 KB

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