Skip to content

Commit

Permalink
opencl: Defer loading kernel until it is needed
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-jr committed Nov 1, 2014
1 parent 1d56821 commit 28e4673
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 44 deletions.
53 changes: 29 additions & 24 deletions driver-opencl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ void *reinit_gpu(void *userdata)
//free(clState);

applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
clStates[thr_id] = opencl_create_clState(virtual_gpu, name, sizeof(name));
if (!clStates[thr_id]) {
applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
goto select_cgpu;
Expand Down Expand Up @@ -1581,7 +1581,7 @@ static bool opencl_thread_prepare(struct thr_info *thr)

strcpy(name, "");
applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
clStates[i] = initCl(virtual_gpu, name, sizeof(name));
clStates[i] = opencl_create_clState(virtual_gpu, name, sizeof(name));
if (!clStates[i]) {
#ifdef HAVE_CURSES
if (use_curses)
Expand Down Expand Up @@ -1627,34 +1627,13 @@ static bool opencl_thread_init(struct thr_info *thr)
cl_int status = 0;
thrdata = calloc(1, sizeof(*thrdata));
thr->cgpu_data = thrdata;
int buffersize = opt_scrypt ? SCRYPT_BUFFERSIZE : BUFFERSIZE;
int buffersize = SCRYPT_BUFFERSIZE;

if (!thrdata) {
applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
return false;
}

switch (clState->chosen_kernel) {
case KL_POCLBM:
thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
break;
case KL_PHATK:
thrdata->queue_kernel_parameters = &queue_phatk_kernel;
break;
case KL_DIAKGCN:
thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
break;
#ifdef USE_SCRYPT
case KL_SCRYPT:
thrdata->queue_kernel_parameters = &queue_scrypt_kernel;
break;
#endif
default:
case KL_DIABLO:
thrdata->queue_kernel_parameters = &queue_diablo_kernel;
break;
}

thrdata->res = calloc(buffersize, 1);

if (!thrdata->res) {
Expand Down Expand Up @@ -1700,6 +1679,32 @@ static int64_t opencl_scanhash(struct thr_info *thr, struct work *work,
struct cgpu_info *gpu = thr->cgpu;
struct opencl_device_data * const data = gpu->device_data;
_clState *clState = clStates[thr_id];
if (!clState->kernel_loaded)
{
if (!opencl_load_kernel(gpu, clState, gpu->name))
applogr(-1, LOG_ERR, "%s: Failed to load kernel", gpu->dev_repr);

switch (clState->chosen_kernel) {
case KL_POCLBM:
thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
break;
case KL_PHATK:
thrdata->queue_kernel_parameters = &queue_phatk_kernel;
break;
case KL_DIAKGCN:
thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
break;
#ifdef USE_SCRYPT
case KL_SCRYPT:
thrdata->queue_kernel_parameters = &queue_scrypt_kernel;
break;
#endif
default:
case KL_DIABLO:
thrdata->queue_kernel_parameters = &queue_diablo_kernel;
break;
}
}
const cl_kernel *kernel = &clState->kernel;
const int dynamic_us = opt_dynamic_interval * 1000;

Expand Down
27 changes: 8 additions & 19 deletions ocl.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,12 @@ _clState *opencl_create_clState(unsigned int gpu, char *name, size_t nameSize)
clState->devid = devices[gpu];
free(devices);

clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, SCRYPT_BUFFERSIZE, NULL, &status);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: clCreateBuffer (outputBuffer)", status);
return false;
}

return clState;
}

Expand Down Expand Up @@ -1129,29 +1135,12 @@ bool opencl_load_kernel(struct cgpu_info * const cgpu, _clState * const clState,
applog(LOG_ERR, "Error %d: clCreateBuffer (CLbuffer0)", status);
return false;
}
clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, SCRYPT_BUFFERSIZE, NULL, &status);
} else
#endif
clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d: clCreateBuffer (outputBuffer)", status);
return false;
}
#endif

clState->kernel_loaded = true;
return true;
}

_clState *initCl(const unsigned int gpu, char * const name, const size_t nameSize)
{
struct cgpu_info * const cgpu = &gpus[gpu];
_clState * const clState = opencl_create_clState(gpu, name, nameSize);
if (!opencl_load_kernel(cgpu, clState, name))
{
free(clState);
return NULL;
}
return clState;
}

#endif /* HAVE_OPENCL */

4 changes: 3 additions & 1 deletion ocl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef struct {
bool is_mesa;

cl_context context;
bool kernel_loaded;
cl_kernel kernel;
cl_command_queue commandQueue;
cl_program program;
Expand All @@ -41,6 +42,7 @@ typedef struct {
extern FILE *opencl_open_kernel(const char *filename);
extern char *file_contents(const char *filename, int *length);
extern int clDevicesNum(void);
extern _clState *initCl(unsigned int gpu, char *name, size_t nameSize);
extern _clState *opencl_create_clState(unsigned int gpu, char *name, size_t nameSize);
extern bool opencl_load_kernel(struct cgpu_info *, _clState *clState, const char *name);
#endif /* HAVE_OPENCL */
#endif /* __OCL_H__ */

0 comments on commit 28e4673

Please sign in to comment.