-
I'm new to FFT and I need to use FFT in my Metal project so I found VkFFT a suitable choice. But after I tried a simple 1D C2C FFT on Here is my simple code: VkFFTResult res = VKFFT_SUCCESS;
// Prepare input data [0, 1, 2, 3, 4, 5, 6, 7]
float* input_buffer = new float[2 * N];
for (int i = 0; i < N; i++) {
input_buffer[i * 2] = (float)i;
input_buffer[i * 2 + 1] = 0.0f;
}
// Configuration + FFT application
VkFFTConfiguration configuration = {};
VkFFTApplication app = {};
configuration.FFTdim = 1; //FFT dimension, 1D, 2D or 3D (default 1).
configuration.size[0] = sizeof(float) * N;
configuration.numberBatches = 1;
configuration.device = MTL::CreateSystemDefaultDevice();
configuration.queue = configuration.device->newCommandQueue();
// Prepare Metal Buffer
uint64_t bufferSize = (uint64_t)sizeof(float) * 2 * N;
MTL::Buffer* buffer = configuration.device->newBuffer(input_buffer, bufferSize, MTL::ResourceStorageModeShared);
configuration.buffer = &buffer;
configuration.bufferSize = &bufferSize;
res = initializeVkFFT(&app, configuration);
if (res != VKFFT_SUCCESS) {
printf("[ERROR] initializeVkFFT returned %d\n", res);
}
// Prepare launch params
VkFFTLaunchParams launchParams = {};
MTL::CommandBuffer* commandBuffer = configuration.queue->commandBuffer();
if (commandBuffer == 0) {
printf("[ERROR] failed to create command buffer.\n");
}
launchParams.commandBuffer = commandBuffer;
MTL::ComputeCommandEncoder* commandEncoder = commandBuffer->computeCommandEncoder();
if (commandEncoder == 0) {
printf("[ERROR] failed to create command encoder.\n");
}
launchParams.commandEncoder = commandEncoder;
// encode FFT
res = VkFFTAppend(&app, -1, &launchParams);
if (res != VKFFT_SUCCESS) {
printf("[ERROR] VkFFTAppend returned %d\n", res);
}
commandEncoder->endEncoding();
// commit and wait
commandBuffer->commit();
commandBuffer->waitUntilCompleted();
// Get outputs
float* output = (float*)buffer->contents();
for (int i = 0; i < N; i++) {
printf("%f + %fj\n", output[i * 2], output[i * 2 + 1]);
}
// Post-processings
commandEncoder->release();
commandBuffer->release();
buffer->release();
deleteVkFFT(&app);
delete[] input_buffer;
printf("Done.\n"); The output is:
But NumPy FFT gives different output: import numpy as np
x = np.arange(start=0.0, stop=8.0, dtype=np.float32)
y = np.fft.fft(x) output:
I don't know what went wrong. Can someone help me? Thanks. |
Beta Was this translation helpful? Give feedback.
Answered by
AtomicVar
Jul 31, 2023
Replies: 1 comment
-
Sorry, I misunderstood the // configuration.size[0] = sizeof(float) * N;
configuration.size[0] = N; |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
AtomicVar
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry, I misunderstood the
size
member ofVkFFTConfiguration
. After I fixed it, the output is the same. I'm closing this discussion.