Full text
OpenMP taskloop dependences Marcos Maro˜nas1,2, Xavier Teruel1, and Vicen¸c Beltran1 1Barcelona Supercomputing Center (BSC), Spain {mmaronas, xteruel, vbeltran}@bsc.es http://www.bsc.es 2Huawei Research, Edinburgh [email protected] Abstract. Exascale systems will contain multicore/manycore processors with high core count in each node. Therefore, using a model that relaxes the synchronization, such as data-flow, is crucial to adequately exploit the potential of the hardware. The flexibility of the data-flow execution model relies on the dynamic management of data-dependences among tasks. The OpenMP standard already provides a construct, known as taskloop, that distributes the loop iteration space into several tasks, but this construct does not support the use of the depend clause yet. In this paper we propose the use of the induction variable to define data dependences in tasks created by the taskloop construct. By using the induction variable, each task will contain its own dependences based on the partition of work they received. We also aim to demonstrate that using taskloop with dependences provides an enhancement in terms of programmability with respect to using stand-alone tasks to parallelize a loop. Our implementation does not introduce any significant overhead on the taskloop implementation and, in certain cases, it outperforms the stand-alone task version. Keywords: OpenMP ·Tasking ·Loops ·Synchronization ·taskloop construct ·depend clause. 1 Introduction The introduction of the first multiprocessor architectures led to the development of shared memory programming models. One of those is OpenMP, which became ade facto standard for parallelization on shared memory environments. OpenMP [12], with its highly optimized fork-join execution model, is a good choice to exploit structured parallelism, especially when the number of cores is small. Worksharing constructs, like the well-known omp for construct, are good examples of how OpenMP can efficiently exploit structured parallelism. However, when the number of cores increase and the work distribution is not perfectly balanced, the rigid fork-join execution model can hinder performance. The omp for construct accepts different scheduling policies that can mitigate load-balancing issues; and the nowait clause avoids the implicit barrier at the The final authenticated version is available online at https://doi.org/10.1007/978-3-030-85262-7_4 © Springer Nature Switzerland AG 2021
2 Marcos Maro˜nas et al. end of an omp for. Still, both techniques are only effective in a few specific situations. Moreover, the fork-join execution model is not well-suited for exploiting irregular, dynamic, or nested parallelism. Task-based programming models were developed to overcome some of the above-mentioned limitations. The first tasking models were based solely on the tasks and taskwaits primitives, which naturally support irregular, dynamic, and nested parallelism. However, these tasking models are still based on the forkjoin execution model. The big step forward came with the introduction of data dependences. Thus, replacing the rigid fork-join execution model by a more flexible data-flow execution model that relies on fine-grained synchronizations among tasks. Modern task-based programming models such as as Cilk, OmpSs or OpenMP tasking model have evolved with advanced features to exploit nested parallelism [13], hardware accelerators [2][6][1], and seamless integration with message passing APIs such as MPI [15][14]. Exascale systems will contain multicore/manycore processors with high core count in each node. Therefore, using a model that relaxes the synchronization, such as data-flow, is crucial to adequately exploit the potential of the hardware. Additionally, worksharing techniques are easier to apply compared to tasking. A single worksharing construct is enough to parallelize a loop. In contrast, using tasks, it requires more effort from the user. There must be at least a task per core, to feed all the cores and prevent lack of parallelism. A frequent technique applied to create enough tasks is blocking. This technique partitions a loop in several blocks, and each block is processed by an independent task. Although this is not a complex technique, it requires more effort than the worksharing alternative. The OpenMP standard contains a directive able to distribute the iteration space of a loop into tasks, which, theoretically, enables users to parallelize a whole loop with a single construct using tasks. This is the taskloop construct. However, in practice, it is not useful for a single reason: it does not support data dependences. Thus, a taskloop creates a set of tasks that cannot have data dependences, and so, the synchronization must be done using coarse-grained synchronization points (i.e., the implicit taskgroup, or explicit taskwaits). So, basically, we end up in a fork-join model but with increased overhead compared to worksharing constructs. We propose adding support for data dependences to the taskloop construct. Our proposal enables programmers to use the induction variable of the loop to specify data dependences. Thus, each task created by the taskloop will register the data dependences specified by the user. If the induction variable is used to specify any dependence, each task will register the dependence using its own value of the induction variable. As a result, apply blocking is possible using a single construct, enhancing programmability.
OpenMP taskloop dependences 3 2 Tasking Programmability Challenges Compared to using worksharing techniques, tasks are more complicated to use. If we simply replace worksharing constructs by task constructs, there is very few parallelism, and most of the cores are idle. This is because a worksharing construct distributes the work among all the available cores, that run concurrently. In contrast, an instantiated task is a piece of code that runs only in a single core at a given instant of time. Figure 1 shows such a problem. The figure also shows one possible solution, which is the use of blocking. #pragma omp parallel for for (i = 0; i < 20; i++) {...} CPU 0 CPU 1 CPU 2 CPU 3 It 0-4 It 5-9 It 10-14 It 15-19 // NO BLOCKING #pragma omp task for (i = 0; i < 20; i++) {...} CPU 0 CPU 1 CPU 2 CPU 3 It 0-19 for (i = 0; i < 20; i+=5) #pragma omp task for (ii = 0; ii < 5; ii++) {...} CPU 0 CPU 1 CPU 2 CPU 3 It 0-4 It 5-9 It 10-14 It 15-19 Fig. 1. Illustration of CPU occupation using different parallelism techniques. Listings 1.1 and 1.2 shows a real code using worksharing constructs and task with blocking respectively. It is possible to see that applying blocking techniques is simple, but also that it requires more effort than using worksharing constructs. For a single loop, worksharing constructs require only three lines of code, while tasks with blockings require five lines of code. The OpenMP standard already provides a construct known as taskloop that distributes work into several tasks. This construct is the natural replacement of worksharing constructs to use tasks. Listing 1.3 shows the very same example using the taskloop construct. Notwithstanding, the tasks created using the taskloop construct cannot have data dependences, so they can only be synchronized using synchronization points (i.e., keeping the implicit taskgroup region or using explicit taskwaits). As a result, we have a fork-join pattern with its rigid synchronization. So, we moved from worksharing constructs to tasks to benefit from a more lightweight data-flow synchronization, but the impossibility of using data dependences when using the taskloop construct prevents us from obtaining all its benefits. In summary, tasks require more effort from users than worksharing constructs. However, tasks provide key benefits that fit the requirements of Exascale systems better than worksharing constructs. For instance, programmers
4 Marcos Maro˜nas et al. Listing 1.1. Simple code using the work-sharing construct #pragma omp f o r f o r ( s i z e t j = 0 ; j <N; j++) b [ j ] = s c a l a r ∗c [ j ] ; Listing 1.2. Simple code using the task construct (and blocking) f o r ( s i z e t j = 0 ; j <N; j+=BS) { s i z e t s i z e = j+BSIZE >N ? N−j : BSIZE ; #pragma omp task depend ( in : c [ j : s i z e ] ) depend ( out : b [ j : s i z e ] ) f o r ( s i z e t j2=j ; j2 <j+s i z e ; j2++) b [ j2 ] = s c a l a r ∗c [ j2 ] ; } Listing 1.3. Simple code using the taskloop construct #pragma omp tas kloop chunksize (BSIZE) f o r ( s i z e t j = 0 ; j <N; j++) { b [ j ] = s c a l a r ∗c [ j ] ; }// i m p l i c i t taskgroup re gion may parallelize different stages of the program by means of tasks, as long as they guarantee a proper task depend annotation. Then, they can rely on the OpenMP run-time library to compute the correct synchronization order among these tasks. In other words, task parallelization improves composability. In the other hand, the taskloop construct enables programmers to use tasks with a similar effort than the effort required by worksharing constructs. Nevertheless, it does not support data dependences, and this prevent users from getting the key benefits of tasking. 3 Related work We already mentioned in the previous section that OpenMP supports both loopbased parallelism and task-based parallelism. The most common way of using loop-based parallelism in OpenMP is by means of the worksharing constructs. In terms of programmability, worksharing constructs enable users to parallelize loops using a single construct. Thus, they are very simple to use. In terms of performance, worksharing constructs deliver good performance in the general case. Nevertheless, they contain an implicit barrier at the end of the worksharing region, introducing very rigid synchronization. The task-based approach is a bit more complex in terms of programmability. It usually requires blocking techniques to uncover parallelism, which require some more code than a single construct. Regarding performance, tasks have a natural ability to deal with load imbalance, but they have associated costs that may introduce some overhead depending on task’s granularity. OpenMP pro-
OpenMP taskloop dependences 5 vides also the taskloop construct, that distributes the iteration space of a loop into several tasks. There is the possibility of specifying a grainsize guaranteeing that each of the tasks created executes no less than grainsize iterations. Thus, the taskloop construct simplifies the use of task-based parallelism, enabling users to parallelize loops with a single construct. Nevertheless, OpenMP does not support dependences in the taskloop construct. As a result, users must rely on fork-join-like synchronization with explicit synchronization points. Consequently, dropping the data-flow execution model of task-based parallelism, and its benefits. By enabling the use of data dependences in the taskloop construct, we offer users the possibility of parallelizing loops in a single construct while keeping the benefits of the data-flow execution model. Additionally, the use of the taskloop construct, may reduce the tasking overhead because allocations could be optimized to be done as a whole, instead of one by one. However, the number of tasks that will be created and scheduled is still proportional to the problem size. On the other hand, OpenMP 4.5 [11] already included the doacross dependences for work-sharing loops based on the source and sink dependence types and the extension of the ordered construct to support the depend clause. This feature allows to express general cross-iteration loop dependences and thereby support doacross parallelization [16]. The loop dependencies that cross the iteration space are enforced via point-to-point synchronization injected where the compiler finds the ordered construct. The main advantage of this extension is that only applying to a unique iteration space, it could be easily implemented by a single 2D matrix of per chunk relationships, imposing a very low overhead to the runtime. This advantage is also its main drawback, as it does not allow to dynamically connect to other loop iteration spaces (neither combine it with any other work-sharing or task generating constructs). Intel Cilk presents the cilk for [7], which is used to parallelize loops. The body of the loop is converted into a function that is called recursively using a divide and conquer strategy for achieving better performance. However, there is a cilk sync at the end of each iteration. Therefore, synchronization is quite rigid, similarly to OpenMP worksharings. Moreover, Cilk tasks do not support data dependencies between tasks. The CUDA programming model [10, 8] allows expressing kernel dependencies using proper streams and events. This approach will be similar to the stand-alone task model implemented by the OpenMP standard and requires manually transform the loop to its blocked version, to decompose the iteration space. Furthermore, the use of multiple streams and their synchronization via events could be non-trivial in some cases. On the other hand, CUDA offers the option to capture and reuse such task instantiation using the CUDA graph set of routines, offering an extra optimization by reducing the overhead. The CUDA graph functionality is out of the scope of this paper and should be considered as an extension that can be widely used, not only in taskloop constructs but in any portion of code parallelized with tasks.
6 Marcos Maro˜nas et al. for (t = 0; t < 100; t++) #pragma omp taskloop grainsize(5) \\ depend(inout: x[i]) for (i = 0; i < 20; i++) {...} It 0-4 It 5-9 It 10-14 It 15-19 T = 0 It 0-4 It 5-9 It 10-14 It 15-19 T = 1 ... It 0-4 It 5-9 It 10-14 It 15-19 T = 100 for (t = 0; t < 100; t++) for (i = 0; i < 20; i+=5) #pragma omp task depend(inout:x[i]) for (ii = i; ii < i+5; ii++) {...} Fig. 2. Partition of work and dependences between tasks created using taskloop 4 Taskloop with Dependences In this section, we detail the syntax of our proposal to support data dependences in the taskloop construct. In short, we propose the use of the induction variable to define data dependences in tasks created by the taskloop construct. By using the induction variable, each task will contain its own dependences based on the partition of work they received. Figure 2 shows an example. There is an outer loop and an inner loop parallelized using the enhanced taskloop construct with data dependences. As we can see, the data dependences contain the induction variable. In this case, it means that each of the tasks register a dependence over the i-th element of x. As each of the tasks receive a part of the iteration space, each of the tasks will have different values for i, thereby allowing them to run concurrently, but defining a dependence with tasks of the next and previous t iteration that work over the same data. Figure 2 also includes the code to get equivalent behavior using regular tasks. With this mechanism, expressivity is enhanced and the taskloop construct becomes usable in many real-world examples while keeping the key benefits of tasking. We would like to point out that the mechanism to define the granularity of a task created by the taskloop construct is the grainsize clause, as shown in Figure 2. As the loop iteration distribution should be deterministic and visible to the programmer (in order to allow him/her to combine tasks generated by the taskloop with stand-alone generated tasks), the use of taskloop dependences will assume the strict modifier within the grainsize clause. If no grainsize is provided, default applies. A second observation relates the task instantiation order and the dependence computation. Currently, taskloop does not guarantee any task creation order, while the proper dependence computation relies on it. Although the implementation does not need to actually create tasks in such order, it must compute dependencies AS IF they were created in the logical iteration order. The OpenMP
OpenMP taskloop dependences 7 runtime can easily follow this behavior due to it can compute the number of tasks and the corresponding task boundaries when encountering the taskloop construct. In addition, the current specification says “Programs that rely on any execution order of the logical iteration are non-conforming”; such part of the specification must be relaxed to take into account synchronization arising from dependences. Although in our implementation the compiler is able to detect the use of the loop control variable and modify the corresponding value for each task instance, a more OpenMP-aligned approach would involve the use of a modifier within the depend clause. We may have two different options: –Leverage the existent iterator modifier by extending its syntax to accept a new loop-based value generator. Then, the example in Figure 2 could be rewritten by annotating the loop using: #pragma omp taskloop depend(iterator(t=taskrange), inout: x[t]) grainsize(5). –Define a new modifier based on the task loop boundaries. Again, the example in Figure 2 could be rewritten using: #pragma omp taskloop depend(taskrange(b=begin), inout: x[b:5]) grainsize(5). While the first option relies in the multiple values generated by the iterator modifer (by means of a new iterator range specifier: taskrange); the second one uses the chunk-associated lower bound (by means of the new OpenMP keyword begin and a new depend modifier: taskrange) combined with the grainsize parameter in order to define an array section. Finally, adding taskloop dependences on the OpenMP specification should relax the implicit taskgroup region defined as part of the taskloop construct. It will be recommended to implicitely consider the nogroup clause when the programmer uses any dependence clause on the taskloop construct. 5 Implementation Our proposal is done in the OmpSs-2 programming model, built on top of the Mercurium compiler and the Nanos6 runtime library. Following, we detail the extensions done in both components to support dependences in the taskloop construct. We also conceptually explain our implementation. Semantics The taskloop construct is a convenient “syntactic sugar” to ease the use of tasks. It can be implemented just by applying an automatic blocking technique in the compiler side, similar to a manual blocking done by the enduser. But it also allows smarter implementations that can improve performance by reducing the associated overhead.
8 Marcos Maro˜nas et al. Mercurium compiler The Mercurium compiler has been extended to support the use of data dependences in the taskloop construct. Mercurium is a source to source compiler, meaning that it receives code as an input, and generates code as an output. Mercurium creates a function to register dependences per each task construct found in the user code. To support the use of the induction variable in the taskloop dependences, Mercurium has to accept a new parameter in the functions used to register dependences. Given that in our implementation it is the runtime who partitions the work and assigns iterations to the tasks, Mercurium must receive the information of the assigned iterations to replace the induction variable by its real value. Additionally, in the same line, Mercurium creates a function per task type including the user code that the task has to run. In this case, it also has to receive an additional parameter: the iterations that each task has to run. Finally, when creating a taskloop entity, Mercurium has to enable some flags to let the runtime system know that this is not a regular task, but a taskloop. Nanos6 runtime library In the runtime system, the first step is to extend the work descriptor of a task to include the iteration space of the loop, and the grainsize specified by the user, if any. This single task will represent the whole taskloop and it will register the whole set of dependencies, which are generated based on the iteration space and the grainsize value. When the task instance that represents the taskloop is executed it will instantiate one sub-task for each partition of the iteration space with its corresponding dependencies. This approach works because we are leveraging the weak dependencies and early release features of OmpSs-2 [13] that enables the parent task to become ready even if the dependencies are still not fulfilled. In this way, the sub-tasks created behave as if they were created on the dependency domain of the parent taskloop. A side effect of this implementation is that the creation and execution of the taskloop is not blocking, so many of them can be executed in parallel. Each sub-task created inside a task-loop will behave as a regular task, waiting for its data-dependencies before it can become ready. We would like to point out that our current implementation focuses on programmability. Therefore, we are trying to provide a simpler way of using tasks that introduces no significant overhead compared to using other techniques such as manual blocking. Nevertheless, the taskloop construct provides the opportunity to apply further optimizations that cannot be applied in the case of manual blocking. Such optimizations could include a single allocation for all the loop tasks, instead of allocating space for each of them individually; or the application of smarter throttle policies to mitigate memory overuse when there are too many tasks in flight. In this way, throttle policies may take into account the total number of tasks needed to instantiate the whole loop iteration space, rather than consider each of the tasks as an independent entity (i.e., making decisions based on each individual item).
OpenMP taskloop dependences 9 6 Experiment results In this section, we wish to demonstrate that the taskloop with dependences provides an enhancement in programmability when using tasks, while introducing no significant overhead compared to a manual equivalent implementation. For that purpose, our evaluation will focus in both programmability and performance. Regarding programmability, we used several different metrics to compare the different implementations: Source Lines of Code (SLOC) [9], Development Estimate Effort (DEE) [5], and Cyclomatic Complexity (CC) [17]. It is important to highlight that for the SLOC metric we only consider the code related to the parallelization. And it is also important to notice that this metric is an approximation to measure code complexity based only on the number of lines but it ignores that some individual lines can be more complex than others. In terms of performance, we compare the different implementations to demonstrate that using the taskloop construct do not add any significant overhead. Environment The experiments were carried out on the Marenostrum 4 supercomputer. It is composed of nodes with 2 sockets Intel Xeon Platinum 8160 2.1GHz 24-core and 96GB of main memory. Regarding the software, we used the Mercurium [3] compiler (v2.3.0) and the Nanos6 runtime library [4] as the baseline components to implement our proposal (described in Section 5); the gcc and gfortran compilers (v7.2.0), in order to compile Mercurium and Nanos6 (included here for reproducibility purposes); and the Intel compilers (v17.0.4), as the native compiler used by Mercurium to generate binary code. Methodology As previously introduced, we focus our evaluation in two different aspects: performance and programmability. Our experiments will use two different versions of each application/benchmark: – T. Version using regular tasks. It requires manual blocking. – TL. Version using the taskloop construct with dependences. Regarding performance, for each of the benchmarks/applications, we select two different problem sizes, one small-medium size, and one big size. For each of the problem sizes, we try several block sizes to show that the differences between the Tand the TL are small or even non-existent in several different scenarios. All the experiments ran using the interleaving policy offered by the numactl command, spreading the data evenly across all the available NUMA nodes, in order to minimize the NUMA effect. The results shown in the figures are averages of five different executions. We decided to use only five executions because the variability across different executions was very small. Related to programmability, we count the SLOC required to parallelize the baseline code for each of the versions, and use the SLOCCount [18] tool and the Lizard [19] tool to retrieve the DEE and CC respectively.