Docsity

Automatic Future-Based Parallelism in Intrepyyd, Essays (university) of Compiler Design

Partial preview of the text

Download Automatic Future-Based Parallelism in Intrepyyd and more Essays (university) Compiler Design in PDF only on Docsity!

Automatic Future-Based Parallelism in

Intrepyyd

Matthew Sklar

Georgia Institute of Technology

School of Computer Science

Habanero Lab

September 2020

1 Abstract

Hardware requirements are reaching record highs, but in the modern post-Moore computing world hardware improvements are decelerating. With fields such as Artificial Intelligence (AI) and data analysis requiring increasing code perfor- mance, new approaches must be taken to meet their needs. Instead of improv- ing hardware, a new approach that is efficient for aiding AI and data analysis is adding more hardware for programs to run on, instead of better hardware. Adding more hardware has the advantage of allowing independent processes within the same program to be run in parallel with each other on different hardware in a process known as parallelization. Parallelization can greatly in- crease program efficiency, and is becoming essential for modern programs. In this paper we propose a method to automatically parallelize non-parallel code, and provide an implementation of it in Intrepyyd, a Python extension designed specifically to improve AI and data analysis code. Improvements will depend on code parallelibility and computing systems, but we expect it to always improve unless code cannot be parallelized.

2 Introduction

Intrepydd is a programming system for Python designed to run code faster and with improved performance. One important feature that Intrepydd’s compiler does not contain is automated parallelism. Parallelism is the process by which independent processes are run at the same time on different threads. This reduces the amount of time that processes need to wait by removing the bottle- neck caused by waiting for processors. Currently in many languages, including Intrepyyd, programmers must manually specify what code to run in parallel, which is time consuming and prone to error or suboptimality. In some situa- tions, it is always beneficial to run specific code in parallel, and compilers can have phases that automatically detect code like this and synthesize code to run it as such. Computer improvements are slowing down, but computing requirements are reaching an all time high. With the massive loads and crucial, yet ever-growing, importance of big data analysis and machine learning in the modern world, soft- ware architecture must make up for the slow down of hardware improvements. Big data analysis and machine learning both have massive runtime improve- ments when parallelism is used due to their high reliance on linear algebra, which is inefficient to compute sequentially, but parallelism can benefit almost any program to some extent. As such, designing a system to automatically parallelize Python code can pave the way to faster programs of all sorts, and developing it into the Intrepydd compiler will give developers an implementation to use this in. I designed and implemented a system to automatically synthesize parallelism using an approach based off of research done by Sarkar and Surendran [5] on automated future-based synthesis in Java which works by holding results from

methods contain code that benefits from being parallelized. Reps, Thomas, et al’s [1] research on precise inter-procedural dataflow anal- ysis (IFDS) proposes a framework to determine code reachability. It defines the Tabulation Algorithm, which generates Control Flow Graphs (CFGs) for all methods and combines them into a ”super CFG” where each call site of a method points to that method’s CFG. The algorithm analyzes the ”super CFG” to determine if there exists any paths from a given start node to a given end node. Sarkar and Surendran’s [5] research uses the IFDS framework to compute the meet-over-all-valid paths set in order to compute the may-be-future and must-be-future sets for each variable. The algorithm they propose accomplishes this by defining new data-flow functions to use when analyzing the ”super CFG”. The may-be-future and must-be-future sets can then be used to determine which pure methods contain references to parallelizable code. Swaine, James, et al’s [2] research demonstrates that futures can be used to parallelize code. Parallel processes can be run as futures on different threads while code that does not require the result continues sequentially on the original thread. Furthermore, if a process can be run on a future, it can be run in parallel. Not all tasks that can be run in parallel should be. Duran, Alejandro’s [8] study shows that scheduling too many tasks on different threads can overwhelm and waste hardware, and parallelizing small tasks may require more overhead than the tasks take to run, slowing down the program as a whole. Thus, there needs to be some metric to determine if a task that can be parallelized should be. Coppa, Emilio, et al’s [4] research shows that runtime tends to positively scale with input data. This means that input data can be a valid correlated input to the metric. Sarkar and Surendran’s [5] research introduces a method using weighted computation graphs to classify each parallelizable pure method as sequential, parallel, or conditional parallel. Sequential methods will always be run sequentially, and parallel methods will always be run in parallel. Con- ditional parallel methods depend on the state of the runtime environment. For each conditional parallel method, Spearman’s correlation coefficient is used to determine how much each element in the input data relates to runtime. This is then used to generate a threshold function on the input data that can be quickly run to estimate if, in the current runtime environment, a conditional parallel method should be run sequentially or in parallel. The final step is to synthesize the program to handle and run parallel code. In Sarkar and Surendran’s [5] research, they propose a Final Future Synthesis step, which generates conditional statements using threshold expressions at call sites for conditional parallel pure methods, annotates pure methods as asyn- cronous expressions, clones input data to ensure values are accurate throughout execution, and finally synthesizes future calls at call sites for pure methods. Once this is done, the program can be run with the same result but often times much better performance. Ray [7] is a runtime distributed system designed to be an improved runtime environment for AI applications. Through method annotations, methods can be assigned worker nodes to run their tasks as seperate threads. Through Ray’s Python API, futures in Intrepyyd code can be run on these nodes in parallel,

and as such, Ray is a great distributed system to base the synthesis algorithms for parallelising Intrepyyd code around. With AI being such a major application for data analysis, Intrepyyd must be catered toward AI development. Currently, Python does not automatically parallelize code, which is an useful feature in optimizing AI programs, so imple- menting it will be a useful addition to Intrepyyd. Previous work on automated future-based parallel synthesis can be used as a framework for implementing this on any distributed system. Ray is a great choice for a distributed system, with it’s runtime environment customized for AI applications, and it’s simplicity will allow for easy synthesis.

4 Approach

The compiler phase is built on top of the python AST module. First, it visits all function definitions, call sites, and variable assignments. It makes a list of all functions tagged as pure, and stores each call site to one of these pure methods. It also stores every variable that has an assignment. In the next stage, it replaces the ”@pure method” tag manually coded above each pure method with Ray’s ”ray.remote” tag, and synthesizes a ”.remote” to each call site that calls a pure method. Ray remote objects return pointers to where the result will be stored upon completion. To obtain the actual values for when they are needed, the compiler assumes that each variable usage might be a future stored as a Ray remote object. The compiler synthesizes a method that checks if its input is a remote object, and returns the returned value if it is or the input as is if it is not. Then, the compiler wraps every variable usage with this function, in order to ensure that we always have variable values when needed. Finally, the compiler synthesizes the required import and setup code to the beginning of the program.

5 Methodology

The ultimate goal of this research was to design and implement a compiler phase that takes as input a program and outputs an equivalent but parallel program. The first step in this process was to predict what types of projects will use this. The compiler phase will be nested in the Python based Intrepyyd compiler, which is designed for assisting in AI and data analysis programs, so in order to meet the goals of the parent program we focused on performance in AI and data analysis projects. We implemented this in Python since Python is the primary language used by the audience. According to research done by Sarkar and Surendran [5], pure methods can always be parallelized. It was decided that the first step to designing the com- piler phase would be designing a system to mark pure methods. Furthermore, it is important to always check if it is beneficial to parallelize a pure method at a specific call site by estimating if more time is saved by parallelizing it at a

import numpy as np

@pure method def my func ( x , alpha , bet a ) : y = np. m u l t i p l y ( np. add ( x , alpha ) , bet a ) return y

i f n a m e == ” m a i n ” : a = np. arange ( 0 , 3 0 , dtype=np. f l o a t 6 4 ). r e s h a p e ( 5 , 6 ) b = my func ( a , 1. 0 , 0. 5 ) c = my func ( a , 1. 0 , 3. 4 ) print ( b + c ) Listing 1: Sequential Python code before compiler phase

import ray ray. i n i t ( )

def get may remote ( var ) : i f i s i ns t a n c e ( var , ray. r a y l e t. ObjectID ) : return ray. g e t ( var ) return var import numpy as np

@ray. remote ( n u m r e t u r n v a l s =1) def my func ( x , alpha , bet a ) : y = np. m u l t i p l y ( np. add ( x , alpha ) , bet a ) return y i f ( n a m e == ’ m a i n ’ ) : a = np. arange ( 0 , 3 0 , dtype=np. f l o a t 6 4 ). r e s h a p e ( 5 , 6 ) b = my func. remote ( a , 1. 0 , 0. 5 ) c = my func. remote ( a , 1. 0 , 3. 4 ) print ( ( get may remote ( b ) + get may remote ( c ) ) ) Listing 2: Parallel Python code after compiler phase

Figure 1: Python code and the equivalent parallelized code synthesized by the compiler phase

must add to their code. The generated compiled program transforms the pure method into a ray.remote object, which can be run through the ray system on a seperate thread as a future. Ray is imported and initialized, and the get may remote method is synthesized, which takes a variable and gets the value from the future if it is a ray remote object (future), or returns itself if it is not since then it holds its value already. Every call site of a pure method is

changed to call the method as a ray remote object on another thread, and every time a variable’s value is needed, it wraps itself in the get may remote method to check if it needs to get its value from a remote object.

6.2 Execution

Runtime improvements were measured by creating 2 sequential benchmark pro- grams and comparing their sequential runtime with the runtime of the compiled program. Originally, each compiled program ran out of memory with large in- puts. The cause of this was that every remote object call is run in a different Ray worker, which without limitation leads to large memory usage. In the fu- ture we plan on implementing a check that monitors the system and limits Ray worker creation based on memory. The 2 benchmark programs we used were quicksort and divide and conquer matrix multiplication (matmul). These were chosen because they are divide and conquer algorithms, which greatly benefit from runtime parallelization. We needed a temporary solution to the memory overflow, so for quicksort we man- ually altered the compiled code to run small tasks sequentially in order to min- imize worker memory usage. This simulates cost analysis which is future work for this project and will fix this issue. For matmul we reduced the input size such that execution completes before memory depletes. Quicksort was run on a list with 500000 elements, where execution becomes sequential when the sublists contain 100000 or fewer elements. Matmul was run between two 8x8 matrices. Table 1 shows the average runtime results.

quicksort matmul Sequential 11.49 0. Parallel 6.47 4.

Table 1: Average sequential and parallel runtime (in seconds) of benchmark programs.

These results show a 44% runtime improvement for the compiled version of quicksort, however, matmul took 140 times longer to run. The quicksort improvements stem from the fact that we simulated code analysis by running small inputs sequentially. The overhead for setting up a Ray worker for a small entry takes longer than running the entry, so parallelizing these increases runtime. In the future, code analysis will automatically prevent parallelization that slows runtime, but when we manually simulate it we see impressive results. This is expressive of true performance since the final product will automatically add what we manually added in this test. Matmul ran much slower, taking 140 times longer to run. This is because matmul has many small inputs and we parallelize all of them without simulating cost analysis. This shows the necessity of cast analysis in the future. These results show that, with cost analysis, the compiler phase improves code

References

[1] Thomas Reps, Susan Horwitz, and Mooly Sagiv. “Precise interprocedu- ral dataflow analysis via graph reachability”. In: Precise interprocedural dataflow analysis via graph reachability — Proceedings of the 22nd ACM SIGPLAN-SIGACT symposium on Principles of programming languages (Jan. 1995). url: https://dl.acm.org/doi/10.1145/199448.199462.

[2] James Swaine Northwestern University et al. “Back to the futures: incre- mental parallelization of existing sequential runtime systems”. In: ACM SIGPLAN Notices (Oct. 2010). url: https://dl.acm.org/doi/abs/10. 1145/1932682.1869507.

[3] Wei Huang et al. “Reim amp; ReImInfer: checking and inference of reference immutability and method purity”. In: ACM SIGPLAN Notices (Oct. 2012). url: https://dl.acm.org/doi/10.1145/2398857.2384680.

[4] Emilio Coppa et al. “Estimating the Empirical Cost Function of Routines with Dynamic Workloads”. In: Estimating the Empirical Cost Function of Routines with Dynamic Workloads — Proceedings of Annual IEEE/ACM International Symposium on Code Generation and Optimization (Feb. 2014). url: https://dl.acm.org/doi/10.1145/2581122.2544143.

[5] Vivek Sarkar and Rishi Surendran. “Automatic parallelization of pure method calls via conditional future synthesis”. In: Automatic parallelization of pure method calls via conditional future synthesis — Proceedings of the 2016 ACM SIGPLAN International Conference on Object-Oriented Program- ming, Systems, Languages, and Applications (Oct. 2016). url: https : //dl.acm.org/doi/10.1145/2983990.2984035.

[6] Mitchell M Waldrop. “The chips are down for Moore’s law”. In: Nature News (2016). url: https://www.nature.com/news/the- chips- are- down-for-moore-s-law-1.19338.

[7] Philipp Moritz et al. “Ray: A Distributed Framework for Emerging AI Applications”. In: arXiv.org (Sept. 2018). url: https://arxiv.org/abs/ 1712.05889.

[8] Alejandro Duran. “An adaptive cut-off for task parallelism”. In: Academia.edu (June 2020). url: https://www.academia.edu/9708576/An_adaptive_ cut-off_for_task_parallelism.