Reuse clccubes context. Makes things go zooom!

This commit is contained in:
Paul Irofti 2025-03-27 20:42:49 +02:00
parent f4ad81c8cc
commit 879f44d6a4
3 changed files with 62 additions and 36 deletions

View file

@ -53,6 +53,10 @@ SEXP CCubes(SEXP tt) {
sprintf(log, "log-ccubes");
config_set_string("out", log);
struct ccubes_context *ctx = ccubes_create();
if (ctx == NULL) {
log_error("ccubes", "ccubes_do_tasks failed");
}
if (PRINT_INFO) {
@ -197,7 +201,6 @@ SEXP CCubes(SEXP tt) {
int n_tasks = nchoosek(ninputs, k);
int n_tasks_batch = 512;
struct ccubes_context *ctx = NULL;
for (int task = 0; task < n_tasks; task+=n_tasks_batch) {
/* adjust if batch size is larger than total job size */
int current_batch = n_tasks < n_tasks_batch ? n_tasks : n_tasks_batch;
@ -209,7 +212,8 @@ SEXP CCubes(SEXP tt) {
unsigned int *fixed_bits;
unsigned int *value_bits;
unsigned int *pichart_values;
ctx = ccubes_do_tasks(current_batch,
ccubes_do_tasks(ctx,
current_batch,
task,
k,
ninputs,
@ -232,9 +236,6 @@ SEXP CCubes(SEXP tt) {
value_bits,
pichart_values
);
if (ctx == NULL) {
log_error("ccubes", "ccubes_do_tasks failed");
}
for (int i = 0; i < current_batch; i++) {
log_debug("ccubes", "Task %d", i);
@ -664,6 +665,8 @@ SEXP CCubes(SEXP tt) {
}
}
ccubes_destroy(ctx);
R_Free(p_pichart);
R_Free(p_implicants_val);
R_Free(p_implicants_pos);

View file

@ -27,20 +27,10 @@
struct ccubes_context *
ccubes_init(int n_tasks,
int n_tasks_off,
int k,
int ninputs,
int posrows,
int negrows,
int implicant_words,
int value_bit_width,
int pichart_words,
int estimPI
)
ccubes_create()
{
int rc = 0;
struct ccubes_context *ctx = NULL;
int rc = 0;
ctx = malloc(sizeof *ctx);
if (ctx == NULL) {
@ -55,6 +45,37 @@ ccubes_init(int n_tasks,
goto err;
}
ctx->clctx->device_type = CL_DEVICE_TYPE_GPU;
strcpy(ctx->clctx->platform_name, "NVIDIA Corporation\0");
/* strcpy(ctx->clctx->platform_name, "Advanced Micro Devices, Inc.\0"); */
/* strcpy(ctx->clctx->platform_name, "Intel(R) Corporation\0"); */
rc = cl_init(ctx->clctx);
if (rc != CL_SUCCESS) {
log_error("clccubes",
"[%d] Failed to initialize the OpenCL framework", rc);
goto err;
}
err:
return ctx;
}
int
ccubes_init(struct ccubes_context *ctx,
int n_tasks,
int n_tasks_off,
int k,
int ninputs,
int posrows,
int negrows,
int implicant_words,
int value_bit_width,
int pichart_words,
int estimPI
)
{
int rc = 0;
ctx->k = k;
ctx->ninputs = ninputs;
ctx->posrows = posrows;
@ -68,7 +89,7 @@ ccubes_init(int n_tasks,
ctx->goff = n_tasks_off;
err:
return ctx;
return rc;
}
int
@ -108,17 +129,6 @@ ccubes_build(struct ccubes_context *ctx)
{
int rc = 0;
ctx->clctx->device_type = CL_DEVICE_TYPE_GPU;
strcpy(ctx->clctx->platform_name, "NVIDIA Corporation\0");
/* strcpy(ctx->clctx->platform_name, "Advanced Micro Devices, Inc.\0"); */
/*strcpy(ctx->clctx->platform_name, "Intel(R) Corporation\0");*/
rc = cl_init(ctx->clctx);
if (rc != CL_SUCCESS) {
printf("[%d] Failed to initialize the OpenCL framework\n",
rc);
goto err;
}
rc = cl_build(*ctx->clctx, CL_DEVICE_TYPE_GPU,
"ccubes.cl", &ctx->ccubes_program);
@ -392,8 +402,9 @@ err:
return rc;
}
struct ccubes_context *
ccubes_do_tasks(int n_tasks,
int
ccubes_do_tasks(struct ccubes_context *ccubesctx,
int n_tasks,
int n_tasks_off,
int k,
int ninputs,
@ -418,9 +429,9 @@ ccubes_do_tasks(int n_tasks,
)
{
int rc = 0;
struct ccubes_context *ccubesctx = NULL;
ccubesctx = ccubes_init(n_tasks,
rc = ccubes_init(ccubesctx,
n_tasks,
n_tasks_off,
k,
ninputs,
@ -477,7 +488,7 @@ ccubes_do_tasks(int n_tasks,
goto err;
}
err:
return ccubesctx;
return rc;
}
void
@ -507,6 +518,12 @@ ccubes_clean_up(struct ccubes_context *ctx)
clReleaseMemObject(ctx->pichart_values);
log_debug("clccubes", "clReleaseMemObject OUTPUTS");
return;
}
void
ccubes_destroy(struct ccubes_context *ctx)
{
cl_clean_up(*ctx->clctx);
log_debug("clccubes", "cl_clean_up");
@ -514,4 +531,3 @@ ccubes_clean_up(struct ccubes_context *ctx)
return;
}

View file

@ -74,7 +74,11 @@ struct ccubes_context {
};
struct ccubes_context *
ccubes_do_tasks(int n_tasks,
ccubes_create();
int
ccubes_do_tasks(struct ccubes_context *ccubesctx,
int n_tasks,
int n_tasks_off,
int k,
int ninputs,
@ -101,4 +105,7 @@ ccubes_do_tasks(int n_tasks,
void
ccubes_clean_up(struct ccubes_context *ctx);
void
ccubes_destroy(struct ccubes_context *ctx);
#endif