Performance Optimization and Tuning for NEC TX7

including MOLPRO, MOLCAS, AMBER, GAMESS-US

Jan Fredin, Ph.D. NEC Solutions America March 5, 2004

1 © NEC Corporation 2004

1 Talk Overview

ƒ NEC TX7 hardware ƒ General TX7 performance tuning techniques ƒ Case Studies: examples of tuning efforts on Chemistry applications: Š MOLPRO Š MOLCAS Š AMBER Š GAMESS-US Š User Code

2 © NEC Corporation 2004

2 NEC TX7 Servers ƒ Second generation technical computing server with Intel’s latest 64-bit CPU “Itanium® 2 processor”.

ƒ Configured with up to 32 CPUs per node. NEC was the first company to release Itanium® 2 systems with 32 processors.

ƒ Designed and built with supercomputer technology from NEC.

ƒ Ideal systems for scientific and engineering applications.

3 © NEC Corporation 2004

3 TX7 Internal Structure • ccNUMA architecture • Near-flat 32-way memory access • Flexible partitioning Cell Photo

Up to 8 Ce lls Cell Itanium2Itanium2 Itanium2Itanium2 Itanium2Itanium2 Itanium2Itanium2

MemoryMemory ControllerController CellCell ControllerController MemoryMemory ControllerController

DDR DIMMs Itanium2 NEC Chipset DIMMs memory

CrossCross-bar-bar interconnectinterconnect Total band width >100GB/s PCIPCI-XPCI-X-X bridgebridge bridge PCIPCI-XPCI-X-X bridgebridge bridge PCI-X PCI-XPCI-X bridge bridge PCI-XPCI-X bridge bridge PCI-X14PCI-X14 PCI-XPCI-X bridge bridge slots slotsPCI-XPCI-X bridge bridge Up to 112slots 14PCI-X14PCI-X PCI-X PCI-X bridge bridge slots slotsPCI-X bridge 1414 PCI-X PCI-X slots slotsPCI-X bridge 1414 PCI-X PCI-X slots slots

TX7 / i6010, i6510, i9510

4 © NEC Corporation 2004

4 Itanium 2 Processor Memory Hierarchy

1.5 GHz Itanium® 2 Processor

L1I 16KB L3 L2 64B lines External 48 GB/s 6MB 256KB 1 CLK Memory 24-way 8-way 48 GB/s 6.4 GB/s 128B lines 128B lines L1D 7-14 CLKS 5-7 CLKS 16KB 48 GB/s 64B lines Banked 1 CLK

128 FP Registers 128 General Registers Core Pipeline (functional units)

5 © NEC Corporation 2004

5 NEC IPF Server Roadmap

® ItaniumItanium®

TX7 Series

Express5800/ 1160Xb (AzusA)

16 way

32 way Performance / Scalability

4 way ® ItaniumItanium® 22 / /Madison Madison

2001 2002 2003 6 © NEC Corporation 2004

6 NEC Worldwide HPC Support and Development Organizations

Canada Germany ● ● ● Korea ▲ NEC HPCE NEC ▲ ● Support Office U.S.A.. Cray JAPAN ■ HMD NECSAM ●

Brasil

● NEC do ● Subsidiaries France Brasil ● ▲ Partner Groupe Bull Singapore Australia S.A. NEC ■ Branch NEC France NEC Australia Singapore Pty. Ltd. Pty. Ltd.

7 © NEC Corporation 2004

7 Support Cycle ƒ Port - successfully run code for all QA available Š typical compile efc -O2 -ftz Š reduce optimization for problem areas ƒ Tune - improve performance Š identify time consuming routines for typical workload Š measure performance using gprof, NEC compiler add-of ftrace, Intel VTUNE or timing calls Š test impact of changes in compile options, compile directives and source code restructure Š can be limited by ISV code maintenance rules

8 © NEC Corporation 2004

8 Software Support Cycle

ƒ Update, supporting software vendor and user sites Š test and verify full QA correctness Š provide changes through vendor distribution or release notes Š update for: - new vendor software release - compiler update - user problems, feedback and benchmarks - hardware upgrades ƒ Repeat the tune and update steps regularly

9 © NEC Corporation 2004

9 Tuning Challenge ƒ keep fast functional units busy, providing all data required Š 2 FMA / clock - 6GFLOPS / sec for Itanium2 1.5GHz ƒ maximize loop optimization Š provide compiler with loops that can achieve high performance Š software pipelining - similar to vectorization Š instructional parallelism - 6 (2 bundles) / clock Š prefetching - avoid waiting at the L3 cache bottleneck ƒ high performance is achieved by balance in the loops Š computational complexity Š data locality Š number of variables

10 © NEC Corporation 2004

10 Software Tuning Techniques ƒ know the good application input and algorithm choices Š memory, processors, site defaults ƒ use tuned kernels like NEC MathKeisan ƒ choose effective compile options ƒ restructure loops ƒ help compiler optimize through source-code directives

11 © NEC Corporation 2004

11 NEC MathKeisan

MathKeisan is a set of NEC highly tuned math libraries.

Name Description BLAS Basic Linear Algebra Subprograms LAPACK Linear Algebra PACKage for high performance computers. ScaLAPACK Scalable Linear Algebra PACKage (contains PBLAS) BLACS Basic Linear Algebra Communication Subprograms. Uses MPI. CBLAS interface to BLAS SBLAS Sparse BLAS (from ACM Algorithm 692) FFT Fast Fourier Transforms METIS Matrix/Graph ordering and partitioning library ParMETIS Parallel Matrix/Graph ordering and partition library. Uses MPI. SOLVER Direct solver for sparse symmetric systems ARPACK Solution of large scale eigenvalue problems

12 © NEC Corporation 2004

12 MathKeisan 1.5 Improvements

DGEMM Performance 1.5 GHz Madison, 6 MB L3 6000

5000 -O3

4000 MathKeisan 1.3

MFLOP/s 3000 MathKeisan 1.5 ( target release 2Q CY2004 ) 2000

1000

0 10 50 100 500 1000 1500 Matrix size

13 © NEC Corporation 2004

13 Compile Choices

ƒ NEC compiler is a superset of the Intel compiler Š NEC compiler provides ftrace function for code section performance tracing ƒ Compiler optimization level Š -O3 highest level of optimization -software pipelining on -activates prefetching -activates other loop optimizations Š -O2 safe optimization level - software pipelining on Š -O0 no optimization for debugging

14 © NEC Corporation 2004

14 Other Compile Options

ƒ -ftz flush-to-zero denormalized values Š should always use Š default at compiler level -O3 ƒ -opt_report compiler optimization report Š messages about pipelining, dependencies, unrolling, other loop restructuring ƒ -ip interprocedural optimization within object files ƒ -prof_gen and -prof_use profile generated optimization ƒ -restrict / -ansi_alias enable strict pointer aliasing ƒ -i8/-r8 integer and real default word size

15 © NEC Corporation 2004

15 Loop Restructuring

ƒ code developers know overall structure ƒ provide compiler with loops that can achieve maximum performance ƒ multi-purpose scientific and engineering codes often have heavy usage of long complex loops with conditionals and indirect indexing ƒ goal of hand recoding is to isolate sections that are hard for the compiler to optimization Š too complex to analyze Š external calls Š data dependencies Š conditionals

16 © NEC Corporation 2004

16 Restructuring Example - Loop Blocking ƒ breaks up multi-level loop into blocks that fit into cache ƒ best blocking sizes determined experimentally ƒ blocked sections are prime targets for parallelism Original Code Modified Code

parameter (kblock = 1024) do ks = 1, m, kblock do j = 1, n do j = 1, n do i = 1, p do i = 1, p do k = 1, m do k = ks, min(ks+kblock-1,m) c(k,j)=c(k,j)-a(k,i)*b(i,j) c(k,j)=c(k,j)-a(k,i)*b(i,j) enddo enddo enddo enddo enddo enddo enddo

17 © NEC Corporation 2004

17 Compile Automated Loop Restructuring

ƒ Loop Unrolling ƒ Unroll and Jam ƒ Reduction Optimization ƒ Loop Interchange ƒ Loop Fusion ƒ Loop Fission

18 © NEC Corporation 2004

18 Loop Unrolling ƒ perform n iterations of the inner loop on each pass ƒ actives at compile level -O3, compiler sets unroll depth n Š !DIR$ unroll (n) or !DIR$ nounroll Š directive controls unroll depth n or switchs off

Original Code Modified Code subroutine vadd(n, a, b) subroutine vadd(n, a, b) integer n, i integer n, i double precision a(*),b(*) double precision a(*), b(*) !DIR$ UNROLL(4) c explicitely unrolled by 4 do i = 1,n do i = 1,n,4 a(i) = a(i) + b(i) a(i) = a(i) + b(i) enddo a(i+1) = a(i+1) + b(i+1) end a(i+2) = a(i+2) + b(i+2) a(i+3) = a(i+3) + b(i+3) Output from -O3 -opt_report enddo Block, Unroll, Jam Report: do i=i,n (loop line numbers, unroll factors and type a(i) = a(i) + b(i) of transformation) enddo Loop at line 6 unrolled with remainder by 4 end 19 © NEC Corporation 2004

19 Unroll and Jam ƒ “jam” n iterations outer loop into each pass of inner loop ƒ actives at compile level -O3, no user control Original Code Modified Code

do j = 1, n do j = 1, n-3, 4 do k = 1, p do k = 1, p do i = 1, m do i = 1, m c(i,j)=c(i,j)+a(i,k)*b(k,j) c(i,j+0)=c(i,j+0)+a(i,k)*b(k,j+0) enddo c(i,j+1)=c(i,j+1)+a(i,k)*b(k,j+1) enddo c(i,j+2)=c(i,j+2)+a(i,k)*b(k,j+2) enddo c(i,j+3)=c(i,j+3)+a(i,k)*b(k,j+3) enddo Output from -opt_report at -O3: Block, Unroll, Jam Report: enddo (loop line numbers, unroll factors enddo and type of transformation) do j = j, n Loop at line 1 unrolled and jammed by 4 do k = 1, p Loop at line 2 unrolled and jammed by 4 do i = 1, m c(i,j)=c(i,j)+a(i,k)*b(k,j) enddo enddo enddo

20 © NEC Corporation 2004

20 Reduction Optimization ƒ split sum into n partial sums then collect partial sums ƒ actives at compiler level -O3, no user control

Original Code Modified Code s = 0.0d0 s1 = 0.0d0 do i = 1, n s2 = 0.0d0 s = s + a(i) s3 = 0.0d0 enddo s4 = 0.0d0 do i = 1, n-3, 4 Output from -opt_report at -O3 s1 = s1 + a(i) s2 = s2 + a(i+1) Block, Unroll, Jam Report: s3 = s3 + a(i+2) (loop line numbers, unroll factors and type of s4 = s4 + a(i+3) transformation) enddo Loop at line 2 unrolled with remainder by 8 do i = i, n (reduction) s1 = s1 + a(i) enddo s = s1 + s2 + s3 + s4

21 © NEC Corporation 2004

21 Loop Interchange ƒ change in nesting order of 2 or more loops ƒ unit stride on loads to reuse data in cache ƒ activates at compile level -O3, no user control

Original Code Modified Code do i = 1, m do k = 1, p do j = 1, n do j = 1, n do k = 1, p do i = 1, m c(i,j)=c(i,j)-a(i,k)*b(k,j) c(i,j)=c(i,j)-a(i,k)*b(k,j) enddo enddo enddo enddo enddo enddo

Output from -opt_report using -O3 LOOP INTERCHANGE in test_ at line 1 LOOP INTERCHANGE in test_ at line 2 LOOP INTERCHANGE in test_ at line 3

22 © NEC Corporation 2004

22 Loop Fusion ƒ combine 2 or more loops with same iteration count into 1 ƒ reduces overhead, can promote data sharing and computational complexity ƒ can block pipelining if too many registers get used ƒ activates at compile level -O3, no user control

Original Code Modified Code

subroutine vmul(n,alpha,x,y) subroutine vmul(n,alpha,x,y) integer n, i integer n, i double precision x(*), y(*) double precision x(*), y(*) do i = 1, n do i = 1, n x(i) = x(i) * alpha x(i) = x(i) * alpha enddo y(i) = y(i) * alpha do i = 1, n enddo y(i) = y(i) * alpha end enddo end Output from -opt_report at -O3 Fused Loops: ( 5 8 ) 23 © NEC Corporation 2004

23 Loop Fission ƒ also called loop distribution or loop splitting ƒ smaller loops for optimization like prefetching and pipelining ƒ activates at compile level -O3 Š !DIR DISTRIBUTE POINT focuses compiler on trying fusion -outside loop, compiler sets location -inside loop, sets location Original Code Modified Code do i = lft, llt do i = lft, llt fail(i)=1.0 fail(i)=1.0 hgener(i)=0.0 hgener(i)=0.0 diagm(i)=1.e+31 diagm(i)=1.e+31 sieu(i)=ies(nnm1+i) sieu(i)=ies(nnm1+i) !dir$ distribute point enddo x1(i)=x(1,ix1(i)) do i = lft, llt y1(i)=x(2,ix1(i)) x1(i)=x(1,ix1(i)) z1(i)=x(3,ix1(i)) y1(i)=x(2,ix1(i)) enddo z1(i)=x(3,ix1(i)) Output of -opt_report for -O3 enddo LOOP DISTRIBUTION in test_ at line 1 Distributed for large ii at __ performed in test_ at line 7 24 © NEC Corporation 2004

24 Compile Directive - Variable Dependencies ƒ compiler cannot determine array references overlap so pipelining is blocked ƒ !dir$ IVDEP - fortran loop level ƒ #pragma IVDEP - C loop level ƒ C restrict pointer - routine call level

subroutine test(nu1,nu2, tx, sxx, dd1, dd2, np) integer nu1, nu2, i3, k, np(*) double precision tx(3,*), sxx(9,*), dd1, dd2

!dir$ ivdep do k = nu1, nu2 i3 = np(k) tx(1,i3) = tx(1,i3) + sxx(1,k)*dd1 + sxx(2,k)*dd2 tx(2,i3) = tx(2,i3) + sxx(4,k)*dd1 + sxx(5,k)*dd2 tx(3,i3) = tx(3,i3) + sxx(7,k)*dd1 + sxx(8,k)*dd2 enddo end

25 © NEC Corporation 2004

25 Other Compile Directives ƒ prefetch Š data values are loaded into cache ahead of access to reduce latency in loading data Š too much prefetching can cause cache misses Š !dir$ prefetch a Š !dir$ noprefetch b ƒ software pipelining Š default in compile level -O2 and -O3 Š directive to switch on/off software pipelining Š !dir$ swp Š !dir$ noswp ƒ loopcount Š hint for compiler to optimize loop Š !dir$ maxloopcount_value

26 © NEC Corporation 2004

26 Case Studies ƒ Examples of tuning efforts on Chemistry applications: Š MOLPRO Š MOLCAS Š AMBER Š GAMESS Š User Molecular Dynamics (MD) Code

27 © NEC Corporation 2004

27 MOLPRO 2002.6 ƒ MOLPRO is designed and maintained by H.-J. Werner (University of Stuttgart) and P. J. Knowles (University of Birmingham) http://www.molpro.net/

ƒ MOLPRO is a complete system of ab initio programs for molecular electronic structure calculations with an emphasis on highly accurate computations and extensive treatment of the electron correlation.

ƒ Mixed Fortran and C using PNNL Global Arrays (GA) for shared memory and parallel MPI tasks. Driver program checks license and exec’s computational executable. 64 bit real, integer, pointer

ƒ normal_dft dataset Š adrenaline SVWN, BLYP from MOLPRO bench directory 28 © NEC Corporation 2004

28 MOLPRO 2002.6 ƒ upgrade source from MolPro2002.5 to MolPro2002.6 ƒ compiler initial optimization -O2 and < 20 routines –O0 ƒ upgrade MathKeisan for blas and lapack ƒ profile top routines using MolPro development environment to run computational unit stand-alone: normal_dft Š 21.8% uncompress_double Š 9.0% dfti_block Š 7.8% dft_rho Š 5.2% dform Š 5.0% dgenrl ƒ aioint, compress=0 turns off compression Š at end of molpro2002.6/bin/molproi.rc add: Š nocompress ƒ dft/dfti.f -O3 gives correct numerical results for all QA ƒ another customer test profile showed top routines addmx, mxmas, fzero tuned with -O3 (3 files) gave 22% improvement 29 © NEC Corporation 2004

29 MOLPRO 2002.6

normal_dft

1200

1000

800 1cpu 2cpu 600 4cpu

real seconds 400

200

0 initial final uncompress

30 © NEC Corporation 2004

30 MOLCAS 5.4 ƒ MOLCAS is developed and distributed from the Department of Theoretical Chemistry at Lund University http://www.teokem.lu.se/molcas/

ƒ MOLCAS is a software package with a focus on methods that will allow an accurate ab initio treatment of very general electronic structure problems for molecular systems in both ground and excited states. ƒ MOLCAS is a research product developed by scientists to be used by scientists.

ƒ Fortran with some C systems control routines, MPI parallel and shared memory through PNNL GA. 64 bit real, integer, pointer. Independent modules are called in sequence from MOLCAS- generated scripts.

ƒ test902 performance test provided in the MOLCAS distribution. Disk-based Hartree-Fock using modules seward and scf

31 © NEC Corporation 2004

31 MOLCAS 5.4 ƒ initial compile optimization -O2 with 4 routines at -O0, static link ƒ serial focus due to limited parallel implementation in MOLCAS 5.4 Š parallel support targeted for next release. ƒ 4 main modules: Š 46% seward Š 26% caspt2 Š 16% scf Š 12% rasscf ƒ add MathKeisan blas – 38% improvement ƒ profile showed pack / nopack are in top routines ƒ added nopack keyword for Seward module input 32 © NEC Corporation 2004

32 MOLCAS 5.4

test902

900 800 modules 700 600 rasscf 500 caspt2 scf 400 seward

real seconds 300 200 100 0 initial final nopack

33 © NEC Corporation 2004

33 AMBER 7.0

ƒ Distributed by University of California, San Francisco http://amber.scripps.edu/

ƒ About 60 programs that work reasonably well together solving molecular modeling and dynamics problems. Š sander - Simulated annealing with NMR-derived energy restraints. - main program used for molecular dynamics simulations.

ƒ Mostly Fortran with C for dynamic memory and file handling and MPI parallel. Each module is a separate executable. Sander is built from 98 source files consisting of 408 subroutines.

ƒ sander gb_mb dataset provided in AMBER benchmarks directory Š Generalized Born myoglobin simulation, 2492 atoms in 100,000 waters Š 20 Ang. cutoff, salt 0.2Molar, long-range forces every 4 cycles

34 © NEC Corporation 2004

34 AMBER 7.0

ƒ initial distribution compile optimization Š compile level2=-O2 Š compile level3=-O2 used for 36 out of 96 files ƒ profile shows 98% of time spent in egb Š subroutine egb is composed of 3 large loops ƒ upgrade MathKeisan blas and lapack ƒ compile tuning Š level3=-O3, QA shows egb has numerical errors Š level3=-O3 used for all other level3 files, used -O2 for egb ƒ code restructuring Š tried splitting egb_loop1 and egb_loop2 - same performance Š tested impact of !dir$ IVDEP in many locations; no improvement

35 © NEC Corporation 2004

35 AMBER 7.0

Sander gb_mb Routines

70 other 60 50 egb_loop3 40 30 egb_loop2

real seconds real 20 egb_loop1 10 0 initial final

36 © NEC Corporation 2004

36 GAMESS-US ƒ GAMESS is maintained by the members of the Gordon research group at Iowa State University http://www.msg.ameslab.gov/GAMESS/GAMESS.ht ml

ƒ The General Atomic and Molecular Electronic Structure System (GAMESS) is a program for ab initio quantum chemistry.

ƒ Fortran single executable using MPI over sockets for parallel. 64bit real, integer, pointer

ƒ gms_hf dataset Š nicotine Hartree Fock SCF single point calculation Š one of GAMESS benchmarks distributed in early 1990’s

37 © NEC Corporation 2004

37 GAMESS-US ƒ profile gms_hf: Š 42.0% hstar Š 9.5% dspdfs Š 3.7% forms ƒ initial compiler optimization -O2 ƒ -O3 top 5 routines: slower ƒ -O1 hstar: slower ƒ hstar: ivdep on nonpipelining loops: slower ƒ turn off packing: slower ƒ use vector code for dspdfc & forms: slower ƒ update source, compiler, MathKeisan ƒ saw some tests with 12% time in __libc_read: comes from compile libraries on cross-mounted disk: use static link and no shared object (lib*.so) 38 © NEC Corporation 2004

38 GAMESS-US

gms_hf

350 300 250 1cpu 200 2cpu 150 4cpu real seconds real 100 50 0 initial final

39 © NEC Corporation 2004

39 User MD Tuning - compile options

ƒ customer provided molecular dynamics application, written in about 12,000 lines of C with customer test input ƒ original compile flags Š ineffective with -mp (maintain precision) severly limits performance Š ecc -O3 -tpp2 -Zp16 -static -mp -IPF_fma -IPF_fltacc ƒ tuned compile flags Š ecc -O3 -tpp2 -Zp16 -static -restrict -ftz -ip Š -restrict (or -ansi_alias) tells compiler that pointers labeled restrict in routine call have independent locations - restrict pointers provides information about potential dependencies that can enable pipelining - restrict pointers declared on most of MD’s malloc’ed locations Š -ip turns on inter-procedural optimizing within the file that is being compiled

40 © NEC Corporation 2004

40 User MD Tuning - code modifications ƒ use tuned library call Š replace original code with MathKeisan function vdSinCos Š computes (sin(a[i],cos(a[i]) for i=0…n Š pipelines multiple sin and cos calculations ƒ programmer level loop recoding Š split main loop of important routine into 3 - non-bonded pair list generation - calculation of force potentials across all pairs - application of forces across all pairs Š enables pipelining

41 © NEC Corporation 2004

41 User MD

25

20 original compile flags 15 restrict pointers 10 vdSinCos() real seconds real split loop 5

0

42 © NEC Corporation 2004

42