From cacf62d13f4aa3128d39ab4bda8ef0151fa64050 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Tue, 4 Jul 2017 15:16:10 +0200 Subject: pattern_sim: Try all OpenCL platforms to find one with a GPU device --- src/cl-utils.c | 9 +++++++++ src/diffraction-gpu.c | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/cl-utils.c b/src/cl-utils.c index dc0a2713..a7e500cd 100644 --- a/src/cl-utils.c +++ b/src/cl-utils.c @@ -50,6 +50,15 @@ const char *clError(cl_int err) case CL_SUCCESS : return "no error"; + case CL_DEVICE_NOT_AVAILABLE : + return "device not available"; + + case CL_DEVICE_NOT_FOUND : + return "device not found"; + + case CL_INVALID_DEVICE_TYPE : + return "invalid device type"; + case CL_INVALID_PLATFORM : return "invalid platform"; diff --git a/src/diffraction-gpu.c b/src/diffraction-gpu.c index 331170ae..22abcfd2 100644 --- a/src/diffraction-gpu.c +++ b/src/diffraction-gpu.c @@ -379,6 +379,7 @@ struct gpu_context *setup_gpu(int no_sfac, float *flags_ptr; size_t maxwgsize; int i; + int have_ctx = 0; char cflags[512] = ""; char *insert_stuff = NULL; @@ -393,16 +394,37 @@ struct gpu_context *setup_gpu(int no_sfac, ERROR("Couldn't find at least one platform!\n"); return NULL; } - prop[0] = CL_CONTEXT_PLATFORM; - prop[1] = (cl_context_properties)platforms[0]; - prop[2] = 0; - gctx = malloc(sizeof(*gctx)); - gctx->ctx = clCreateContextFromType(prop, CL_DEVICE_TYPE_GPU, - NULL, NULL, &err); - if ( err != CL_SUCCESS ) { - ERROR("Couldn't create OpenCL context: %i\n", err); - free(gctx); + /* Find a GPU platform in the list */ + for ( i=0; ictx = clCreateContextFromType(prop, CL_DEVICE_TYPE_GPU, + NULL, NULL, &err); + + if ( err != CL_SUCCESS ) { + if ( err == CL_DEVICE_NOT_FOUND ) { + /* No GPU device in this platform */ + continue; /* Try next platform */ + } else { + ERROR("Couldn't create OpenCL context: %s (%i)\n", + clError(err), err); + free(gctx); + return NULL; + } + } else { + STATUS("Using OpenCL platform %i (%i total)\n", i, nplat); + have_ctx = 1; + break; + } + } + + if ( !have_ctx ) { + ERROR("Couldn't find a GPU device in any platform.\n"); return NULL; } -- cgit v1.2.3