Skip to content

Commit 39e45c7

Browse files
opencl: fail gracefully if opencl devices are not available
Also for unsupported GPUs.
1 parent 2268c27 commit 39e45c7

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

ggml/src/ggml-opencl/ggml-opencl.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,16 @@ struct ggml_backend_opencl_context {
187187
#endif // GGML_OPENCL_USE_ADRENO_KERNELS
188188
};
189189

190-
static ggml_backend_device g_ggml_backend_opencl_device;
190+
static ggml_backend_device g_ggml_backend_opencl_device;
191191
static ggml_backend_opencl_device_context g_ggml_ctx_dev_main {
192192
/*.platform =*/ nullptr,
193193
/*.platform_nane =*/ "",
194194
/*.device =*/ nullptr,
195195
/*.device_name =*/ "",
196196
};
197197

198+
static int ggml_backend_opencl_n_devices = 0;
199+
198200
// Profiling
199201
#ifdef GGML_OPENCL_PROFILING
200202
struct ProfilingInfo {
@@ -270,6 +272,7 @@ static ggml_backend_opencl_context * ggml_cl2_init(ggml_backend_dev_t dev) {
270272

271273
initialized = true;
272274
backend_ctx = new ggml_backend_opencl_context();
275+
backend_ctx->gpu_family = GPU_FAMILY::UNKNOWN;
273276

274277
cl_int err;
275278

@@ -305,7 +308,10 @@ static ggml_backend_opencl_context * ggml_cl2_init(ggml_backend_dev_t dev) {
305308
struct cl_device * default_device = NULL;
306309

307310
cl_platform_id platform_ids[NPLAT];
308-
CL_CHECK(clGetPlatformIDs(NPLAT, platform_ids, &n_platforms));
311+
if (clGetPlatformIDs(NPLAT, platform_ids, &n_platforms) != CL_SUCCESS) {
312+
GGML_LOG_ERROR("ggml_opencl: plaform IDs not available.\n");
313+
return backend_ctx;
314+
}
309315

310316
for (unsigned i = 0; i < n_platforms; i++) {
311317
struct cl_platform * p = &platforms[i];
@@ -344,7 +350,7 @@ static ggml_backend_opencl_context * ggml_cl2_init(ggml_backend_dev_t dev) {
344350

345351
if (n_devices == 0) {
346352
GGML_LOG_ERROR("ggml_opencl: could find any OpenCL devices.\n");
347-
exit(1);
353+
return backend_ctx;
348354
}
349355

350356
char * user_platform_string = getenv("GGML_OPENCL_PLATFORM");
@@ -453,7 +459,8 @@ static ggml_backend_opencl_context * ggml_cl2_init(ggml_backend_dev_t dev) {
453459
backend_ctx->gpu_family = GPU_FAMILY::INTEL;
454460
} else {
455461
GGML_LOG_ERROR("Unknown GPU: %s\n", default_device->name);
456-
exit(1);
462+
backend_ctx->gpu_family = GPU_FAMILY::UNKNOWN;
463+
return backend_ctx;
457464
}
458465

459466
// Populate backend device name
@@ -758,6 +765,9 @@ static ggml_backend_opencl_context * ggml_cl2_init(ggml_backend_dev_t dev) {
758765
CL_CHECK((backend_ctx->B_d_max = clCreateBuffer(context, 0, max_B_d_bytes, NULL, &err), err));
759766
#endif // GGML_OPENCL_USE_ADRENO_KERNELS
760767

768+
// For now we support a single devices
769+
ggml_backend_opencl_n_devices = 1;
770+
761771
return backend_ctx;
762772
}
763773

@@ -1728,7 +1738,7 @@ static const char * ggml_backend_opencl_reg_get_name(ggml_backend_reg_t reg) {
17281738
}
17291739

17301740
static size_t ggml_backend_opencl_reg_device_count(ggml_backend_reg_t reg) {
1731-
return 1;
1741+
return ggml_backend_opencl_n_devices;
17321742

17331743
GGML_UNUSED(reg);
17341744
}

0 commit comments

Comments
 (0)