From a71c596fd2d49e58bdbc2e312c0e9abad950de69 Mon Sep 17 00:00:00 2001
From: Michael Sarahan <msarahan@gmail.com>
Date: Mon, 11 Jun 2018 16:30:08 -0500
Subject: [PATCH 06/14] use mklfft when available

---
 numpy/fft/__init__.py | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/numpy/fft/__init__.py b/numpy/fft/__init__.py
index 72d61a728..73dc10eb6 100644
--- a/numpy/fft/__init__.py
+++ b/numpy/fft/__init__.py
@@ -9,3 +9,47 @@ from .helper import *
 from numpy.testing import _numpy_tester
 test = _numpy_tester().test
 bench = _numpy_tester().bench
+
+try:
+    import mkl_fft._numpy_fft as _nfft
+    patch_fft = True
+    __patched_functions__ = _nfft.__all__
+except ImportError:
+    patch_fft = False
+
+if patch_fft:
+    _restore_dict = {}
+    import sys
+
+    def register_func(name, func):
+        if name not in __patched_functions__:
+            raise ValueError("%s not an mkl_fft function." % name)
+        f = sys._getframe(0).f_globals
+        _restore_dict[name] = f[name]
+        f[name] = func
+
+    def restore_func(name):
+        if name not in __patched_functions__:
+            raise ValueError("%s not an mkl_fft function." % name)
+        try:
+            val = _restore_dict[name]
+        except KeyError:
+            print('failed to restore')
+            return
+        else:
+            print('found and restoring...')
+            sys._getframe(0).f_globals[name] = val
+
+    def restore_all():
+        for name in _restore_dict.keys():
+            restore_func(name)
+
+    for f in __patched_functions__:
+        register_func(f, getattr(_nfft, f))
+    del _nfft
+    using_mklfft = True
+else:
+    using_mklfft = False
+
+del patch_fft
+
-- 
2.11.1

