public FftwArray(params int[] lengths) { _lengths = lengths; long size = LongLength * Marshal.SizeOf <T>(); _ptr = FftwInterop.fftw_malloc(new IntPtr(size)); GC.AddMemoryPressure(size); }
public void Execute() { if (_plan == IntPtr.Zero) { throw new ObjectDisposedException(this.GetType().FullName); } FftwInterop.fftw_execute(_plan); }
public static string fftw_export_wisdom_to_string() { // We cannot use the fftw_export_wisdom_to_string function here // because we have no way of releasing the returned memory. StringBuilder sb = new StringBuilder(); FftwInterop.WriteCharHandler writeChar = (c, ptr) => sb.Append((char)c); FftwInterop.fftw_export_wisdom(writeChar, IntPtr.Zero); return(sb.ToString()); }
public void Dispose() { if (_ptr == IntPtr.Zero) { return; } FftwInterop.fftw_free(_ptr); _ptr = IntPtr.Zero; GC.RemoveMemoryPressure(LongLength * Marshal.SizeOf <T>()); }
protected override IntPtr GetPlan(int rank, int[] n, IntPtr bufferReal, IntPtr bufferComplex, DftDirection direction, PlannerFlags plannerFlags) { if (direction == DftDirection.Forwards) { return(FftwInterop.fftw_plan_dft_r2c(rank, n, bufferReal, bufferComplex, plannerFlags)); } else { return(FftwInterop.fftw_plan_dft_c2r(rank, n, bufferComplex, bufferReal, plannerFlags)); } }
public void Dispose() { if (_plan == IntPtr.Zero) { return; } lock (FftwInterop.Lock) { if (_plan == IntPtr.Zero) { return; } FftwInterop.fftw_destroy_plan(_plan); _plan = IntPtr.Zero; } }
static string GetVersion() { const string VersionPrefix = "fftw-"; const byte WhiteSpace = (byte)' '; byte[] prefix = Encoding.ASCII.GetBytes(VersionPrefix); int i = 0; StringBuilder sb = new StringBuilder(); FftwInterop.WriteCharHandler writeChar = (c, ptr) => { if (i < 0) { return; } if (i == VersionPrefix.Length) { if (c == WhiteSpace) { i = -1; } else { sb.Append((char)c); } } else if (c == prefix[i]) { i++; } else { i = 0; } }; // This is only called on initialization, so no synchronization/lock is required FftwInterop.fftw_export_wisdom(writeChar, IntPtr.Zero); return(sb.ToString()); }
internal protected FftwPlan(IPinnedArray <T1> buffer1, IPinnedArray <T2> buffer2, int rank, int[] n, bool verifyRankAndSize, DftDirection direction, PlannerFlags plannerFlags, int nThreads) { if (!FftwInterop.IsAvailable) { throw new InvalidOperationException($"{nameof(FftwInterop.IsAvailable)} returns false."); } if (buffer1.IsDisposed) { throw new ObjectDisposedException(nameof(buffer1)); } if (buffer2.IsDisposed) { throw new ObjectDisposedException(nameof(buffer2)); } if (verifyRankAndSize) { VerifyRankAndSize(buffer1, buffer2); } else { VerifyMinSize(buffer1, buffer2, n); } if (nThreads < 1) { nThreads = Environment.ProcessorCount; } _buffer1 = buffer1; _buffer2 = buffer2; _plan = IntPtr.Zero; lock (FftwInterop.Lock) { FftwInterop.fftw_plan_with_nthreads(nThreads); _plan = GetPlan(rank, n, _buffer1.Pointer, _buffer2.Pointer, direction, plannerFlags); } }
protected override IntPtr GetPlan(int rank, int[] n, IntPtr input, IntPtr output, DftDirection direction, PlannerFlags plannerFlags) { return(FftwInterop.fftw_plan_dft(rank, n, input, output, direction, plannerFlags)); }
/// <summary> /// Clears the current wisdom. /// </summary> /// <seealso cref="http://www.fftw.org/fftw3_doc/Forgetting-Wisdom.html#Forgetting-Wisdom"/> public static void Clear() { lock (FftwInterop.Lock) { FftwInterop.fftw_forget_wisdom(); } }
/// <summary> /// Imports wisdom from a file. The Current accumulated wisdom is replaced. /// Wisdom is hardware specific, thus importing wisdom created with different hardware /// can result in sub-optimal plans and should not be done. /// <seealso cref="http://www.fftw.org/fftw3_doc/Wisdom-Import.html#Wisdom-Import"/> /// <seealso cref="http://www.fftw.org/fftw3_doc/Caveats-in-Using-Wisdom.html#Caveats-in-Using-Wisdom"/> public static bool Import(string filename) { lock (FftwInterop.Lock) { return(FftwInterop.fftw_import_wisdom_from_filename(filename)); } }
/// <summary> /// Exports the accumulated wisdom to a file. /// </summary> /// <seealso cref="http://www.fftw.org/fftw3_doc/Wisdom-Export.html#Wisdom-Export"/> /// <seealso cref="http://www.fftw.org/fftw3_doc/Caveats-in-Using-Wisdom.html#Caveats-in-Using-Wisdom"/> public static bool Export(string filename) { lock (FftwInterop.Lock) { return(FftwInterop.fftw_export_wisdom_to_filename(filename)); } }