Last active
June 12, 2017 17:16
-
-
Save 9prady9/68d284787d743c1432b2481828862b21 to your computer and use it in GitHub Desktop.
This file contains 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
/* ************************************************************************ | |
* Copyright 2013 Advanced Micro Devices, Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* ************************************************************************/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
/* No need to explicitely include the OpenCL headers */ | |
#include <clFFT.h> | |
double r01() | |
{ | |
return (double)rand() / (double)RAND_MAX ; | |
} | |
int main(int argc, char** argv) | |
{ | |
int choice = argc > 1 ? atoi(argv[1]) : 0; | |
cl_int err; | |
cl_platform_id platform[2] = {0, 0}; | |
cl_device_id device = 0; | |
cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 }; | |
cl_context ctx = 0; | |
cl_command_queue queue = 0; | |
cl_mem bufX; | |
float *X; | |
cl_event event = NULL; | |
int ret = 0; | |
size_t N = 16; | |
size_t batch = 2; | |
char platform_name[128]; | |
char device_name[128]; | |
/* FFT library realted declarations */ | |
clfftPlanHandle planHandle; | |
clfftDim dim = CLFFT_1D; | |
size_t clLengths[3] = {N, batch, 1}; | |
size_t strides[3] = {1, N, N*batch}; | |
cl_int plat_avail = 0; | |
err = clGetPlatformIDs( 0, NULL, &plat_avail ); | |
printf("Available platforms %d\n", plat_avail); | |
/* Setup OpenCL environment. */ | |
err = clGetPlatformIDs( plat_avail, platform, NULL ); | |
size_t ret_param_size = 0; | |
err = clGetPlatformInfo(platform[choice], CL_PLATFORM_NAME, | |
sizeof(platform_name), platform_name, | |
&ret_param_size); | |
printf("Platform found: %s\n", platform_name); | |
err = clGetDeviceIDs( platform[choice], CL_DEVICE_TYPE_DEFAULT, 1, &device, NULL ); | |
err = clGetDeviceInfo(device, CL_DEVICE_NAME, | |
sizeof(device_name), device_name, | |
&ret_param_size); | |
printf("Device found on the above platform: %s\n", device_name); | |
props[1] = (cl_context_properties)platform[choice]; | |
ctx = clCreateContext( props, 1, &device, NULL, NULL, &err ); | |
queue = clCreateCommandQueue( ctx, device, 0, &err ); | |
/* Setup clFFT. */ | |
clfftSetupData fftSetup; | |
err = clfftInitSetupData(&fftSetup); | |
err = clfftSetup(&fftSetup); | |
/* Allocate host & initialize data. */ | |
/* Only allocation shown for simplicity. */ | |
X = (float *)malloc(N * 2 * sizeof(*X) * batch); | |
/* print input array */ | |
printf("\nPerforming fft on an one dimensional array of size N = %lu\n", (unsigned long)N); | |
for (int b=0; b<batch; ++b) | |
{ | |
unsigned offset = 2*b*N; | |
int print_iter = 0; | |
while(print_iter<N) { | |
float x , y; | |
if (print_iter<5) { | |
x = 1; | |
y = 0; | |
} else { | |
x = 0; | |
y = 0; | |
} | |
X[offset + 2*print_iter ] = x; | |
X[offset + 2*print_iter+1] = y; | |
printf("(%f, %f) ", x, y); | |
print_iter++; | |
} | |
printf("\n"); | |
printf("\n"); | |
} | |
printf("\n\nfft result: \n"); | |
/* Prepare OpenCL memory objects and place data inside them. */ | |
bufX = clCreateBuffer( ctx, CL_MEM_READ_WRITE, N * 2 * sizeof(*X) * batch, NULL, &err ); | |
err = clEnqueueWriteBuffer( queue, bufX, CL_TRUE, 0, | |
N * 2 * sizeof( *X ) * batch, X, 0, NULL, NULL ); | |
/* Create a default plan for a complex FFT. */ | |
err = clfftCreateDefaultPlan(&planHandle, ctx, dim, clLengths); | |
/* Set plan parameters. */ | |
err = clfftSetPlanPrecision(planHandle, CLFFT_SINGLE); | |
err = clfftSetLayout(planHandle, CLFFT_COMPLEX_INTERLEAVED, CLFFT_COMPLEX_INTERLEAVED); | |
err = clfftSetResultLocation(planHandle, CLFFT_INPLACE); | |
err = clfftSetPlanBatchSize(planHandle, batch); | |
err = clfftSetPlanDistance(planHandle, strides[dim], strides[dim]); | |
err = clfftSetPlanInStride(planHandle, dim, strides); | |
err = clfftSetPlanOutStride(planHandle, dim, strides); | |
err = clfftSetPlanScale(planHandle, CLFFT_FORWARD, 1.0); | |
/* Bake the plan. */ | |
err = clfftBakePlan(planHandle, 1, &queue, NULL, NULL); | |
/* Execute the plan. */ | |
err = clfftEnqueueTransform(planHandle, CLFFT_FORWARD, 1, &queue, 0, NULL, NULL, &bufX, &bufX, NULL); | |
/* Wait for calculations to be finished. */ | |
err = clFinish(queue); | |
/* Fetch results of calculations. */ | |
err = clEnqueueReadBuffer( queue, bufX, CL_TRUE, 0, N * 2 * sizeof( *X ) * batch, X, 0, NULL, NULL ); | |
/* print output array */ | |
for (int b=0; b<batch; ++b) | |
{ | |
int offset = 2*b*N; | |
int print_iter = 0; | |
while(print_iter<N) { | |
printf("(%f, %f) ", X[offset+2*print_iter], X[offset+2*print_iter+1]); | |
print_iter++; | |
} | |
printf("\n"); | |
printf("\n"); | |
} | |
/* Release OpenCL memory objects. */ | |
clReleaseMemObject( bufX ); | |
free(X); | |
/* Release the plan. */ | |
err = clfftDestroyPlan( &planHandle ); | |
/* Release clFFT library. */ | |
clfftTeardown( ); | |
/* Release OpenCL working objects. */ | |
clReleaseCommandQueue( queue ); | |
clReleaseContext( ctx ); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment