Stclang: State Thread Composition As a Foundation for Monadic Dataflow Parallelism Sebastian Ertel∗ Justus Adam Norman A
Total Page:16
File Type:pdf, Size:1020Kb
STCLang: State Thread Composition as a Foundation for Monadic Dataflow Parallelism Sebastian Ertel∗ Justus Adam Norman A. Rink Dresden Research Lab Chair for Compiler Construction Chair for Compiler Construction Huawei Technologies Technische Universität Dresden Technische Universität Dresden Dresden, Germany Dresden, Germany Dresden, Germany [email protected] [email protected] [email protected] Andrés Goens Jeronimo Castrillon Chair for Compiler Construction Chair for Compiler Construction Technische Universität Dresden Technische Universität Dresden Dresden, Germany Dresden, Germany [email protected] [email protected] Abstract using monad-par and LVars to expose parallelism explicitly Dataflow execution models are used to build highly scalable and reach the same level of performance, showing that our parallel systems. A programming model that targets parallel programming model successfully extracts parallelism that dataflow execution must answer the following question: How is present in an algorithm. Further evaluation shows that can parallelism between two dependent nodes in a dataflow smap is expressive enough to implement parallel reductions graph be exploited? This is difficult when the dataflow lan- and our programming model resolves short-comings of the guage or programming model is implemented by a monad, stream-based programming model for current state-of-the- as is common in the functional community, since express- art big data processing systems. ing dependence between nodes by a monadic bind suggests CCS Concepts • Software and its engineering → Func- sequential execution. Even in monadic constructs that explic- tional languages. itly separate state from computation, problems arise due to the need to reason about opaquely defined state. Specifically, Keywords parallel programming, functional languages, par- when abstractions of the chosen programming model do not titioned state enable adequate reasoning about state, it is difficult to detect ACM Reference Format: parallelism between composed stateful computations. Sebastian Ertel, Justus Adam, Norman A. Rink, Andrés Goens, In this paper, we propose a programming model that en- and Jeronimo Castrillon. 2019. STCLang: State Thread Composition ables the composition of stateful computations and still ex- as a Foundation for Monadic Dataflow Parallelism. In Proceedings poses opportunities for parallelization. We also introduce of the 12th ACM SIGPLAN International Haskell Symposium (Haskell smap, a higher-order function that can exploit parallelism in ’19), August 22–23, 2019, Berlin, Germany. ACM, New York, NY, USA, 16 pages. https://doi.org/10.1145/3331545.3342600 stateful computations. We present an implementation of our programming model and smap in Haskell and show that basic 1 Introduction concepts from functional reactive programming can be built on top of our programming model with little effort. We com- Dataflow graphs are an abstraction for scalable concurrent pare these implementations to a state-of-the-art approach computations [12]. Although the concept has a strong func- tional foundation, it has received little attention in the func- ∗Work done while at TU Dresden. tional programming commmunity. Specifically, to the best of our knowledge, no proposal exists that combines the explicit Permission to make digital or hard copies of all or part of this work for concurrency of a dataflow graph with the simple and elegant personal or classroom use is granted without fee provided that copies monad type class. In fact, the sequential evaluation semantics are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights of monadic actions seem to prevent this combination. for components of this work owned by others than the author(s) must Dataflow is the de-facto standard for systems that achieve be honored. Abstracting with credit is permitted. To copy otherwise, or scalability by exploiting parallelism. It is the foundation on republish, to post on servers or to redistribute to lists, requires prior specific which data management systems implement highly parallel permission and/or a fee. Request permissions from [email protected]. execution engines. So-called Extract-Transform-Load (ETL) Haskell ’19, August 22–23, 2019, Berlin, Germany tools bring data into a data warehouse [10]. Query execution © 2019 Copyright held by the owner/author(s). Publication rights licensed to ACM. over that data is dataflow-based18 [ ], and the engines that in- ACM ISBN 978-1-4503-6813-1/19/08...$15.00 tegrate data that continuously arrives in the form of streams https://doi.org/10.1145/3331545.3342600 build a dataflow graph pipeline [6, 44]. Similar streaming Haskell ’19, August 22–23, 2019, Berlin, Germany Sebastian Ertel, Justus Adam, Norman A. Rink, Andrés Goens, and Jeronimo Castrillon signals (>>=) :: Monad m => m a -> (a -> m b) -> m b ewma αhigh alarm (a) Monadic Composition smap_ / > threshold if’ dn rn−1 f g ewma αlow () (b) Dataflow Composition Figure 2. Dataflow Graph of the earthquake detection pro- Figure 1. The sequential nature of the monadic composition gram. and the concurrency in a pipeline of a dataflow graph. only on data retrieved via I/O operations but also for data pipelines and dataflow graphs underpin signal processing that resides in memory, e.g., a normal list. The main contri- STCLang and programs for embedded systems [27, 36]. They are also bution of in Section3 is a higher-order function used to build scalable server architectures [15, 42], for fine- called smap that is similar to the well-known map function. It grained parallelism in big data systems [13], for optimizing supports stateful computations, but unlike the state monad’s smap I/O in micro services [16], and concurrent UIs [3,9]. mapM, introduces concurrency into the computation of smap Studying dataflow in the monadic context is interesting the result. Our construction introduces pipeline paral- both from a theoretical and a practical perspective. On the lelism for stateful computations and exploits data parallelism one hand, the dataflow abstraction is so close to functional for stateless nodes in the graph. Afterwards, we show that STCLang programming that many optimizations for functional lan- can support the FRP abstractions (Section4). STCLang guages may directly apply to dataflow [1]. For example, fu- In Section5, we evaluate our implementation sion is studied in the context of both functional languages against other Haskell libraries for parallelism, in particu- monad-par LVars and dataflow systems [17, 20]. On the other hand, a monadic lar and its extension to . We show that STCLang composition of the dataflow graph offers improvement over does not incur additional overheads and that its per- monad-par its manual construction. Manually constructing a dataflow formance on standard benchmarks is on par with LVars STCLang graph does not scale well to larger applications, which is why and . We show that programs are just as easy monad-par many established dataflow approaches provide a GUI rather to write as programs but enhance the program- than a language [22]. Implementing dataflow as a monad has ming model with implicit parallelism, pipeline parallelism smap the benefit of integrating nicely with the rest of the program, and state. In combination with state threads, our func- for example with the monad transformers framework. tion is even expressive enough to implement parallel reduc- Monadic composition and concurrency seem to be at odds tions. In our evaluation, we also show that the transforma- with each other. Consider the monadic composition, i.e., tions in the GHC harmonize with our programming model bind, as defined in Figure 1a. An implementation has to apply and even find data parallelism inside state threads. Asafi- the function passed in as a second parameter to the result of nal contribution, we ported a state-of-the-art benchmark STCLang the first parameter. In Figure 1b, we show the corresponding for big data processing engines to Haskell/ . The re- STCLang composition of the two nodes in a dataflow graph. An arc sulting code removes short-comings of the current facilitates the data dependency and denotes data flowing stream-based programming model used in these systems. We also report preliminary results that identify weaknesses of from node f to node g. In this pipeline, applications f dn monad-par’s scheduler and open up new research opportu- and g rn−1 are independent and, as such, concurrent. Hughes noticed this problem as well and suggested that nities for the functional community. In Section6 we review stream processors be implemented using arrows, a gener- related work, and we conclude in Section7. alization of monads [21]. Arrows became the dominating abstraction for composing dataflow computations in the con- 2 Intuition and Motivation text of functional reactive programming (FRP) [8]. However, We now motivate and introduce our programming model. we believe monads to be simpler and better suited to com- posing functionality than arrows in most cases. 2.1 Use Case: Earthquake Detection In this paper, we propose STCLang (State Thread Composi- To motivate STCLang, we study an application for earthquake tion Language), a monad for dataflow computations that en- detection that served as a motivation for Flask, a language ables a parallel execution at runtime, without targeting FRP for programming sensor networks [29]. Figure2