A Data-Parallel Algorithmic Modelica Extension for Efficient Execution on Multi-Core Platforms

Mahder Gebremedhin, Afshin Hemmati Moghadam, Peter Fritzson, Kristian Stavåker Department of Computer and Information Science Linköping University, SE-581 83 Linköping, Sweden {mahder.gebremedin, peter.fritzson, Kristian.stavaker}@liu.se, [email protected]

Abstract compilation of purely equation-based Modelica models for simulation on NVIDIA Graphic Processing Units New multi-core CPU and GPU architectures promise (GPUs). Several parallel architectures have been target- high computational power at a low cost if suitable ed, such as standard Intel multi-core CPUs, IBM Cell computational algorithms can be developed. However, B.E, and NVIDIA GPUs. All the implementation work parallel programming for such architectures is usually has been done in the OpenModelica compiler frame- non-portable, low-level and error-prone. To make the work [2], which is an open-source implementation of a computational power of new multi-core architectures Modelica compiler, simulator, and development envi- more easily available to Modelica modelers, we have ronment. Related research on parallel numeric solvers developed the ParModelica algorithmic language ex- can for example be found in [9]. tension to the high-level Modelica modeling language, The work presented in this paper presents an algo- together with a prototype implementation in the rithmic Modelica language extension called ParModeli- OpenModelica framework. This enables the Modelica ca for efficient portable explicit parallel Modelica pro- modeler to express parallel algorithms directly at the gramming. Portability is achieved based on the Modelica language level. The generated code is porta- OpenCL [14] standard which is available on several ble between several multi-core architectures since it is multi-core architectures. ParModelica is evaluated us- based on the OpenCL programming model. The im- ing a benchmark test suite called Modelica PARallel plementation has been evaluated on a benchmark suite benchmark suite (MPAR) which makes use of these containing models with matrix multiplication, Eigen language extensions and includes models which repre- value computation, and stationary heat conduction. sent heavy computations. Good speedups were obtained for large problem sizes This paper is organized as follows. Section 2 gives a on both multi-core CPUs and GPUs. To our general introduction to Modelica simulation on parallel knowledge, this is the first high-performing portable architectures. Section 3 gives an overview of GPUs, explicit parallel programming extension to Modelica. CUDA and OpenCL, whereas the new parallel Modeli- ca language extensions are presented in Section 4. Sec- Keywords: Parallel, Simulation, Benchmarking, tion 5 briefly describes measurements using the parallel Modelica, Compiler, GPU, OpenCL, Multi-Core benchmark test suite. Finally, Section 6 gives pro- gramming guidelines to use ParModelica, and Section 7 presents conclusions and future work. 1 Introduction Models of large industrial systems are becoming in- 2 Parallel Simulation of Modelica creasingly complex, causing long computation time for simulation. This makes is attractive to investigate Models on Multi-Core Computers methods to use modern multi-core architectures to The of compiling and simulating Modelica computations. models to sequential code is described e.g. in [3] and Efficient parallel execution of Modelica models has [12]. The handling of equations is rather complex and been a research goal of our group for a long time [4], involves symbolic index reduction, topological sorting [5], [6], [7], involving improvements both in the com- according to the causal dependencies between the equa- pilation process and in the run-time system for parallel tions, conversion into assignment statement form, etc. execution. Our previous work on compilation of data- Simulation corresponds to "solving" the compiled parallel models, [7] and [8], has primarily addressed

DOI Proceedings of the 9th International Modelica Conference 393 10.3384/ecp12076393 September 3-5, 2012, Munich, Germany A Data-Parallel Algorithmic Modelica Extension for Efficient Execution on Multi-Core …

equation system with respect to time using a numerical In Section 3.1 the NVIDIA GPU with its CUDA integration method. programming model is presented as an influential ex- Compiling Modelica models for efficient parallel ample of GPU architecture, followed by the portable simulation on multi-core architectures requires addi- OpenCL parallel programming model in Section 3.2. tional methods compared to the typical approaches de- scribed in [3] and [12]. The parallel methods can be 3.1 NVIDIA GPU CUDA – Compute Unified roughly divided into the following three groups: Device Architecture  Automatic parallelization of Modelica models. Sev- An important concept in NVIDIA CUDA (Computer eral approaches have been investigated: centralized Unified Device Architecture) for GPU programming is solver approach, distributed solver approach and the distinction between host and device. The host is compilation of unexpanded array equations. With what executes normal programs, and the device works the first approach the solver is run on one core and as a coprocessor to the host which runs CUDA threads in each time-step the computation of the equation by instruction from the host. This typically means that a system is done in parallel over several cores [4]. In CPU is the host and a GPU is the device, but it is also the second approach the solver and the equation sys- possible to debug CUDA programs by using the CPU tem are distributed across several cores [5]. With as both host and device. The host and the device are the third approach Modelica models with array assumed to have their own separate address spaces, the equations are compiled unexpanded and simulated host memory and the device memory. The host can use on multi-core architectures. the CUDA runtime API to control the device, for ex-  Coarse-grained explicit parallelization using com- ample to allocate memory on the device and to transfer ponents. Components of the model are simulated in memory to and from the device. parallel partly de-coupled using time delays be- tween the different components, see [11] for a summary. A different solver, with different time step, etc., can be used for each component. A relat- ed approach has been used in the xMOD tool [26].  Explicit parallel programming language constructs. This approach is explored in the NestStepModelica prototype [10] and in this paper with the ParModeli- ca language extension. Parallel extensions have been developed for other languages, e.g. parfor loop and gpu arrays in Matlab, Visual C++ parallel_for, Mathematica parallelDo, etc. Figure 1. Simplified schematic of NVIDIA GPU architecture, consisting of a set of Streaming Multiprocessors (SM), each containing a number of Scalar 3 GPU Architectures, CUDA, and Processors (SP) with fast private memory and on-ship OpenCL local . The GPU also has off-chip DRAM. Graphics Processing Units (GPUs) have recently be- The building block of the NVIDIA CUDA hardware come increasingly programmable and applicable to architecture is the Streaming Multiprocessor (SM). In general purpose numeric computing. The theoretical the NVIDIA Fermi-Tesla M2050 GPU, each SM con- processing power of GPUs has in recent years far sur- tains 32 Scalar Processors (SPs). The entire GPU has passed that of CPUs due to the highly parallel compu- 14 such SMs totaling to 448 SPs, as well as some off- ting approach of GPUs. chip DRAM memory, see Figure 1. This gives a scala- However, to get good performance, GPU architec- ble architecture where the performance of the GPU can tures should be used for simulation of models of a regu- be varied by having more or fewer SMs. lar structure with large numbers of similar data objects. To be able to take advantage of this architecture a The computations related to each data object can then program meant to run on the GPU, known as a kernel, be executed in parallel, one or more data objects on needs to be massively multi-threaded. A kernel is just a each core, so-called data-. It is also C-function meant to execute on the GPU. When a ker- very important to use the GPU memory hierarchy ef- nel is executed on the GPU it is divided into fectively in order to get good performance. blocks, where each thread block contains an equal number of threads. These thread blocks are automati- cally distributed among the SMs, so a programmer

394 Proceedings of the 9th International Modelica Conference DOI September 3-5, 2012, Munich Germany 10.3384/ecp12076393 Session 3C: Language and Compilation Concepts I need not consider the number of SMs a certain GPU chip memory on modern graphics cards range from has. All threads execute one common instruction at a several hundred megabytes to few gigabytes. The time. If any threads take divergent execution paths, DRAM memory is much slower than the on-chip mem- then each of these paths will be executed separately, ories, and is also the only memory that is accessible to and the threads will then converge again when all paths the host CPU, e.g. through DMA transfers. To summa- have been executed. This means that some SPs will be rize: idle if the thread executions diverge. It is thus im-  Each scalar processor (SP) has a set of fast registers. portant that all threads agree on an execution path for (private memory) optimal performance. This architecture is similar to the Single Instruction,  Each streaming multiprocessor (SM) has a small lo- Multiple Data (SIMD) architecture that vector proces- cal shared memory (48KB on Tesla M2050 ) with sors use, and that most modern general-purpose CPUs relatively fast access. have limited capabilities for too. NVIDIA call this ar-  Each GPU device has a slower off-chip DRAM chitecture Single Instruction, Multiple Thread (SIMT) (2GB on Tesla M2050) which is accessible from all instead, the difference being that each thread can exe- streaming multiprocessors and externally e.g. from cute independently, although at the cost of reduced per- the CPU with DMA transfers. formance. It is also possible to regard each SM as a separate processor, which enables Multiple Instruc- 3.2 OpenCL – the Open Computing Language tions, Multiple Data (MIMD) parallelism. Using only OpenCL [14] is the first open, free parallel computing MIMD parallelism will not make it possible to take full standard for cross-platform parallel programming of advantage of a GPU’s power, since each SM is a SIMD modern processors including GPUs. The OpenCL pro- processor. To summarize: gramming language is based on C99 with some exten-  Streaming Multiprocessors (SM) can work with dif- sions for parallel execution management. By using ferent code, performing different operations with OpenCL it is possible to write parallel algorithms that entirely different data (MIMD execution, Multiple can be easily ported between multiple devices with Instruction Multiple Data). minimal or no changes to the source code. The OpenCL framework consists of the OpenCL  All Scalar processors (SP) in one streaming multi- programming language, API, libraries, and a runtime processor execute the same instruction at the same system to support software development. The frame- time but work on different data (SIMT/SIMD exe- work can be divided into a hierarchy of models: Plat- cution, Single Instruction Multiple Data). form Model, Memory model, Execution model, and 3.1.1 NVIDIA GPU Memory Hierarchy Programming model. As can be seen in Figure 1 there are several different types of memory in the CUDA hardware architecture. At the lowest level each SP has a set of registers, the number depending on the GPU’s capabilities. These registers are shared between all threads allocated to a SM, so the number of thread blocks that a SM can have active at the same time is limited by the register usage of each thread. Accessing a register typically requires no extra clock cycles per instruction, except for some special cases where delays may occur. Figure 2. OpenCL platform architecture. Besides the registers there is also the shared (local) The OpenCL platform architecture in Figure 2 is simi- memory, which is shared by all SPs in a SM. The lar to the NVIDIA CUDA architecture in Figure 1: shared memory is implemented as fast on-chip memory, and accessing the shared memory is generally  Compute device – Graphics Processing Unit (GPU) as fast as accessing a register. Since the shared memory  Compute unit – Streaming Multiprocessor (SM) is accessible to all threads in a block it allows the  Processing element – Scalar Processor (SP) threads to cooperate efficiently by giving them fast ac-  Work-item – thread cess to the same data.  Work-group – thread block Most of the GPU memory is off-chip Dynamic The memory hierarchy (Figure 3) is also very similar: Random Access Memory (DRAM). The amount of off-  Global memory – GPU off-chip DRAM memory

DOI Proceedings of the 9th International Modelica Conference 395 10.3384/ecp12076393 September 3-5, 2012, Munich, Germany A Data-Parallel Algorithmic Modelica Extension for Efficient Execution on Multi-Core …

 Constant memory – read-only cache of off-chip memory  Local memory – on-chip shared memory that can be accessed by threads in the same SM  Private memory – on-chip registers in the same

Figure 4. OpenCL execution model, work-groups depicted as groups of squares corresponding to work- items. Each work-group can be referred to by a unique ID, and each work-item by a unique local ID. The work-items in a given work-group execute concur- rently on the processing elements of a single compute unit as depicted in Figure 4. Several programming models can be mapped onto Figure 3. Memory hierarchy in the OpenCL memory model, closely related to typical GPU architectures such this execution model. OpenCL explicitly supports two as NVIDIA. of these models: primarily the data parallel program- ming model, but also the task parallel programming The memory regions can be accessed in the following model way:

Memory Regions Access to Memory Constant Memory All work-items in all work-groups 4 ParModelica: Extending Modelica Local Memory All work-items in a work-group for Explicit Algorithmic Parallel Private Memory Private to a work-item Programming Global Memory All work-items in all work-groups As mentioned in the introduction, the focus of the cur- 3.2.1 OpenCL Execution Model rent work is an extension (ParModelica) of the algo- rithmic subset of Modelica for efficient explicit parallel The execution of an OpenCL program consists of two programming on highly data-parallel SPMD (Single parts, the host program which executes on the host and Program Multiple Data) architectures. The current the parallel OpenCL program, i.e., a collection of ker- ParModelica implementation generates OpenCL [14] nels (also called kernel functions), which execute on code for parallel algorithms. OpenCL was selected in- the OpenCL device. The host program manages the stead of CUDA [15] because of its portability between execution of the OpenCL program. several multi-core platforms. Generating OpenCL code Kernels are executed simultaneously by all threads ensures that simulations can be run with parallel sup- specified for the kernel execution. The number and port on OpenCL enabled Graphics and Central Proces- mapping of threads to Computing Units of the OpenCL sor Units (GPUs and CPUs). This includes many multi- device is handled by the host program. core CPUs from [19] and Advanced Micro Devices Each thread executing an instance of a kernel is (AMD) [18] as well as a range of GPUs from NVIDIA called a work-item. Each thread or work item has [17] and AMD [18]. unique id to help identify it. Work items can have addi- As mentioned earlier most previous work regarding tional id fields depending on the arrangement specified parallel execution support in the OpenModelica com- by the host program. piler has been focused on automatic parallelization Work-items can be arranged into work-groups. Each where the burden of finding and analyzing parallelism work-group has a unique ID. Work-items are assigned has been put on the compiler. In this work, however, a unique local ID within a work-group so that a single we have decided to leave this responsibility to the end work-item can be uniquely identified by its global ID user programmer. The compiler provides additional or by a combination of its local ID and work-group ID. high level language constructs needed for explicitly stating parallelism in the algorithmic part of the model- ing language. These, among others, include parallel variables, parallel functions, kernel functions and paral-

396 Proceedings of the 9th International Modelica Conference DOI September 3-5, 2012, Munich Germany 10.3384/ecp12076393 Session 3C: Language and Compilation Concepts I lel for loops indicated by the parfor keyword. There are functions. These are functions available for distributed also some target language specific constructs and func- (parallel) independent execution in each thread execut- tions (in this case related to OpenCL). ing on the parallel device. For example, if a parallel array has been distributed with one element in each 4.1 Parallel Variables thread, a parallel function may operate locally in paral- OpenCL code can be executed on a host CPU as well lel on each element. However, unlike kernel functions, as on GPUs whereas CUDA code executes only on parallel functions cannot be called from serial code in GPUs. Since the OpenCL and CUDA enabled GPUs normal Modelica functions on the host computer just as use their own local (different from CPU) memory for parallel OpenCL functions are not allowed to be called execution, all necessary data should be copied to the from serial C code on the host. Parallel functions have specific device's memory. Parallel variables are allocat- the following constraints, primarily since they are as- ed on the specific device memory instead of the host sumed to be called within a parallel context in work- CPU. An example is shown below: items: function parvar  Parallel function bodies may not contain parfor- protected loops. The reason is that the kernel containing the Integer m = 1000; // Host Scalar parallel functions is already distributed on each Integer A[m,m]; // Host Matrix Integer B[m,m]; // Host Matrix thread. // global and local device memories  Explicitly declared parallel variables are not al- parglobal Integer pm; // Global Scalar lowed since execution is already taking place on the parglobal Integer pA[m,m];// Glob Matrix parallel device. parglobal Integer pB[m,m];// Glob Matrix parlocal Integer pn; // Local Scalar  All memory allocation will be on the parallel de- parlocal Integer pS[m]; // Local Array vice's memory. end parvar;  Nested parallelism as in NestStepModelica [10] is The first two matrices A and B are allocated in normal not supported by this implementation. host memory. The next two matrices pA and pB are  Called functions must be parallel functions or sup- allocated on the global memory space of the OpenCL ported built-in functions since execution is on the device to be used for execution. These global variables parallel device. can be initialized from normal or host variables. The  Parallel functions can only be called from the body last array pS is allocated in the local memory space of of a parfor-loop, from parallel functions, or from each processor on the OpenCL device. These variables kernel functions. are shared between threads in a single work-group and Parallel functions in ParModelica are defined in the cannot be initialized from hast variables. same way as normal Modelica functions, except that Copying of data between the host memory and the they are preceded by the parallel keyword as in the device memory used for parallel execution is as simple multiply function below: as assigning the variables to each other. The compiler parallel function multiply and the runtime system handle the details of the opera- input parglobal Integer a; tion. The assignments below are all valid in the func- input parlocal Integer b; tion given above output parprivate Integer c; // same as output Integer c;  Normal assignment - A := B algorithm  Copy from host memory to parallel execution de- c := a * b; vice memory - pA := A end multiply;  Copy from parallel execution device memory to host memory - B := pB 4.3 Kernel Functions  Copy from device memory to other device memory ParModelica kernel functions correspond to OpenCL – pA := pB kernel functions [14] or CUDA global functions [16]. Modelica parallel arrays are passed to functions on- They are simply functions compiled to execute on an ly by reference. This is done to reduce the rather expen- OpenCL parallel device, typically a GPU. ParModelica sive copy operations. kernel functions are allowed to have several return- or output variables unlike their OpenCL or CUDA coun- 4.2 Parallel Functions terparts. They can also allocate memory in the global ParModelica parallel functions correspond to OpenCL address space. Kernel functions can be called from se- functions defined in kernel files or to CUDA device rial host code, and are executed by each thread in the

DOI Proceedings of the 9th International Modelica Conference 397 10.3384/ecp12076393 September 3-5, 2012, Munich, Germany A Data-Parallel Algorithmic Modelica Extension for Efficient Execution on Multi-Core …

launch of the kernel. Kernels functions share the first ParModelica parallel for loops, compared to normal three constraints stated above for parallel functions. Modelica for loops, have some additional constraints: However, unlike parallel functions, kernel functions  All variable references in the loop body must be to cannot be called from the body of a parfor-loop or from parallel variables. other kernel functions.  Iterations should not be dependent on other itera- Kernel functions in ParModelica are defined in the tions i.e. no loop-carried dependencies. same way as normal Modelica functions, except that  All function calls in the body should be to parallel they are preceded by the kernel keyword. An example functions or supported built-in functions only. usage of kernel functions is shown by the kernel func- tion arrayElemtWiseMult. The thread id function 4.5 Executing User-written OpenCL Code oclGetGlobalId() (see Section 4.5) returns the integer from ParModelica. id of a work-item in the first dimension of a work There are also some additional ParModelica features group. available for directly compiling and executing user- kernel function arrayElemWiseMultiply written OpenCL code: input Integer m; input Integer A[m];  oclbuild(String) takes a name of an OpenCL source input Integer B[m]; file and builds it. It returns an OpenCL program output Integer C[m]; object which can be used later. protected Integer id;  oclkernel(oclprogram, String) takes a previously algorithm id := oclGetGlobalId(1); built OpenCL program and create the kernel speci- // calling the parallel function fied by the second argument. It returns an OpenCL multiply is OK from kernel functions kernel object which can be used later. C[id] := multiply(A[id],B[id]); // multiply can be replaced by A[id]*B[id]  oclsetargs(oclkernel,...) takes a previously created end arrayElemWiseMultiply; kernel object variable and a variable number of ar- guments and sets each argument to its correspond- 4.4 Parallel For Loop: parfor ing one in the kernel definition. The iterations of a ParModelica parfor-loop are execut-  oclexecute(oclkernel) executes the specified kernel. ed without any specific order in parallel and inde- pendently by multiple threads. The iterations of a par- All of the above operations are synchronous in the for-loop are equally distributed among available pro- OpenCL jargon. They will return only when the speci- cessing units. If the range of the iteration is smaller fied operation is completed. Further functionality is than or equal to the number of threads the parallel de- planned to be added to these functions to provide better vice supports, each iteration will be done by a separate control over execution. thread. If the number of iterations is larger than the number of threads available, some threads might per- 4.6 Synchronization and Thread Management form more than one iteration. In future enhancements All OpenCL work-item functions [20] are available in parfor will be given the extra feature of specifying the ParModelica. They perform the same operations and desired number of threads explicitly instead of auto- have the “same” types and number of arguments. How- matically launching threads as described above. An ever, there are two main differences: example of using the parfor-loop is shown below:  Thread/work-item index ids start from 1 in Par- // Matrix multiplication using parfor loop Modelica, whereas the OpenCL C implementation parfor i in 1:m loop for j in 1:pm loop counts from 0. ptemp := 0;  Array dimensions start from 1 in Modelica and for h in 1:pm loop // calling the from 0 in OpenCL and C. // parallel function multiply is OK // from parfor-loops For example oclGetGlobalId(1) call in the above ptemp := multiply(pA[i,h], pB[h,j]) arrayElemWiseMultiply will return the integer ID of + ptemp; end for; a work-item or thread in the first dimension of a work pC[i,j] := ptemp; group. The first thread gets an ID of 1. The OpenCL C end for; call for the same operation would be end parfor; ocl_get_global_id(0) with the first thread obtain- ing an ID of 0.

398 Proceedings of the 9th International Modelica Conference DOI September 3-5, 2012, Munich Germany 10.3384/ecp12076393 Session 3C: Language and Compilation Concepts I

In addition to the above features, special built-in brary. This function is called once when the simulation functions for building user written OpenCL code di- loop starts and once when the simulation loop finishes. rectly from source code, creating a kernel, setting ar- The difference between the returned values gives the guments to kernel and execution of kernels are also simulation time. available. In addition parallel versions of some built-in All benchmarks have been simulated on both the In- algorithm functions are also available. tel Xeon E5520 CPU (16 cores) and the NVIDIA Fer- mi-Tesla M2050 GPU (448 cores).

5 Benchmarking and Evaluation 5.3 Simulation Results To be able to evaluate the relative performance and The Matrix Multiplication model (Appendix A) pro- behavior of the new language extensions described in duces an M×K matrix C from multiplying an M×N ma- Section 4, performing systematic benchmarking on a trix A by an N×K matrix B. This model presents a very set of appropriate Modelica models is required. For this large level of data-parallelism for which a considerable purpose we have constructed a benchmark test suite speedup has been achieved as a result of parallel simu- containing some models that represent heavy and high- lation of this model on parallel platforms. The simula- performance computation, relevant for simulation on tion results are illustrated in Figure 5 and Figure 6. The parallel architectures. obtained speedup of matrix multiplication using kernel functions is as follows compared to the sequential algo- 5.1 The MPAR Benchmark Suite rithm on Intel Xeon E5520 CPU: The MPAR benchmark test suite contains seven differ-  Intel 16-core CPU – speedup 26 ent algorithms from well-known benchmark applica-  NVIDIA 448-core GPU – speedup 115 tions such as the LINear equations software PACKage (LINPACK) [21], and Heat Conduction [23]. These Speedup 114,67 benchmarks have been collected and implemented as CPU E5520 GPU M2050 algorithmic time-independent Modelica models. The algorithms implemented in this suite involve ra- ther large computations and impose well defined work- 35,95 loads on the OpenModelica compiler and the run-time 24,76 26,34 13,41 system. Moreover, they include different kinds of for- 4,36 0,61 4,61 loops and function calls which provide parallelism for 64 128 256 512 domain and task decomposition. For space reasons we Parameter M (Matrix sizes MxM) have provided results for only three models here. Figure 5. Speedup for matrix multiplication, Intel 16-core Time measurements have been performed of both CPU and Nvidia 448 core GPU. sequential and parallel implementations of three mod- els: Matrix Multiplication, Eigen value computation, The measured matrix multiplication model simulation and Stationary Heat Conduction, on both CPU and times can be found in Figure 6. GPU architectures. For executing sequential codes gen- erated by the standard sequential OpenModelica com- 512 piler we have used the Intel Xeon E5520 CPU [24] 256 128 which has 16 cores, each with 2.27 GHz clock frequen- 64 32 16 cy. For executing generated code by our new OpenCL 8 4 based parallel code generator, we have used the same 2 1 0,5 CPU as well as the NVIDIA Fermi-Tesla M2050 GPU 0,25 0,125 [25]. (second) Time Simulation 0,0625 32 64 128 256 512 CPU E5520 (Serial) 0,093 0,741 5,875 58,426 465,234 5.2 Measurements CPU E5520 (Parallel) 0,137 0,17 0,438 2,36 17,66 GPU M2050 (Parallel) 1,215 1,217 1,274 1,625 4,057 In this section we present the result of measurements for simulating three models from the implemented Figure 6. Simulation time for matrix multiplication, Intel 1-core, 16-core CPU, NVidia 448 core GPU. benchmark suite. On each hardware configuration all simulations are performed five times with start time The second benchmark model performs Eigen-value 0.0, stop time of 0.2 seconds and 0.2 seconds time step, computation, with the following speedups: measuring the average simulation time using the  Intel 16-core CPU – speedup 3 clock_gettime() function from the C standard li-

DOI Proceedings of the 9th International Modelica Conference 399 10.3384/ecp12076393 September 3-5, 2012, Munich, Germany A Data-Parallel Algorithmic Modelica Extension for Efficient Execution on Multi-Core …

 NVIDIA 448-core GPU – speedup 48 512 256 128 Speedup 64 32 47,71 16 CPU E5520 GPU M2050 8 33,25 4 Simulation Time (second) Time Simulation 2 1 0,5 102 204 16,95 128 256 512 4 8 6,68 CPU E5520 (Serial) 1,958 7,903 32,104 122,754 487,342 2,75 1,020,71 1,992,27 2,24 2,32 2,51 CPU E5520 (Parallel) 0,959 1,875 5,488 19,711 76,077 GPU M2050 (Parallel) 8,704 9,048 9,67 12,153 21,694 256 512 1024 2048 4096 8192 Array size Figure 10. Simulation time (seconds) for heat conduction model as a function of model size parameter M, for 1-core Figure 7. Speedup for Eigen value computation as a CPU, 16-core CPU, and 448 core GPU. function of model array size, for Intel 16-core CPU and NVIDIA 448 core GPU, compared to the sequential According to the results of our measurements illustrat- algorithm on Intel Xeon E5520 CPU. ed in Figure 5, Figure 7, and Figure 9, absolute The measured simulation times for the Eigen-value speedups of 114, 48, and 22 respectively were achieved model are shown in Figure 8. when running generated ParModelica OpenCL code on the Fermi-Tesla M2050 GPU compared to serial code 1024 on the Intel Xeon E5520 CPU with the largest data siz- 512 256 es. 128 64 It should be noted that when the problem size is not 32 very large the sequential execution has better perfor- 16 8 mance than the parallel execution. This is not surpris- 4 Simulation Time (second) Time Simulation 2 ing since for executing even a simple code on OpenCL 1 128 256 512 1024 2048 4096 8192 devices it is required to create an OpenCL context with- CPU E5520 (Serial) 1,543 5,116 16,7 52,462 147,411 363,114 574,057 in those devices, allocate OpenCL memory objects, CPU E5520 (Parallel) 3,049 5,034 8,385 23,413 63,419 144,747 208,789 GPU M2050 (Parallel) 7,188 7,176 7,373 7,853 8,695 10,922 12,032 transfer input data from host to those memory objects,

perform computations, and finally transfer back the Figure 8. Simulation time for Eigen-value computation as result to the host. Consequently, performing all these a function of model array size, for Intel 1-core CPU, 16- core CPU, and NVIDIA 448 core GPU. operations normally takes more time compared to the sequential execution when the problem size is small. The third benchmark model computes stationary heat It can also be seen that, as the sizes of the models conduction, with the following speedups: increase, the simulations get better relative performance  Intel 16-core CPU – speedup 7 on the GPU compared to multi-core CPU. Thus, to ful-  NVIDIA 448-core GPU – speedup 22 ly utilize the power of parallelism using GPUs it is re- quired to have large regular data structures which can Speedup be operated on simultaneously by being decomposed to 22,46 CPU E5520 GPU M2050 all blocks and threads available on GPU. Otherwise, executing parallel codes on a multi-core CPU would be 10,1 a better choice than a GPU to achieve more efficiency 5,85 6,23 6,41 4,21 3,32 and speedup. 2,04 0,22 0,87

128 256 512 1024 2048 Parameter M (Matrix size MxM) 6 Guidelines for Using the New Par- allel Language Constructs Figure 9. Speedup for the heat conduction model as a function of model size parameter M, Intel 16-core CPU The most important task in all approaches regarding and Nvidia 448 core GPU, compared to sequential parallel code generation is to provide an appropriate algorithm on Intel Xeon E5520 CPU. way for analyzing and finding parallelism in sequential The measured simulation times for the stationary heat codes. In automatic parallelization approaches, the conduction model are shown in Figure 10. whole burden of this task is on the compiler and tool developer. However, in explicit parallelization ap- proaches as in this paper, it is the responsibility of the modeler to analyze the source code and define which

400 Proceedings of the 9th International Modelica Conference DOI September 3-5, 2012, Munich Germany 10.3384/ecp12076393 Session 3C: Language and Compilation Concepts I parts of the code are more appropriate to be explicitly global size should be evenly divisible by the local parallelized. This requires a good understanding of the size. concepts of parallelism to avoid inefficient and incor-  The current implementation of OpenCL does not rect generated code. In addition, it is necessary to know support recursive functions; therefore it is not pos- the constraints and limitations involved with using ex- sible to declare a recursive function as a parallel plicit parallel language constructs to avoid compile function. time errors. Therefore we give some advice on how to use the ParModelica language extensions to parallelize Modelica models efficiently: 7 Conclusions  Try to declare parallel variables as well as copy as- New multi-core CPU and GPU architectures promise signments among normal and parallel variables as high computational power at a low cost if suitable less as possible since the costs of data transfers from computational algorithms can be developed. The host to devices and vice versa are very expensive. OpenCL C-based parallel programming model provides  In order to minimize the number of parallel varia- a way of writing portable parallel algorithms that per- bles as well as data transfers between host and de- form well on a number of multi-core architectures. vices, it is better not to convert forloops with few it- However, the OpenCL programming model is rather erations over simple operations to parallel for-loops low-level and error-prone to use and intended for paral- (parfor-loops). lel programming specialists.  It is not always useful to have parallel variables and This paper presents the ParModelica algorithmic parfor-loops in the body of a normal for-loop which language extension to the high-level Modelica model- has many iterations. Especially in cases where there ing language together with a prototype implementation are many copy assignments among normal and par- in the OpenModelica compiler. This makes it possible allel variables. for the Modelica modeler to directly write efficient par-  Although it is possible to declare parallel variables allel algorithms in Modelica which are automatically and also parfor-loops in a function, there are no ad- compiled to efficient low-level OpenCL code. A vantages when there are many calls to the function benchmark suite called MPAR has been developed to (especially in the body of a big for-loop). This will evaluate the prototype. Good speedups have been ob- increase the number of memory allocations for par- tained for large problem sizes of matrix multiplication, allel variables as well as the number of expensive Eigen value computation, and stationary heat condition. copies required to transfer data between host and Future work includes integration of the ParModelica devices. explicit parallel programming approach with automatic  Do not directly convert a for-loop to a parfor-loop and semi-automatic approaches for compilation of when the result of each iteration depends on other equation-based Modelica models to parallel code. Au- iterations. In this case, although the compiler will totuning could be applied to further increase the per- correctly generate parallel code for the loop, the re- formance and automatically adapt it to varying problem sult of the computation may be incorrect. configurations. Some of the ParModelica code needed  Use a parfor-loop in situations where the loop has to specify kernel functions could be automatically gen- many independent iterations and each iteration takes erated. a long time to be completed.  Try to parallelize models using kernel functions as 8 Acknowledgements much as possible rather than using parfor-loops. This will enable you to explicitly specify the desired This work has been supported by Serc, by Elliit, by the number of threads and work-groups to get the best Swedish Strategic Research Foundation in the EDOp performance. and HIPo projects and by Vinnova in the RTSIM and  If the global work size (total number of threads to ITEA2 OPENPROD projects. The Open Source Mod- be run in parallel) and the local work size (total elica Consortium supports the OpenModelica work. number of threads in each work-group) need to be Thanks to Per Östlund for contributions to Section 3.1. specified explicitly, then the following points should be considered. First, the work-group size References (local size) should not be zero, and also it should not exceed the maximum work-group size supported [1] Modelica Association. The Modelica Language Specification Version 3.2, March 24th 2010. by the parallel device. Second, the local size should http://www.modelica.org. Modelica Association. be less or equal than the global-size. Third, the

DOI Proceedings of the 9th International Modelica Conference 401 10.3384/ecp12076393 September 3-5, 2012, Munich, Germany A Data-Parallel Algorithmic Modelica Extension for Efficient Execution on Multi-Core …

Modelica Standard 3.1. Aug. 2009. [13] Khronos Group, Open Standards for Media Au- http://www.modelica.org./ thoring and Acceleration, OpenCL 1.1, accessed Sept 15, 2011. http://www.khronos.org/opencl/ [2] Open Source Modelica Consortium. OpenModel- ica System Documentation Version 1.8.1, April [14] The OpenCL Specication, Version: 1.1, Docu- 2012. http://www.openmodelica.org/ ment Revision: 44, accessed June 30 2011. http://www.khronos.org/registry/cl/specs/opencl- [3] Peter Fritzson. Principles of Object-Oriented 1.1.pdf Modeling and Simulation with Modelica 2.1. Wiley-IEEE Press, 2004. [15] NVIDIA CUDA, accessed September 15 2011. http://www.nvidia.com/object/cuda home [4] Peter Aronsson. Automatic Parallelization of new.html Equation-Based Simulation Programs, PhD the- sis, Dissertation No. 1022, Linköping University, [16] NVIDIA CUDA programming guide, accessed 30 2006. June 2011. http://developer.download.nvidia.com/ compute/cuda/4 0 rc2/toolkit/docs/CUDA C Pro- [5] Håkan Lundvall. Automatic Parallelization using gramming Guide.pdf Pipelining for Equation-Based Simulation Lan- guages, Licentiate thesis No. 1381, Linköping [17] OpenCL Programming Guide for the CUDA Ar- University, 2008. chitecture, Appendix A, accessed June 30 2011. http://developer.download.nvidia.com/compute/D [6] Håkan Lundvall, Kristian Stavåker, Peter evZone/docs/html/OpenCL/doc/OpenCL Pro- Fritzson, Christoph Kessler: Automatic Parallel- gramming Guide.pdf ization of Simulation Code for Equation-based Models with Software Pipelining and Measure- [18] AMD OpenCL, System Requirements & Driver ments on Three Platforms. MCC'08 Workshop, Compatibility, accessed June 30 2011. Ronneby, Sweden, November 27-28, 2008. http://developer.amd.com/sdks/AMDAPPSDK/pa ges/DriverCompatibility.aspx [7] Per Östlund. Simulation of Modelica Models on the CUDA Architecture. Master Thesis. LIU- [19] INTEL OpenCL, Technical Requirements, ac- IDA/LITH-EX-A{09/062{SE. Linköping Univer- cessed June 30 2011. sity, 2009. http://software.intel.com/enus/articles/opencl- release-notes/ [8] Kristian Stavåker, Peter Fritzson. Generation of Simulation Code from Equation-Based Models [20] OpenCL Work-Item Built-In Functions, accessed for Execution on CUDA-Enabled GPUs. MCC'10 June 30 2011. Workshop, Gothenburg, Sweden, November 18- http://www.khronos.org/registry/cl/sdk/1.0/docs/ 19, 2010. man/xhtml/workItemFunctions.html [9] Matthias Korch and Thomas Rauber. Scalable [21] Jack J. Dongarra, J. Bunch, Cleve Moler, and G. parallel rk solvers for odes derived by the method W. Stewart. LINPACK User's Guide. SIAM, of lines. In Harald Kosch, Laszlo Böszörményi, Philadelphia, PA, 1979. and Hermann Hellwagner, editors, Euro-Par, vol- [22] Ian N. Sneddon. Fourier Transforms. Dover Pub- ume 2790 of Lecture Notes in Computer Science, lications, 2010. ISBN-13: 978-0486685229. pages 830-839. Springer, 2003. [23] John H. Lienhard IV and John H. Lienhard V. A [10] Christoph Kessler and Peter Fritzson. NestStep- Heat Transfer Textbook. Phlogiston Press Cam- Modelica – Mathematical Modeling and Bulk- bridge, Massachusetts, U.S.A, 4th edition, 2011. Synchronous Parallel Simulation. In Proc. of PARA'06, Umeå, June 19-20, 2006. In Lecture [24] Intel Xeon E5520 CPU Specifications, accessed Notes of Computer Science (LNCS) Vol 4699, pp October 28 2011. 1006-1015, Springer Verlag, 2006. http://ark.intel.com/products/40200/Intel-Xeon- Processor-E5520-(8M-Cache-2 26-GHz-5 86- [11] Martin Sjölund, Robert Braun, Peter Fritzson and GTs-Intel-QPI) Petter Krus. Towards Efficient Distributed Simu- lation in Modelica using Transmission Line Mod- [25] NVIDIA Tesla M2050 GPU Specifications, ac- eling. In Proceedings of the 3rd International cessed June 30 2011. Workshop on Equation-Based Object-Oriented http://www.nvidia.com/docs/IO/43395/BD- Modeling Languages and Tools, (EOOLT'2010), 05238-001 v03.pdf Published by Linköping University Electronic [26] Cyril Faure. Real-time simulation of physical Press, www.ep.liu.se, In conjunction with MOD- models toward hardware-in-the-loop validation. ELS’2010, Oslo, Norway, Oct 3, 2010. PhD Thesis. University of Paris East, October [12] Francois Cellier and Ernesto Kofman. Continuous 2011. System Simulation. Springer, 2006.

402 Proceedings of the 9th International Modelica Conference DOI September 3-5, 2012, Munich Germany 10.3384/ecp12076393 Session 3C: Language and Compilation Concepts I

Appendix A. Serial Matrix Multiply Appendix B. Parallel Matrix-Matrix

model MatrixMultiplication Multiplication with parfor and Kernel parameter Integer m=256 ,n=256 ,k =256; functions Real result ; algorithm model MatrixMultiplicationP result := mainF (m,n,k); parameter Integer m=32,n=32,k=32; MatrixMultiplication ; end Real result; algorithm mainF function result := mainF(m,n,k); Integer m; input end MatrixMultiplicationP ; Integer n; input Integer k; input function mainF Real result ; output input Integer m;

protected input Integer n; Real A[m,n]; input Integer k; Real B[n,k]; output Real result ; Real C[m,k]; protected algorithm Real C[m,k]; // initialize matrix A, and B parglobal Real pA[m,n]; (A,B) := initialize (m,n,k); parglobal Real pB[n,k]; // multiply matrices A and B parglobal Real pC[m,k]; C := matrixMultiply (m,n,k,A,B); parglobal Integer pm; // only one item is returned to speed up parglobal Integer pn; // computation parglobal Integer pk; result := C[m,k]; // the total number of global threads mainF; end // executing in parallel in the kernel Integer globalSize [2] = {m,k}; initialize function // the total number of local threads Integer m; input // in parallel in each workgroup Integer n; input Integer localSize [2] = {16 ,16}; Integer k; input algorithm Real A[m,n]; output // copy from host to device Real B[n,k]; output pm := m; algorithm pn := n; i 1:m for in loop pk := k; j 1:n for in loop (pA ,pB) := initialize(m,n,k,pn ,pk); A[i,j] := j; ; end for // specify the number of threads and ; end for // workgroups j 1:n for in loop // to be used for a kernel function h 1:k for in loop // execution B[j,h] := h; oclSetNumThreads(globalSize, localSize); ; end for pC := matrixMultiply(pn ,pA ,pB ); ; end for initialize ; end // copy matrix from device to host // and resturn result matrixMultiply function C := pC; Integer m; input result := C[m,k]; Integer p; input Integer n; input // set the number of threads to Real A[m,p]; input // the available number Real B[p,n]; input // supported by device Real C[m,n]; output oclSetNumThreads(0); Real localtmp ; end mainF ; algorithm i 1:m for in loop j 1:n for in loop function initialize localtmp := 0; input Integer m; k 1:p for in loop input Integer n; localtmp := localtmp +(A[i,k]* input Integer k; B[k,j]); input parglobal Integer pn; ; end for input parglobal Integer pk; C[i,j] := localtmp ; output parglobal Real pA[m,n]; ; end for output parglobal Real pB[n,k]; ; end for algorithm end matrixMultiply;

DOI Proceedings of the 9th International Modelica Conference 403 10.3384/ecp12076393 September 3-5, 2012, Munich, Germany A Data-Parallel Algorithmic Modelica Extension for Efficient Execution on Multi-Core …

parfor i in 1:m loop output parglobal Real pC[size(pA,1), for j in 1: pn loop size(pB,2)]; pA[i,j] := j; protected end for; Real plocaltmp ; end parfor; Integer i,j; parfor j in 1:n loop algorithm for h in 1: pk loop // Returns unique global thread Id value pB[j,h] := h; // for first and second dimension end for; i := oclGetGlobalId (1); end parfor ; j := oclGetGlobalId (2); end initialize ; plocaltmp := 0; for h in 1: pn loop parkernel function matrixmultiply plocaltmp := plocaltmp + (pA[i,h] * input parglobal Integer pn; pB[h,j]); input parglobal Real pA [: ,:]; end for; input parglobal Real pB [: ,:]; pC[i,j] := plocaltmp; end matrixmultiply;

404 Proceedings of the 9th International Modelica Conference DOI September 3-5, 2012, Munich Germany 10.3384/ecp12076393