Last active
April 28, 2021 07:43
-
-
Save OlegJakushkin/a0f1b6e6bbf65e0b2987c73eba2e5df7 to your computer and use it in GitHub Desktop.
ManagedCuda 3d Complex FFT and reverse fft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Numerics; | |
using System.Runtime.InteropServices; | |
using ManagedCuda.BasicTypes; | |
using ManagedCuda; | |
using ManagedCuda.CudaFFT; | |
using ManagedCuda.VectorTypes; | |
class ManagedCuda3DComplexFFT | |
{ | |
public static class Extensions | |
{ | |
public delegate void ActOn3DArrayElement(ref cuFloatComplex element, int i, int j, int k); | |
public static void ForEach(ref cuFloatComplex[,,] arr, ActOn3DArrayElement act) | |
{ | |
for (int i = 0; i < arr.GetLength(0); i++) | |
{ | |
for (int j = 0; j < arr.GetLength(1); j++) | |
{ | |
for (int k = 0; k < arr.GetLength(2); k++) | |
{ | |
act(ref arr[i, j, k], i, j, k); | |
} | |
} | |
} | |
} | |
public static string ToString(ref cuFloatComplex[,,] arr) | |
{ | |
string result = ""; | |
for (int i = 0; i < arr.GetLength(0); i++) | |
{ | |
result += "\n" + i.ToString() + " {[\n"; | |
for (int j = 0; j < arr.GetLength(1); j++) | |
{ | |
result += "["; | |
for (int k = 0; k < arr.GetLength(2); k++) | |
{ | |
result += arr[i, j, k].ToString() + "; "; | |
} | |
result += "]\n"; | |
} | |
result += "]};"; | |
} | |
return result; | |
} | |
} | |
static void Main(string[] args) | |
{ | |
const int DIM = 2; | |
CudaContext ctx = new CudaContext(0); | |
var dx = new CudaDeviceVariable<cuFloatComplex>( DIM*DIM*DIM); | |
var data = new cuFloatComplex[DIM,DIM,DIM]; | |
var fftdData = new cuFloatComplex[DIM,DIM,DIM]; | |
var ifftdData = new cuFloatComplex[DIM,DIM,DIM]; | |
Extensions.ForEach(ref data, (ref cuFloatComplex element, int i, int j, int k) => | |
{ | |
element.real = 1; | |
element.imag = 1; | |
} ); | |
Console.WriteLine("source: " + Extensions.ToString(ref data)); | |
dx.CopyToDevice(data); | |
var plan = new CudaFFTPlan3D(DIM, DIM, DIM, cufftType.C2C); | |
for (int z = 0; z < 3; z++) | |
{ | |
Console.WriteLine("iteration == " + z); | |
plan.Exec(dx.DevicePointer, TransformDirection.Forward); // now inside dx.DevicePointer fftd data | |
dx.CopyToHost(fftdData); | |
plan.Exec(dx.DevicePointer, TransformDirection.Inverse); // now inside dx.DevicePointer ifftd data | |
dx.CopyToHost(ifftdData); | |
Console.WriteLine("fft: " + Extensions.ToString(ref fftdData)); | |
Console.WriteLine("ifft: " + Extensions.ToString(ref ifftdData)); | |
Console.WriteLine("=========================="); | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outputs
/usr/bin/mono /headless/Downloads/ShrodingerFlow-master/source/bin/Debug/ShrodingerFlow.exe
iteration == 0
source:
0 {[
[1 + 1i; 1 + 1i; ]
[1 + 1i; 1 + 1i; ]
]};
1 {[
[1 + 1i; 1 + 1i; ]
[1 + 1i; 1 + 1i; ]
]};
fft:
0 {[
[8 + 8i; 0 + 0i; ]
[0 + 0i; 0 + 0i; ]
]};
1 {[
[0 + 0i; 0 + 0i; ]
[0 + 0i; 0 + 0i; ]
]};
ifft:
0 {[
[8 + 8i; 8 + 8i; ]
[8 + 8i; 8 + 8i; ]
]};
1 {[
[8 + 8i; 8 + 8i; ]
[8 + 8i; 8 + 8i; ]
]};
iteration == 1
source:
0 {[
[1 + 1i; 1 + 1i; ]
[1 + 1i; 1 + 1i; ]
]};
1 {[
[1 + 1i; 1 + 1i; ]
[1 + 1i; 1 + 1i; ]
]};
fft:
0 {[
[64 + 64i; 0 + 0i; ]
[0 + 0i; 0 + 0i; ]
]};
1 {[
[0 + 0i; 0 + 0i; ]
[0 + 0i; 0 + 0i; ]
]};
ifft:
0 {[
[64 + 64i; 64 + 64i; ]
[64 + 64i; 64 + 64i; ]
]};
1 {[
[64 + 64i; 64 + 64i; ]
[64 + 64i; 64 + 64i; ]
]};
iteration == 2
source:
0 {[
[1 + 1i; 1 + 1i; ]
[1 + 1i; 1 + 1i; ]
]};
1 {[
[1 + 1i; 1 + 1i; ]
[1 + 1i; 1 + 1i; ]
]};
fft:
0 {[
[512 + 512i; 0 + 0i; ]
[0 + 0i; 0 + 0i; ]
]};
1 {[
[0 + 0i; 0 + 0i; ]
[0 + 0i; 0 + 0i; ]
]};
ifft:
0 {[
[512 + 512i; 512 + 512i; ]
[512 + 512i; 512 + 512i; ]
]};
1 {[
[512 + 512i; 512 + 512i; ]
[512 + 512i; 512 + 512i; ]
]};
Process finished with exit code 0.