1 // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC. 2 // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707. 3 // All Rights reserved. See files LICENSE and NOTICE for details. 4 // 5 // This file is part of CEED, a collection of benchmarks, miniapps, software 6 // libraries and APIs for efficient high-order finite element and spectral 7 // element discretizations for exascale applications. For more information and 8 // source code availability see http://github.com/ceed. 9 // 10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11 // a collaborative effort of two U.S. Department of Energy organizations (Office 12 // of Science and the National Nuclear Security Administration) responsible for 13 // the planning and preparation of a capable exascale ecosystem, including 14 // software, applications, hardware, advanced system engineering and early 15 // testbed platforms, in support of the nation's exascale computing imperative. 16 17 #include <ceed/ceed.h> 18 #include <ceed/backend.h> 19 #include <hip/hip_runtime.h> 20 #include <stdio.h> 21 #include <string.h> 22 #include "ceed-hip-gen.h" 23 #include "../hip/ceed-hip.h" 24 25 //------------------------------------------------------------------------------ 26 // Apply QFunction 27 //------------------------------------------------------------------------------ 28 static int CeedQFunctionApply_Hip_gen(CeedQFunction qf, CeedInt Q, 29 CeedVector *U, CeedVector *V) { 30 int ierr; 31 Ceed ceed; 32 ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr); 33 return CeedError(ceed, CEED_ERROR_BACKEND, 34 "Backend does not implement QFunctionApply"); 35 } 36 37 //------------------------------------------------------------------------------ 38 // Destroy QFunction 39 //------------------------------------------------------------------------------ 40 static int CeedQFunctionDestroy_Hip_gen(CeedQFunction qf) { 41 int ierr; 42 CeedQFunction_Hip_gen *data; 43 ierr = CeedQFunctionGetData(qf, &data); CeedChkBackend(ierr); 44 Ceed ceed; 45 ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr); 46 ierr = hipFree(data->d_c); CeedChk_Hip(ceed, ierr); 47 ierr = CeedFree(&data->qFunctionSource); CeedChkBackend(ierr); 48 ierr = CeedFree(&data); CeedChkBackend(ierr); 49 return CEED_ERROR_SUCCESS; 50 } 51 52 //------------------------------------------------------------------------------ 53 // Load QFunction 54 //------------------------------------------------------------------------------ 55 static int loadHipFunction(CeedQFunction qf, char *c_src_file) { 56 int ierr; 57 Ceed ceed; 58 CeedQFunctionGetCeed(qf, &ceed); 59 CeedQFunction_Hip_gen *data; 60 ierr = CeedQFunctionGetData(qf, &data); CeedChkBackend(ierr); 61 62 // Find source file 63 char *hip_file; 64 ierr = CeedCalloc(HIP_MAX_PATH, &hip_file); CeedChkBackend(ierr); 65 memcpy(hip_file, c_src_file, strlen(c_src_file)); 66 const char *last_dot = strrchr(hip_file, '.'); 67 if (!last_dot) 68 return CeedError(ceed, 1, "Cannot find file's extension!"); 69 const size_t hip_path_len = last_dot - hip_file; 70 strncpy(&hip_file[hip_path_len], ".h", 3); 71 72 // Open source file 73 FILE *fp; 74 long lSize; 75 char *buffer; 76 fp = fopen (hip_file, "rb"); 77 if (!fp) 78 // LCOV_EXCL_START 79 CeedError(ceed, 1, "Couldn't open the Hip file for the QFunction."); 80 // LCOV_EXCL_STOP 81 82 // Compute size of source file 83 fseek(fp, 0L, SEEK_END); 84 lSize = ftell(fp); 85 rewind(fp); 86 87 // Allocate memory for entire content 88 ierr = CeedCalloc(lSize+1, &buffer); CeedChkBackend(ierr); 89 90 // Copy the file into the buffer 91 if (1 != fread(buffer, lSize, 1, fp)) { 92 // LCOV_EXCL_START 93 fclose(fp); 94 ierr = CeedFree(&buffer); CeedChkBackend(ierr); 95 CeedError(ceed, 1, "Couldn't read the Hip file for the QFunction."); 96 // LCOV_EXCL_STOP 97 } 98 99 // Append typedef and save source string 100 // FIXME: the magic number 16 should be defined somewhere... 101 char *fields_string = 102 "typedef struct { const CeedScalar* inputs[16]; CeedScalar* outputs[16]; } Fields_Hip_gen;"; 103 ierr = CeedMalloc(1 + strlen(fields_string) + strlen(buffer), 104 &data->qFunctionSource); CeedChkBackend(ierr); 105 memcpy(data->qFunctionSource, fields_string, strlen(fields_string)); 106 memcpy(data->qFunctionSource + strlen(fields_string), buffer, 107 strlen(buffer) + 1); 108 109 // Cleanup 110 ierr = CeedFree(&buffer); CeedChkBackend(ierr); 111 fclose(fp); 112 ierr = CeedFree(&hip_file); CeedChkBackend(ierr); 113 return CEED_ERROR_SUCCESS; 114 } 115 116 //------------------------------------------------------------------------------ 117 // Create QFunction 118 //------------------------------------------------------------------------------ 119 int CeedQFunctionCreate_Hip_gen(CeedQFunction qf) { 120 int ierr; 121 Ceed ceed; 122 CeedQFunctionGetCeed(qf, &ceed); 123 CeedQFunction_Hip_gen *data; 124 ierr = CeedCalloc(1, &data); CeedChkBackend(ierr); 125 ierr = CeedQFunctionSetData(qf, data); CeedChkBackend(ierr); 126 127 char *source; 128 ierr = CeedQFunctionGetSourcePath(qf, &source); CeedChkBackend(ierr); 129 const char *funname = strrchr(source, ':') + 1; 130 data->qFunctionName = (char *)funname; 131 const int filenamelen = funname - source; 132 char filename[filenamelen]; 133 memcpy(filename, source, filenamelen - 1); 134 filename[filenamelen - 1] = '\0'; 135 ierr = loadHipFunction(qf, filename); CeedChkBackend(ierr); 136 137 ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", 138 CeedQFunctionApply_Hip_gen); CeedChkBackend(ierr); 139 ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", 140 CeedQFunctionDestroy_Hip_gen); CeedChkBackend(ierr); 141 return CEED_ERROR_SUCCESS; 142 } 143 //------------------------------------------------------------------------------ 144