scieee AI-readable full text Open interactive document viewer

Code modernization strategies to 3-D Stencil-based applications on Intel Xeon Phi: KNC and KNL

Cebrián, Juan M.,Cecilia, José M.,Hernández, Mario,García, José M.

Abstract

Partial Differential Equations (PDEs) are widely used to simulate many scenarios in science and engineering, usually solved through iterative techniques (e.g., Jacobi, Gauss–Seidel). These methods produce an approximate solution to the problem based on Stencil patterns of computation. The complexity, granularity and dimensionality of the problem require of substantial computational resources that are not affordable by regular CPU-based architectures. Emerging massively data-parallel architectures, such as Intel Xeon Phi, offer a great opportunity to address challenging problems based on PDEs. However, the code migration to these architectures is not straight-forward. To achieve this code modernization programming cycle, it is mandatory to identify the key issues in the code that will determine performance in future hardware evolutions. In this paper we look for (1) scalability with core count, (2) data-parallelism exposure to explore vectorization capabilities, and (3) data-locality aware techniques. These techniques lead a performance gain of up to 15x for the first generation of Xeon Phi: Knights Corner (KNC), and an additional average 2.5x improvement for Knights Landing (KNL).

Full text

Code Modernization Strategies to 3-D Stencil-based applications on Intel Xeon Phi: KNC and KNL Juan M. Cebriána, José M. Ceciliab, Mario Hernándezc,∗, José M. Garcíad aBarcelona Supercomputing Center (BSC), 08034-Barcelona, Spain bComputer Science Department, Universidad Católica de Murcia (UCAM), Spain cFacultad de Ingeniería, Universidad Autónoma de Guerrero, Chilpancingo, Gro., México dDept. of Computer Engineering, University of Murcia, 30100, Murcia, Spain Abstract Partial Differential Equations (PDEs) are widely used to simulate many scenarios in science and engineering, usually solved through iterative techniques (e.g., Jacobi, Gauss-Seidel). These methods produce an approximate solution to the problem based on Stencil patterns of computation. The complexity, granularity and dimensionality of the problem require of substantial computational resources that are not affordable by regular CPU-based architectures. Emerging massively data-parallel architectures, such as Intel Xeon Phi, offer a great opportunity to address challenging problems based on PDEs. However, the code migration to these architectures is not straight-forward. To achieve this code modernization programming cycle, it is mandatory to identify the key issues in the code that will determine performance in future hardware evolutions. In this paper we look for (1) scalability with core count, (2) data-parallelism exposure to explore vectorization capabilities, and (3) data-locality aware techniques. These techniques lead a performance gains of up to 15x for the first generation of Xeon Phi: Knights Corner (KNC), and an additional average 2.5x improvement for Knights Landing (KNL). Keywords: Code Modernization, PDEs, Stencil, Xeon Phi, KNC, KNL. ∗Corresponding author Email addresses: [email protected] (Juan M. Cebrián), [email protected] (José M. Cecilia), [email protected] (Mario Hernández), [email protected] (José M. García) Preprint submitted to Computers & Mathematics with Applications June 16, 2017 © 2017. This manuscript version is made available under the CC-BY-NC-ND 4.0 license http://creativecommons.org/licenses/by-nc-nd/4.0/ 1. Introduction The computational resources required in scientific research for key areas of interest, such as medicine, physics, bio-informatics or climate modelling, are continuously increasing every year [36]. To meet this demand, highperformance computing (HPC) systems keep growing in scale and complexity [28]. However, the race for higher processor frequency is (temporally) over. The most profitable way to use the available silicon resources is to increase the parallelism (both data and thread-level) of new-generation multi/many core processors. Scalar or sequential codes will use only a small percentage of the capabilities of modern hardware [21]. At the same time, applications need to evolve so that they can fully exploit the performance of these systems [5]. However, it is a challenge to find expert developers with enough knowledge to deal with applications in this scenario. Both developers and users are usually reluctant to modify their working codes to adapt to new systems. While the benefits of migrating codes to new systems are clear, it is important to evaluate how to do so in a simple and general way. “Code modernization” is a new paradigm that aims to provide both code and performance portability [22]. The main goal to achieve a modernized code include: a) the ability to scale as with core count, b) ensure a proper usage of the vectorization capabilities of the system and c) the exploitation of data locality. Among the key applications used in computational science, we may highlight 3-D Stencil-based computations as they are the basis for solving partial differential equations [4]. Partial differential equations (PDEs) are widely used as a mathematical model in many applications from a wide variety of fields of science and engineering. PDEs can be approximated by the finite difference method, which iteratively computes a solution to the problem sweeping through a structured computational mesh. The calculation at each mesh point depends on a fixed number of neighbouring mesh points, i.e., a computational Stencil. Due to the simple data structures and regular accesses involved, stencil computations are good candidates for massively parallel architectures such as Intel Xeon Phi [13] or GPUs [16]. This paper shows a case study of the code modernization process for three 3-D Stencil algorithms. Our modernized codes are optimized on the first Xeon Phi generation (Knights Corner, or KNC), and then migrated to the (recently released) second generation, Knights Landing (or KNL). The major contributions of this paper are the following: 1) Provide simple 2 guidelines to modernize Stencil codes on the Xeon Phi: How to vectorize and parallelize the code; 2) Improve the usage of the memory subsystem through data locality; 3) Evaluate the energy efficiency of the KNC using the figure of merit EDP (Energy Delay Product or runtime product by energy consumed in such execution); and 4) Show results comparing them with those obtained for a multicore Intel Xeon processor. We have found that the migration of the 3D Stencil codes to run on the Intel Xeon Phi architecture is straightforward. For Knights Corner, we need to recompile the codes by adding the compilation flag -mmic. For Knights Landing that’s not even necessary, since it is binary compatible with regular Intel CPUs. However, in order to make use of the 512-bit vector units we need to build the code using the -axAVX,MIC-AVX512 flag, so that the code runs on both AVX and AVX512 machines. With regard to the modernization of the code, the selected 3D Stencil codes have been: a) vectorized (using data alignment and padding when needed); b) parallelized using loop fusion (#pragma collapse) and loop scheduling and c) improved in terms of memory efficiency, by using the streaming stores and cache blocking. The disparity between the floating-point capability and memory bandwidth poses a serious performance limitation (especially for low-order stencils), so it is impossible to achieve the theoretical peak floating-point performance [25] in the Xeon Phi architecture. Nevertheless, performance improvements achieved range from 10x to 15x on KNC when compared with the base version running on the Xeon multicore. When comparing the modernized code versions, KNC outperforms Xeon by a factor of 5x to 7x. KNL outperforms KNC by around 2.5x on average. Finally, the energy efficiency of the selected KNC is 10x to 28x better than the evaluated Xeon processor. The rest of the paper is structured as follows. In Section 2 we give some background for the Xeon Phi architecture and the Stencil algorithm. Section 3 introduces our code modernization process, showing in Section 4 the results we obtained for all the steps we have taken in our development. Section 2.3 offers some related work, and the paper concludes in Section 5 giving some remarkable conclusions. 2. Background and Related Work 2.1. Xeon Phi Architecture and its Programming Mode Both KNC and KNL architectures are based on an dual-issue core, the key difference is that KNL cores are out-of-order. Cores are capable of 4- 3 way simultaneous multi-threading. The processor itself consists of up to 61 cores connected by a ring interconnect for KNC. KNL can have up to 72 cores, divided into 36 coherent tiles of two cores each, connected by a 2D Mesh. Each core has 32KB of first level instruction and data caches and 512KB of unified L2 cache (1MB per tile in KNL). Caches are coherent, and cores have access to shared GDDR5 memory in KNC, and to a MCDRAM (High Bandwidth Memory) in KNL. The most important enhancements to the old-fashioned cores is the inclusion of a 512-bit wide SIMD vector unit (two in the case of KNL). These units allow to perform eight double-precision floating-point (DPFP) operations with a single instruction. In addition, new instructions are added over previous SIMD extensions (SSE, AVX), namely KNCNI (KNC) and AVX512 (KNL). All these enhancements lead to a double-precision peak performance of 1 TFlops for KNC and 3 TFlops for KNL. However, this peak performance heavily relies on the latency of the memory accesses and the available bandwidth, that can reach a bandwidth up to 352 GB/s for KNC and 490 GB/s for KNL, with slightly higher latency in the later. KNL also has access to DDR4 memory (90 GB/s). One of the key benefits of this architecture (and accelerators in general) is their high energy efficiency. In addition to providing high performance and computing power (runtime), the associated energy consumption is much lower than that used by traditional multi-core architectures. This implies that accelerators provide a better ratio flops/watt than complex out-of- order CPUs tailored for single-thread performance, making them specially suitable for high performance computing. The Xeon Phi family has two main modes of operation, the accelerator mode (also called offload mode), and the native mode. Offload mode is the traditional way of using accelerators, where the host processor begins execution of the application and executes (offloads) remote parts of the code on the accelerator, usually massively parallel critical sections. In contrast, the native mode allows Xeon Phi to behave like an independent processor, that runs an application by itself from beginning to end. Source codes for the Xeon Phi architecture require programming models with support for multi-threading. Examples of such models include OpenMP, Fortran "do concurrent", Intel Threading Building Blocks (TBB) or Intel Cilk Plus. The most straightforward way to modernize an old code written in Fortran or C is to use the OpenMP extensions. OpenMP is a user-directed shared-memory programming specification (based on #pragma declarations). The design allows programmers to annotate parallel regions of the applica- 4 tion, and separate them from serial sections with different synchronization constructs. This programming model ensures code portability but the exact behaviour will depend on the actual implementation for that system. OpenMP requires from runtime system libraries and compiler support (C, C++ or Fortran). The compiler needs to be notified to recognize OpenMP extensions by including a header file (<omp.h>) in the code source, and by including a flag to link with the OpenMP libraries (-(q)openmp). 2.2. Stencil Computations Partial differential equations (or PDEs) are one of the main sources of computational requirements in many scientific and engineering applications, including geophysics, astrophysics, acoustics, oceanography, etc [32, 7, 10, 37, 17]. PDEs can be solved using the finite-difference (FD) method, that relies on stencil computations to calculate the differential operators, which account for a large fraction of the total execution time. In stencil computations, each point of the computational domain for the time step "t", is computed as the weighted contribution of its neighbour points in all N dimensions represented in the model (in the current or previous time-steps), thus solving the spatial differential operator. From an architectural point of view, stencil computations are hard to optimize. Stencils require to access neighbour points in different dimensions, making it hard to design a data structure that allows to minimize non-contiguous memory accesses. In addition, stencil computations usually perform few operations over the loaded data, leading to a low arithmetic intensity (AI) [35]. In this paper, we have measured the AI as the number of floating point computations performed per load operation (in double precision). In this work we will focus on the modernization of 3D Stencil algorithms. These algorithms are implemented as a triple nested loop that runs along the entire data structure, updating each point on the grid with an specific weighted function. Algorithm 1 shows the pseudo-code of a generic threedimension (3D) Stencil kernel solver with an additional outer loop to control time steps. As discussed before, 3D stencil codes suffer from poor cache performance because data reuse is minimal for realistic input sizes, since all the data does not fit in cache. This article evaluates three Stencil 3-D codes from different scientific fields and complexities [7]. We have used a fairly common 7-point spatial secondorder temporal acoustic diffusion code, and the simpler 11-point spatial firstorder temporal heat diffusion code. We have also evaluated a larger stencil 5 Algorithm 1 The 3-D Stencil solver kernel. width, height, depth are the dimensions of the data set including border (halo) points. 1: for time = 0; time < T imeMax;time + + do 2: for z= 1; z < depth −BorderSize;z+ + do 3: for y= 1; y < height −BorderSize;y+ + do 4: for x= 1; x < width −BorderSize;x+ + do 5: Stencil_solver_kernel(); 6: end for 7: end for 8: end for 9: tmp =Input_Grid;Input_Grid =Output_Grid;Output_Grid =tmp; 10: end for with more neighbouring points, such as the isotropic seismic wave of 25 spatial points and second-order temporal which is similar that the NAS Parallel MG (Multigrid) benchmark utilizes [2]. Efficiently modelling acoustic/heat/- seismic data sets in complex 3D media by solving the 3D finite-difference time-domain solver is an important challenge in computational physics (see for example [34]). 2.3. Related Work To investigate the performance of stencil computations, some work focused on using various optimizations to manually improve the data locality and parallelism on multicore and many-core processors [15, 24, 30, 19, 9]. Datta et al. [7] reviewed the cache reuse optimizations of stencil computations on modern microprocessors. They also studied the impact of different blocking sizes on performance, focusing on the cache miss ratio of modern processors. Kamil et al. [14] also proposed a performance model to study the performance of stencil computations on modern processors. Recently, de la Cruz and Araya-Polo [8] studied the performance of stencil computations on modern HPC architectures. They focused on quantifying the cache misses on multicore or many-core architectures. Stengel et al. quantified the performance bottlenecks of stencil computations based on the executioncache-memory model [29]. Beyond the established cache blocking techniques, leveraging SIMD capabilities [11] and an efficient shared memory parallelization [31] are subject to recent research. An overview about the state of the art in stencil optimizations can be found in a review by Datta et al. [6]. Regarding other performance modelling works, Williams et al. [35] proposed the Roofline model, which can be used to visualize compute-bounded or memory-bounded multicore architectures. 6 Finally, there are several books that show the computing capabilities of the Xeon Phi coprocessor [18, 13, 26, 27, 23, 33]. More specifically for Stencil codes we have studies in [12, 19] and [1]. 3. Modernizing Stencil codes for Intel Xeon Phi This Section shows the process of modernizing three Stencil codes for Intel Xeon Phi architecture, describing in detail the process of vectorization, parallelization, and memory access optimizations. This case study will help developers in the process of modernizing similar codes in the future. We start from a simple parallel implementation of the sequential code written in C/C++ using OpenMP (labelled as naïve). This version implements the Algorithm 1 for each of the evaluated kernel Stencil codes. The parallelization consisted of adding pragma annotations (#pragma omp parallel for) into the code before the nested triple loop that traverses the data input. These codes were not optimized neither vectorized for scalar execution. To ensure that the compiler did not automatically vectorize the inner loops, the codes were compiled with the -no-vec flag both for Intel Xeon and Xeon Phi. Building native code for the Xeon Phi KNC only requires to add the -mmic flag to the Intel compiler (icc), while KNL does not require any specific flag if vectorization is not to be used. Over this naïve code we will perform several optimizations, described in the next subSections. 3.1. Scalar optimizations First, we describe some text-book optimizations applied to our base code: •Optimization of arithmetic expressions: We have reorganized the operations of the Stencil computation to better exploit temporal and spatial locality, aiming to minimize cache miss latency. We have also moved invariant expressions of the innermost loop calculation to the next outer loop (the loop in the ydimension). •Reduction of precision of the arithmetic operations. Replacing costly operations in term of resources and latency by approximations is feasible in stencil kernels, since the convergence criteria is met despite loss of precision. Approximations can be performed at the compilation time with the following flags: -fimf-precision=low -no-prec-sqrt -no-prec-div -fp-model fast=2 -fast-transcendentals. 7 •Include the const qualifier. All the local variables or function arguments that do not change value over time can be declared with the const qualifier. That allows the compiler to generate more efficient code. •Save critical variables into registers. The register qualifier can be used in the declaration of variables as a hint for compiler, meaning that the variable should be stored in a register to reduce memory movements and accelerate function execution. •Linearization of data matrices. The data matrices used in our Stencil codes were declared as a linear array of width * height * depth, avoiding using three-dimensional matrices that may have a negative effect on performance. Each data element is accessed using a displacement from the base address of the matrix, avoiding pointer indirection. 3.2. Code Vectorization There are several strategies for vectorizing code in Intel architectures. We want to avoid the use of very low-level constructions such as assembly instructions or Intel’s intrinsics, because of the high development complexity and the lack of portability between different architectures. Instead, we embrace the use of user-directed and automatic vectorization capabilities of the Intel compiler (compiled with the -O2 or higher option). Hints can be given to the compiler by means of specific pragmas (SIMD). The compiler can provide a report of the vectorization process (building with the -qopt-report flag), that includes information about the loops that have been vectorized, and the causes of those that have not. Next we show the steps to be taken to obtain a good performance from the vectorization process: Avoid dependencies between data elements. Although there are no data dependencies in the evaluated algorithms, the function that calculates the Stencil has as parameters the pointers to the input and output matrices. Since the declaration of such matrices is in the main program, the compiler does not know the exact addresses of these variables, so it assumes that there might be some overlapping between the input and output matrices. There are several ways to overcome this problem. First, use the keyword restrict in the statement of the function variables (REAL* restrict fin, REAL* restrict fout), which will instruct the compiler that the memory reference passed in the pointers is not an alias. This requires of the inclusion 8 of the -restrict flag when compiling, so that the Intel compiler recognizes this keyword as reserved. The second way to inform the compiler that the variables don’t overlap themselves is to add #pragma ivdep directive before the internal loop to be vectorized. This notifies the compiler that it can assume that the memory addresses of the different matrices point to disjoint memory allocations. Data Alignment. This optimization enforces the compiler to create data objects in memory at specific byte boundaries. Data alignment increases data movement efficiency to and from the processor. This is a key aspect for vectoring in the Intel Xeon Phi KNC architecture, since unaligned loads are costly. For vector architectures that use 512-bit registers, memory movement is optimal when data is placed at 64-byte aligned addresses. Data alignment of application variables requires to: a) assigning the dynamic memory with calls to functions in C/C ++ that support a data alignment factor (e.g., _mm_malloc(Data, 64) and _mm_free(Data)); and b) to use variable modifiers that notify the compiler about the alignment of the memory addresses. We use the __assume_aligned(Data, 64) clause for our case study. An alternative is to use the #pragma vector aligned just before the loop to be vectorized. Padding. This is an interesting technique that can be used to ensure that the first element of each row in the data matrices is aligned in the desired boundary (64 bytes for Intel Xeon Phi). To do this, additional space (not actually used for algorithm calculations) is allocated for each row in the data matrices. Although we use more memory, this additional “fictitious” space avoids misalignment between rows. The new width with padding is calculated as width_PADD = ((((width ∗sizeof(REAL)) + 63)/64) ∗ (64/sizeof(REAL))). REAL stands for the type of floating-point data used in the kernel (double precision, in our case). 3.3. Parallelization This Section shows how to improve the performance of the OpenMP parallelization. The first optimization is to add the collapse (2) modifier to the OpenMP pragma that parallelizes the loop. This modifier merges or fuses the two outermost loops of the evaluated kernels in a same loop, and then parallelizes. This increases the number of work units that can be given to each thread, and produces a better load balance at a runtime level. 9 the vectorized version running on the Xeon Phi KNC and the base version running on Xeon. Comparing with the baseline version running on the Xeon Phi KNC, this improvement is around 4x. This benefit is lower in KNL (30 to 60%) because of the excellent performance of the scalar code. 4.3. Parallelization In this Section we evaluate the policies for planning and affinity to further improve the performance of the parallel loops. Three planning policies have been evaluated: dynamic,static and guided, with a size of 2, 4 and the default value. These policies are applied over the vectorized versions of the three kernels on which the streaming stores optimization has already been applied using the directive #pragma vector non temporal (fout). 0 20 40 60 80 100 120 (dynamic) (dynamic,2) (dynamic,4) (static) (static,2) static,4) (guided) (guided,2) (guided,4) (dynamic) (dynamic,2) (dynamic,4) (static) (static,2) static,4) (guided) (guided,2) (guided,4) (dynamic) (dynamic,2) (dynamic,4) (static) (static,2) static,4) (guided) (guided,2) (guided,4) Acoustic Seismic Heat GFLOPs Figure 2: Planning policies of the OpenMP loop. The result obtained for KNC is shown in Figure 2. As it can be seen, static is the least performing option in all three kernels, although in the case of (static, 2) the performance rises by around 15.23% for all Kernels, whereas (static, 4) only improves by 8.77% for seismic and heat. Using the 16 0 20 40 60 80 100 120 140 none balanced scatter compact none balanced scatter compact none balanced scatter compact Acoustic Seismic Heat GFLOPs Figure 3: Planning policies of the OpenMP affinity. guided policy, performance improves by 5.51% when compared to static (regardless of the chosen parameter). The best performing policy was dynamic. Comparing our results with those obtained without specifying any planning policy (which is the same as using schedule(static), since it is taken by default), we achieve a 34% acceleration for the acoustic kernel and 42% for the seismic kernel, both using the schedule(dynamic, 4) policy. The heat kernel improves performance by 30% using a (dynamic, 2) policy. This leads to the conclusion that there is work imbalance among the different cores, that is, some cores can compute more work units than others. This can be either because of how data is allocated in the different caches or because of slight variability in processing speed. Using a balanced policy benefits the overall performance. For KNL, the variability between configurations is minimal, being dynamic the best option for all cases, as it can be seen in Figures 4 (for cache mode) and 6 (for flat mode without MCDRAM). Next, we evaluate the affinity feature that has up to three types of affinity 17 (compact, scatter, balanced) along with not using any affinity (none). Figures 3, 5 and 7 show the results obtained. We can see that the affinity has little influence on the execution of our three kernels. Although we do not see a significant difference with any of the three types of affinity (except for KNL-compact), we understand that for other algorithms this parameter should be taken into account and properly tuned. 0 20 40 60 80 100 120 140 160 180 200 220 (dynamic) (dynamic,2) (dynamic,4) (static) (static,2) static,4) (guided) (guided,2) (guided,4) (dynamic) (dynamic,2) (dynamic,4) (static) (static,2) static,4) (guided) (guided,2) (guided,4) (dynamic) (dynamic,2) (dynamic,4) (static) (static,2) static,4) (guided) (guided,2) (guided,4) Acoustic Seismic Heat GFLOPs Figure 4: OpenMP loop policies for KNL (cache) with 64 threads. 4.4. Optimizing Memory Accesses In this Section we evaluate the blocking technique to optimize accesses to memory. First, we will determine the best size of the blocks for the YZ axes, and then look for the best block size in X. Figure 8 shows the performance achieved by our three Stencil kernels, identified as Acoustic, Seismic and Heat. The data obtained for this Figure uses a fixed size for the block on the X axis, equal to the size of the dimension X in the original matrix (width_Tblock = 800). Therefore, the performance achieved is only related to the blocking used in the Y and Z axes. 18 0 20 40 60 80 100 120 140 160 180 200 220 balanced scatter compact balanced scatter compact balanced scatter compact Acoustic Seismic Heat GFLOPs Figure 5: OpenMP loop affinity for KNL (cache) with 64 threads. Our results show that for acoustic Stencil, the best block sizes are 2 for the Y axis and 8, 16 or 32 for the Z axis. For the seismic Stencil, blocking in Y performs better when using 2 as block size, while using 2 or 4 for the Z axis. Finally, blocking for heat Stencil, achieves the best performance when using 2 or 4 on the Y axis and also 2 or 4 for the Z axis. Once the best block sizes for the Y and Z dimensions are obtained, we proceed to evaluate the block size in the X dimension. Figure 9 shows the analysis of blocking over the X axis. We have evaluated different block sizes for the X axis, including 150, 200, 250, 400 and 600. Taking into account the results of Figure 8, the Y and Z axes have been set to 2 and 4. As we can see in this Figure, exploiting blocking in the X axis also leads to additional performance gains for the seismic kernel (15.19%) and the heat kernel (3.20%), whereas the acoustic kernel remains unaffected. The acoustic kernel has the lowest arithmetic intensity (AI) of all kernels (floating point computations per loaded double precision operand), around 6.5. Therefore, 19 0 20 40 60 80 (dynamic) (dynamic,2) (dynamic,4) (static) (static,2) static,4) (guided) (guided,2) (guided,4) (dynamic) (dynamic,2) (dynamic,4) (static) (static,2) static,4) (guided) (guided,2) (guided,4) (dynamic) (dynamic,2) (dynamic,4) (static) (static,2) static,4) (guided) (guided,2) (guided,4) Acoustic Seismic Heat GFLOPs Figure 6: OpenMP loop policies for KNL (flat) with 64 threads. even if we improve data locality we still don’t perform enough computations to pay for the delay of the data transfer. On the other hand, the seismic AI (9.5) and heat AI (10.5) kernels benefit from the increased data locality. Finally, we tried one of the best blocking configurations (200,4,4) in the KNL. Results show negative effects on both scalar codes and vector codes when working in cache mode (Figure 10). On the other hand, scalar codes only benefit from blocking in the seismic kernel (45 to 61 Gflops ) when working in flat mode (without MCDRAM). This shows the importance of re-evaluating the whole spectrum of block sizes when moving to a new architecture. 4.5. Energy Efficiency This Section shows the energy evaluation of the Intel Xeon Phi KNC compared with the Intel Xeon multicore. This is an important point in our article, since we want to verify if the Intel Xeon Phi architecture is as energy 20 0 20 40 60 80 balanced scatter compact balanced scatter compact balanced scatter compact Acoustic Seismic Heat GFLOPs Figure 7: OpenMP loop affinity for KNL (flat) with 64 threads. efficient as the manufacturer says. Reducing the energy consumption of an HPC platform is of key importance in today’s and future designs. Figure 12 shows the energy consumed and the average power dissipation (Y-axis) for different implementations of the three Stencil codes, namely: Sstore) vectorized with a naïve parallelization using streaming stores optimization, SstoreShed) same code but using the best loop planning policy obtained in the 4.3 Section, and Blocking) best code and planning plus blocking with the best parameters obtained in Section 4.4. The energy consumed in multi-core Xeon is about 4 times higher than in Xeon Phi KNC, reaching values of 50 to 60 KJoules, while in Xeon Phi reaches a maximum of 12 KJoules. As for the average power, for multi-core Xeon remains fairly constant and is close to 145 Watts. The Xeon Phi shows greater variability depending on the code executed, but all values are around 200 Watts. The EDP metric takes into account the execution times obtained for the different codes versions in addition to the energy consumed. Figure 13 shows 21 0 20 40 60 80 100 120 140 x=800,y=2,z=2 x=800,y=2,z=4 x=800,y=2,z=8 x=800,y=2,z=16 x=800,y=2,z=32 x=800,y=4,z=4 x=800,=4,z=8 x=800,y=4,z=16 x=800,y=4,z=32 x=800,y=16,z=4 x=800,y=16,z=8 x=800,y=16,z=16 x=800,y=16,z=32 x=800,y=2,z=2 x=800,y=2,z=4 x=800,y=2,z=8 x=800,y=2,z=16 x=800,y=2,z=32 x=800,y=4,z=4 x=800,=4,z=8 x=800,y=4,z=16 x=800,y=4,z=32 x=800,y=16,z=4 x=800,y=16,z=8 x=800,y=16,z=16 x=800,y=16,z=32 x=800,y=2,z=2 x=800,y=2,z=4 x=800,y=2,z=8 x=800,y=2,z=16 x=800,y=2,z=32 x=800,y=4,z=4 x=800,=4,z=8 x=800,y=4,z=16 x=800,y=4,z=32 x=800,y=16,z=4 x=800,y=16,z=8 x=800,y=16,z=16 x=800,y=16,z=32 Acoustic Seismic Heat GFLOPs Figure 8: Blocking applied to the solution of our Stencil codes. Blocking on the Y and Z axes. the EDP (primary Y axis) and EDP gain factor between the multi-core Xeon and the Xeon Phi for the same codes versions. Results show superior global energy efficiency for the Xeon Phi. In general, the better the performance (GFLOPs), the better the energy efficiency, and therefore the lower EDP. All three kernels show improvements in energy efficiency for both platforms when optimized. Finally, between the best version running on Xeon Phi KNC and that same version running on multi-core Xeon we have achieved 28X better energy efficiency for the acoustic kernel, 10X for the seismic kernel, and 21X for the heat kernel. 5. Conclusions Iterative solvers for partial differential equations (PDEs) are a key component for scientific computing. The complexity, granularity and dimensionality of the problem targeted by PDEs require of substantial computational 22 0 20 40 60 80 100 120 140 x=800,y=2,z=16 x=150,y=2,z=4 x=150,y=4,z=4 x=200,y=2,z=4 x=200,y=4,z=4 x=250,y=2,z=4 x=250,y=4,z=4 x=400,y=2,z=4 x=400,y=4,z=4 x=600,y=2,z=4 x=600,y=4,z=4 x=800,y=2,z=2 x=150,y=2,z=4 x=150,y=4,z=4 x=200,y=2,z=4 x=200,y=4,z=4 x=250,y=2,z=4 x=250,y=4,z=4 x=400,y=2,z=4 x=400,y=4,z=4 x=600,y=2,z=4 x=600,y=4,z=4 x=800,y=2,z=2 x=150,y=2,z=4 x=150,y=4,z=4 x=200,y=2,z=4 x=200,y=4,z=4 x=250,y=2,z=4 x=250,y=4,z=4 x=400,y=2,z=4 x=400,y=4,z=4 x=600,y=2,z=4 x=600,y=4,z=4 Acoustic Seismic Heat GFLOPs Figure 9: Blocking applied to the solution of our Stencil codes. Blocking on the X axis. resources that are not affordable by traditional CPU-based architectures. Emerging massively data-parallel architectures, such as Intel Xeon Phi, offer a great opportunity to address challenging problems based on PDEs. This paper discusses the new challenging issue known as code modernization in HPC programming brought by the emergence of vector-type manycore processors like Xeon Phi. The remarkable features of Xeon Phi namely 244+-scale multi-threading and 512-bit SIMD units provides a cost- and power-efficient solution for TFlops-class computation. We have described the main guidelines to deploy a proper modernization of code for three kernels based on 3-D Stencil pattern. The target high performance architectures were the first and second generation Intel Xeon Phi (KNC and KNL). We have programmed these Stencil kernels with the C/C++ language using the OpenMP extensions to manage this multi-threading platform. Results show that the portability of 3D- Stencil codes for execution in an Intel Xeon Phi architecture is immediate. From the base code developers only 23 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 64thr 128thr 256thr 64thr 128thr 256thr 64thr 128thr 256thr 64thr 128thr 256thr 64thr 128thr 256thr 64thr 128thr 256thr Scalar Vectoriz Scalar Vectoriz Scalar Vectoriz Acoustic Seismic Heat GFOPs Figure 10: Blocking applied to the solution of our Stencil codes KNL (Cache) need to re-compile the codes by adding a compiler option (-mmic) to tell the Intel compiler that it must generate code for the Xeon Phi architecture. This is not even necessary for KNL, since it is binary compatible with regular CPU x86 code. However, in order to exploit the 512-bit vector units we need to build the code with the appropriate compile flags (-axAVX,MIC-AVX512), so that the code runs on both AVX and AVX512 machines. The process of code modernization focuses on three areas: vectorization, parallelization and memory traffic reduction. For vectorization we have used the ability of the Intel icc compiler to automatically vectorize the code. We needed to add the directive #pragma ivdep to force the compiler to ignore false dependencies. Also, to facilitate vectorization we have aligned the data to 64 bytes and have used the padding technique. Parallelization benefits from using the modifier collapse (2) in order to improve the amount of work units and produce work for the high number of threads available in the system as well as from using an appropriate planning policy for the loops 24 0 10 20 30 40 50 60 70 80 64thr 128thr 256thr 64thr 128thr 256thr 64thr 128thr 256thr 64thr 128thr 256thr 64thr 128thr 256thr 64thr 128thr 256thr Scalar Vectoriz Scalar Vectoriz Scalar Vectoriz Acoustic Seismic Heat GFOPs Figure 11: Blocking applied to the solution of our Stencil codes KNL (FLAT) (i.e., schedule (dynamic)). In memory access, it has been useful to use the Streaming stores technique together blocking to improve the reuse of the data in the memory access in each dimension for KNC. KNL does not share the same optimal block sizes, and further analysis is required to adjust the configuration. Once this modernization was finished for the three stencil kernels, we obtained a reduction of execution time of 10 to 15 times between the base code running on a multi-core Xeon processor and the best version of the code running on the KNC. Performance improvements are more than doubled from KNC to KNL. If we compare the best versions in both architectures, this reduction of time oscillates between 5x and 7x in favour of KNC and 2.5 times that for KNL when using the MCDRAM in cache mode. We have also shown how critical it is for the KNL architecture to make use of the MCDRAM, either in cache mode or in flat mode allocating memory for the application on the MCDRAM NUMA domain. In fact, while we have not 25