Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/common/dlopencl.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
This file is part of darktable,
Copyright (C) 2011-2023 darktable developers.
Copyright (C) 2011-2026 darktable developers.

darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -22,6 +22,10 @@
#include "common/dynload.h"
#include "common/dlopencl.h"

#ifdef __APPLE__
#include <dlfcn.h>
#endif

#include <assert.h>
#include <signal.h>
#include <stdio.h>
Expand All @@ -36,6 +40,19 @@ static const char *ocllib[] = { "/System/Library/Frameworks/OpenCL.framework/Ver
static const char *ocllib[] = { "libOpenCL", "libOpenCL.so", "libOpenCL.so.1", NULL };
#endif

void dt_dlopencl_close(dt_dlopencl_t *ocl)
{
free(ocl->symbols);
g_free(ocl->library);
#ifndef __APPLE__
if(!g_module_close(ocl->gmodule))
dt_print(DT_DEBUG_OPENCL, "Couldn't close OpenCL library");
#else
if(dlclose(ocl->gmodule))
dt_print(DT_DEBUG_OPENCL, "Couldn't close OpenCL library");
#endif
free(ocl);
}

/* only for debugging: default noop function for all unassigned function pointers */
void dt_dlopencl_noop(void)
Expand Down Expand Up @@ -63,9 +80,9 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name)
library = name;
module = dt_gmodule_open(library);
if(module == NULL)
dt_print(DT_DEBUG_OPENCL, "[dt_dlopencl_init] could not find specified opencl runtime library '%s'", library);
dt_print_nts(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] could not find specified opencl runtime library '%s'\n", library);
else
dt_print(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] found specified opencl runtime library '%s'", library);
dt_print_nts(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] found specified opencl runtime library '%s'\n", library);
}
else
{
Expand All @@ -75,15 +92,18 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name)
library = *iter;
module = dt_gmodule_open(library);
if(module == NULL)
dt_print(DT_DEBUG_OPENCL, "[dt_dlopencl_init] could not find default opencl runtime library '%s'", library);
dt_print_nts(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] could not find default opencl runtime library '%s'\n", library);
else
dt_print(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] found default opencl runtime library '%s'", library);
dt_print_nts(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] found default opencl runtime library '%s'\n", library);
iter++;
}
}

if(module == NULL)
{
dt_print_nts(DT_DEBUG_ALWAYS, "[dt_dlopencl_init] could not find any opencl runtime library\n");
return NULL;
}

/* now bind symbols */
ocl = malloc(sizeof(dt_dlopencl_t));
Expand All @@ -98,6 +118,7 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name)

if(ocl->symbols == NULL)
{
dt_print_nts(DT_DEBUG_ALWAYS, "[dt_dlopencl_init] could not find symbols in opencl runtime library\n");
free(ocl);
free(module);
return NULL;
Expand Down Expand Up @@ -209,7 +230,9 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name)
ocl->have_opencl = success;

if(!success)
dt_print(DT_DEBUG_OPENCL, "[opencl_init] could not load all required symbols from library");
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] could not load all required symbols from library\n");
else
ocl->gmodule = module->gmodule;

free(module);

Expand Down
10 changes: 8 additions & 2 deletions src/common/dlopencl.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,16 @@ typedef struct dt_dlopencl_symbols_t
dt_clEnqueueWaitForEvents_t dt_clEnqueueWaitForEvents;
} dt_dlopencl_symbols_t;



typedef struct dt_dlopencl_t
{
gboolean have_opencl;
dt_dlopencl_symbols_t *symbols;
char *library;
#ifndef __APPLE__
GModule *gmodule;
#else
void *gmodule;
#endif
} dt_dlopencl_t;

/* default noop function for all unassigned function pointers */
Expand All @@ -223,6 +226,9 @@ void dt_dlopencl_noop(void);
/* dynamically load OpenCL library and bind needed functions */
dt_dlopencl_t *dt_dlopencl_init(const char *);

/* dynamically close OpenCL library opened by dt_dlopencl_init() */
void dt_dlopencl_close(dt_dlopencl_t *ocl);

#endif // HAVE_OPENCL

// clang-format off
Expand Down
Loading
Loading