ningshuxia
2022-10-08 afedb8fd4e17a5a911deee3dae04a10a93e6a39a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// <copyright file="MklFourierTransformProvider.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// https://numerics.mathdotnet.com
//
// Copyright (c) 2009-2018 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
 
#if NATIVE
 
using System;
using System.Threading;
using IStation.Numerics.Providers.Common.Mkl;
using Complex = System.Numerics.Complex;
 
namespace IStation.Numerics.Providers.FourierTransform.Mkl
{
    internal class MklFourierTransformProvider : IFourierTransformProvider, IDisposable
    {
        const int MinimumCompatibleRevision = 11;
 
        class Kernel
        {
            public IntPtr Handle;
            public int[] Dimensions;
            public FourierTransformScaling Scaling;
            public bool Real;
            public bool Single;
        }
 
        readonly string _hintPath;
        Kernel _kernel;
 
        /// <param name="hintPath">Hint path where to look for the native binaries</param>
        internal MklFourierTransformProvider(string hintPath)
        {
            _hintPath = hintPath;
        }
 
        /// <summary>
        /// Try to find out whether the provider is available, at least in principle.
        /// Verification may still fail if available, but it will certainly fail if unavailable.
        /// </summary>
        public bool IsAvailable()
        {
            return MklProvider.IsAvailable(hintPath: _hintPath);
        }
 
        /// <summary>
        /// Initialize and verify that the provided is indeed available. If not, fall back to alternatives like the managed provider
        /// </summary>
        public void InitializeVerify()
        {
            int revision = MklProvider.Load(hintPath: _hintPath);
            if (revision < MinimumCompatibleRevision)
            {
                throw new NotSupportedException(FormattableString.Invariant($"MKL Native Provider revision r{revision} is too old. Consider upgrading to a newer version. Revision r{MinimumCompatibleRevision} and newer are supported."));
            }
 
            // we only support exactly one major version, since major version changes imply a breaking change.
            int fftMajor = SafeNativeMethods.query_capability((int) ProviderCapability.FourierTransformMajor);
            int fftMinor = SafeNativeMethods.query_capability((int) ProviderCapability.FourierTransformMinor);
            if (!(fftMajor == 1 && fftMinor >= 0))
            {
                throw new NotSupportedException(FormattableString.Invariant($"MKL Native Provider not compatible. Expecting Fourier transform v1 but provider implements v{fftMajor}."));
            }
        }
 
        /// <summary>
        /// Frees memory buffers, caches and handles allocated in or to the provider.
        /// Does not unload the provider itself, it is still usable afterwards.
        /// </summary>
        public virtual void FreeResources()
        {
            Kernel kernel = Interlocked.Exchange(ref _kernel, null);
            if (kernel != null)
            {
                SafeNativeMethods.x_fft_free(ref kernel.Handle);
            }
 
            MklProvider.FreeResources();
        }
 
        public override string ToString()
        {
            return MklProvider.Describe();
        }
 
        Kernel Configure(int length, FourierTransformScaling scaling, bool real, bool single)
        {
            Kernel kernel = Interlocked.Exchange(ref _kernel, null);
 
            if (kernel == null)
            {
                kernel = new Kernel
                {
                    Dimensions = new[] {length},
                    Scaling = scaling,
                    Real = real,
                    Single = single
                };
 
                if (single)
                {
                    if (real) SafeNativeMethods.s_fft_create(out kernel.Handle, length, (float)ForwardScaling(scaling, length), (float)BackwardScaling(scaling, length));
                    else SafeNativeMethods.c_fft_create(out kernel.Handle, length, (float)ForwardScaling(scaling, length), (float)BackwardScaling(scaling, length));
                }
                else
                {
                    if (real) SafeNativeMethods.d_fft_create(out kernel.Handle, length, ForwardScaling(scaling, length), BackwardScaling(scaling, length));
                    else SafeNativeMethods.z_fft_create(out kernel.Handle, length, ForwardScaling(scaling, length), BackwardScaling(scaling, length));
                }
 
                return kernel;
            }
 
            if (kernel.Dimensions.Length != 1 || kernel.Dimensions[0] != length || kernel.Scaling != scaling || kernel.Real != real || kernel.Single != single)
            {
                SafeNativeMethods.x_fft_free(ref kernel.Handle);
 
                if (single)
                {
                    if (real) SafeNativeMethods.s_fft_create(out kernel.Handle, length, (float)ForwardScaling(scaling, length), (float)BackwardScaling(scaling, length));
                    else SafeNativeMethods.c_fft_create(out kernel.Handle, length, (float)ForwardScaling(scaling, length), (float)BackwardScaling(scaling, length));
                }
                else
                {
                    if (real) SafeNativeMethods.d_fft_create(out kernel.Handle, length, ForwardScaling(scaling, length), BackwardScaling(scaling, length));
                    else SafeNativeMethods.z_fft_create(out kernel.Handle, length, ForwardScaling(scaling, length), BackwardScaling(scaling, length));
                }
 
                kernel.Dimensions = new[] {length};
                kernel.Scaling = scaling;
                kernel.Real = real;
                kernel.Single = single;
                return kernel;
            }
 
            return kernel;
        }
 
        Kernel Configure(int[] dimensions, FourierTransformScaling scaling, bool single)
        {
            if (dimensions.Length == 1)
            {
                return Configure(dimensions[0], scaling, false, single);
            }
 
            Kernel kernel = Interlocked.Exchange(ref _kernel, null);
 
            if (kernel == null)
            {
                kernel = new Kernel
                {
                    Dimensions = dimensions,
                    Scaling = scaling,
                    Real = false,
                    Single = single
                };
 
                long length = 1;
                for (int i = 0; i < dimensions.Length; i++)
                {
                    length *= dimensions[i];
                }
 
                if (single)
                {
                    SafeNativeMethods.c_fft_create_multidim(out kernel.Handle, dimensions.Length, dimensions, (float)ForwardScaling(scaling, length), (float)BackwardScaling(scaling, length));
                }
                else
                {
                    SafeNativeMethods.z_fft_create_multidim(out kernel.Handle, dimensions.Length, dimensions, ForwardScaling(scaling, length), BackwardScaling(scaling, length));
                }
 
                return kernel;
            }
 
            bool mismatch = kernel.Dimensions.Length != dimensions.Length || kernel.Scaling != scaling || kernel.Real != false || kernel.Single != single;
            if (!mismatch)
            {
                for (int i = 0; i < dimensions.Length; i++)
                {
                    if (dimensions[i] != kernel.Dimensions[i])
                    {
                        mismatch = true;
                        break;
                    }
                }
            }
 
            if (mismatch)
            {
                long length = 1;
                for (int i = 0; i < dimensions.Length; i++)
                {
                    length *= dimensions[i];
                }
 
                SafeNativeMethods.x_fft_free(ref kernel.Handle);
 
                if (single)
                {
                    SafeNativeMethods.c_fft_create_multidim(out kernel.Handle, dimensions.Length, dimensions, (float)ForwardScaling(scaling, length), (float)BackwardScaling(scaling, length));
                }
                else
                {
                    SafeNativeMethods.z_fft_create_multidim(out kernel.Handle, dimensions.Length, dimensions, ForwardScaling(scaling, length), BackwardScaling(scaling, length));
                }
 
                kernel.Dimensions = dimensions;
                kernel.Scaling = scaling;
                kernel.Real = false;
                kernel.Single = single;
                return kernel;
            }
 
            return kernel;
        }
 
        void Release(Kernel kernel)
        {
            Kernel existing = Interlocked.Exchange(ref _kernel, kernel);
            if (existing != null)
            {
                SafeNativeMethods.x_fft_free(ref existing.Handle);
            }
        }
 
        public void Forward(Complex32[] samples, FourierTransformScaling scaling)
        {
            Kernel kernel = Configure(samples.Length, scaling, false, true);
            SafeNativeMethods.c_fft_forward(kernel.Handle, samples);
            Release(kernel);
        }
 
        public void Forward(Complex[] samples, FourierTransformScaling scaling)
        {
            Kernel kernel = Configure(samples.Length, scaling, false, false);
            SafeNativeMethods.z_fft_forward(kernel.Handle, samples);
            Release(kernel);
        }
 
        public void Backward(Complex32[] spectrum, FourierTransformScaling scaling)
        {
            Kernel kernel = Configure(spectrum.Length, scaling, false, true);
            SafeNativeMethods.c_fft_backward(kernel.Handle, spectrum);
            Release(kernel);
        }
 
        public void Backward(Complex[] spectrum, FourierTransformScaling scaling)
        {
            Kernel kernel = Configure(spectrum.Length, scaling, false, false);
            SafeNativeMethods.z_fft_backward(kernel.Handle, spectrum);
            Release(kernel);
        }
 
        public void ForwardReal(float[] samples, int n, FourierTransformScaling scaling)
        {
            Kernel kernel = Configure(n, scaling, true, true);
            SafeNativeMethods.s_fft_forward(kernel.Handle, samples);
            Release(kernel);
        }
 
        public void ForwardReal(double[] samples, int n, FourierTransformScaling scaling)
        {
            Kernel kernel = Configure(n, scaling, true, false);
            SafeNativeMethods.d_fft_forward(kernel.Handle, samples);
            Release(kernel);
        }
 
        public void BackwardReal(float[] spectrum, int n, FourierTransformScaling scaling)
        {
            Kernel kernel = Configure(n, scaling, true, true);
            SafeNativeMethods.s_fft_backward(kernel.Handle, spectrum);
            Release(kernel);
 
            spectrum[n] = 0f;
        }
 
        public void BackwardReal(double[] spectrum, int n, FourierTransformScaling scaling)
        {
            Kernel kernel = Configure(n, scaling, true, false);
            SafeNativeMethods.d_fft_backward(kernel.Handle, spectrum);
            Release(kernel);
 
            spectrum[n] = 0d;
        }
 
        public void ForwardMultidim(Complex32[] samples, int[] dimensions, FourierTransformScaling scaling)
        {
            Kernel kernel = Configure(dimensions, scaling, true);
            SafeNativeMethods.c_fft_forward(kernel.Handle, samples);
            Release(kernel);
        }
 
        public void ForwardMultidim(Complex[] samples, int[] dimensions, FourierTransformScaling scaling)
        {
            Kernel kernel = Configure(dimensions, scaling, false);
            SafeNativeMethods.z_fft_forward(kernel.Handle, samples);
            Release(kernel);
        }
 
        public void BackwardMultidim(Complex32[] spectrum, int[] dimensions, FourierTransformScaling scaling)
        {
            Kernel kernel = Configure(dimensions, scaling, true);
            SafeNativeMethods.c_fft_backward(kernel.Handle, spectrum);
            Release(kernel);
        }
 
        public void BackwardMultidim(Complex[] spectrum, int[] dimensions, FourierTransformScaling scaling)
        {
            Kernel kernel = Configure(dimensions, scaling, false);
            SafeNativeMethods.z_fft_backward(kernel.Handle, spectrum);
            Release(kernel);
        }
 
        static double ForwardScaling(FourierTransformScaling scaling, long length)
        {
            switch (scaling)
            {
                case FourierTransformScaling.SymmetricScaling:
                    return Math.Sqrt(1.0/length);
                case FourierTransformScaling.ForwardScaling:
                    return 1.0/length;
                default:
                    return 1.0;
            }
        }
 
        static double BackwardScaling(FourierTransformScaling scaling, long length)
        {
            switch (scaling)
            {
                case FourierTransformScaling.SymmetricScaling:
                    return Math.Sqrt(1.0/length);
                case FourierTransformScaling.BackwardScaling:
                    return 1.0/length;
                default:
                    return 1.0;
            }
        }
 
        public void Dispose()
        {
            FreeResources();
        }
    }
}
 
#endif