aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2017-07-04 15:16:10 +0200
committerThomas White <taw@physics.org>2017-07-04 15:16:38 +0200
commitcacf62d13f4aa3128d39ab4bda8ef0151fa64050 (patch)
treee7be749105a69eae5a1336e59aea00e4f1288418
parent696da1488e09147ec89a26dd77a117ce35eea925 (diff)
pattern_sim: Try all OpenCL platforms to find one with a GPU device
-rw-r--r--src/cl-utils.c9
-rw-r--r--src/diffraction-gpu.c40
2 files changed, 40 insertions, 9 deletions
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; i<nplat; i++ ) {
+
+ prop[0] = CL_CONTEXT_PLATFORM;
+ prop[1] = (cl_context_properties)platforms[i];
+ prop[2] = 0;
+
+ gctx = malloc(sizeof(*gctx));
+ gctx->ctx = 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;
}