diff options
author | Thomas White <taw@bitwiz.org.uk> | 2010-02-20 22:25:39 +0100 |
---|---|---|
committer | Thomas White <taw@bitwiz.org.uk> | 2010-02-20 22:26:22 +0100 |
commit | cc0db0054d76474e55393c05806f715a0e2f6d08 (patch) | |
tree | f7c357a2b08507bcddf080ff8c8594f1ef567f23 | |
parent | 0fab44d676aacde5e04156ad2b2e24a580bfab18 (diff) |
Move OpenCL utility stuff to a separate module
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/cl-utils.c | 117 | ||||
-rw-r--r-- | src/cl-utils.h | 26 | ||||
-rw-r--r-- | src/diffraction-gpu.c | 101 |
5 files changed, 146 insertions, 102 deletions
diff --git a/Makefile.am b/Makefile.am index 148e9e8b..22f39ce1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,5 +3,5 @@ EXTRA_DIST = configure src/cell.h src/hdf5-file.h src/image.h src/relrod.h \ src/sfac.h src/intensities.h src/reflections.h src/list_tmp.h \ src/statistics.h src/displaywindow.h src/render.h src/hdfsee.h \ data/displaywindow.ui src/dirax.h src/peaks.h src/index.h \ - src/filters.h + src/filters.h src/diffraction-gpu.h src/cl-utils.h SUBDIRS = src data diff --git a/src/Makefile.am b/src/Makefile.am index 887271ea..7fd07cad 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,7 +11,7 @@ pattern_sim_SOURCES = pattern_sim.c diffraction.c utils.c image.c cell.c \ hdf5-file.c detector.c sfac.c intensities.c \ reflections.c if HAVE_OPENCL -pattern_sim_SOURCES += diffraction-gpu.c +pattern_sim_SOURCES += diffraction-gpu.c cl-utils.c endif pattern_sim_LDADD = @LIBS@ diff --git a/src/cl-utils.c b/src/cl-utils.c new file mode 100644 index 00000000..7d64e690 --- /dev/null +++ b/src/cl-utils.c @@ -0,0 +1,117 @@ +/* + * cl-utils.c + * + * OpenCL utility functions + * + * (c) 2006-2010 Thomas White <taw@physics.org> + * + * Part of CrystFEL - crystallography with a FEL + * + */ + + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <CL/cl.h> + +#include "utils.h" + + +const char *clError(cl_int err) +{ + switch ( err ) { + case CL_SUCCESS : return "no error"; + case CL_INVALID_PLATFORM : return "invalid platform"; + case CL_INVALID_KERNEL : return "invalid kernel"; + case CL_INVALID_ARG_INDEX : return "invalid argument index"; + case CL_INVALID_ARG_VALUE : return "invalid argument value"; + case CL_INVALID_MEM_OBJECT : return "invalid memory object"; + case CL_INVALID_SAMPLER : return "invalid sampler"; + case CL_INVALID_ARG_SIZE : return "invalid argument size"; + case CL_INVALID_COMMAND_QUEUE : return "invalid command queue"; + case CL_INVALID_CONTEXT : return "invalid context"; + case CL_INVALID_VALUE : return "invalid value"; + case CL_INVALID_EVENT_WAIT_LIST : return "invalid wait list"; + case CL_MAP_FAILURE : return "map failure"; + case CL_MEM_OBJECT_ALLOCATION_FAILURE : return "object allocation failure"; + case CL_OUT_OF_HOST_MEMORY : return "out of host memory"; + case CL_OUT_OF_RESOURCES : return "out of resources"; + case CL_INVALID_KERNEL_NAME : return "invalid kernel name"; + case CL_INVALID_KERNEL_ARGS : return "invalid kernel arguments"; + default : + ERROR("Error code: %i\n", err); + return "unknown error"; + } +} + + +cl_device_id get_first_dev(cl_context ctx) +{ + cl_device_id dev; + cl_int r; + + r = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, sizeof(dev), &dev, NULL); + if ( r != CL_SUCCESS ) { + ERROR("Couldn't get device\n"); + return 0; + } + + return dev; +} + + +static void show_build_log(cl_program prog, cl_device_id dev) +{ + cl_int r; + char log[4096]; + size_t s; + + r = clGetProgramBuildInfo(prog, dev, CL_PROGRAM_BUILD_LOG, 4096, log, + &s); + + STATUS("%s\n", log); +} + + +cl_program load_program(const char *filename, cl_context ctx, + cl_device_id dev, cl_int *err) +{ + FILE *fh; + cl_program prog; + char *source; + size_t len; + cl_int r; + + fh = fopen(filename, "r"); + if ( fh == NULL ) { + ERROR("Couldn't open '%s'\n", filename); + *err = CL_INVALID_PROGRAM; + return 0; + } + source = malloc(16384); + len = fread(source, 1, 16383, fh); + fclose(fh); + source[len] = '\0'; + + prog = clCreateProgramWithSource(ctx, 1, (const char **)&source, + NULL, err); + if ( *err != CL_SUCCESS ) { + ERROR("Couldn't load source\n"); + return 0; + } + + r = clBuildProgram(prog, 0, NULL, + "-Werror -I"DATADIR"/crystfel/ -cl-no-signed-zeros", + NULL, NULL); + if ( r != CL_SUCCESS ) { + ERROR("Couldn't build program '%s'\n", filename); + show_build_log(prog, dev); + *err = r; + return 0; + } + + free(source); + *err = CL_SUCCESS; + return prog; +} diff --git a/src/cl-utils.h b/src/cl-utils.h new file mode 100644 index 00000000..1725ea1e --- /dev/null +++ b/src/cl-utils.h @@ -0,0 +1,26 @@ +/* + * cl-utils.h + * + * OpenCL utility functions + * + * (c) 2006-2010 Thomas White <taw@physics.org> + * + * Part of CrystFEL - crystallography with a FEL + * + */ + +#ifndef CLUTILS_H +#define CLUTILS_H + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + + +extern const char *clError(cl_int err); +extern cl_device_id get_first_dev(cl_context ctx); +extern cl_program load_program(const char *filename, cl_context ctx, + cl_device_id dev, cl_int *err); + + +#endif /* CLUTILS_H */ diff --git a/src/diffraction-gpu.c b/src/diffraction-gpu.c index 7896c2d5..1da23e91 100644 --- a/src/diffraction-gpu.c +++ b/src/diffraction-gpu.c @@ -22,6 +22,7 @@ #include "cell.h" #include "diffraction.h" #include "sfac.h" +#include "cl-utils.h" #define SAMPLING (4) @@ -42,109 +43,9 @@ struct gpu_context cl_mem diff; size_t diff_size; - }; -static const char *clError(cl_int err) -{ - switch ( err ) { - case CL_SUCCESS : return "no error"; - case CL_INVALID_PLATFORM : return "invalid platform"; - case CL_INVALID_KERNEL : return "invalid kernel"; - case CL_INVALID_ARG_INDEX : return "invalid argument index"; - case CL_INVALID_ARG_VALUE : return "invalid argument value"; - case CL_INVALID_MEM_OBJECT : return "invalid memory object"; - case CL_INVALID_SAMPLER : return "invalid sampler"; - case CL_INVALID_ARG_SIZE : return "invalid argument size"; - case CL_INVALID_COMMAND_QUEUE : return "invalid command queue"; - case CL_INVALID_CONTEXT : return "invalid context"; - case CL_INVALID_VALUE : return "invalid value"; - case CL_INVALID_EVENT_WAIT_LIST : return "invalid wait list"; - case CL_MAP_FAILURE : return "map failure"; - case CL_MEM_OBJECT_ALLOCATION_FAILURE : return "object allocation failure"; - case CL_OUT_OF_HOST_MEMORY : return "out of host memory"; - case CL_OUT_OF_RESOURCES : return "out of resources"; - case CL_INVALID_KERNEL_NAME : return "invalid kernel name"; - case CL_INVALID_KERNEL_ARGS : return "invalid kernel arguments"; - default : - ERROR("Error code: %i\n", err); - return "unknown error"; - } -} - - -static cl_device_id get_first_dev(cl_context ctx) -{ - cl_device_id dev; - cl_int r; - - r = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, sizeof(dev), &dev, NULL); - if ( r != CL_SUCCESS ) { - ERROR("Couldn't get device\n"); - return 0; - } - - return dev; -} - - -static void show_build_log(cl_program prog, cl_device_id dev) -{ - cl_int r; - char log[4096]; - size_t s; - - r = clGetProgramBuildInfo(prog, dev, CL_PROGRAM_BUILD_LOG, 4096, log, - &s); - - STATUS("%s\n", log); -} - - -static cl_program load_program(const char *filename, cl_context ctx, - cl_device_id dev, cl_int *err) -{ - FILE *fh; - cl_program prog; - char *source; - size_t len; - cl_int r; - - fh = fopen(filename, "r"); - if ( fh == NULL ) { - ERROR("Couldn't open '%s'\n", filename); - *err = CL_INVALID_PROGRAM; - return 0; - } - source = malloc(16384); - len = fread(source, 1, 16383, fh); - fclose(fh); - source[len] = '\0'; - - prog = clCreateProgramWithSource(ctx, 1, (const char **)&source, - NULL, err); - if ( *err != CL_SUCCESS ) { - ERROR("Couldn't load source\n"); - return 0; - } - - r = clBuildProgram(prog, 0, NULL, - "-Werror -I"DATADIR"/crystfel/ -cl-no-signed-zeros", - NULL, NULL); - if ( r != CL_SUCCESS ) { - ERROR("Couldn't build program '%s'\n", filename); - show_build_log(prog, dev); - *err = r; - return 0; - } - - free(source); - *err = CL_SUCCESS; - return prog; -} - - void get_diffraction_gpu(struct gpu_context *gctx, struct image *image, int na, int nb, int nc, int no_sfac) { |