forked from tsai/mathnet-numerics
11 changed files with 284 additions and 14 deletions
@ -0,0 +1,33 @@ |
|||
#include "wrapper_common.h" |
|||
|
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <math.h> |
|||
#include <float.h> |
|||
#include "mkl_service.h" |
|||
#include "mkl_dfti.h" |
|||
|
|||
extern "C" { |
|||
|
|||
DLLEXPORT MKL_LONG z_fft_forward_inplace(MKL_LONG n, MKL_Complex16 x[]) |
|||
{ |
|||
MKL_LONG status = 0; |
|||
DFTI_DESCRIPTOR_HANDLE hand = 0; |
|||
status = DftiCreateDescriptor(&hand, DFTI_DOUBLE, DFTI_COMPLEX, 1, n); |
|||
if (0 != status) goto failed; |
|||
|
|||
status = DftiCommitDescriptor(hand); |
|||
if (0 != status) goto failed; |
|||
|
|||
status = DftiComputeForward(hand, x); |
|||
if (0 != status) goto failed; |
|||
|
|||
cleanup: |
|||
DftiFreeDescriptor(&hand); |
|||
return status; |
|||
|
|||
failed: |
|||
status = 1; |
|||
goto cleanup; |
|||
} |
|||
} |
|||
@ -0,0 +1,93 @@ |
|||
// <copyright file="SafeNativeMethods.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://mathnet.opensourcedotnet.info
|
|||
//
|
|||
// Copyright (c) 2009-2016 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.Numerics; |
|||
using System.Runtime.InteropServices; |
|||
using System.Security; |
|||
|
|||
namespace MathNet.Numerics.Providers.FourierTransform.Mkl |
|||
{ |
|||
/// <summary>
|
|||
/// P/Invoke methods to the native math libraries.
|
|||
/// </summary>
|
|||
[SuppressUnmanagedCodeSecurity] |
|||
[SecurityCritical] |
|||
internal static class SafeNativeMethods |
|||
{ |
|||
// ReSharper disable InconsistentNaming
|
|||
|
|||
/// <summary>
|
|||
/// Name of the native DLL.
|
|||
/// </summary>
|
|||
const string _DllName = "MathNet.Numerics.MKL.dll"; |
|||
internal static string DllName { get { return _DllName; } } |
|||
|
|||
[DllImport(_DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] |
|||
internal static extern int query_capability(int capability); |
|||
|
|||
[DllImport(_DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] |
|||
internal static extern void set_consistency_mode(int mode); |
|||
|
|||
[DllImport(_DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] |
|||
internal static extern void set_vml_mode(uint mode); |
|||
|
|||
[DllImport(_DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] |
|||
internal static extern void set_max_threads(int num_threads); |
|||
|
|||
#region Memory
|
|||
[DllImport(_DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] |
|||
internal static extern void free_buffers(); |
|||
|
|||
[DllImport(_DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] |
|||
internal static extern void thread_free_buffers(); |
|||
|
|||
[DllImport(_DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] |
|||
internal static extern int disable_fast_mm(); |
|||
|
|||
[DllImport(_DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] |
|||
internal static extern long mem_stat([Out]out int allocatedBuffers); |
|||
|
|||
[DllImport(_DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] |
|||
internal static extern long peak_mem_usage(int mode); |
|||
|
|||
#endregion Memory
|
|||
|
|||
#region FFT
|
|||
|
|||
[DllImport(_DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] |
|||
internal static extern long z_fft_forward_inplace(long n, [In, Out] Complex[] x); |
|||
|
|||
#endregion FFT
|
|||
|
|||
// ReSharper restore InconsistentNaming
|
|||
} |
|||
} |
|||
|
|||
#endif
|
|||
@ -0,0 +1,81 @@ |
|||
// <copyright file="FourierTransformProviderTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
//
|
|||
// Copyright (c) 2009-2016 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>
|
|||
|
|||
using System; |
|||
using NUnit.Framework; |
|||
|
|||
namespace MathNet.Numerics.UnitTests.FourierTransformProviderTests |
|||
{ |
|||
#if NOSYSNUMERICS
|
|||
using Complex = Numerics.Complex; |
|||
#else
|
|||
using Complex = System.Numerics.Complex; |
|||
#endif
|
|||
|
|||
/// <summary>
|
|||
/// Base class for linear algebra provider tests.
|
|||
/// </summary>
|
|||
[TestFixture, Category("LAProvider")] |
|||
public class LinearAlgebraProviderTests |
|||
{ |
|||
[Test] |
|||
public void ForwardInplace() |
|||
{ |
|||
var samples = Generate.PeriodicMap(16, w => new Complex(Math.Sin(w), 0), 16, 1.0, Constants.Pi2); |
|||
var spectrum = new Complex[samples.Length]; |
|||
|
|||
// real-odd transforms to imaginary odd
|
|||
samples.Copy(spectrum); |
|||
Control.FourierTransformProvider.ForwardInplace(spectrum); |
|||
|
|||
// all real components must be zero
|
|||
foreach (var c in spectrum) |
|||
{ |
|||
Assert.AreEqual(0, c.Real, 1e-12, "real"); |
|||
} |
|||
|
|||
// all imaginary components except second and last musth be zero
|
|||
for (var i = 0; i < spectrum.Length; i++) |
|||
{ |
|||
if (i == 1) |
|||
{ |
|||
Assert.AreEqual(-8, spectrum[i].Imaginary, 1e-12, "imag second"); |
|||
} |
|||
else if (i == spectrum.Length - 1) |
|||
{ |
|||
Assert.AreEqual(8, spectrum[i].Imaginary, 1e-12, "imag last"); |
|||
} |
|||
else |
|||
{ |
|||
Assert.AreEqual(0, spectrum[i].Imaginary, 1e-12, "imag"); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue