From aa8592305e55e8deb1079714a253ad563b018e71 Mon Sep 17 00:00:00 2001
From: Michael Sarahan <msarahan@gmail.com>
Date: Thu, 26 Jul 2018 11:23:44 -0500
Subject: [PATCH 06/14] use mklfft when available

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

diff --git a/numpy/fft/__init__.py b/numpy/fft/__init__.py
index bbb6ec8c7..7658c2e25 100644
--- a/numpy/fft/__init__.py
+++ b/numpy/fft/__init__.py
@@ -9,3 +9,44 @@ from .helper import *
 from numpy.testing._private.pytesttester import PytestTester
 test = PytestTester(__name__)
 del PytestTester
+
+
+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
+
+del patch_fft
-- 
2.17.1

