490_clustering_a_machine.doxy 9.5 KB

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