[clfft] 29/64: examples framework

Jérôme Kieffer kieffer-guest at moszumanska.debian.org
Wed May 20 07:33:34 UTC 2015


This is an automated email from the git hooks/post-receive script.

kieffer-guest pushed a commit to branch develop
in repository clfft.

commit c2eedd88d1447b9ea04b7cf245d702864d1deb80
Author: Pradeep <pradeep at arrayfire.com>
Date:   Mon Mar 9 11:19:54 2015 -0400

    examples framework
    
    one dimensional fft example has been added. The example from README
    has been copied over with some additional modifications to display
    the input, output and the OpenCL device on which fft is performed.
---
 .gitignore                  |   3 +
 src/CMakeLists.txt          |   5 ++
 src/examples/CMakeLists.txt |  44 ++++++++++++++
 src/examples/fft1d.c        | 136 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 188 insertions(+)

diff --git a/.gitignore b/.gitignore
index 620d3dc..391e7c1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,3 +11,6 @@
 *.lai
 *.la
 *.a
+
+# ignore build directory if name is 'build'
+build/
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index e04b535..b56d266 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -71,6 +71,7 @@ option( BUILD_CLIENT "Build a command line clFFT client program with a variety o
 option( BUILD_TEST "Build the library testing suite (dependency on google test, Boost, and FFTW)" ON )
 option( BUILD_LOADLIBRARIES "Build the optional dynamic load libraries that the FFT runtime will search for" ON )
 option( BUILD_SHARED_LIBRARY "Build shared libraries." ON)
+option( BUILD_EXAMPLES "Build examples." ON)
 
 # If BOOST_ROOT is defined as an environment value, use that value and cache it so it's visible in the cmake-gui.
 # Otherwise, create a sensible default that the user can change
@@ -279,6 +280,10 @@ else( )
 	message( "GoogleTest unit tests will NOT be built" )
 endif( )
 
+if( BUILD_EXAMPLES )
+    add_subdirectory( examples )
+endif()
+
 # The following code is setting variables to control the behavior of CPack to generate our
 if( WIN32 )
 	set( CPACK_SOURCE_GENERATOR "ZIP" )
diff --git a/src/examples/CMakeLists.txt b/src/examples/CMakeLists.txt
new file mode 100644
index 0000000..5655e5d
--- /dev/null
+++ b/src/examples/CMakeLists.txt
@@ -0,0 +1,44 @@
+# ########################################################################
+# 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_DIRECTORIES(
+    "${CMAKE_CURRENT_SOURCE_DIR}"
+    "${OPENCL_INCLUDE_DIRS}"
+    "${PROJECT_SOURCE_DIR}/include"
+    "${PROJECT_BINARY_DIR}/include"
+    )
+
+LINK_DIRECTORIES("${PROJECT_BINARY_DIR}/package/lib${SUFFIX_LIB}")
+
+FILE(GLOB FILES "*.c")
+
+FOREACH(FILE ${FILES})
+    GET_FILENAME_COMPONENT(EXAMPLE ${FILE} NAME_WE)
+    GET_FILENAME_COMPONENT(FULL_DIR_NAME ${FILE} PATH)
+    GET_FILENAME_COMPONENT(DIR_NAME ${FULL_DIR_NAME} NAME)
+    SET(EXAMPLE_NAME example_${DIR_NAME}_${EXAMPLE})
+    ADD_EXECUTABLE(${EXAMPLE_NAME} ${FILE})
+
+    TARGET_LINK_LIBRARIES(${EXAMPLE_NAME} clFFT ${OPENCL_LIBRARIES})
+
+    SET_TARGET_PROPERTIES(${EXAMPLE_NAME}
+        PROPERTIES
+        OUTPUT_NAME ${EXAMPLE}
+        RUNTIME_OUTPUT_DIRECTORY ${DIR_NAME})
+
+    INSTALL(TARGETS ${EXAMPLE_NAME}
+        RUNTIME DESTINATION "bin${SUFFIX_BIN}/examples")
+ENDFOREACH()
diff --git a/src/examples/fft1d.c b/src/examples/fft1d.c
new file mode 100644
index 0000000..0fccbac
--- /dev/null
+++ b/src/examples/fft1d.c
@@ -0,0 +1,136 @@
+/* ************************************************************************
+ * 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>
+
+int main( void )
+{
+    cl_int err;
+    cl_platform_id platform = 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;
+    char platform_name[128];
+    char device_name[128];
+
+    /* FFT library realted declarations */
+    clfftPlanHandle planHandle;
+    clfftDim dim = CLFFT_1D;
+    size_t clLengths[1] = {N};
+
+    /* Setup OpenCL environment. */
+    err = clGetPlatformIDs( 1, &platform, NULL );
+
+    size_t ret_param_size = 0;
+    err = clGetPlatformInfo(platform, CL_PLATFORM_NAME,
+            sizeof(platform_name), platform_name,
+            &ret_param_size);
+    printf("Platform found: %s\n", platform_name);
+
+    err = clGetDeviceIDs( platform, 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;
+    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));
+
+    /* print input array */
+    printf("\nPerforming fft on an one dimensional array of size N = %ld\n", N);
+    int print_iter = 0;
+    while(print_iter<N) {
+        float x = print_iter;
+        float y = print_iter*3;
+        X[2*print_iter  ] = x;
+        X[2*print_iter+1] = y;
+        printf("(%f, %f) ", x, y);
+        print_iter++;
+    }
+    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), NULL, &err );
+
+    err = clEnqueueWriteBuffer( queue, bufX, CL_TRUE, 0,
+            N * 2 * sizeof( *X ), 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);
+
+    /* Bake the plan. */
+    err = clfftBakePlan(planHandle, 1, &queue, NULL, NULL);
+
+    /* Execute the plan. */
+    err = clfftEnqueueTransform(planHandle, CLFFT_FORWARD, 1, &queue, 0, NULL, NULL, &bufX, NULL, 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 ), X, 0, NULL, NULL );
+
+    /* print output array */
+    print_iter = 0;
+    while(print_iter<N) {
+        printf("(%f, %f) ", X[2*print_iter], X[2*print_iter+1]);
+        print_iter++;
+    }
+    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;
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/clfft.git



More information about the debian-science-commits mailing list