From 84bc1d6654760e7a58d15cff15b98baf51adb483 Mon Sep 17 00:00:00 2001
From: Tiziano Müller <tm@dev-zero.ch>
Date: Tue, 20 Nov 2018 18:23:01 +0100
Subject: CP2K related fixes and update (#9308)

* openblas: enable parallel builds

* cp2k: enable parallel builds

* cp2k: fix building on multilib/Suse distros

use the actual directory path where files where installed to instead of
the default prefix+'/lib'

* cp2k: ensure we have a non-header-only libxsmm

* openblas: disable max num CPU detection on virtualized build

* cp2k: install data and set compiled-in DATA_DIR

* cp2k: make libxc an optional dependency (enabled by default)

* cp2k: link libint statically

* cp2k: declare statically linked library deps as type=build

* cp2k: add support for PGI compiler

* cp2k: rename smm=none to smm=blas for clarification

* cp2k: blacklist unsupported compilers

* cp2k: mark wannier90 a build-time dep since statically linked

* cp2k: make pexsi and elpa optional

* cp2k: add support for v6.1

* libxc: add version 4.2.3

* cp2k: use pkg-config to link properly to libxsmm

* cp2k: fix OpenMP support by making it explicit

Previously, CP2K accepted threaded ELPA or BLAS, leading to #(CPU) processes
being spawned even though no explicit OpenMP was requested. Now the
`popt` variant should truly be thread free while the `psmp` variant uses
threads also internally.

* cp2k: source tarballs moved to GitHub
---
 var/spack/repos/builtin/packages/cp2k/package.py   | 244 ++++++++++++++-------
 var/spack/repos/builtin/packages/libxc/package.py  |  16 +-
 .../repos/builtin/packages/libxsmm/package.py      |  10 +-
 .../repos/builtin/packages/openblas/package.py     |  26 ++-
 4 files changed, 205 insertions(+), 91 deletions(-)

(limited to 'var')

diff --git a/var/spack/repos/builtin/packages/cp2k/package.py b/var/spack/repos/builtin/packages/cp2k/package.py
index e810c84eb3..ef8fe50bc6 100644
--- a/var/spack/repos/builtin/packages/cp2k/package.py
+++ b/var/spack/repos/builtin/packages/cp2k/package.py
@@ -15,48 +15,91 @@ class Cp2k(Package):
     periodic, material, crystal, and biological systems
     """
     homepage = 'https://www.cp2k.org'
-    url = 'https://sourceforge.net/projects/cp2k/files/cp2k-3.0.tar.bz2'
-    list_url = 'https://sourceforge.net/projects/cp2k/files/'
+    url = 'https://github.com/cp2k/cp2k/releases/download/v3.0.0/cp2k-3.0.tar.bz2'
+    list_url = 'https://github.com/cp2k/cp2k/releases'
 
+    version('6.1', '573a4de5a0ee2aaabb213e04543cb10f')
     version('5.1', 'f25cf301aec471d7059179de4dac3ee7')
     version('4.1', 'b0534b530592de15ac89828b1541185e')
     version('3.0', 'c05bc47335f68597a310b1ed75601d35')
 
     variant('mpi', default=True, description='Enable MPI support')
-    variant('smm', default='libxsmm', values=('libxsmm', 'libsmm', 'none'),
+    variant('blas', default='openblas', values=('openblas', 'mkl', 'accelerate'),
+            description='Enable the use of OpenBlas/MKL/Accelerate')
+    variant('openmp', default=False, description='Enable OpenMP support')
+    variant('smm', default='libxsmm', values=('libxsmm', 'libsmm', 'blas'),
             description='Library for small matrix multiplications')
     variant('plumed', default=False, description='Enable PLUMED support')
+    variant('libxc', default=True,
+            description='Support additional functionals via libxc')
+    variant('pexsi', default=False,
+            description=('Enable the alternative PEXSI method'
+                         'for density matrix evaluation'))
+    variant('elpa', default=False,
+            description='Enable optimised diagonalisation routines from ELPA')
 
     depends_on('python', type='build')
 
-    depends_on('lapack')
-    depends_on('blas')
-    depends_on('fftw@3:')
-    depends_on('libint@1.1.4:1.2', when='@3.0:5.999')
-    depends_on('libxsmm', when='smm=libxsmm')
-    depends_on('libxc@2.2.2:')
+    depends_on('fftw@3:', when='~openmp')
+    depends_on('fftw@3:+openmp', when='+openmp')
+
+    # see #1712 for the reason to enumerate BLAS libraries here
+    depends_on('openblas threads=none', when='blas=openblas ~openmp')
+    depends_on('openblas threads=openmp', when='blas=openblas +openmp')
+    depends_on('lapack', when='blas=openblas ~openmp')
+
+    depends_on('intel-mkl', when="blas=mkl ~openmp")
+    depends_on('intel-mkl threads=openmp', when='blas=mkl +openmp')
+
+    conflicts('blas=accelerate', '+openmp')  # there is no Accelerate with OpenMP support
+
+    depends_on('libxsmm@1.10:~header-only', when='smm=libxsmm')
+    # use pkg-config (support added in libxsmm-1.10) to link to libxsmm
+    depends_on('pkgconfig', type='build', when='smm=libxsmm')
+
+    # libint & libxc are always statically linked
+    depends_on('libint@1.1.4:1.2', when='@3.0:6.999', type='build')
+    depends_on('libxc@2.2.2:', when='+libxc@:5.5999', type='build')
+    depends_on('libxc@4.0.3:', when='+libxc@6.0:', type='build')
 
     depends_on('mpi@2:', when='+mpi')
     depends_on('scalapack', when='+mpi')
-    depends_on('elpa@2011.12:2016.13', when='+mpi')
-    depends_on('pexsi+fortran@0.9.0:0.9.999', when='+mpi@:4.999')
-    depends_on('pexsi+fortran@0.10.0:', when='+mpi@5.0:')
+    depends_on('elpa@2011.12:2016.13+openmp', when='+openmp+elpa@:5.999')
+    depends_on('elpa@2011.12:2017.11+openmp', when='+openmp+elpa@6.0:')
+    depends_on('elpa@2011.12:2016.13~openmp', when='~openmp+elpa@:5.999')
+    depends_on('elpa@2011.12:2017.11~openmp', when='~openmp+elpa@6.0:')
     depends_on('plumed+shared+mpi', when='+plumed+mpi')
     depends_on('plumed+shared~mpi', when='+plumed~mpi')
 
+    # while we link statically against PEXSI, its own deps may be linked in
+    # dynamically, therefore can't set this as pure build-type dependency.
+    depends_on('pexsi+fortran@0.9.0:0.9.999', when='+pexsi@:4.999')
+    depends_on('pexsi+fortran@0.10.0:', when='+pexsi@5.0:')
+
+    # PEXSI and ELPA need MPI in CP2K
+    conflicts('~mpi', '+pexsi')
+    conflicts('~mpi', '+elpa')
+
     # Apparently cp2k@4.1 needs an "experimental" version of libwannier.a
     # which is only available contacting the developer directly. See INSTALL
     # in the stage of cp2k@4.1
-    depends_on('wannier90', when='@3.0+mpi')
+    depends_on('wannier90', when='@3.0+mpi', type='build')
 
     # TODO : add dependency on CUDA
 
-    parallel = False
+    # CP2K needs compiler specific compilation flags, e.g. optflags
+    conflicts('%clang')
+    conflicts('%cray')
+    conflicts('%nag')
+    conflicts('%xl')
 
     def install(self, spec, prefix):
         # Construct a proper filename for the architecture file
         cp2k_architecture = '{0.architecture}-{0.compiler.name}'.format(spec)
-        cp2k_version = 'sopt' if '~mpi' in spec else 'popt'
+        cp2k_version = ('{prefix}{suffix}'
+                        .format(prefix='p' if '+mpi' in spec else 's',
+                                suffix='smp' if '+openmp' in spec else 'opt'))
+
         makefile_basename = '.'.join([cp2k_architecture, cp2k_version])
         makefile = join_path('arch', makefile_basename)
 
@@ -70,25 +113,30 @@ class Cp2k(Package):
                     '-funroll-loops',
                     '-ffast-math',
                     '-ftree-vectorize',
-                ], 'intel': [
+                ],
+                'intel': [
                     '-O2',
                     '-pc64',
                     '-unroll',
-                ]
+                ],
+                'pgi': [
+                    '-fast',
+                ],
             }
 
             dflags = ['-DNDEBUG']
 
-            libxc = spec['libxc:fortran,static']
+            if '+openmp' in spec:
+                fftw = spec['fftw:openmp']
+            else:
+                fftw = spec['fftw']
 
             cppflags = [
                 '-D__FFTW3',
                 '-D__LIBINT',
                 '-D__LIBINT_MAX_AM=6',
                 '-D__LIBDERIV_MAX_AM1=5',
-                '-D__LIBXC',
-                spec['fftw'].headers.cpp_flags,
-                libxc.headers.cpp_flags
+                fftw.headers.cpp_flags,
             ]
 
             if '^mpi@3:' in spec:
@@ -102,25 +150,40 @@ class Cp2k(Package):
             cflags = copy.deepcopy(optflags[self.spec.compiler.name])
             cxxflags = copy.deepcopy(optflags[self.spec.compiler.name])
             fcflags = copy.deepcopy(optflags[self.spec.compiler.name])
+            ldflags = []
+            libs = []
 
             if '%intel' in spec:
                 cflags.append('-fp-model precise')
                 cxxflags.append('-fp-model precise')
                 fcflags.extend(['-fp-model source', '-heap-arrays 64'])
+                if '+openmp' in spec:
+                    fcflags.append('-openmp')
+                    ldflags.append('-openmp')
             elif '%gcc' in spec:
                 fcflags.extend(['-ffree-form', '-ffree-line-length-none'])
+                if '+openmp' in spec:
+                    fcflags.append('-fopenmp')
+                    ldflags.append('-fopenmp')
+            elif '%pgi' in spec:
+                fcflags.extend(['-Mfreeform', '-Mextend'])
+                if '+openmp' in spec:
+                    fcflags.append('-mp')
+                    ldflags.append('-mp')
 
-            fftw = spec['fftw'].libs
-            ldflags = [fftw.search_flags]
+            ldflags.append(fftw.libs.search_flags)
 
             if 'superlu-dist@4.3' in spec:
-                ldflags = ['-Wl,--allow-multiple-definition'] + ldflags
+                ldflags.insert(0, '-Wl,--allow-multiple-definition')
 
-            libs = [
-                join_path(spec['libint'].prefix.lib, 'libint.so'),
-                join_path(spec['libint'].prefix.lib, 'libderiv.so'),
-                join_path(spec['libint'].prefix.lib, 'libr12.so')
-            ]
+            # libint-1.x.y has to be linked statically to work around
+            # inconsistencies in its Fortran interface definition
+            # (short-int vs int) which otherwise causes segfaults at runtime
+            # due to wrong offsets into the shared library symbols.
+            libs.extend([
+                join_path(spec['libint'].libs.directories[0], 'libderiv.a'),
+                join_path(spec['libint'].libs.directories[0], 'libint.a'),
+            ])
 
             if '+plumed' in self.spec:
                 # Include Plumed.inc in the Makefile
@@ -155,6 +218,7 @@ class Cp2k(Package):
             fc = self.compiler.fc if '~mpi' in spec else self.spec['mpi'].mpifc
             mkf.write('FC = {0}\n'.format(fc))
             mkf.write('LD = {0}\n'.format(fc))
+
             # Intel
             if '%intel' in self.spec:
                 cppflags.extend([
@@ -168,79 +232,90 @@ class Cp2k(Package):
                     '-free',
                     '-fpp'
                 ])
+
+            # FFTW, LAPACK, BLAS
+            lapack = spec['lapack'].libs
+            blas = spec['blas'].libs
+            ldflags.append((lapack + blas).search_flags)
+            libs.extend([str(x) for x in (fftw.libs, lapack, blas)])
+
             # MPI
             if '+mpi' in self.spec:
                 cppflags.extend([
                     '-D__parallel',
-                    '-D__LIBPEXSI',
                     '-D__SCALAPACK'
                 ])
 
-                elpa = spec['elpa']
-                if spec.satisfies('@:4.999'):
-                    if elpa.satisfies('@:2014.5.999'):
-                        cppflags.append('-D__ELPA')
-                    elif elpa.satisfies('@2014.6:2015.10.999'):
-                        cppflags.append('-D__ELPA2')
-                    else:
-                        cppflags.append('-D__ELPA3')
-                else:
-                    cppflags.append('-D__ELPA={0}{1:02d}'.format(
-                        elpa.version[0], int(elpa.version[1])))
-                    fcflags.append('-I' + join_path(
-                        elpa.prefix, 'include',
-                        'elpa-{0}'.format(str(elpa.version)), 'elpa'
-                    ))
+                scalapack = spec['scalapack'].libs
+                ldflags.append(scalapack.search_flags)
+
+                libs.extend(scalapack)
+                libs.extend(self.spec['mpi:cxx'].libs)
+                libs.extend(self.compiler.stdcxx_libs)
 
                 if 'wannier90' in spec:
                     cppflags.append('-D__WANNIER90')
+                    wannier = join_path(
+                        spec['wannier90'].libs.directories[0], 'libwannier.a'
+                    )
+                    libs.append(wannier)
 
-                fcflags.extend([
-                    # spec['elpa:fortran'].headers.cpp_flags
-                    '-I' + join_path(
-                        elpa.prefix,
-                        'include',
-                        'elpa-{0}'.format(str(elpa.version)),
-                        'modules'
-                    ),
-                    # spec[pexsi:fortran].headers.cpp_flags
-                    '-I' + join_path(spec['pexsi'].prefix, 'fortran')
-                ])
-                scalapack = spec['scalapack'].libs
-                ldflags.append(scalapack.search_flags)
+            if '+libxc' in spec:
+                libxc = spec['libxc:fortran,static']
+                cppflags += [
+                    '-D__LIBXC',
+                    libxc.headers.cpp_flags
+                ]
+
+                ldflags.append(libxc.libs.search_flags)
+                libs.append(str(libxc.libs))
+
+            if '+pexsi' in self.spec:
+                cppflags.append('-D__LIBPEXSI')
+                fcflags.append('-I' + join_path(
+                    spec['pexsi'].prefix, 'fortran'))
                 libs.extend([
-                    join_path(elpa.prefix.lib,
-                              'libelpa.{0}'.format(dso_suffix)),
-                    join_path(spec['pexsi'].prefix.lib, 'libpexsi.a'),
-                    join_path(spec['superlu-dist'].prefix.lib,
+                    join_path(spec['pexsi'].libs.directories[0],
+                              'libpexsi.a'),
+                    join_path(spec['superlu-dist'].libs.directories[0],
                               'libsuperlu_dist.a'),
                     join_path(
-                        spec['parmetis'].prefix.lib,
+                        spec['parmetis'].libs.directories[0],
                         'libparmetis.{0}'.format(dso_suffix)
                     ),
                     join_path(
-                        spec['metis'].prefix.lib,
+                        spec['metis'].libs.directories[0],
                         'libmetis.{0}'.format(dso_suffix)
                     ),
                 ])
 
-                if 'wannier90' in spec:
-                    wannier = join_path(
-                        spec['wannier90'].prefix.lib, 'libwannier.a'
-                    )
-                    libs.append(wannier)
-
-                libs.extend(scalapack)
-                libs.extend(self.spec['mpi:cxx'].libs)
-                libs.extend(self.compiler.stdcxx_libs)
-            # LAPACK / BLAS
-            lapack = spec['lapack'].libs
-            blas = spec['blas'].libs
-            ldflags.append((lapack + blas).search_flags)
-
-            ldflags.append(libxc.libs.search_flags)
+            if '+elpa' in self.spec:
+                elpa = spec['elpa']
+                elpa_suffix = '_openmp' if '+openmp' in elpa else ''
+                elpa_base_path = join_path(
+                    elpa.prefix,
+                    'include',
+                    'elpa{suffix}-{version!s}'.format(
+                        suffix=elpa_suffix, version=elpa.version))
+
+                fcflags.append('-I' + join_path(elpa_base_path, 'modules'))
+                libs.append(join_path(elpa.libs.directories[0],
+                                      ('libelpa{elpa_suffix}.{dso_suffix}'
+                                       .format(elpa_suffix=elpa_suffix,
+                                               dso_suffix=dso_suffix))))
 
-            libs.extend([str(x) for x in (fftw, lapack, blas, libxc.libs)])
+                if spec.satisfies('@:4.999'):
+                    if elpa.satisfies('@:2014.5.999'):
+                        cppflags.append('-D__ELPA')
+                    elif elpa.satisfies('@2014.6:2015.10.999'):
+                        cppflags.append('-D__ELPA2')
+                    else:
+                        cppflags.append('-D__ELPA3')
+                else:
+                    cppflags.append('-D__ELPA={0}{1:02d}'
+                                    .format(elpa.version[0],
+                                            int(elpa.version[1])))
+                    fcflags.append('-I' + join_path(elpa_base_path, 'elpa'))
 
             if 'smm=libsmm' in spec:
                 lib_dir = join_path('lib', cp2k_architecture, cp2k_version)
@@ -261,11 +336,10 @@ class Cp2k(Package):
             elif 'smm=libxsmm' in spec:
                 cppflags.extend([
                     '-D__LIBXSMM',
-                    spec['libxsmm'].headers.cpp_flags,
+                    '$(shell pkg-config --cflags-only-other libxsmmf)',
                 ])
-                libxsmm = spec['libxsmm'].libs
-                ldflags.append(libxsmm.search_flags)
-                libs.append(str(libxsmm))
+                fcflags.append('$(shell pkg-config --cflags-only-I libxsmmf)')
+                libs.append('$(shell pkg-config --libs libxsmmf)')
 
             dflags.extend(cppflags)
             cflags.extend(cppflags)
@@ -284,6 +358,7 @@ class Cp2k(Package):
                     ' '.join(ldflags) + ' -nofor_main')
                 )
             mkf.write('LIBS = {0}\n\n'.format(' '.join(libs)))
+            mkf.write('DATA_DIR = {0}\n\n'.format(self.prefix.share.data))
 
         with working_dir('makefiles'):
             # Apparently the Makefile bases its paths on PWD
@@ -295,3 +370,4 @@ class Cp2k(Package):
             env['PWD'] = pwd_backup
         exe_dir = join_path('exe', cp2k_architecture)
         install_tree(exe_dir, self.prefix.bin)
+        install_tree('data', self.prefix.share.data)
diff --git a/var/spack/repos/builtin/packages/libxc/package.py b/var/spack/repos/builtin/packages/libxc/package.py
index bdb5fcabd2..3eb3902e45 100644
--- a/var/spack/repos/builtin/packages/libxc/package.py
+++ b/var/spack/repos/builtin/packages/libxc/package.py
@@ -13,10 +13,21 @@ class Libxc(AutotoolsPackage):
     homepage = "http://www.tddft.org/programs/octopus/wiki/index.php/Libxc"
     url      = "http://www.tddft.org/programs/octopus/down.php?file=libxc/libxc-2.2.2.tar.gz"
 
+    version('4.2.3', '6176ac7edf234425d973903f82199350')
     version('3.0.0', '8227fa3053f8fc215bd9d7b0d36de03c')
     version('2.2.2', 'd9f90a0d6e36df6c1312b6422280f2ec')
     version('2.2.1', '38dc3a067524baf4f8521d5bb1cd0b8f')
 
+    def url_for_version(self, version):
+        if version < Version('3.0.0'):
+            return ("http://www.tddft.org/programs/octopus/"
+                    "down.php?file=libxc/libxc-{0}.tar.gz"
+                    .format(version))
+
+        return ("http://www.tddft.org/programs/octopus/"
+                "down.php?file=libxc/{0}/libxc-{0}.tar.gz"
+                .format(version))
+
     @property
     def libs(self):
         """Libxc can be queried for the following parameters:
@@ -38,7 +49,10 @@ class Libxc(AutotoolsPackage):
         # Libxc has a fortran90 interface: give clients the
         # possibility to query for it
         if 'fortran' in query_parameters:
-            libraries = ['libxcf90'] + libraries
+            if self.version < Version('4.0.0'):
+                libraries = ['libxcf90'] + libraries
+            else:  # starting from version 4 there is also a stable f03 iface
+                libraries = ['libxcf90', 'libxcf03'] + libraries
 
         return find_libraries(
             libraries, root=self.prefix, shared=shared, recursive=True
diff --git a/var/spack/repos/builtin/packages/libxsmm/package.py b/var/spack/repos/builtin/packages/libxsmm/package.py
index 848351ce1c..176fd61b37 100644
--- a/var/spack/repos/builtin/packages/libxsmm/package.py
+++ b/var/spack/repos/builtin/packages/libxsmm/package.py
@@ -4,6 +4,7 @@
 # SPDX-License-Identifier: (Apache-2.0 OR MIT)
 
 from spack import *
+import os
 from glob import glob
 
 
@@ -72,7 +73,7 @@ class Libxsmm(MakefilePackage):
 
     def build(self, spec, prefix):
         # include symbols by default
-        make_args = ['SYM=1']
+        make_args = ['SYM=1', 'PREFIX=%s' % prefix]
 
         if '+header-only' in spec:
             make_args += ['header-only']
@@ -89,6 +90,13 @@ class Libxsmm(MakefilePackage):
 
     def install(self, spec, prefix):
         install_tree('include', prefix.include)
+
+        # move pkg-config files to their right place
+        mkdirp('lib/pkgconfig')
+        for pcfile in glob('lib/*.pc'):
+            os.rename(pcfile, os.path.join('lib/pkgconfig',
+                                           os.path.basename(pcfile)))
+
         if '+header-only' in spec:
             install_tree('src', prefix.src)
         else:
diff --git a/var/spack/repos/builtin/packages/openblas/package.py b/var/spack/repos/builtin/packages/openblas/package.py
index dfe1506d1c..0d3cf91db7 100644
--- a/var/spack/repos/builtin/packages/openblas/package.py
+++ b/var/spack/repos/builtin/packages/openblas/package.py
@@ -58,7 +58,8 @@ class Openblas(MakefilePackage):
     provides('blas')
     provides('lapack')
 
-    patch('make.patch', when='@0.2.16:')
+    # OpenBLAS >=3.0 has an official way to disable internal parallel builds
+    patch('make.patch', when='@0.2.16:0.2.20')
     #  This patch is in a pull request to OpenBLAS that has not been handled
     #  https://github.com/xianyi/OpenBLAS/pull/915
     #  UPD: the patch has been merged starting version 0.2.20
@@ -87,10 +88,19 @@ class Openblas(MakefilePackage):
           sha256='714aea33692304a50bd0ccde42590c176c82ded4a8ac7f06e573dc8071929c33',
           when='@0.3.3')
 
-    parallel = False
+    # Fix parallel build issues on filesystems
+    # with missing sub-second timestamp resolution
+    patch('https://github.com/xianyi/OpenBLAS/commit/79ea839b635d1fd84b6ce8a47e086f01d64198e6.patch',
+          sha256='f1b066a4481a50678caeb7656bf3e6764f45619686ac465f257c8017a2dc1ff0',
+          when='@0.3.0:0.3.3')
 
     conflicts('%intel@16', when='@0.2.15:0.2.19')
 
+    @property
+    def parallel(self):
+        # unclear whether setting `-j N` externally was supported before 0.3
+        return self.spec.version >= Version('0.3.0')
+
     @run_before('edit')
     def check_compilers(self):
         # As of 06/2016 there is no mechanism to specify that packages which
@@ -124,13 +134,19 @@ class Openblas(MakefilePackage):
         make_defs = [
             'CC={0}'.format(spack_cc),
             'FC={0}'.format(spack_fc),
-            'MAKE_NO_J=1'
         ]
 
+        # force OpenBLAS to use externally defined parallel build
+        if self.spec.version < Version('0.3'):
+            make_defs.append('MAKE_NO_J=1')  # flag defined by our make.patch
+        else:
+            make_defs.append('MAKE_NB_JOBS=0')  # flag provided by OpenBLAS
+
         if self.spec.variants['virtual_machine'].value:
             make_defs += [
                 'DYNAMIC_ARCH=1',
-                'NO_AVX2=1'
+                'NO_AVX2=1',
+                'NUM_THREADS=64',  # OpenBLAS stores present no of CPUs as max
             ]
 
         if self.spec.variants['cpu_target'].value:
@@ -192,7 +208,7 @@ class Openblas(MakefilePackage):
     @run_after('build')
     @on_package_attributes(run_tests=True)
     def check_build(self):
-        make('tests', *self.make_defs)
+        make('tests', *self.make_defs, parallel=False)
 
     @property
     def install_targets(self):
-- 
cgit v1.2.3-70-g09d2