490_clustering_a_machine.doxy 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. /*! \page ClusteringAMachine Clustering A Machine
  17. \section ClusteringGeneralIdeas General Ideas
  18. Clusters are a concept introduced in this
  19. <a href="https://hal.inria.fr/view/index/docid/1181135">paper</a>.
  20. The granularity problem is tackled by using resource aggregation:
  21. instead of dynamically splitting tasks, resources are aggregated
  22. to process coarse grain tasks in a parallel fashion. This is built on
  23. top of scheduling contexts to be able to handle any type of parallel
  24. tasks.
  25. This comes from a basic idea, making use of two levels of parallelism
  26. in a DAG.
  27. We keep the DAG parallelism but consider on top of it that a task can
  28. contain internal parallelism. A good example is if each task in the DAG
  29. is OpenMP enabled.
  30. The particularity of such tasks is that we will combine the power of two
  31. runtime systems: StarPU will manage the DAG parallelism and another
  32. runtime (e.g. OpenMP) will manage the internal parallelism. The challenge
  33. is in creating an interface between the two runtime systems so that StarPU
  34. can regroup cores inside a machine (creating what we call a \b cluster) on
  35. top of which the parallel tasks (e.g. OpenMP tasks) will be run in a
  36. contained fashion.
  37. The aim of the cluster API is to facilitate this process in an automatic
  38. fashion. For this purpose, we depend on the \c hwloc tool to detect the
  39. machine configuration and then partition it into usable clusters.
  40. <br>
  41. An example of code running on clusters is available in
  42. <c>examples/sched_ctx/parallel_tasks_with_cluster_api.c</c>.
  43. <br>
  44. Let's first look at how to create a cluster.
  45. To enable clusters in StarPU, one needs to set the configure option
  46. \ref enable-cluster "--enable-cluster".
  47. \section CreatingClusters Creating Clusters
  48. Partitioning a machine into clusters with the cluster API is fairly
  49. straightforward. The simplest way is to state under which machine
  50. topology level we wish to regroup all resources. This level is an \c hwloc
  51. object, of the type <c>hwloc_obj_type_t</c>. More information can be found in the
  52. <a href="https://www.open-mpi.org/projects/hwloc/doc/v2.0.3/">hwloc
  53. documentation</a>.
  54. Once a cluster is created, the full machine is represented with an opaque
  55. structure starpu_cluster_machine. This can be printed to show the
  56. current machine state.
  57. \code{.c}
  58. struct starpu_cluster_machine *clusters;
  59. clusters = starpu_cluster_machine(HWLOC_OBJ_SOCKET, 0);
  60. starpu_cluster_print(clusters);
  61. /* submit some tasks with OpenMP computations */
  62. starpu_uncluster_machine(clusters);
  63. /* we are back in the default StarPU state */
  64. \endcode
  65. The following graphic is an example of what a particular machine can
  66. look like once clusterized. The main difference is that we have less
  67. worker queues and tasks which will be executed on several resources at
  68. once. The execution of these tasks will be left to the internal runtime
  69. system, represented with a dashed box around the resources.
  70. \image latex runtime-par.eps "StarPU using parallel tasks" width=0.5\textwidth
  71. \image html runtime-par.png "StarPU using parallel tasks"
  72. Creating clusters as shown in the example above will create workers able to
  73. execute OpenMP code by default. The cluster creation function
  74. starpu_cluster_machine() takes optional parameters after the \c hwloc
  75. object (always terminated by the value \c 0) which allow to parametrize the
  76. cluster creation. These parameters can help creating clusters of a
  77. type different from OpenMP, or create a more precise partition of the
  78. machine.
  79. This is explained in Section \ref CreatingCustomClusters.
  80. \section ExampleOfConstrainingOpenMP Example Of Constraining OpenMP
  81. Clusters require being able to constrain the runtime managing the internal
  82. task parallelism (internal runtime) to the resources set by StarPU. The
  83. purpose of this is to express how StarPU must communicate with the internal
  84. runtime to achieve the required cooperation. In the case of OpenMP, StarPU
  85. will provide an awake thread from the cluster to execute this liaison. It
  86. will then provide on demand the process ids of the other resources supposed
  87. to be in the region. Finally, thanks to an OpenMP region we can create the
  88. required number of threads and bind each of them on the correct region.
  89. These will then be reused each time we encounter a <c>\#pragma omp
  90. parallel</c> in the following computations of our program.
  91. The following graphic is an example of what an OpenMP-type cluster looks
  92. like and how it represented in StarPU. We can see that one StarPU (black)
  93. thread is awake, and we need to create on the other resources the OpenMP
  94. threads (in pink).
  95. \image latex parallel_worker2.eps "StarPU with an OpenMP cluster" width=0.3\textwidth
  96. \image html parallel_worker2.png "StarPU with an OpenMP cluster"
  97. Finally, the following code shows how to force OpenMP to cooperate with StarPU
  98. and create the aforementioned OpenMP threads constrained in the cluster's
  99. resources set:
  100. \code{.c}
  101. void starpu_openmp_prologue(void * sched_ctx_id)
  102. {
  103. int sched_ctx = *(int*)sched_ctx_id;
  104. int *cpuids = NULL;
  105. int ncpuids = 0;
  106. int workerid = starpu_worker_get_id();
  107. //we can target only CPU workers
  108. if (starpu_worker_get_type(workerid) == STARPU_CPU_WORKER)
  109. {
  110. //grab all the ids inside the cluster
  111. starpu_sched_ctx_get_available_cpuids(sched_ctx, &cpuids, &ncpuids);
  112. //set the number of threads
  113. omp_set_num_threads(ncpuids);
  114. #pragma omp parallel
  115. {
  116. //bind each threads to its respective resource
  117. starpu_sched_ctx_bind_current_thread_to_cpuid(cpuids[omp_get_thread_num()]);
  118. }
  119. free(cpuids);
  120. }
  121. return;
  122. }
  123. \endcode
  124. This function is the default function used when calling starpu_cluster_machine() without extra parameter.
  125. Cluster are based on several tools and models already available within
  126. StarPU contexts, and merely extend contexts. More on contexts can be
  127. read in Section \ref SchedulingContexts.
  128. \section CreatingCustomClusters Creating Custom Clusters
  129. Clusters can be created either with the predefined types provided
  130. within StarPU, or with user-defined functions to bind another runtime
  131. inside StarPU.
  132. The predefined cluster types provided by StarPU are
  133. ::STARPU_CLUSTER_OPENMP, ::STARPU_CLUSTER_INTEL_OPENMP_MKL and
  134. ::STARPU_CLUSTER_GNU_OPENMP_MKL. The last one is only provided if
  135. StarPU is compiled with the \c MKL library. It uses MKL functions to
  136. set the number of threads which is more reliable when using an OpenMP
  137. implementation different from the Intel one.
  138. The cluster type is set when calling the function
  139. starpu_cluster_machine() with the parameter ::STARPU_CLUSTER_TYPE as
  140. in the example below, which is creating a \c MKL cluster.
  141. \code{.c}
  142. struct starpu_cluster_machine *clusters;
  143. clusters = starpu_cluster_machine(HWLOC_OBJ_SOCKET,
  144. STARPU_CLUSTER_TYPE, STARPU_CLUSTER_GNU_OPENMP_MKL,
  145. 0);
  146. \endcode
  147. Using the default type ::STARPU_CLUSTER_OPENMP is similar to calling
  148. starpu_cluster_machine() without any extra parameter.
  149. <br>
  150. Users can also define their own function.
  151. \code{.c}
  152. void foo_func(void* foo_arg);
  153. int foo_arg = 0;
  154. struct starpu_cluster_machine *clusters;
  155. clusters = starpu_cluster_machine(HWLOC_OBJ_SOCKET,
  156. STARPU_CLUSTER_CREATE_FUNC, &foo_func,
  157. STARPU_CLUSTER_CREATE_FUNC_ARG, &foo_arg,
  158. 0);
  159. \endcode
  160. Parameters that can be given to starpu_cluster_machine() are
  161. ::STARPU_CLUSTER_MIN_NB,
  162. ::STARPU_CLUSTER_MAX_NB, ::STARPU_CLUSTER_NB,
  163. ::STARPU_CLUSTER_POLICY_NAME, ::STARPU_CLUSTER_POLICY_STRUCT,
  164. ::STARPU_CLUSTER_KEEP_HOMOGENEOUS, ::STARPU_CLUSTER_PREFERE_MIN,
  165. ::STARPU_CLUSTER_CREATE_FUNC, ::STARPU_CLUSTER_CREATE_FUNC_ARG,
  166. ::STARPU_CLUSTER_TYPE, ::STARPU_CLUSTER_AWAKE_WORKERS,
  167. ::STARPU_CLUSTER_PARTITION_ONE, ::STARPU_CLUSTER_NEW and
  168. ::STARPU_CLUSTER_NCORES.
  169. \section ClustersWithSchedulingContextsAPI Clusters With Scheduling
  170. As previously mentioned, the cluster API is implemented
  171. on top of \ref SchedulingContexts. Its main addition is to ease the
  172. creation of a machine CPU partition with no overlapping by using
  173. \c hwloc, whereas scheduling contexts can use any number of any type
  174. of resources.
  175. It is therefore possible, but not recommended, to create clusters
  176. using the scheduling contexts API. This can be useful mostly in the
  177. most complex machine configurations where users have to dimension
  178. precisely clusters by hand using their own algorithm.
  179. \code{.c}
  180. /* the list of resources the context will manage */
  181. int workerids[3] = {1, 3, 10};
  182. /* indicate the list of workers assigned to it, the number of workers,
  183. the name of the context and the scheduling policy to be used within
  184. the context */
  185. int id_ctx = starpu_sched_ctx_create(workerids, 3, "my_ctx", 0);
  186. /* let StarPU know that the following tasks will be submitted to this context */
  187. starpu_sched_ctx_set_task_context(id);
  188. task->prologue_callback_pop_func=&runtime_interface_function_here;
  189. /* submit the task to StarPU */
  190. starpu_task_submit(task);
  191. \endcode
  192. As this example illustrates, creating a context without scheduling
  193. policy will create a cluster. The interface function between StarPU
  194. and the other runtime must be specified through the field
  195. starpu_task::prologue_callback_pop_func. Such a function can be
  196. similar to the OpenMP thread team creation one (see above).
  197. <br>
  198. Note that the OpenMP mode is the default mode both for clusters and
  199. contexts. The result of a cluster creation is a woken-up master worker
  200. and sleeping "slaves" which allow the master to run tasks on their
  201. resources.
  202. To create a cluster with woken-up workers, the flag
  203. ::STARPU_SCHED_CTX_AWAKE_WORKERS must be set when using the scheduling
  204. context API function starpu_sched_ctx_create(), or the flag
  205. ::STARPU_CLUSTER_AWAKE_WORKERS must be set when using the cluster API
  206. function starpu_cluster_machine().
  207. */