Repository: spack/spack

Issue:
Installation error: py-torch can't find mkl
After updating spack to the develop branch, I was able to get past issue #14216 with cmake 3.15.5 (actually, it took cmake 3.16.1).  Now my pytorch build fails because it can not find the mkl installation:
```
==> No patches needed for py-torch
==> Building py-torch [PythonPackage]
==> Executing phase: 'install'
==> Error: ProcessError: Command exited with status 1:
    '/lus/theta-fs0/projects/datascience/cadams/spack/cooley/spack/opt/spack/linux-rhel7-haswell/gcc-8.2.0/python-3.7.4-j4sbfz26j6k2mt2vzmvxhtklskffam33/bin/python3.7' '-s' 'setup.py' '--no-user-cfg' 'install' '--prefix=/lus/theta-fs0/projects/datascience/cadams/spack/cooley/spack/opt/spack/linux-rhel7-haswell/gcc-8.2.0/py-torch-1.3.1-wjp6uqx72jm7cybffnbumeosie6nk6og' '--single-version-externally-managed' '--root=/'

1 error found in build log:
     142    -- Checking for [mkl_gf_lp64 - mkl_core - pthread - m - dl]
     143    --   Library mkl_gf_lp64: not found
     144    -- Checking for [mkl_gf - mkl_core - pthread - m - dl]
     145    --   Library mkl_gf: not found
     146    -- Checking for [mkl - guide - pthread - m]
     147    --   Library mkl: not found
  >> 148    CMake Error at cmake/Modules/FindMKL.cmake:370 (MESSAGE):
     149      MKL library not found.  Please specify library location
     150    Call Stack (most recent call first):
     151      cmake/Dependencies.cmake:127 (find_package)
     152      CMakeLists.txt:373 (include)
     153
     154

See build log for details:
  /tmp/cadams/spack-stage/spack-stage-py-torch-1.3.1-wjp6uqx72jm7cybffnbumeosie6nk6og/spack-build-out.txt
```

Earlier in the build, I see intel-mkl and intel-mkldnn are installed:
```
==> intel-mkl is already installed in /lus/theta-fs0/projects/datascience/cadams/spack/cooley/spack/opt/spack/linux-rhel7-haswell/gcc-8.2.0/intel-mkl-2019.5.281-enqkpm3tb4irpjdudkvutqezekmpd7ju
==> intel-mkl-dnn is already installed in /lus/theta-fs0/projects/datascience/cadams/spack/cooley/spack/opt/spack/linux-rhel7-haswell/gcc-8.2.0/intel-mkl-dnn-1.1.1-cbpjooepbrhnkfcfaqv6h4ngqdr6gfar
```

Is there a way to tell pytorch that mkl is installed and have it find it?


System:
```console
$ uname -a
Linux cc047 3.10.0-957.27.2.el7.x86_64 #1 SMP Tue Jul 9 16:53:14 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
$ lsb_release
LSB Version:	:core-4.1-amd64:core-4.1-noarch
```


Discussion:
@coreyjadams I ran into the same issue, reported at https://github.com/pytorch/pytorch/issues/24334. The workaround for the time being is to symlink all of the necessary libraries in a place where PyTorch expects to find them. For example:
```bash
cd <mkl-installation-prefix>/mkl/lib
for i in intel64_lin/*
do
    ln -s $i $(basename $i)
done
```
Can you comment on https://github.com/pytorch/pytorch/issues/24334 so they know I'm not the only one facing this issue?

Resolving patch:
diff --git a/lib/spack/spack/build_systems/intel.py b/lib/spack/spack/build_systems/intel.py
--- a/lib/spack/spack/build_systems/intel.py
+++ b/lib/spack/spack/build_systems/intel.py
@@ -1048,11 +1048,19 @@ def _setup_dependent_env_callback(
             env_mods = {
                 'MKLROOT': self.normalize_path('mkl'),
                 'SPACK_COMPILER_EXTRA_RPATHS': self.component_lib_dir('mkl'),
+                'CMAKE_PREFIX_PATH': self.normalize_path('mkl'),
+                'CMAKE_LIBRARY_PATH': self.component_lib_dir('mkl'),
+                'CMAKE_INCLUDE_PATH': self.component_include_dir('mkl'),
             }
 
             env.set('MKLROOT', env_mods['MKLROOT'])
             env.append_path('SPACK_COMPILER_EXTRA_RPATHS',
                             env_mods['SPACK_COMPILER_EXTRA_RPATHS'])
+            env.append_path('CMAKE_PREFIX_PATH', env_mods['CMAKE_PREFIX_PATH'])
+            env.append_path('CMAKE_LIBRARY_PATH',
+                            env_mods['CMAKE_LIBRARY_PATH'])
+            env.append_path('CMAKE_INCLUDE_PATH',
+                            env_mods['CMAKE_INCLUDE_PATH'])
 
             debug_print("adding/modifying build env:", env_mods)
 
diff --git a/var/spack/repos/builtin/packages/py-torch/package.py b/var/spack/repos/builtin/packages/py-torch/package.py
--- a/var/spack/repos/builtin/packages/py-torch/package.py
+++ b/var/spack/repos/builtin/packages/py-torch/package.py
@@ -180,11 +180,6 @@ def enable_or_disable(variant, keyword='USE', var=None, newer=False):
                 else:
                     env.set('NO_' + var, 'ON')
 
-        # Build system has problems locating MKL libraries
-        # See https://github.com/pytorch/pytorch/issues/24334
-        if 'mkl' in self.spec:
-            env.prepend_path('CMAKE_PREFIX_PATH', self.spec['mkl'].prefix.mkl)
-
         # Build in parallel to speed up build times
         env.set('MAX_JOBS', make_jobs)
 


Test patch: