Repository: spack/spack

Issue:
Installation issue: quantum espresso does not build correctly with OpenMP threading
$ spack install quantum-espresso %gcc@10.2.0 +openmp ^fftw@3:+openmp ^openblas threads=openmp
..
     2767    /nvme/martinh/work/spack/opt/spack/linux-centos8-broadwell/gcc-10.2.0/openmpi-4.0.5-wb
             hohbw6htvmkj4qgo5qtvmtzpcqdydi/bin/mpif90 -g -pthread -fopenmp -o pw.x \
     2768       pwscf.o  libpw.a ../../Modules/libqemod.a ../../KS_Solvers/libks_solvers.a ../../up
             flib/libupf.a ../../FFTXlib/libqefft.a ../../LAXlib/libqela.a ../../UtilXlib/libutil.a
              ../../dft-d3/libdftd3qe.a /tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-l
             o637bp5qaoqb37i6hoygwdfzcozrxmj/spack-src//clib/clib.a /tmp/martinh/spack-stage/spack-
             stage-quantum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcozrxmj/spack-src//LIBBEEF/libbeef
             .a   -L/nvme/martinh/work/spack/opt/spack/linux-centos8-broadwell/gcc-10.2.0/netlib-sc
             alapack-2.1.0-wp57p2d4eg3z36ldyrj7ontpgtak5hen/lib -lscalapack -L/usr/local/lib -L/nvm
             e/martinh/work/spack/opt/spack/linux-centos8-broadwell/gcc-10.2.0/openblas-0.3.14-y7hg
             6m6qiqejzywdrjfmlyfqaxcheqmg/lib -lopenblas -L/tmp/martinh/spack-stage/spack-stage-qua
             ntum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcozrxmj/spack-src//FoX/lib  -lFoX_dom -lFoX
             _sax -lFoX_wxml -lFoX_common -lFoX_utils -lFoX_fsys  -L/nvme/martinh/work/spack/opt/sp
             ack/linux-centos8-broadwell/gcc-10.2.0/fftw-3.3.9-zfbeifj7jv4tx4ospgbppinlo27x2um6/lib
              -lfftw3 -L/nvme/martinh/work/spack/opt/spack/linux-centos8-broadwell/gcc-10.2.0/openb
             las-0.3.14-y7hg6m6qiqejzywdrjfmlyfqaxcheqmg/lib -lopenblas
     2769    ../../FFTXlib/libqefft.a(fft_scalar.FFTW3.o): In function `init_plan':
  >> 2770    /tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcoz
             rxmj/spack-src/FFTXlib/fft_scalar.FFTW3.f90:631: undefined reference to `dfftw_cleanup
             _threads_'
  >> 2771    /tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcoz
             rxmj/spack-src/FFTXlib/fft_scalar.FFTW3.f90:632: undefined reference to `fftw_init_thr
             eads'
  >> 2772    /tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcoz
             rxmj/spack-src/FFTXlib/fft_scalar.FFTW3.f90:633: undefined reference to `dfftw_plan_wi
             th_nthreads_'
  >> 2773    /tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcoz
             rxmj/spack-src/FFTXlib/fft_scalar.FFTW3.f90:447: undefined reference to `dfftw_cleanup
             _threads_'
  >> 2774    /tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcoz
             rxmj/spack-src/FFTXlib/fft_scalar.FFTW3.f90:448: undefined reference to `fftw_init_thr
             eads'
  >> 2775    /tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcoz
             rxmj/spack-src/FFTXlib/fft_scalar.FFTW3.f90:449: undefined reference to `dfftw_plan_wi
             th_nthreads_'
  >> 2776    /tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcoz
             rxmj/spack-src/FFTXlib/fft_scalar.FFTW3.f90:301: undefined reference to `dfftw_cleanup
             _threads_'
  >> 2777    /tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcoz
             rxmj/spack-src/FFTXlib/fft_scalar.FFTW3.f90:302: undefined reference to `fftw_init_thr
             eads'
  >> 2778    /tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcoz
             rxmj/spack-src/FFTXlib/fft_scalar.FFTW3.f90:303: undefined reference to `dfftw_plan_wi
             th_nthreads_'
  >> 2779    /tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcoz
             rxmj/spack-src/FFTXlib/fft_scalar.FFTW3.f90:148: undefined reference to `dfftw_cleanup
             _threads_'
  >> 2780    /tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcoz
             rxmj/spack-src/FFTXlib/fft_scalar.FFTW3.f90:149: undefined reference to `fftw_init_thr
             eads'
  >> 2781    /tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-lo637bp5qaoqb37i6hoygwdfzcoz
             rxmj/spack-src/FFTXlib/fft_scalar.FFTW3.f90:150: undefined reference to `dfftw_plan_wi
             th_nthreads_'
  >> 2782    collect2: error: ld returned 1 exit status
     2783    make[2]: *** [Makefile:262: pw.x] Error 1
     2784    make[2]: Leaving directory '/tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-
             lo637bp5qaoqb37i6hoygwdfzcozrxmj/spack-src/PW/src'
     2785    make[1]: *** [Makefile:9: pw] Error 1
     2786    make[1]: Leaving directory '/tmp/martinh/spack-stage/spack-stage-quantum-espresso-6.7-
             lo637bp5qaoqb37i6hoygwdfzcozrxmj/spack-src/PW'
     2787    make: *** [Makefile:66: pw] Error 1



### Information on your system

$ spack debug report
* **Spack:** 0.16.1-2129-5500eed60c
* **Python:** 3.6.8
* **Platform:** linux-centos8-broadwell
* **Concretizer:** original

@naromero77 

### General information

<!-- These boxes can be checked by replacing [ ] with [x] or by clicking them after submitting the issue. -->
- [x] I have run `spack debug report` and reported the version of Spack/Python/Platform
- [x] I have run `spack maintainers <name-of-the-package>` and @mentioned any maintainers
- [x] I have uploaded the build log and environment files
- [x] I have searched the issues of this repo and believe this is not a duplicate

### Resolution
The following patch fixes the problem:

```
--- a/var/spack/repos/builtin/packages/quantum-espresso/package.py      2021-04-09 15:21:39.628603709 +0200
+++ b/var/spack/repos/builtin/packages/quantum-espresso/package.py      2021-04-09 15:21:56.060603484 +0200
@@ -204,6 +204,13 @@
     conflicts('@6.5:', when='+environ',
               msg='6.4.x is the latest QE series supported by Environ')
 
+    # Need OpenMP threaded FFTW and BLAS libraries when configured 
+    # with OpenMP support
+    conflicts('^fftw~openmp', when='+openmp')
+    conflicts('^amdfftw~openmp', when='+openmp')
+    conflicts('^openblas threads=none', when='+openmp')
+    conflicts('^openblas threads=pthreads', when='+openmp')
+
     # 6.4.1
     patch_url = 'https://raw.githubusercontent.com/QMCPACK/qmcpack/develop/external_codes/quantum_espresso/add_pw2qmcpack_to_qe-6.4.1.diff'
     patch_checksum = '57cb1b06ee2653a87c3acc0dd4f09032fcf6ce6b8cbb9677ae9ceeb6a78f85e2'
@@ -346,7 +353,10 @@
         if '^fftw@3:' in spec:
             fftw_prefix = spec['fftw'].prefix
             options.append('FFTW_INCLUDE={0}'.format(fftw_prefix.include))
-            fftw_ld_flags = spec['fftw'].libs.ld_flags
+            if '+openmp' in spec:
+                fftw_ld_flags = spec['fftw:openmp'].libs.ld_flags
+            else:
+                fftw_ld_flags = spec['fftw'].libs.ld_flags
             options.append('FFT_LIBS={0}'.format(fftw_ld_flags))
 
         if '^amdfftw' in spec:
```
[spack-build-env.txt](https://github.com/spack/spack/files/6286053/spack-build-env.txt)
[spack-build-out.txt](https://github.com/spack/spack/files/6286055/spack-build-out.txt)




Discussion:
Thank you for your contribution. Can you please go ahead and make a pull request with your patch?

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/quantum-espresso/package.py b/var/spack/repos/builtin/packages/quantum-espresso/package.py
--- a/var/spack/repos/builtin/packages/quantum-espresso/package.py
+++ b/var/spack/repos/builtin/packages/quantum-espresso/package.py
@@ -209,6 +209,13 @@ class QuantumEspresso(Package):
     patch_checksum = '72564c168231dd4a1279a74e76919af701d47cee9a851db6e205753004fe9bb5'
     patch(patch_url, sha256=patch_checksum, when='@6.7+qmcpack')
 
+    # Need OpenMP threaded FFTW and BLAS libraries when configured
+    # with OpenMP support
+    conflicts('^fftw~openmp', when='+openmp')
+    conflicts('^amdfftw~openmp', when='+openmp')
+    conflicts('^openblas threads=none', when='+openmp')
+    conflicts('^openblas threads=pthreads', when='+openmp')
+
     # 6.4.1
     patch_url = 'https://raw.githubusercontent.com/QMCPACK/qmcpack/develop/external_codes/quantum_espresso/add_pw2qmcpack_to_qe-6.4.1.diff'
     patch_checksum = '57cb1b06ee2653a87c3acc0dd4f09032fcf6ce6b8cbb9677ae9ceeb6a78f85e2'
@@ -347,7 +354,10 @@ def install(self, spec, prefix):
         if '^fftw@3:' in spec:
             fftw_prefix = spec['fftw'].prefix
             options.append('FFTW_INCLUDE={0}'.format(fftw_prefix.include))
-            fftw_ld_flags = spec['fftw'].libs.ld_flags
+            if '+openmp' in spec:
+                fftw_ld_flags = spec['fftw:openmp'].libs.ld_flags
+            else:
+                fftw_ld_flags = spec['fftw'].libs.ld_flags
             options.append('FFT_LIBS={0}'.format(fftw_ld_flags))
 
         if '^amdfftw' in spec:


Test patch: