#include <ceed-impl.h>
#include <ceed.h>
#include <ceed/backend.h>
#include <ceed/jit-tools.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
static struct CeedQFunction_private ceed_qfunction_none;
const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none;
static struct {
char name[CEED_MAX_RESOURCE_LEN];
char source[CEED_MAX_RESOURCE_LEN];
CeedInt vec_length;
CeedQFunctionUser f;
int (*init)(Ceed ceed, const char *name, CeedQFunction qf);
} gallery_qfunctions[1024];
static size_t num_qfunctions;
int CeedQFunctionRegister(const char *name, const char *source, CeedInt vec_length, CeedQFunctionUser f,
int (*init)(Ceed, const char *, CeedQFunction)) {
const char *relative_file_path;
int ierr = 0;
CeedDebugEnv("Gallery Register: %s", name);
CeedCall(CeedGetJitRelativePath(source, &relative_file_path));
CeedPragmaCritical(CeedQFunctionRegister) {
if (num_qfunctions < sizeof(gallery_qfunctions) / sizeof(gallery_qfunctions[0])) {
strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN);
gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN - 1] = 0;
strncpy(gallery_qfunctions[num_qfunctions].source, relative_file_path, CEED_MAX_RESOURCE_LEN);
gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN - 1] = 0;
gallery_qfunctions[num_qfunctions].vec_length = vec_length;
gallery_qfunctions[num_qfunctions].f = f;
gallery_qfunctions[num_qfunctions].init = init;
num_qfunctions++;
} else {
ierr = 1;
}
}
CeedCheck(ierr == 0, NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions");
return CEED_ERROR_SUCCESS;
}
static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
CeedCall(CeedCalloc(1, f));
CeedCall(CeedStringAllocCopy(field_name, (char **)&(*f)->field_name));
(*f)->size = size;
(*f)->eval_mode = eval_mode;
return CEED_ERROR_SUCCESS;
}
static int CeedQFunctionFieldView(CeedQFunctionField field, CeedInt field_number, bool in, FILE *stream) {
const char *inout = in ? "Input" : "Output";
char *field_name;
CeedInt size;
CeedEvalMode eval_mode;
CeedCall(CeedQFunctionFieldGetName(field, &field_name));
CeedCall(CeedQFunctionFieldGetSize(field, &size));
CeedCall(CeedQFunctionFieldGetEvalMode(field, &eval_mode));
fprintf(stream,
" %s field %" CeedInt_FMT
":\n"
" Name: \"%s\"\n"
" Size: %" CeedInt_FMT
"\n"
" EvalMode: \"%s\"\n",
inout, field_number, field_name, size, CeedEvalModes[eval_mode]);
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) {
qf->is_fortran = status;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) {
*vec_length = qf->vec_length;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, CeedInt *num_output) {
if (num_input) *num_input = qf->num_input_fields;
if (num_output) *num_output = qf->num_output_fields;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) {
if (!qf->kernel_name) {
Ceed ceed;
char *kernel_name_copy;
CeedCall(CeedQFunctionGetCeed(qf, &ceed));
if (qf->user_source) {
const char *kernel_name = strrchr(qf->user_source, ':') + 1;
size_t kernel_name_len = strlen(kernel_name);
CeedCall(CeedCalloc(kernel_name_len + 1, &kernel_name_copy));
memcpy(kernel_name_copy, kernel_name, kernel_name_len);
} else {
CeedCall(CeedCalloc(1, &kernel_name_copy));
}
qf->kernel_name = kernel_name_copy;
}
*kernel_name = (char *)qf->kernel_name;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) {
if (!qf->source_path && qf->user_source) {
Ceed ceed;
bool is_absolute_path;
char *absolute_path, *source_path_copy;
const char *kernel_name = strrchr(qf->user_source, ':') + 1;
size_t kernel_name_len = strlen(kernel_name);
CeedCall(CeedQFunctionGetCeed(qf, &ceed));
CeedCall(CeedCheckFilePath(ceed, qf->user_source, &is_absolute_path));
if (is_absolute_path) {
absolute_path = (char *)qf->user_source;
} else {
CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path));
}
size_t source_len = strlen(absolute_path) - kernel_name_len - 1;
CeedCall(CeedCalloc(source_len + 1, &source_path_copy));
memcpy(source_path_copy, absolute_path, source_len);
qf->source_path = source_path_copy;
if (!is_absolute_path) CeedCall(CeedFree(&absolute_path));
}
*source_path = (char *)qf->source_path;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) {
char *source_path;
CeedCall(CeedQFunctionGetSourcePath(qf, &source_path));
*source_buffer = NULL;
if (source_path) {
CeedCall(CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer));
}
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) {
*f = qf->function;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
*ctx = qf->ctx;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
bool is_writable;
CeedQFunctionContext ctx;
CeedCall(CeedQFunctionGetContext(qf, &ctx));
if (ctx) {
CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
if (is_writable) {
CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
} else {
CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
}
} else {
*(void **)data = NULL;
}
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) {
bool is_writable;
CeedQFunctionContext ctx;
CeedCall(CeedQFunctionGetContext(qf, &ctx));
if (ctx) {
CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
if (is_writable) {
CeedCall(CeedQFunctionContextRestoreData(ctx, data));
} else {
CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
}
}
*(void **)data = NULL;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) {
if (qf->is_fortran) {
CeedFortranContext fortran_ctx = NULL;
CeedCall(CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx));
*ctx = fortran_ctx->inner_ctx;
CeedCall(CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx));
} else {
*ctx = qf->ctx;
}
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, void *data) {
bool is_writable;
CeedQFunctionContext ctx;
CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
if (ctx) {
CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
if (is_writable) {
CeedCall(CeedQFunctionContextGetData(ctx, mem_type, data));
} else {
CeedCall(CeedQFunctionContextGetDataRead(ctx, mem_type, data));
}
} else {
*(void **)data = NULL;
}
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) {
bool is_writable;
CeedQFunctionContext ctx;
CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
if (ctx) {
CeedCall(CeedQFunctionIsContextWritable(qf, &is_writable));
if (is_writable) {
CeedCall(CeedQFunctionContextRestoreData(ctx, data));
} else {
CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data));
}
}
*(void **)data = NULL;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) {
*is_identity = qf->is_identity;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) {
*is_writable = qf->is_context_writable;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionGetData(CeedQFunction qf, void *data) {
*(void **)data = qf->data;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionSetData(CeedQFunction qf, void *data) {
qf->data = data;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionReference(CeedQFunction qf) {
qf->ref_count++;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionGetFlopsEstimate(CeedQFunction qf, CeedSize *flops) {
CeedCheck(qf->user_flop_estimate > -1, qf->ceed, CEED_ERROR_INCOMPLETE, "Must set FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
*flops = qf->user_flop_estimate;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser f, const char *source, CeedQFunction *qf) {
char *user_source_copy;
if (!ceed->QFunctionCreate) {
Ceed delegate;
CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction"));
CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionCreate");
CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf));
return CEED_ERROR_SUCCESS;
}
CeedCheck(!strlen(source) || strrchr(source, ':'), ceed, CEED_ERROR_INCOMPLETE,
"Provided path to source does not include function name. Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"", source);
CeedCall(CeedCalloc(1, qf));
CeedCall(CeedReferenceCopy(ceed, &(*qf)->ceed));
(*qf)->ref_count = 1;
(*qf)->vec_length = vec_length;
(*qf)->is_identity = false;
(*qf)->is_context_writable = true;
(*qf)->function = f;
(*qf)->user_flop_estimate = -1;
if (strlen(source)) {
size_t user_source_len = strlen(source);
CeedCall(CeedCalloc(user_source_len + 1, &user_source_copy));
memcpy(user_source_copy, source, user_source_len);
(*qf)->user_source = user_source_copy;
}
CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields));
CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields));
CeedCall(ceed->QFunctionCreate(*qf));
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, CeedQFunction *qf) {
size_t match_len = 0, match_index = UINT_MAX;
CeedCall(CeedQFunctionRegisterAll());
CeedCheck(name, ceed, CEED_ERROR_INCOMPLETE, "No QFunction name provided");
for (size_t i = 0; i < num_qfunctions; i++) {
size_t n;
const char *curr_name = gallery_qfunctions[i].name;
for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {
}
if (n > match_len) {
match_len = n;
match_index = i;
}
}
CeedCheck(match_len > 0, ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction");
CeedCall(CeedQFunctionCreateInterior(ceed, gallery_qfunctions[match_index].vec_length, gallery_qfunctions[match_index].f,
gallery_qfunctions[match_index].source, qf));
CeedCall(gallery_qfunctions[match_index].init(ceed, name, *qf));
CeedCall(CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name));
(*qf)->is_gallery = true;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, CeedEvalMode out_mode, CeedQFunction *qf) {
CeedQFunctionContext ctx;
CeedContextFieldLabel size_label;
CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity", qf));
CeedCall(CeedQFunctionAddInput(*qf, "input", size, in_mode));
CeedCall(CeedQFunctionAddOutput(*qf, "output", size, out_mode));
(*qf)->is_identity = true;
CeedCall(CeedQFunctionGetContext(*qf, &ctx));
CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label));
CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size));
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) {
CeedCall(CeedQFunctionReference(qf));
CeedCall(CeedQFunctionDestroy(qf_copy));
*qf_copy = qf;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
CeedCheck(!qf->is_immutable, qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
CeedCheck(eval_mode != CEED_EVAL_WEIGHT || size == 1, qf->ceed, CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1");
for (CeedInt i = 0; i < qf->num_input_fields; i++) {
CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
}
for (CeedInt i = 0; i < qf->num_output_fields; i++) {
CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
}
CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode));
qf->num_input_fields++;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) {
CeedCheck(!qf->is_immutable, qf->ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable");
CeedCheck(eval_mode != CEED_EVAL_WEIGHT, qf->ceed, CEED_ERROR_DIMENSION, "Cannot create QFunction output with CEED_EVAL_WEIGHT");
for (CeedInt i = 0; i < qf->num_input_fields; i++) {
CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
}
for (CeedInt i = 0; i < qf->num_output_fields; i++) {
CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), qf->ceed, CEED_ERROR_MINOR, "QFunction field names must be unique");
}
CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode));
qf->num_output_fields++;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, CeedQFunctionField **input_fields, CeedInt *num_output_fields,
CeedQFunctionField **output_fields) {
qf->is_immutable = true;
if (num_input_fields) *num_input_fields = qf->num_input_fields;
if (input_fields) *input_fields = qf->input_fields;
if (num_output_fields) *num_output_fields = qf->num_output_fields;
if (output_fields) *output_fields = qf->output_fields;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) {
*field_name = (char *)qf_field->field_name;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) {
*size = qf_field->size;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, CeedEvalMode *eval_mode) {
*eval_mode = qf_field->eval_mode;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) {
CeedCall(CeedQFunctionContextDestroy(&qf->ctx));
qf->ctx = ctx;
if (ctx) CeedCall(CeedQFunctionContextReference(ctx));
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) {
qf->is_context_writable = is_writable;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionSetUserFlopsEstimate(CeedQFunction qf, CeedSize flops) {
CeedCheck(flops >= 0, qf->ceed, CEED_ERROR_INCOMPATIBLE, "Must set non-negative FLOPs estimate");
qf->user_flop_estimate = flops;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionView(CeedQFunction qf, FILE *stream) {
char *kernel_name;
CeedCall(CeedQFunctionGetKernelName(qf, &kernel_name));
fprintf(stream, "%sCeedQFunction - %s\n", qf->is_gallery ? "Gallery " : "User ", qf->is_gallery ? qf->gallery_name : kernel_name);
fprintf(stream, " %" CeedInt_FMT " input field%s:\n", qf->num_input_fields, qf->num_input_fields > 1 ? "s" : "");
for (CeedInt i = 0; i < qf->num_input_fields; i++) {
CeedCall(CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream));
}
fprintf(stream, " %" CeedInt_FMT " output field%s:\n", qf->num_output_fields, qf->num_output_fields > 1 ? "s" : "");
for (CeedInt i = 0; i < qf->num_output_fields; i++) {
CeedCall(CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream));
}
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) {
*ceed = qf->ceed;
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) {
CeedCheck(qf->Apply, qf->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support QFunctionApply");
CeedCheck(Q % qf->vec_length == 0, qf->ceed, CEED_ERROR_DIMENSION,
"Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q, qf->vec_length);
qf->is_immutable = true;
CeedCall(qf->Apply(qf, Q, u, v));
return CEED_ERROR_SUCCESS;
}
int CeedQFunctionDestroy(CeedQFunction *qf) {
if (!*qf || --(*qf)->ref_count > 0) {
*qf = NULL;
return CEED_ERROR_SUCCESS;
}
if ((*qf)->Destroy) {
CeedCall((*qf)->Destroy(*qf));
}
for (CeedInt i = 0; i < (*qf)->num_input_fields; i++) {
CeedCall(CeedFree(&(*(*qf)->input_fields[i]).field_name));
CeedCall(CeedFree(&(*qf)->input_fields[i]));
}
for (CeedInt i = 0; i < (*qf)->num_output_fields; i++) {
CeedCall(CeedFree(&(*(*qf)->output_fields[i]).field_name));
CeedCall(CeedFree(&(*qf)->output_fields[i]));
}
CeedCall(CeedFree(&(*qf)->input_fields));
CeedCall(CeedFree(&(*qf)->output_fields));
CeedCall(CeedQFunctionContextDestroy(&(*qf)->ctx));
CeedCall(CeedFree(&(*qf)->user_source));
CeedCall(CeedFree(&(*qf)->source_path));
CeedCall(CeedFree(&(*qf)->gallery_name));
CeedCall(CeedFree(&(*qf)->kernel_name));
CeedCall(CeedDestroy(&(*qf)->ceed));
CeedCall(CeedFree(qf));
return CEED_ERROR_SUCCESS;
}