diff options
author | Thomas White <taw@physics.org> | 2017-07-04 15:16:10 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2017-07-04 15:16:38 +0200 |
commit | cacf62d13f4aa3128d39ab4bda8ef0151fa64050 (patch) | |
tree | e7be749105a69eae5a1336e59aea00e4f1288418 /src/diffraction-gpu.c | |
parent | 696da1488e09147ec89a26dd77a117ce35eea925 (diff) |
pattern_sim: Try all OpenCL platforms to find one with a GPU device
Diffstat (limited to 'src/diffraction-gpu.c')
-rw-r--r-- | src/diffraction-gpu.c | 40 |
1 files changed, 31 insertions, 9 deletions
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; } |