Skip to content

Commit

Permalink
Merge pull request #841 from CEED/jeremy/qf-multi-source
Browse files Browse the repository at this point in the history
QFunction multi source support
  • Loading branch information
jeremylt authored Nov 18, 2021
2 parents bbc5590 + b44031f commit 098b678
Show file tree
Hide file tree
Showing 45 changed files with 482 additions and 402 deletions.
85 changes: 10 additions & 75 deletions backends/cuda-gen/ceed-cuda-gen-qfunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,72 +49,6 @@ static int CeedQFunctionDestroy_Cuda_gen(CeedQFunction qf) {
return CEED_ERROR_SUCCESS;
}

//------------------------------------------------------------------------------
// Load QFunction
//------------------------------------------------------------------------------
static int loadCudaFunction(CeedQFunction qf, char *c_src_file) {
int ierr;
Ceed ceed;
CeedQFunctionGetCeed(qf, &ceed);
CeedQFunction_Cuda_gen *data;
ierr = CeedQFunctionGetData(qf, &data); CeedChkBackend(ierr);

// Find source file
char *cuda_file;
ierr = CeedCalloc(CUDA_MAX_PATH, &cuda_file); CeedChkBackend(ierr);
memcpy(cuda_file, c_src_file, strlen(c_src_file));
const char *last_dot = strrchr(cuda_file, '.');
if (!last_dot)
return CeedError(ceed, CEED_ERROR_BACKEND, "Cannot find file's extension!");
const size_t cuda_path_len = last_dot - cuda_file;
strncpy(&cuda_file[cuda_path_len], ".h", 3);

// Open source file
FILE *fp;
long lSize;
char *buffer;
fp = fopen (cuda_file, "rb");
if (!fp)
// LCOV_EXCL_START
return CeedError(ceed, CEED_ERROR_BACKEND,
"Couldn't open the Cuda file for the QFunction.");
// LCOV_EXCL_STOP

// Compute size of source file
fseek(fp, 0L, SEEK_END);
lSize = ftell(fp);
rewind(fp);

// Allocate memory for entire content
ierr = CeedCalloc(lSize+1, &buffer); CeedChkBackend(ierr);

// Copy the file into the buffer
if (1 != fread(buffer, lSize, 1, fp)) {
// LCOV_EXCL_START
fclose(fp);
ierr = CeedFree(&buffer); CeedChkBackend(ierr);
return CeedError(ceed, CEED_ERROR_BACKEND,
"Couldn't read the Cuda file for the QFunction.");
// LCOV_EXCL_STOP
}

// Append typedef and save source string
// FIXME: the magic number 16 should be defined somewhere...
char *fields_string =
"typedef struct { const CeedScalar* inputs[16]; CeedScalar* outputs[16]; } Fields_Cuda_gen;";
ierr = CeedMalloc(1 + strlen(fields_string) + strlen(buffer),
&data->qFunctionSource); CeedChkBackend(ierr);
memcpy(data->qFunctionSource, fields_string, strlen(fields_string));
memcpy(data->qFunctionSource + strlen(fields_string), buffer,
strlen(buffer) + 1);

// Cleanup
ierr = CeedFree(&buffer); CeedChkBackend(ierr);
fclose(fp);
ierr = CeedFree(&cuda_file); CeedChkBackend(ierr);
return CEED_ERROR_SUCCESS;
}

//------------------------------------------------------------------------------
// Create QFunction
//------------------------------------------------------------------------------
Expand All @@ -126,15 +60,16 @@ int CeedQFunctionCreate_Cuda_gen(CeedQFunction qf) {
ierr = CeedCalloc(1, &data); CeedChkBackend(ierr);
ierr = CeedQFunctionSetData(qf, data); CeedChkBackend(ierr);

char *source;
ierr = CeedQFunctionGetSourcePath(qf, &source); CeedChkBackend(ierr);
const char *funname = strrchr(source, ':') + 1;
data->qFunctionName = (char *)funname;
const int filenamelen = funname - source;
char filename[filenamelen];
memcpy(filename, source, filenamelen - 1);
filename[filenamelen - 1] = '\0';
ierr = loadCudaFunction(qf, filename); CeedChkBackend(ierr);
// Read QFunction source
ierr = CeedQFunctionGetKernelName(qf, &data->qFunctionName);
CeedChkBackend(ierr);
ierr = CeedQFunctionLoadSourceToBuffer(qf, &data->qFunctionSource);
CeedChkBackend(ierr);
if (!data->qFunctionSource)
// LCOV_EXCL_START
return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
"/gpu/cuda/gen backend requires QFunction source code file");
// LCOV_EXCL_STOP

ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
CeedQFunctionApply_Cuda_gen); CeedChkBackend(ierr);
Expand Down
84 changes: 5 additions & 79 deletions backends/cuda/ceed-cuda-qfunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,67 +110,6 @@ static int CeedQFunctionSetCUDAUserFunction_Cuda(CeedQFunction qf,
return CEED_ERROR_SUCCESS;
}

//------------------------------------------------------------------------------
// Load QFunction source file
//------------------------------------------------------------------------------
static int CeedCudaLoadQFunction(CeedQFunction qf, char *c_src_file) {
int ierr;
Ceed ceed;
CeedQFunctionGetCeed(qf, &ceed);

// Find source file
char *cuda_file;
ierr = CeedCalloc(CUDA_MAX_PATH, &cuda_file); CeedChkBackend(ierr);
memcpy(cuda_file, c_src_file, strlen(c_src_file));
const char *last_dot = strrchr(cuda_file, '.');
if (!last_dot)
// LCOV_EXCL_START
return CeedError(ceed, CEED_ERROR_BACKEND, "Cannot find file's extension!");
// LCOV_EXCL_STOP
const size_t cuda_path_len = last_dot - cuda_file;
strncpy(&cuda_file[cuda_path_len], ".h", 3);

// Open source file
FILE *fp;
long lSize;
char *buffer;
fp = fopen (cuda_file, "rb");
if (!fp)
// LCOV_EXCL_START
return CeedError(ceed, CEED_ERROR_BACKEND,
"Couldn't open the Cuda file for the QFunction.");
// LCOV_EXCL_STOP

// Compute size of source
fseek(fp, 0L, SEEK_END);
lSize = ftell(fp);
rewind(fp);

// Allocate memory for entire content
ierr = CeedCalloc(lSize+1, &buffer); CeedChkBackend(ierr);

// Copy the file into the buffer
if(1 != fread(buffer, lSize, 1, fp)) {
// LCOV_EXCL_START
fclose(fp);
ierr = CeedFree(&buffer); CeedChkBackend(ierr);
return CeedError(ceed, CEED_ERROR_BACKEND,
"Couldn't read the Cuda file for the QFunction.");
// LCOV_EXCL_STOP
}

// Cleanup
fclose(fp);
ierr = CeedFree(&cuda_file); CeedChkBackend(ierr);

// Save QFunction source
CeedQFunction_Cuda *data;
ierr = CeedQFunctionGetData(qf, &data); CeedChkBackend(ierr);
data->qFunctionSource = buffer;
data->qFunction = NULL;
return CEED_ERROR_SUCCESS;
}

//------------------------------------------------------------------------------
// Create QFunction
//------------------------------------------------------------------------------
Expand All @@ -182,24 +121,11 @@ int CeedQFunctionCreate_Cuda(CeedQFunction qf) {
ierr = CeedCalloc(1, &data); CeedChkBackend(ierr);
ierr = CeedQFunctionSetData(qf, data); CeedChkBackend(ierr);

// Read source
char *source;
ierr = CeedQFunctionGetSourcePath(qf, &source); CeedChkBackend(ierr);
// Empty source path indicates user must supply Q-Function
if (source[0] != '\0') {
const char *funname = strrchr(source, ':') + 1;
data->qFunctionName = (char *)funname;
const int filenamelen = funname - source;
char filename[filenamelen];
memcpy(filename, source, filenamelen - 1);
filename[filenamelen - 1] = '\0';
ierr = CeedCudaLoadQFunction(qf, filename); CeedChkBackend(ierr);
} else {
data->module = NULL;
data->qFunctionName = "";
data->qFunctionSource = "";
data->qFunction = NULL;
}
// Read QFunction source
ierr = CeedQFunctionGetKernelName(qf, &data->qFunctionName);
CeedChkBackend(ierr);
ierr = CeedQFunctionLoadSourceToBuffer(qf, &data->qFunctionSource);
CeedChkBackend(ierr);

// Register backend functions
ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
Expand Down
2 changes: 1 addition & 1 deletion backends/cuda/ceed-cuda.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ int CeedRunKernelCuda(Ceed ceed, CUfunction kernel, const int gridSize,
return CeedError(ceed, CEED_ERROR_BACKEND,
"CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES: max_threads_per_block %d on block size (%d,%d,%d), shared_size %d, num_regs %d",
max_threads_per_block, blockSize, 1, 1, shared_size_bytes, num_regs);
} else CeedChk_Cu(ceed, result);
} else CeedChk_Cu(ceed, result); // NOLINT
return CEED_ERROR_SUCCESS;
}

Expand Down
83 changes: 10 additions & 73 deletions backends/hip-gen/ceed-hip-gen-qfunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,70 +49,6 @@ static int CeedQFunctionDestroy_Hip_gen(CeedQFunction qf) {
return CEED_ERROR_SUCCESS;
}

//------------------------------------------------------------------------------
// Load QFunction
//------------------------------------------------------------------------------
static int loadHipFunction(CeedQFunction qf, char *c_src_file) {
int ierr;
Ceed ceed;
CeedQFunctionGetCeed(qf, &ceed);
CeedQFunction_Hip_gen *data;
ierr = CeedQFunctionGetData(qf, &data); CeedChkBackend(ierr);

// Find source file
char *hip_file;
ierr = CeedCalloc(HIP_MAX_PATH, &hip_file); CeedChkBackend(ierr);
memcpy(hip_file, c_src_file, strlen(c_src_file));
const char *last_dot = strrchr(hip_file, '.');
if (!last_dot)
return CeedError(ceed, 1, "Cannot find file's extension!");
const size_t hip_path_len = last_dot - hip_file;
strncpy(&hip_file[hip_path_len], ".h", 3);

// Open source file
FILE *fp;
long lSize;
char *buffer;
fp = fopen (hip_file, "rb");
if (!fp)
// LCOV_EXCL_START
return CeedError(ceed, 1, "Couldn't open the Hip file for the QFunction.");
// LCOV_EXCL_STOP

// Compute size of source file
fseek(fp, 0L, SEEK_END);
lSize = ftell(fp);
rewind(fp);

// Allocate memory for entire content
ierr = CeedCalloc(lSize+1, &buffer); CeedChkBackend(ierr);

// Copy the file into the buffer
if (1 != fread(buffer, lSize, 1, fp)) {
// LCOV_EXCL_START
fclose(fp);
ierr = CeedFree(&buffer); CeedChkBackend(ierr);
return CeedError(ceed, 1, "Couldn't read the Hip file for the QFunction.");
// LCOV_EXCL_STOP
}

// Append typedef and save source string
// FIXME: the magic number 16 should be defined somewhere...
char *fields_string =
"typedef struct { const CeedScalar* inputs[16]; CeedScalar* outputs[16]; } Fields_Hip_gen;";
ierr = CeedMalloc(1 + strlen(fields_string) + strlen(buffer),
&data->qFunctionSource); CeedChkBackend(ierr);
memcpy(data->qFunctionSource, fields_string, strlen(fields_string));
memcpy(data->qFunctionSource + strlen(fields_string), buffer,
strlen(buffer) + 1);

// Cleanup
ierr = CeedFree(&buffer); CeedChkBackend(ierr);
fclose(fp);
ierr = CeedFree(&hip_file); CeedChkBackend(ierr);
return CEED_ERROR_SUCCESS;
}

//------------------------------------------------------------------------------
// Create QFunction
//------------------------------------------------------------------------------
Expand All @@ -124,15 +60,16 @@ int CeedQFunctionCreate_Hip_gen(CeedQFunction qf) {
ierr = CeedCalloc(1, &data); CeedChkBackend(ierr);
ierr = CeedQFunctionSetData(qf, data); CeedChkBackend(ierr);

char *source;
ierr = CeedQFunctionGetSourcePath(qf, &source); CeedChkBackend(ierr);
const char *funname = strrchr(source, ':') + 1;
data->qFunctionName = (char *)funname;
const int filenamelen = funname - source;
char filename[filenamelen];
memcpy(filename, source, filenamelen - 1);
filename[filenamelen - 1] = '\0';
ierr = loadHipFunction(qf, filename); CeedChkBackend(ierr);
// Read QFunction source
ierr = CeedQFunctionGetKernelName(qf, &data->qFunctionName);
CeedChkBackend(ierr);
ierr = CeedQFunctionLoadSourceToBuffer(qf, &data->qFunctionSource);
CeedChkBackend(ierr);
if (!data->qFunctionSource)
// LCOV_EXCL_START
return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
"/gpu/hip/gen backend requires QFunction source code file");
// LCOV_EXCL_STOP

ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
CeedQFunctionApply_Hip_gen); CeedChkBackend(ierr);
Expand Down
75 changes: 5 additions & 70 deletions backends/hip/ceed-hip-qfunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,66 +100,6 @@ static int CeedQFunctionDestroy_Hip(CeedQFunction qf) {
return CEED_ERROR_SUCCESS;
}

//------------------------------------------------------------------------------
// Load QFunction source file
//------------------------------------------------------------------------------
static int CeedHipLoadQFunction(CeedQFunction qf, char *c_src_file) {
int ierr;
Ceed ceed;
CeedQFunctionGetCeed(qf, &ceed);

// Find source file
char *hip_file;
ierr = CeedCalloc(HIP_MAX_PATH, &hip_file); CeedChkBackend(ierr);
memcpy(hip_file, c_src_file, strlen(c_src_file));
const char *last_dot = strrchr(hip_file, '.');
if (!last_dot)
// LCOV_EXCL_START
return CeedError(ceed, CEED_ERROR_BACKEND, "Cannot find file's extension!");
// LCOV_EXCL_STOP
const size_t hip_path_len = last_dot - hip_file;
strncpy(&hip_file[hip_path_len], ".h", 3);

// Open source file
FILE *fp;
long lSize;
char *buffer;
fp = fopen (hip_file, "rb");
if (!fp)
// LCOV_EXCL_START
return CeedError(ceed, CEED_ERROR_BACKEND,
"Couldn't open the Hip file for the QFunction.");
// LCOV_EXCL_STOP

// Compute size of source
fseek(fp, 0L, SEEK_END);
lSize = ftell(fp);
rewind(fp);

// Allocate memory for entire content
ierr = CeedCalloc(lSize+1, &buffer); CeedChkBackend(ierr);

// Copy the file into the buffer
if(1!=fread(buffer, lSize, 1, fp)) {
// LCOV_EXCL_START
fclose(fp);
ierr = CeedFree(&buffer); CeedChkBackend(ierr);
return CeedError(ceed, CEED_ERROR_BACKEND,
"Couldn't read the Hip file for the QFunction.");
// LCOV_EXCL_STOP
}

// Cleanup
fclose(fp);
ierr = CeedFree(&hip_file); CeedChkBackend(ierr);

// Save QFunction source
CeedQFunction_Hip *data;
ierr = CeedQFunctionGetData(qf, &data); CeedChkBackend(ierr);
data->qFunctionSource = buffer;
return CEED_ERROR_SUCCESS;
}

//------------------------------------------------------------------------------
// Create QFunction
//------------------------------------------------------------------------------
Expand All @@ -174,16 +114,11 @@ int CeedQFunctionCreate_Hip(CeedQFunction qf) {
ierr = CeedQFunctionGetNumArgs(qf, &numinputfields, &numoutputfields);
CeedChkBackend(ierr);

// Read source
char *source;
ierr = CeedQFunctionGetSourcePath(qf, &source); CeedChkBackend(ierr);
const char *funname = strrchr(source, ':') + 1;
data->qFunctionName = (char *)funname;
const int filenamelen = funname - source;
char filename[filenamelen];
memcpy(filename, source, filenamelen - 1);
filename[filenamelen - 1] = '\0';
ierr = CeedHipLoadQFunction(qf, filename); CeedChkBackend(ierr);
// Read QFunction source
ierr = CeedQFunctionGetKernelName(qf, &data->qFunctionName);
CeedChkBackend(ierr);
ierr = CeedQFunctionLoadSourceToBuffer(qf, &data->qFunctionSource);
CeedChkBackend(ierr);

// Register backend functions
ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
Expand Down
Loading

0 comments on commit 098b678

Please sign in to comment.