/
Interop.cs
873 lines (811 loc) · 42.5 KB
/
Interop.cs
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
// The code in this file is provided courtesy of Tamas Szalay. Some functionality has been added.
// FFTWSharp
// ===========
// Basic C# wrapper for FFTW.
//
// Features
// ============
// * Unmanaged function calls to main FFTW functions for both single and double precision
// * Basic managed wrappers for FFTW plans and unmanaged arrays
// * Test program that demonstrates basic functionality
//
// Notes
// ============
// * Most of this was written in 2005
// * Slightly updated since to get it running with Visual Studio Express 2010
// * If you have a question about FFTW, ask the FFTW people, and not me. I did not write FFTW.
// * If you have a question about this wrapper, probably still don't ask me, since I wrote it almost a decade ago.
using System;
using System.Runtime.InteropServices;
namespace FFTWSharp
{
// Various Flags used by FFTW
#region Enums
/// <summary>
/// FFTW planner flags
/// </summary>
[Flags]
public enum fftw_flags : uint
{
/// <summary>
/// Tells FFTW to find an optimized plan by actually computing several FFTs and measuring their execution time.
/// Depending on your machine, this can take some time (often a few seconds). Default (0x0).
/// </summary>
Measure=0,
/// <summary>
/// Specifies that an out-of-place transform is allowed to overwrite its
/// input array with arbitrary data; this can sometimes allow more efficient algorithms to be employed.
/// </summary>
DestroyInput=1,
/// <summary>
/// Rarely used. Specifies that the algorithm may not impose any unusual alignment requirements on the input/output
/// arrays (i.e. no SIMD). This flag is normally not necessary, since the planner automatically detects
/// misaligned arrays. The only use for this flag is if you want to use the guru interface to execute a given
/// plan on a different array that may not be aligned like the original.
/// </summary>
Unaligned=2,
/// <summary>
/// Not used.
/// </summary>
ConserveMemory=4,
/// <summary>
/// Like Patient, but considers an even wider range of algorithms, including many that we think are
/// unlikely to be fast, to produce the most optimal plan but with a substantially increased planning time.
/// </summary>
Exhaustive=8,
/// <summary>
/// Specifies that an out-of-place transform must not change its input array.
/// </summary>
/// <remarks>
/// This is ordinarily the default,
/// except for c2r and hc2r (i.e. complex-to-real) transforms for which DestroyInput is the default.
/// In the latter cases, passing PreserveInput will attempt to use algorithms that do not destroy the
/// input, at the expense of worse performance; for multi-dimensional c2r transforms, however, no
/// input-preserving algorithms are implemented and the planner will return null if one is requested.
/// </remarks>
PreserveInput=16,
/// <summary>
/// Like Measure, but considers a wider range of algorithms and often produces a “more optimal” plan
/// (especially for large transforms), but at the expense of several times longer planning time
/// (especially for large transforms).
/// </summary>
Patient=32,
/// <summary>
/// Specifies that, instead of actual measurements of different algorithms, a simple heuristic is
/// used to pick a (probably sub-optimal) plan quickly. With this flag, the input/output arrays
/// are not overwritten during planning.
/// </summary>
Estimate=64
}
/// <summary>
/// Defines direction of operation
/// </summary>
public enum fftw_direction : int
{
/// <summary>
/// Computes a regular DFT
/// </summary>
Forward=-1,
/// <summary>
/// Computes the inverse DFT
/// </summary>
Backward=1
}
/// <summary>
/// Kinds of real-to-real transforms
/// </summary>
public enum fftw_kind : uint
{
R2HC=0,
HC2R=1,
DHT=2,
REDFT00=3,
REDFT01=4,
REDFT10=5,
REDFT11=6,
RODFT00=7,
RODFT01=8,
RODFT10=9,
RODFT11=10
}
#endregion
// FFTW Interop Classes
#region Single Precision
/// <summary>
/// Contains the Basic Interface FFTW functions for single-precision (float) operations
/// </summary>
public class fftwf
{
/// <summary>
/// Allocates FFTW-optimized unmanaged memory
/// </summary>
/// <param name="length">Amount to allocate, in bytes</param>
/// <returns>Pointer to allocated memory</returns>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_malloc",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr malloc(int length);
/// <summary>
/// Deallocates memory allocated by FFTW malloc
/// </summary>
/// <param name="mem">Pointer to memory to release</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_free",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void free(IntPtr mem);
/// <summary>
/// Deallocates an FFTW plan and all associated resources
/// </summary>
/// <param name="plan">Pointer to the plan to release</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_destroy_plan",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void destroy_plan(IntPtr plan);
/// <summary>
/// Clears all memory used by FFTW, resets it to initial state. Does not replace destroy_plan and free
/// </summary>
/// <remarks>After calling fftw_cleanup, all existing plans become undefined, and you should not
/// attempt to execute them nor to destroy them. You can however create and execute/destroy new plans,
/// in which case FFTW starts accumulating wisdom information again.
/// fftw_cleanup does not deallocate your plans; you should still call fftw_destroy_plan for this purpose.</remarks>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_cleanup",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void cleanup();
/// <summary>
/// Sets the maximum time that can be used by the planner.
/// </summary>
/// <param name="seconds">Maximum time, in seconds.</param>
/// <remarks>This function instructs FFTW to spend at most seconds seconds (approximately) in the planner.
/// If seconds == -1.0 (the default value), then planning time is unbounded.
/// Otherwise, FFTW plans with a progressively wider range of algorithms until the the given time limit is
/// reached or the given range of algorithms is explored, returning the best available plan. For example,
/// specifying fftw_flags.Patient first plans in Estimate mode, then in Measure mode, then finally (time
/// permitting) in Patient. If fftw_flags.Exhaustive is specified instead, the planner will further progress to
/// Exhaustive mode.
/// </remarks>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_set_timelimit",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void set_timelimit(double seconds);
/// <summary>
/// Executes an FFTW plan, provided that the input and output arrays still exist
/// </summary>
/// <param name="plan">Pointer to the plan to execute</param>
/// <remarks>execute (and equivalents) is the only function in FFTW guaranteed to be thread-safe.</remarks>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_execute",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void execute(IntPtr plan);
/// <summary>
/// Creates a plan for a 1-dimensional complex-to-complex DFT
/// </summary>
/// <param name="n">The logical size of the transform</param>
/// <param name="direction">Specifies the direction of the transform</param>
/// <param name="input">Pointer to an array of 8-byte complex numbers</param>
/// <param name="output">Pointer to an array of 8-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_dft_1d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_1d(int n, IntPtr input, IntPtr output,
fftw_direction direction, fftw_flags flags);
/// <summary>
/// Creates a plan for a 2-dimensional complex-to-complex DFT
/// </summary>
/// <param name="nx">The logical size of the transform along the first dimension</param>
/// <param name="ny">The logical size of the transform along the second dimension</param>
/// <param name="direction">Specifies the direction of the transform</param>
/// <param name="input">Pointer to an array of 8-byte complex numbers</param>
/// <param name="output">Pointer to an array of 8-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_dft_2d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_2d(int nx, int ny, IntPtr input, IntPtr output,
fftw_direction direction, fftw_flags flags);
/// <summary>
/// Creates a plan for a 3-dimensional complex-to-complex DFT
/// </summary>
/// <param name="nx">The logical size of the transform along the first dimension</param>
/// <param name="ny">The logical size of the transform along the second dimension</param>
/// <param name="nz">The logical size of the transform along the third dimension</param>
/// <param name="direction">Specifies the direction of the transform</param>
/// <param name="input">Pointer to an array of 8-byte complex numbers</param>
/// <param name="output">Pointer to an array of 8-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_dft_3d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_3d(int nx, int ny, int nz, IntPtr input, IntPtr output,
fftw_direction direction, fftw_flags flags);
/// <summary>
/// Creates a plan for an n-dimensional complex-to-complex DFT
/// </summary>
/// <param name="rank">Number of dimensions</param>
/// <param name="n">Array containing the logical size along each dimension</param>
/// <param name="direction">Specifies the direction of the transform</param>
/// <param name="input">Pointer to an array of 8-byte complex numbers</param>
/// <param name="output">Pointer to an array of 8-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_dft",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft(int rank, int[] n, IntPtr input, IntPtr output,
fftw_direction direction, fftw_flags flags);
/// <summary>
/// Creates a plan for a 1-dimensional real-to-complex DFT
/// </summary>
/// <param name="n">Number of REAL (input) elements in the transform</param>
/// <param name="input">Pointer to an array of 4-byte real numbers</param>
/// <param name="output">Pointer to an array of 8-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_dft_r2c_1d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_r2c_1d(int n, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for a 2-dimensional real-to-complex DFT
/// </summary>
/// <param name="nx">Number of REAL (input) elements in the transform along the first dimension</param>
/// <param name="ny">Number of REAL (input) elements in the transform along the second dimension</param>
/// <param name="input">Pointer to an array of 4-byte real numbers</param>
/// <param name="output">Pointer to an array of 8-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_dft_r2c_2d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_r2c_2d(int nx, int ny, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for a 3-dimensional real-to-complex DFT
/// </summary>
/// <param name="nx">Number of REAL (input) elements in the transform along the first dimension</param>
/// <param name="ny">Number of REAL (input) elements in the transform along the second dimension</param>
/// <param name="nz">Number of REAL (input) elements in the transform along the third dimension</param>
/// <param name="input">Pointer to an array of 4-byte real numbers</param>
/// <param name="output">Pointer to an array of 8-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_dft_r2c_3d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_r2c_3d(int nx, int ny, int nz, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for an n-dimensional real-to-complex DFT
/// </summary>
/// <param name="rank">Number of dimensions</param>
/// <param name="n">Array containing the number of REAL (input) elements along each dimension</param>
/// <param name="input">Pointer to an array of 4-byte real numbers</param>
/// <param name="output">Pointer to an array of 8-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_dft_r2c",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_r2c(int rank, int[] n, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for a 1-dimensional complex-to-real DFT
/// </summary>
/// <param name="n">Number of REAL (output) elements in the transform</param>
/// <param name="input">Pointer to an array of 8-byte complex numbers</param>
/// <param name="output">Pointer to an array of 4-byte real numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_dft_c2r_1d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_c2r_1d(int n, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for a 2-dimensional complex-to-real DFT
/// </summary>
/// <param name="nx">Number of REAL (output) elements in the transform along the first dimension</param>
/// <param name="ny">Number of REAL (output) elements in the transform along the second dimension</param>
/// <param name="input">Pointer to an array of 8-byte complex numbers</param>
/// <param name="output">Pointer to an array of 4-byte real numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_dft_c2r_2d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_c2r_2d(int nx, int ny, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for a 3-dimensional complex-to-real DFT
/// </summary>
/// <param name="nx">Number of REAL (output) elements in the transform along the first dimension</param>
/// <param name="ny">Number of REAL (output) elements in the transform along the second dimension</param>
/// <param name="nz">Number of REAL (output) elements in the transform along the third dimension</param>
/// <param name="input">Pointer to an array of 8-byte complex numbers</param>
/// <param name="output">Pointer to an array of 4-byte real numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_dft_c2r_3d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_c2r_3d(int nx, int ny, int nz, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for an n-dimensional complex-to-real DFT
/// </summary>
/// <param name="rank">Number of dimensions</param>
/// <param name="n">Array containing the number of REAL (output) elements along each dimension</param>
/// <param name="input">Pointer to an array of 8-byte complex numbers</param>
/// <param name="output">Pointer to an array of 4-byte real numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_dft_c2r",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_c2r(int rank, int[] n, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for a 1-dimensional real-to-real DFT
/// </summary>
/// <param name="n">Number of elements in the transform</param>
/// <param name="input">Pointer to an array of 4-byte real numbers</param>
/// <param name="output">Pointer to an array of 4-byte real numbers</param>
/// <param name="kind">The kind of real-to-real transform to compute</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_r2r_1d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr r2r_1d(int n, IntPtr input, IntPtr output, fftw_kind kind, fftw_flags flags);
/// <summary>
/// Creates a plan for a 2-dimensional real-to-real DFT
/// </summary>
/// <param name="nx">Number of elements in the transform along the first dimension</param>
/// <param name="ny">Number of elements in the transform along the second dimension</param>
/// <param name="input">Pointer to an array of 4-byte real numbers</param>
/// <param name="output">Pointer to an array of 4-byte real numbers</param>
/// <param name="kindx">The kind of real-to-real transform to compute along the first dimension</param>
/// <param name="kindy">The kind of real-to-real transform to compute along the second dimension</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_r2r_2d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr r2r_2d(int nx, int ny, IntPtr input, IntPtr output,
fftw_kind kindx, fftw_kind kindy, fftw_flags flags);
/// <summary>
/// Creates a plan for a 3-dimensional real-to-real DFT
/// </summary>
/// <param name="nx">Number of elements in the transform along the first dimension</param>
/// <param name="ny">Number of elements in the transform along the second dimension</param>
/// <param name="nz">Number of elements in the transform along the third dimension</param>
/// <param name="input">Pointer to an array of 4-byte real numbers</param>
/// <param name="output">Pointer to an array of 4-byte real numbers</param>
/// <param name="kindx">The kind of real-to-real transform to compute along the first dimension</param>
/// <param name="kindy">The kind of real-to-real transform to compute along the second dimension</param>
/// <param name="kindz">The kind of real-to-real transform to compute along the third dimension</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_r2r_3d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr r2r_3d(int nx, int ny, int nz, IntPtr input, IntPtr output,
fftw_kind kindx, fftw_kind kindy, fftw_kind kindz, fftw_flags flags);
/// <summary>
/// Creates a plan for an n-dimensional real-to-real DFT
/// </summary>
/// <param name="rank">Number of dimensions</param>
/// <param name="n">Array containing the number of elements in the transform along each dimension</param>
/// <param name="input">Pointer to an array of 4-byte real numbers</param>
/// <param name="output">Pointer to an array of 4-byte real numbers</param>
/// <param name="kind">An array containing the kind of real-to-real transform to compute along each dimension</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_plan_r2r",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr r2r(int rank, int[] n, IntPtr input, IntPtr output,
fftw_kind[] kind, fftw_flags flags);
/// <summary>
/// Returns (approximately) the number of flops used by a certain plan
/// </summary>
/// <param name="plan">The plan to measure</param>
/// <param name="add">Reference to double to hold number of adds</param>
/// <param name="mul">Reference to double to hold number of muls</param>
/// <param name="fma">Reference to double to hold number of fmas (fused multiply-add)</param>
/// <remarks>Total flops ~= add+mul+2*fma or add+mul+fma if fma is supported</remarks>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_flops",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void flops(IntPtr plan, ref double add, ref double mul, ref double fma);
/// <summary>
/// Outputs a "nerd-readable" version of the specified plan to stdout
/// </summary>
/// <param name="plan">The plan to output</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_print_plan",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void print_plan(IntPtr plan);
/// <summary>
/// Exports the accumulated Wisdom to the provided filename
/// </summary>
/// <param name="filename">The target filename</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_export_wisdom_to_filename",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void export_wisdom_to_filename(string filename);
/// <summary>
/// Imports Wisdom from provided filename
/// </summary>
/// <param name="filename">The filename to read from</param>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_import_wisdom_from_filename",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void import_wisdom_from_filename(string filename);
/// <summary>
/// Forgets the Wisdom
/// </summary>
[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_forget_wisdom",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void fftwf_forget_wisdom();
}
#endregion
#region Double Precision
/// <summary>
/// Contains the Basic Interface FFTW functions for double-precision (double) operations
/// </summary>
public class fftw
{
/// <summary>
/// Allocates FFTW-optimized unmanaged memory
/// </summary>
/// <param name="length">Amount to allocate, in bytes</param>
/// <returns>Pointer to allocated memory</returns>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_malloc",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr malloc(int length);
/// <summary>
/// Deallocates memory allocated by FFTW malloc
/// </summary>
/// <param name="mem">Pointer to memory to release</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_free",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void free(IntPtr mem);
/// <summary>
/// Deallocates an FFTW plan and all associated resources
/// </summary>
/// <param name="plan">Pointer to the plan to release</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_destroy_plan",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void destroy_plan(IntPtr plan);
/// <summary>
/// Clears all memory used by FFTW, resets it to initial state. Does not replace destroy_plan and free
/// </summary>
/// <remarks>After calling fftw_cleanup, all existing plans become undefined, and you should not
/// attempt to execute them nor to destroy them. You can however create and execute/destroy new plans,
/// in which case FFTW starts accumulating wisdom information again.
/// fftw_cleanup does not deallocate your plans; you should still call fftw_destroy_plan for this purpose.</remarks>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_cleanup",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void cleanup();
/// <summary>
/// Sets the maximum time that can be used by the planner.
/// </summary>
/// <param name="seconds">Maximum time, in seconds.</param>
/// <remarks>This function instructs FFTW to spend at most seconds seconds (approximately) in the planner.
/// If seconds == -1.0 (the default value), then planning time is unbounded.
/// Otherwise, FFTW plans with a progressively wider range of algorithms until the the given time limit is
/// reached or the given range of algorithms is explored, returning the best available plan. For example,
/// specifying fftw_flags.Patient first plans in Estimate mode, then in Measure mode, then finally (time
/// permitting) in Patient. If fftw_flags.Exhaustive is specified instead, the planner will further progress to
/// Exhaustive mode.
/// </remarks>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_set_timelimit",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void set_timelimit(double seconds);
/// <summary>
/// Executes an FFTW plan, provided that the input and output arrays still exist
/// </summary>
/// <param name="plan">Pointer to the plan to execute</param>
/// <remarks>execute (and equivalents) is the only function in FFTW guaranteed to be thread-safe.</remarks>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_execute",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void execute(IntPtr plan);
/// <summary>
/// Creates a plan for a 1-dimensional complex-to-complex DFT
/// </summary>
/// <param name="n">The logical size of the transform</param>
/// <param name="direction">Specifies the direction of the transform</param>
/// <param name="input">Pointer to an array of 16-byte complex numbers</param>
/// <param name="output">Pointer to an array of 16-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_dft_1d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_1d(int n, IntPtr input, IntPtr output,
fftw_direction direction, fftw_flags flags);
/// <summary>
/// Creates a plan for a 2-dimensional complex-to-complex DFT
/// </summary>
/// <param name="nx">The logical size of the transform along the first dimension</param>
/// <param name="ny">The logical size of the transform along the second dimension</param>
/// <param name="direction">Specifies the direction of the transform</param>
/// <param name="input">Pointer to an array of 16-byte complex numbers</param>
/// <param name="output">Pointer to an array of 16-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_dft_2d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_2d(int nx, int ny, IntPtr input, IntPtr output,
fftw_direction direction, fftw_flags flags);
/// <summary>
/// Creates a plan for a 3-dimensional complex-to-complex DFT
/// </summary>
/// <param name="nx">The logical size of the transform along the first dimension</param>
/// <param name="ny">The logical size of the transform along the second dimension</param>
/// <param name="nz">The logical size of the transform along the third dimension</param>
/// <param name="direction">Specifies the direction of the transform</param>
/// <param name="input">Pointer to an array of 16-byte complex numbers</param>
/// <param name="output">Pointer to an array of 16-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_dft_3d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_3d(int nx, int ny, int nz, IntPtr input, IntPtr output,
fftw_direction direction, fftw_flags flags);
/// <summary>
/// Creates a plan for an n-dimensional complex-to-complex DFT
/// </summary>
/// <param name="rank">Number of dimensions</param>
/// <param name="n">Array containing the logical size along each dimension</param>
/// <param name="direction">Specifies the direction of the transform</param>
/// <param name="input">Pointer to an array of 16-byte complex numbers</param>
/// <param name="output">Pointer to an array of 16-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_dft",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft(int rank, int[] n, IntPtr input, IntPtr output,
fftw_direction direction, fftw_flags flags);
/// <summary>
/// Creates a plan for a 1-dimensional real-to-complex DFT
/// </summary>
/// <param name="n">Number of REAL (input) elements in the transform</param>
/// <param name="input">Pointer to an array of 8-byte real numbers</param>
/// <param name="output">Pointer to an array of 16-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_dft_r2c_1d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_r2c_1d(int n, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for a 2-dimensional real-to-complex DFT
/// </summary>
/// <param name="nx">Number of REAL (input) elements in the transform along the first dimension</param>
/// <param name="ny">Number of REAL (input) elements in the transform along the second dimension</param>
/// <param name="input">Pointer to an array of 8-byte real numbers</param>
/// <param name="output">Pointer to an array of 16-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_dft_r2c_2d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_r2c_2d(int nx, int ny, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for a 3-dimensional real-to-complex DFT
/// </summary>
/// <param name="nx">Number of REAL (input) elements in the transform along the first dimension</param>
/// <param name="ny">Number of REAL (input) elements in the transform along the second dimension</param>
/// <param name="nz">Number of REAL (input) elements in the transform along the third dimension</param>
/// <param name="input">Pointer to an array of 8-byte real numbers</param>
/// <param name="output">Pointer to an array of 16-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_dft_r2c_3d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_r2c_3d(int nx, int ny, int nz, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for an n-dimensional real-to-complex DFT
/// </summary>
/// <param name="rank">Number of dimensions</param>
/// <param name="n">Array containing the number of REAL (input) elements along each dimension</param>
/// <param name="input">Pointer to an array of 8-byte real numbers</param>
/// <param name="output">Pointer to an array of 16-byte complex numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_dft_r2c",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_r2c(int rank, int[] n, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for a 1-dimensional complex-to-real DFT
/// </summary>
/// <param name="n">Number of REAL (output) elements in the transform</param>
/// <param name="input">Pointer to an array of 16-byte complex numbers</param>
/// <param name="output">Pointer to an array of 8-byte real numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_dft_c2r_1d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_c2r_1d(int n, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for a 2-dimensional complex-to-real DFT
/// </summary>
/// <param name="nx">Number of REAL (output) elements in the transform along the first dimension</param>
/// <param name="ny">Number of REAL (output) elements in the transform along the second dimension</param>
/// <param name="input">Pointer to an array of 16-byte complex numbers</param>
/// <param name="output">Pointer to an array of 8-byte real numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_dft_c2r_2d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_c2r_2d(int nx, int ny, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for a 3-dimensional complex-to-real DFT
/// </summary>
/// <param name="nx">Number of REAL (output) elements in the transform along the first dimension</param>
/// <param name="ny">Number of REAL (output) elements in the transform along the second dimension</param>
/// <param name="nz">Number of REAL (output) elements in the transform along the third dimension</param>
/// <param name="input">Pointer to an array of 16-byte complex numbers</param>
/// <param name="output">Pointer to an array of 8-byte real numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_dft_c2r_3d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_c2r_3d(int nx, int ny, int nz, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for an n-dimensional complex-to-real DFT
/// </summary>
/// <param name="rank">Number of dimensions</param>
/// <param name="n">Array containing the number of REAL (output) elements along each dimension</param>
/// <param name="input">Pointer to an array of 16-byte complex numbers</param>
/// <param name="output">Pointer to an array of 8-byte real numbers</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_dft_c2r",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr dft_c2r(int rank, int[] n, IntPtr input, IntPtr output, fftw_flags flags);
/// <summary>
/// Creates a plan for a 1-dimensional real-to-real DFT
/// </summary>
/// <param name="n">Number of elements in the transform</param>
/// <param name="input">Pointer to an array of 8-byte real numbers</param>
/// <param name="output">Pointer to an array of 8-byte real numbers</param>
/// <param name="kind">The kind of real-to-real transform to compute</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_r2r_1d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr r2r_1d(int n, IntPtr input, IntPtr output, fftw_kind kind, fftw_flags flags);
/// <summary>
/// Creates a plan for a 2-dimensional real-to-real DFT
/// </summary>
/// <param name="nx">Number of elements in the transform along the first dimension</param>
/// <param name="ny">Number of elements in the transform along the second dimension</param>
/// <param name="input">Pointer to an array of 8-byte real numbers</param>
/// <param name="output">Pointer to an array of 8-byte real numbers</param>
/// <param name="kindx">The kind of real-to-real transform to compute along the first dimension</param>
/// <param name="kindy">The kind of real-to-real transform to compute along the second dimension</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_r2r_2d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr r2r_2d(int nx, int ny, IntPtr input, IntPtr output,
fftw_kind kindx, fftw_kind kindy, fftw_flags flags);
/// <summary>
/// Creates a plan for a 3-dimensional real-to-real DFT
/// </summary>
/// <param name="nx">Number of elements in the transform along the first dimension</param>
/// <param name="ny">Number of elements in the transform along the second dimension</param>
/// <param name="nz">Number of elements in the transform along the third dimension</param>
/// <param name="input">Pointer to an array of 8-byte real numbers</param>
/// <param name="output">Pointer to an array of 8-byte real numbers</param>
/// <param name="kindx">The kind of real-to-real transform to compute along the first dimension</param>
/// <param name="kindy">The kind of real-to-real transform to compute along the second dimension</param>
/// <param name="kindz">The kind of real-to-real transform to compute along the third dimension</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_r2r_3d",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr r2r_3d(int nx, int ny, int nz, IntPtr input, IntPtr output,
fftw_kind kindx, fftw_kind kindy, fftw_kind kindz, fftw_flags flags);
/// <summary>
/// Creates a plan for an n-dimensional real-to-real DFT
/// </summary>
/// <param name="rank">Number of dimensions</param>
/// <param name="n">Array containing the number of elements in the transform along each dimension</param>
/// <param name="input">Pointer to an array of 8-byte real numbers</param>
/// <param name="output">Pointer to an array of 8-byte real numbers</param>
/// <param name="kind">An array containing the kind of real-to-real transform to compute along each dimension</param>
/// <param name="flags">Flags that specify the behavior of the planner</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_plan_r2r",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr r2r(int rank, int[] n, IntPtr input, IntPtr output,
fftw_kind[] kind, fftw_flags flags);
/// <summary>
/// Returns (approximately) the number of flops used by a certain plan
/// </summary>
/// <param name="plan">The plan to measure</param>
/// <param name="add">Reference to double to hold number of adds</param>
/// <param name="mul">Reference to double to hold number of muls</param>
/// <param name="fma">Reference to double to hold number of fmas (fused multiply-add)</param>
/// <remarks>Total flops ~= add+mul+2*fma or add+mul+fma if fma is supported</remarks>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_flops",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void flops(IntPtr plan, ref double add, ref double mul, ref double fma);
/// <summary>
/// Outputs a "nerd-readable" version of the specified plan to stdout
/// </summary>
/// <param name="plan">The plan to output</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_print_plan",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void print_plan(IntPtr plan);
/// <summary>
/// Exports the accumulated Wisdom to the provided filename
/// </summary>
/// <param name="filename">The target filename</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_export_wisdom_to_filename",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void export_wisdom_to_filename(string filename);
/// <summary>
/// Imports Wisdom from provided filename
/// </summary>
/// <param name="filename">The filename to read from</param>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_import_wisdom_from_filename",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void import_wisdom_from_filename(string filename);
/// <summary>
/// Forgets the Wisdom
/// </summary>
[DllImport("libfftw3-3.dll",
EntryPoint = "fftw_forget_wisdom",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern void fftw_forget_wisdom();
}
#endregion
}