Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Millions of atoms in DFTB

Anthony Scemama
September 29, 2014

Millions of atoms in DFTB

Anthony Scemama

September 29, 2014
Tweet

More Decks by Anthony Scemama

Other Decks in Research

Transcript

  1. Millions of atoms in DFTB Anthony Scemama¹ <[email protected]> Mathias Rapacioli¹

    Nicolas Renon² ¹ Labratoire de Chimie et Physique Quantiques IRSAMC (Toulouse) ² CALMIP (Toulouse) 30/09/2014
  2. Large molecular systems: • No chemistry can be done with

    a single point: need to calculate the energy for multiple geometries (dynamics, Monte Carlo, ...) • Memory grows as • Computational complexity grows as • Energy differences represent a very small fraction of the total energy -> approximations should be carefully controlled The calculation of the energy needs to be dramatically accelerated. Linear-scaling techniques are a very common and efficient solution: • Linear-Scaling in storage : going from to • Linear-Scaling in operations : going from to 1
  3. DFTB is well adapted to linear scaling techniques: • No

    integrals to compute • Minimal basis set (no diffuse functions) • Larger systems are quite sparse Linear scaling DFTB is not new, and is already available: • DFTB+ : Divide and Conquer (Yang et al, 1991) • CP2K : Matrix sign function • ADF : Density-matrix based method 2
  4. Is "linear scaling" a goal? Reducing flops is not necessarily

    good. For example: do j=1,n do i=1,j dist1(i,j) = X(i,1)*X(j,1) + X(i,2)*X(j,2) + X(i,3)*X(j,3) end do end do do j=1,n do i=j+1,n dist1(i,j) = dist1(j,i) end do end do t( n=133 ) = 13.0 s, 3.0 GFlops/s t( n=4125 ) = 95.4 ms, 0.44 GFlops/s (Large is 6.8x less efficient) 3
  5. do j=1,n do i=1,n ! <-- 2x more flops! dist3(i,j)

    = X(i,1)*X(j,1) + X(i,2)*X(j,2) + X(i,3)*X(j,3) end do end do t( n=133 ) = 10.3 s : 1.26x speed up, 8.2 GFlops/s t( n=4125 ) = 15.7 ms : 6.07x speed up, 5.4 GFlops/s (Large is 1.5x less efficient) With data aligned on a 256-bit boundary using compiler directives: t( n=133 ) = 7.2 s : 1.80x speed-up, 12.1 GFlops/s t( n=4125 ) = 15.5 ms : 6.15x speed-up, 7.5 GFlops/s. (Large is 1.6x less efficient) 4
  6. WARNING Linear-scaling is a limit when N goes to infinity.

    What matters is the wall time in the useful range, and the control of the approximations. 0 2e+06 4e+06 6e+06 8e+06 1e+07 O(N) O(N^2) O(N^3) 5
  7. Difficulties arising with Linear scaling: • Computers are better at

    making flops than moving data in memory • Reduction of the arithmetic intensity (nb of operations per loaded or stored byte) -> the bottleneck becomes data access • Data access is never uniform (different levels of cache, hardware prefetching, etc) : NUMA (Non Uniform Memory Access) • Scaling curves are linear only if the data access is uniform (uniformly good or uniformly bad) 6
  8. Goal of this work • Accelerate DFTB. Whatever the scaling,

    it has to be fast! • OpenMP implementation: it will be so fast that MPI will be the bottleneck • Large simulations have to fit in memory • Results should be trusted when the calculation can't be done • Small simulations should not suffer from the optimizations for large systems Two reasons for "million of atoms" in the title: • An impressive title for this presentation • Test our implementation for much larger systems than needed Long term project: • Investigate DNA hairpins in water, PAH/water interactions etc • Add a layer of distributed parallelism for massively parallel Monte Carlo simulations 7
  9. Outline 1. Presentation of the algorithm 2. Hardware considerations 3.

    Technical implementation details 4. Benchmarks on boxes of liquid water 8
  10. SCF Algorithm in DFTB The choice of the algorithm was

    not driven by the reduction of flops, but on the possibility of the hardware to be efficient at doing the calculation, even for medium-sized systems. We chose a MO-based formalism. 1. Generate an initial guess of local orbitals 2. Re-order the orbitals in packets of spatially close orbitals 3. Orthonormalize the guess 4. SCF steps • Instead of diagonalizing , only cancel the occupied-virtual block (Brillouin's theorem, Stewart et al) • At every SCF iteration, the occupied-virtual block is approximately canceled • At convergence, the occupied-virtual block is zero to a given numerical precision 5. Re-orthonormalize MOs before computing the final energy 9
  11. Initial guess The system is partitioned using a constrained variant

    of the k-means clustering algorithm: 1. A set of m centers (means) is first distributed evenly in the 3D-space 2. Each fragment is attached to its closest center, such that each center is connected to 4 molecules 3. The position of the centers is moved to the centroid of the connected fragments 4. Go back to step 2 until the partition doesn’t change 10
  12. 11

  13. 12

  14. 13

  15. 14

  16. • The atoms are then ordered by k-means centers •

    k-means neighbours are centers with at least 2 atoms less than 20 a.u • A non-SCC DFTB calculation is performed in parallel on each fragment • All occupied MOs are packed together • All virtual MOs are packed together 15
  17. Remarks: • MOs belonging to the same molecule are orthonormal

    • MOs belonging to k-means centers which are not neighbours have a zero overlap • MOs have non-zero coefficients only on basis functions which belong to the same molecule • The orbitals are not orthonormal, but both the overlap ( ) and the MO coefficient ( ) matrix are sparse. This step takes less than 1% of the total wall time. 16
  18. Orthonormalization of the MOs Diagonalization of : 1. The matrix

    is already stored sparse 2. Compute (sparse) for each connected k-means groups (neighbours) 3. Compute (sparse) 4. Normalize using diagonal elements 5. Perform 1st order Jacobi-like rotations to remove the largest off-diagonal elements of 6. Go back to step 3 until the largest off-diagonal element is below a threshold • is calculated on the fly with • Orthonormalization takes 40-50% of the total wall time. • Bottleneck: 17
  19. Parallel implementation of orbital rotations Difficulty: each MO can be

    rotated only by one thread at at a time. • First, dress the list of rotations to do. • Prepare a 1D-array of OpenMP locks, (one lock for each MO). • All CPUs do at the same time: 1. Pick the next rotation (i, j) 2. If rotation (i, j) is already done, go to step 1 3. Try to take lock i. If not possible, go to step 1 4. Try to take lock j 5. If not possible, free lock i and go to step 1 6. Rotate i and j and mark (i, j) as done 7. Free locks i and j 8. Go back to step 1 until all rotations are done 18
  20. SCF steps Partial diagonalization of 1. The matrix is already

    stored sparse 2. Compute (sparse) 3. Compute (sparse) 4. Perform exact Jacobi rotations to preserve orthonormality, but with approximate angles: is not updated after a rotation 5. Go back to step 3 until the largest off-diagonal element is below a threshold • is calculated on the fly with This step can be safely performed in single precision • data is 2x smaller, so data bandwidth is virtually 2x larger • caches contain 2x more elements • vectorization is 2x more efficient. 19
  21. • Exact Jacobi rotations are performed to preserve orthonormality, but

    with approximate angles: is not updated after a rotation. • Rotations are done only in the occupied-virtual MO block • Occupied MOs don't rotate between each other • Virtual MOs don't rotate between each other • The locality of occupied and virtual MOs is preserved • MO rotations represent 3-6% of the total wall time • Adding the step yields 33-40% 20
  22. 0 5 10 15 20 25 30 35 40 45

    50 55 60 65 70 75 80 85 10 100 1000 10000 100000 1e+06 nanoseconds KiB Random read Stride=16 Stride=128 Stride=256 22
  23. Measures obtained with LMbench 1 cycle = 0.29 ns, 1

    peak flop SP = 0.018 ns Integer (ns) bit ADD MUL DIV MOD 32 bit 64 bit 0.3 0.3 0.04 0.04 0.9 0.9 6.7 13.2 7.7 12.9 Floating Point (ns) ADD MUL DIV 32 bit 64 bit 0.9 0.9 1.5 1.5 4.4 6.8 Data read (ns) Random Prefetched L1 cache L2 cache L3 cache Memory on socket 1.18 3.5 13 75-80 1.18 1.6 1.7 3. LMBench : http://www.bitmover.com/lmbench/ 23
  24. Strategy to optimize shared-memory access Low aritmetic intensity : Bring

    the data to the CPU cores as fast as possible • Reduce storage as much as possible, and re-compute • Avoid thread migration and allocate/initialize memory locally in parallel regions (first-touch policy) • Every thread uses as much as possible memory which is close • Use stride-1 access as much as possible to benefit from prefetching • Reuse data which is in the caches • Avoid to synchronize threads • Static scheduling keeps the access to close memory and avoids thread communication 24
  25. Sparse storage • Every column contains at least one non-zero

    element • List of lists allows a direct indexing on the columns 25
  26. Takes a little more memory than CSR or CSC, but:

    • Columns can be easily expanded or contracted • Leading dimension of is fixed : • α × 512 • each column is 256 bit-aligned and starts at the beginning of a cache line • optimized data access • enables vectorization of small data fragments • two columns start on distinct memory pages : prefetch only the columns • 24 : Avoid 4k aliasing (round-robin over the cache lines) • has LD+1 elements (start at zero) : reduced 4k aliasing Total memory usage: • 982 GiB for 504 896 water molecules (1.5 million atoms) • 680 KiB per atom 26
  27. Dense x Sparse Matrix Product from QMC=Chem with a very

    small prefactor. Inner-most loops, analyzed with MAQAO : • Perfect ADD/MUL balance • Does not saturate load/store units • Only vector operations with no peel/tail loops • Uses 15 AVX registers. No register spilling • If all data fits in L1, 100% peak is reached (16 flops/cycle) • In practice: memory bound, so 50-60% peak is measured. MAQAO: Modular assembler quality Analyzer and Optimizer for Itanium 2 L.Djoudi, D.Barthou, P.Carribault, C.Lemuet, A.-T.Acquaviva, and W.Jalby, Workshop on EPIC Architectures and Compiler Technology, San Jose, (2005). QMC for large chemical systems: Implementing efficient strategies for petascale platforms and beyond A.Scemama, M.Caffarel, E.Oseret, W.Jalby, J. Comput. Chem., 34:11(938--951) (2013). 27
  28. kmax1 = min(indices(0),n_basis) kmax2 = kmax1 - mod(kmax1,4) do kao=0,kmax2-1,4

    id1 = indices(kao+1) ; d1 = values(kao+1) id2 = indices(kao+2) ; d2 = values(kao+2) id3 = indices(kao+3) ; d3 = values(kao+3) id4 = indices(kao+4) ; d4 = values(kao+4) !DEC$ VECTOR ALWAYS !DIR$ VECTOR ALIGNED do j=1,16 C(j) = C(j) + A(j,id1)*d1 + A(j,id2)*d2 & + A(j,id3)*d3 + A(j,id4)*d4 end do end do Efficient sparse x sparse matrix product in deMon-Nano: Represent one sparse matrix as a collection of small dense sub-matrices. 28
  29. 29

  30. Parallel implementation of CSC Each thread computes 16 columns of

    Data layout is SC(16, 16, N/16) : All 16x16 matrices are 256-bit aligned and fit in L1 cache. 100% vectorized 30
  31. Parallel implementation of CSC Each thread computes 16 columns of

    Data layout is SC(16, 16, N/16) : All 16x16 matrices are 256-bit aligned and fit in L1 cache. 100% vectorized 31
  32. Parallel implementation of CSC Each thread computes 16 columns of

    Data layout is SC(16, 16, N/16) : Fast in-place transposition in cache 32
  33. Parallel implementation of CSC Each thread computes 16 columns of

    Data layout is SC(16, 16, N/16) : Fast in-place transposition in cache 33
  34. Benchmarks Boxes of water from 384 to 504 896 water

    molecules. Two machines: • Dual-socket Intel Xeon E5-2670, 8c @ 2.6GHz, Hyperthreading on, Turbo on, 64GiB RAM • SGI Altix UV, large SMP machine : 384 cores and 3TiB RAM. Enormous NUMA effects. 34
  35. SGI Altiv UV 24 blades: 2x8 cores, 128 GB RAM,

    connected with NUMAlink Single OS, shared memory, 384 cores, 3 TB RAM 35
  36. Parallel speed-up of (CXC) Dual-socket server (16 cores): 0 2

    4 6 8 10 12 14 16 18 0 2000 4000 6000 8000 10000 12000 14000 16000 Parallel speed-up Number of water molecules 32 (HT) threads, 16 threads, 8 threads, 4 threads, 2 threads, 37
  37. Altix-UV : 1 2 3 4 5 0 20000 40000

    60000 80000 100000 120000 140000 160000 180000 200000 Parallel speed-up Number of water molecules 8 blades, 4 blades, 2 blades, 38
  38. Wall time of CXC Dual-socket server (16 cores): 0.001 0.01

    0.1 1 10 100 100 1000 10000 100000 Wall time (minutes) Number of water molecules 1 thread 2 threads 4 threads 8 threads 16 threads 32 threads O(N^1.24) 39
  39. Altix-UV : 1 10 100 1000 10000 100000 1e+06 Wall

    time (minutes) Number of water molecules 8 blades 4 blades 2 blades 1 blade O(N^1.49) 40
  40. Global scaling Dual-socket server (16 cores): 0 10 20 30

    40 50 60 70 80 90 0 5000 10000 15000 20000 25000 30000 35000 Wall time (minutes) Number of water molecules LAPACK 1 thread 2 threads 4 threads 8 threads 16 threads 32 threads O(N^1.71) 41
  41. Altix-UV: 10 100 1000 10000 100000 1e+06 Wall time (minutes)

    Number of water molecules 1 blade 2 blades 4 blades 8 blades O(N^1.81) 42
  42. Observed scaling is not linear: • The behavior comes from

    the terms in the Hamiltonian: • For accurate results, it is important not to truncate 1/R. • Very efficiently parallelized • However, this quadratic scaling appears for large sizes 16 cores, 100 000 atoms 10% of total time 128 cores, 1 500 000 atoms 18% of total time • We see it because all the rest is very fast! • Could be improved by computing only the contributions where the charges have changed 43
  43. Error control For 3312 water molecules SCF convergence E(DSYGVD) -13495.30553

    928 -13495.306178 32 E(deMon-Nano) -13495.30553 764 -13495.306178 28 Error 1.2e-10 3.0e-12 t(DSYGVD) (s) 9 636.1 10 612.1 t(deMon-Nano) (s) 149.8 259.0 • Converges to the correct value • Error of the method below the SCF convergence error For 1 million water molecules, the total energy is ~ -4.7e6 a.u. To get the chemical accuracy with 1 million water molecules, we need a precision of 1e-9 a.u per molecule: an absolute error of 2.5e-10 44
  44. Comparison with CP2K 0 2 4 6 8 10 12

    14 16 18 20 0 200000 400000 600000 800000 1e+06 1.2e+06 1.4e+06 1.6e+06 Average CPU time per atom (s) Number of atoms CP2K Dual-socket Altix-UV Comparison is not quantitative: different architectures, different method, different number of SCF cycles Linear Scaling Self-Consistent Field Calculations with Millions of Atoms in the Condensed Phase J. VandeVondele, U. Borštnik, J. Hutter, JCTC, 8 (10), 3565-3573 (2012) 45
  45. • deMon-Nano exploits very well the hardware, especially for medium-sized

    systems • Wall time is better with CP2K for a single point with millions of atoms, because it can use >9000 cores • MPI communications are indeed important in CP2K (2s / atom) • Latency of Numalink of Altix-UV is 1.5-3x lower than MPI/Infiniband • Hardware memory prefetchers make automatically asynchronous inter-blade communications 46
  46. Conclusion • We have accelerated deMon-Nano • Parallel speedup is

    satisfactory • Scaling is not linear, but our implementation is very efficient in the useful range (->100 000 atoms) • More and more cores/node -> Buying a newer machine will make the code run faster • Doesn't require petascale computers and expensive hardware for standard simulations • Results equivalent to diagnonalization, error below SCF threshold. • Approximations below chemical accuracy for one million atoms. Project for the next years: Distributed large scale computations (EGI grid?, PRACE?, etc) for Monte Carlo simulations of DNA in water. 47
  47. A Sparse SCF algorithm and its parallel implementation: Application to

    DFTB A.Scemama, N.Renon, M.Rapacioli, J. Chem. Theor. Comp., 10:6(2344-2354), (2014). 48