Fork/Join Parallelism in the Wild: Documenting Patterns and Anti-Patterns in Java Programs Using the Fork/Join Framework Mattias De Wael, Stefan Marr, Tom Van Cutsem

Fork/Join Parallelism in the Wild: Documenting Patterns and Anti-Patterns in Java Programs Using the Fork/Join Framework Mattias De Wael, Stefan Marr, Tom Van Cutsem

Fork/Join Parallelism in the Wild: Documenting Patterns and Anti-patterns in Java Programs Using the Fork/Join Framework Mattias de Wael, Stefan Marr, Tom van Cutsem To cite this version: Mattias de Wael, Stefan Marr, Tom van Cutsem. Fork/Join Parallelism in the Wild: Documenting Patterns and Anti-patterns in Java Programs Using the Fork/Join Framework. PPPJ’14, International Conference on Principles and Practices of Programming on the Java Platform: Virtual Machines, Languages, and Tools, Sep 2014, Cracow, Poland. 10.1145/2647508.2647511. hal-01064872 HAL Id: hal-01064872 https://hal.inria.fr/hal-01064872 Submitted on 17 Sep 2014 HAL is a multi-disciplinary open access L’archive ouverte pluridisciplinaire HAL, est archive for the deposit and dissemination of sci- destinée au dépôt et à la diffusion de documents entific research documents, whether they are pub- scientifiques de niveau recherche, publiés ou non, lished or not. The documents may come from émanant des établissements d’enseignement et de teaching and research institutions in France or recherche français ou étrangers, des laboratoires abroad, or from public or private research centers. publics ou privés. Fork/Join Parallelism in the Wild Documenting Patterns and Anti-Patterns in Java Programs using the Fork/Join Framework Mattias De Wael Stefan Marr Tom Van Cutsem Software Languages Lab RMoD, INRIA Lille – Nord Europe Software Languages Lab Vrije Universiteit Brussel, Belgium France Vrije Universiteit Brussel, Belgium [email protected] [email protected] [email protected] Abstract grammers must turn to parallelism. This has caused parallel pro- Now that multicore processors are commonplace, developing par- gramming to outgrow its original niche of High Performance Com- allel software has escaped the confines of high-performance com- puting (HPC). Today, parallel programming is no longer the terri- puting and enters the mainstream. The Fork/Join framework, for tory of a handful of experts. As a testament to this, libraries and instance, is part of the standard Java platform since version 7. frameworks for parallel programming are now commonplace, even Fork/Join is a high-level parallel programming model advocated on so-called “managed runtimes”, such as the JVM and the CLR. to make parallelizing recursive divide-and-conquer algorithms par- In general, writing parallel software remains notoriously diffi- ticularly easy. While, in theory, Fork/Join is a simple and effective cult [20]. However, good abstractions exist for certain classes of technique to expose parallelism in applications, it has not been in- applications. One such class is that of recursive divide-and-conquer vestigated before whether and how the technique is applied in prac- algorithms, which can be parallelized using Fork/Join. tice. We therefore performed an empirical study on a corpus of 120 The use of Fork/Join as an efficient means of parallelizing open source Java projects that use the framework for roughly 362 divide-and-conquer algorithms was pioneered in MIT Cilk [3, 4, 8], different tasks. which combined the Fork/Join programming model with an effi- On the one hand, we confirm the frequent use of four best- cient so-called work stealing scheduler. Cilk has inspired modern practice patterns (Sequential Cutoff, Linked Subtasks, Leaf Tasks, parallel programming libraries for various mainstream languages, and avoiding unnecessary forking) in actual projects. On the other including Intel Cilk Plus and Intel Threading Building Blocks for hand, we also discovered three recurring anti-patterns that poten- C/C++, the Task Parallel Library for .NET, and the Java Fork/Join tially limit parallel performance: sub-optimal use of Java collec- framework. tions when splitting tasks into subtasks as well as when merging The efficiency of scheduling Fork/Join tasks with work steal- the results of subtasks, and finally the inappropriate sharing of re- ing has been investigated from a theoretical [3] as well as from a sources between tasks. We document these anti-patterns and study practical [12] perspective. However, the actual use of the Fork/Join their impact on performance. model has, to the best of our knowledge, never been studied before. In this paper, we study the use of the Java Fork/Join framework on Categories and Subject Descriptors D.1.3 [Programming Tech- a corpus of 120 open source Java projects. niques]: Parallel programming; D.3.3 [Programming Languages]: Our goal is to obtain insight into how the Fork/Join model is ap- Patterns; D.3.3 [Programming Languages]: Concurrent program- plied in practice, primarily by identifying best-practice patterns and ming structures anti-patterns. These observations can then in turn guide language or framework designers to propose new or improved abstractions that General Terms Languages, Performance steer programmers toward using the right patterns and away from Keywords Java, Fork/Join, empirical study, open source projects, using the anti-patterns. patterns, anti-patterns Contributions The contributions of this paper are: • A qualitative analysis of a corpus of 120 open source Java 1. Introduction projects that use Fork/Join. Since roughly 2004, multicore processors have become common- • The identification of three anti-patterns in these projects: place on commodity machines such as desktops, laptops, and even (i) sub-optimal use of Java collections when dividing tasks into mobile devices. To use such platforms to their full potential, pro- subtasks, (ii) sub-optimal use of Java collections when merging results of subtasks, and (iii) inappropriate sharing of resources Permission to make digital or hard copies of all or part of this work for personal or between tasks. classroom use is granted without fee provided that copies are not made or distributed • for profit or commercial advantage and that copies bear this notice and the full citation A study of the impact on performance of the three anti-patterns. on the first page. Copyrights for components of this work owned by others than the • Proposals on how future languages or frameworks could help author(s) must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission avoid the identified anti-patterns. and/or a fee. Request permissions from [email protected]. Paper Outline In Section 2 we introduce the Java Fork/Join PPPJ ’14, September 23 - 26 2014, Cracow, Poland. Copyright is held by the owner/author(s). Publication rights licensed to ACM. framework and some best-practices on how to use the frame- ACM 978-1-4503-2837-1/14/09. $15.00. work effectively. We then turn to our empirical study of how the http://dx.doi.org/10.1145/2647508.2647511 Fork/Join framework is used in practice. Section 3 describes our methodology, that is, how we identified our corpus of 120 open the actual computation in the compute() method which also re- source Java projects and how we identified the patterns. The pecu- turns the result. liarities of benchmarking on the Java platform that we took into ac- In this example, compute() contains both the base case (lines count are also discussed in Section 3. Section 4 sheds light on how 5-6) as well as the recursive case (lines 7-14). In order to reach the the Java Fork/Join framework is used in practice. We confirm the base case, each recursive step creates two new tasks initialized with use of four best-practice patterns (Section 4.1). Next, we identify the corresponding argument. A task t can subsequently be sched- and document three recurring anti-patterns (Section 4.2). Section 5 uled for parallel execution by calling t.fork(). Finally, synchro- revisits the three anti-patterns and studies their impact on perfor- nize on the completion of t and to obtain the result, call t.join(). mance. In Section 6 we describe how the identified anti-patterns Figure 2 shows how to schedule a task on a ForkJoinPool to could be avoided. Finally, Section 7 highlights related empirical start the actual computation. Internally, a ForkJoinPool is imple- studies. mented as a thread pool that uses work stealing to balance the load of executing tasks among parallel threads. 2. The Java Fork/Join Framework 1 ForkJoinPool poolOfWorkerThreads = new ForkJoinPool(); We give an introduction on how to implement recursive divide-and- 2 Fibonacci task = new Fibonacci( n ); conquer algorithms with Java’s Fork/Join framework. This section 3 int result = poolOfWorkerThreads. invoke ( task ); is primarily based on Concurrent Programming in Java: Design Principles and Patterns [11, Section 4.4], which documents best Figure 2. Starting the Fibonacci computation on a ForkJoinPool practices and gives advice on how to implement efficient parallel programs. All code examples in this text are Java snippets that re- semble executable code as much as possible. For editorial reasons 2.2 Efficiency Maxims for Fork/Join Computations. we sometimes omitted exception handling, concrete method imple- To obtain maximum efficiency with Fork/Join, it is important to mentations, and generic types. (i) maximize parallelism, (ii) minimize overhead, (iii) minimize Divide-and-conquer algorithms consist of a base case, in which contention, and (iv) maximize locality(Lea [11]). We refer to these the problem is small enough to be readily solved, and a recursive as the efficiency maxims of the Fork/Join framework. The anti- case, in which the problem is split into smaller subproblems and patterns identified in Section 4.2 are essentially violations of these the solution to each subproblem is merged. Such algorithms are maxims. In the following paragraphs, we translate these efficiency particularly easy to parallelize when the subproblems act on dis- maxims into documented best-practices. junct parts of the problem dataset. It then suffices to split the prob- lem into subproblems, fork these computations in order to execute Choose Task Granularity Carefully.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    13 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us