From 0c46ec57feed80911178063ef9a706c08b6f70e2 Mon Sep 17 00:00:00 2001 From: Paul Irofti Date: Sun, 30 Mar 2025 16:16:59 +0300 Subject: [PATCH] Separate constant buffers at create and destroy. --- src/CCubes.c | 42 ++++++++++---------- src/clccubes.c | 106 +++++++++++++++++++++++++++---------------------- src/clccubes.h | 19 +++++---- 3 files changed, 91 insertions(+), 76 deletions(-) diff --git a/src/CCubes.c b/src/CCubes.c index 9805d5a..0efc555 100755 --- a/src/CCubes.c +++ b/src/CCubes.c @@ -66,20 +66,6 @@ SEXP CCubes(SEXP tt) { struct timeval start, end; double elapsed_time; - config_set_int("log", LOG_LEVEL_WARN); - config_set_int("log:clccubes", LOG_LEVEL_DEBUG); - config_set_int("log:ccubes", LOG_LEVEL_DEBUG); - config_set_int("log:cl", LOG_LEVEL_DEBUG); - - char log[100] = {0}; - sprintf(log, "log-ccubes"); - config_set_string("out", log); - - struct ccubes_context *ctx = ccubes_create(kernel_file); - if (ctx == NULL) { - log_error("ccubes", "ccubes_do_tasks failed"); - } - if (PRINT_INFO) { Rprintf("--- START ---\n"); @@ -215,6 +201,28 @@ SEXP CCubes(SEXP tt) { Rprintf("ON-set minterms: %d\n", posrows); } + /* OPENCL SETUP */ + config_set_int("log", LOG_LEVEL_WARN); + config_set_int("log:clccubes", LOG_LEVEL_DEBUG); + config_set_int("log:ccubes", LOG_LEVEL_DEBUG); + config_set_int("log:cl", LOG_LEVEL_DEBUG); + + char log[100] = {0}; + sprintf(log, "log-ccubes"); + config_set_string("out", log); + + struct ccubes_context *ctx = ccubes_create( + kernel_file, + ninputs, + posrows, + negrows, + nofvalues, + ON_set, + OFF_set + ); + if (ctx == NULL) { + log_error("ccubes", "ccubes_do_tasks failed"); + } int stop_counter = 0; // to stop if two consecutive levels of complexity yield no PIs int k; @@ -255,17 +263,11 @@ SEXP CCubes(SEXP tt) { task, k, prevfoundPI, - ninputs, - posrows, - negrows, implicant_words, value_bit_width, value_bit_mask, pichart_words, estimPI, - nofvalues, - ON_set, - OFF_set, p_implicants_pos, p_implicants_val, last_index, diff --git a/src/clccubes.c b/src/clccubes.c index e12b6d7..b35bda5 100755 --- a/src/clccubes.c +++ b/src/clccubes.c @@ -26,8 +26,50 @@ #include "logging.h" +int +ccubes_create_alloc(struct ccubes_context *ctx, + int *nofvalues, /* IN: RC */ + int *ON_set, /* IN: RC */ + int *OFF_set /* IN: RC */ + ) +{ + int rc = 0; + + /* __global const int *nofvalues, IN: RC */ + ctx->nofvalues = clCreateBuffer(ctx->clctx->ctx, + CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, + ctx->ninputs * sizeof(int), nofvalues, &rc); + if (rc != CL_SUCCESS) { + goto err; + } + /* __global const int *ON_set, IN: RC */ + ctx->ON_set = clCreateBuffer(ctx->clctx->ctx, + CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, + ctx->posrows * ctx->ninputs * sizeof(int), ON_set, &rc); + if (rc != CL_SUCCESS) { + goto err; + } + /* __global const int *OFF_set, IN: RC */ + ctx->OFF_set = clCreateBuffer(ctx->clctx->ctx, + CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, + ctx->ninputs * ctx->negrows * sizeof(int), OFF_set, &rc); + if (rc != CL_SUCCESS) { + goto err; + } + +err: + return rc; +} + struct ccubes_context * -ccubes_create(const char *ccubes_kernel_file) +ccubes_create(const char *ccubes_kernel_file, + int ninputs, + int posrows, + int negrows, + int *nofvalues, /* IN: RC */ + int *ON_set, /* IN: RC */ + int *OFF_set /* IN: RC */ + ) { struct ccubes_context *ctx = NULL; int rc = 0; @@ -69,6 +111,16 @@ ccubes_create(const char *ccubes_kernel_file) goto err; } + ctx->ninputs = ninputs; + ctx->posrows = posrows; + ctx->negrows = negrows; + rc = ccubes_create_alloc(ctx, nofvalues, ON_set, OFF_set); + if (rc != CL_SUCCESS) { + log_error("clccubes", + "Failed to allocate initial buffers"); + goto err; + } + err: return ctx; } @@ -78,10 +130,7 @@ ccubes_init(struct ccubes_context *ctx, int n_tasks, int n_tasks_off, int k, - int prevfoundPI, - int ninputs, - int posrows, - int negrows, + int prevfoundPI, int implicant_words, int value_bit_width, int value_bit_mask, @@ -93,9 +142,6 @@ ccubes_init(struct ccubes_context *ctx, ctx->k = k; ctx->prevfoundPI = prevfoundPI; - ctx->ninputs = ninputs; - ctx->posrows = posrows; - ctx->negrows = negrows; ctx->implicant_words = implicant_words; ctx->value_bit_width = value_bit_width; ctx->value_bit_mask = value_bit_mask; @@ -104,7 +150,6 @@ ccubes_init(struct ccubes_context *ctx, ctx->gws = n_tasks; ctx->goff = n_tasks_off; - err: return rc; } @@ -168,9 +213,6 @@ err: int ccubes_alloc(struct ccubes_context *ctx, - int *nofvalues, /* IN: RC */ - int *ON_set, /* IN: RC */ - int *OFF_set, /* IN: RC */ unsigned int *p_implicants_pos, /* IN: RC */ unsigned int *p_implicants_val, /* IN: RC */ int *last_index, /* IN: RC */ @@ -200,27 +242,6 @@ ccubes_alloc(struct ccubes_context *ctx, * INPUTS */ - /* __global const int *nofvalues, IN: RC */ - ctx->nofvalues = clCreateBuffer(ctx->clctx->ctx, - CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, - ctx->ninputs * sizeof(int), nofvalues, &rc); - if (rc != CL_SUCCESS) { - goto err; - } - /* __global const int *ON_set, IN: RC */ - ctx->ON_set = clCreateBuffer(ctx->clctx->ctx, - CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, - ctx->posrows * ctx->ninputs * sizeof(int), ON_set, &rc); - if (rc != CL_SUCCESS) { - goto err; - } - /* __global const int *OFF_set, IN: RC */ - ctx->OFF_set = clCreateBuffer(ctx->clctx->ctx, - CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, - ctx->ninputs * ctx->negrows * sizeof(int), OFF_set, &rc); - if (rc != CL_SUCCESS) { - goto err; - } /* __global const unsigned int *p_implicants_pos, IN: RC */ ctx->p_implicants_pos = clCreateBuffer(ctx->clctx->ctx, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, @@ -468,17 +489,11 @@ ccubes_do_tasks(struct ccubes_context *ccubesctx, int n_tasks_off, int k, int prevfoundPI, - int ninputs, - int posrows, - int negrows, int implicant_words, int value_bit_width, int value_bit_mask, int pichart_words, int estimPI, - int *nofvalues, /* IN: RC */ - int *ON_set, /* IN: RC */ - int *OFF_set, /* IN: RC */ unsigned int *p_implicants_pos, /* IN: RC */ unsigned int *p_implicants_val, /* IN: RC */ int *last_index, /* IN: RC */ @@ -499,9 +514,6 @@ ccubes_do_tasks(struct ccubes_context *ccubesctx, n_tasks_off, k, prevfoundPI, - ninputs, - posrows, - negrows, implicant_words, value_bit_width, value_bit_mask, @@ -525,9 +537,6 @@ ccubes_do_tasks(struct ccubes_context *ccubesctx, } rc = ccubes_alloc(ccubesctx, - nofvalues, - ON_set, - OFF_set, p_implicants_pos, p_implicants_val, last_index, @@ -569,9 +578,6 @@ ccubes_clean_up(struct ccubes_context *ctx) log_debug("clccubes", "ccubes_unmap"); /* INPUTS */ - clReleaseMemObject(ctx->nofvalues); - clReleaseMemObject(ctx->ON_set); - clReleaseMemObject(ctx->OFF_set); clReleaseMemObject(ctx->p_implicants_pos); clReleaseMemObject(ctx->p_implicants_val); clReleaseMemObject(ctx->last_index); @@ -594,6 +600,10 @@ ccubes_clean_up(struct ccubes_context *ctx) void ccubes_destroy(struct ccubes_context *ctx) { + clReleaseMemObject(ctx->nofvalues); + clReleaseMemObject(ctx->ON_set); + clReleaseMemObject(ctx->OFF_set); + cl_clean_up(*ctx->clctx); log_debug("clccubes", "cl_clean_up"); diff --git a/src/clccubes.h b/src/clccubes.h index 17eadee..b9b441e 100755 --- a/src/clccubes.h +++ b/src/clccubes.h @@ -49,10 +49,12 @@ struct ccubes_context { int pichart_words; int estimPI; - /* INPUTS */ + /* CONSTANT INPUTS */ cl_mem nofvalues; cl_mem ON_set; cl_mem OFF_set; + + /* INPUTS */ cl_mem p_implicants_pos; cl_mem p_implicants_val; cl_mem last_index; @@ -81,7 +83,14 @@ struct ccubes_context { }; struct ccubes_context * -ccubes_create(const char *ccubes_kernel_file); +ccubes_create(const char *ccubes_kernel_file, + int ninputs, + int posrows, + int negrows, + int *nofvalues, /* IN: RC */ + int *ON_set, /* IN: RC */ + int *OFF_set /* IN: RC */ + ); int ccubes_do_tasks(struct ccubes_context *ccubesctx, @@ -89,17 +98,11 @@ ccubes_do_tasks(struct ccubes_context *ccubesctx, int n_tasks_off, int k, int prevfoundPI, - int ninputs, - int posrows, - int negrows, int implicant_words, int value_bit_width, int value_bit_mask, int pichart_words, int estimPI, - int *nofvalues, /* IN: RC */ - int *ON_set, /* IN: RC */ - int *OFF_set, /* IN: RC */ unsigned int *p_implicants_pos, /* IN: RC */ unsigned int *p_implicants_val, /* IN: RC */ int *last_index, /* IN: RC */