1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at 2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights 3 // 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-impl.h> 18 #include <ceed-backend.h> 19 #include <string.h> 20 21 /// @file 22 /// Implementation of public CeedQFunction interfaces 23 /// 24 /// @addtogroup CeedQFunction 25 /// @{ 26 27 /** 28 @brief Create a CeedQFunction for evaluating interior (volumetric) terms. 29 30 @param ceed A Ceed object where the CeedQFunction will be created 31 @param vlength Vector length. Caller must ensure that number of quadrature 32 points is a multiple of vlength. 33 @param f Function pointer to evaluate action at quadrature points. 34 See below. 35 @param focca OCCA identifier "file.c:function_name" for definition of `f` 36 @param[out] qf Address of the variable where the newly created 37 CeedQFunction will be stored 38 39 @return An error code: 0 - success, otherwise - failure 40 41 The arguments of the call-back 'function' are: 42 43 1. [void *ctx][in/out] - user data, this is the 'ctx' pointer stored in 44 the CeedQFunction, set by calling CeedQFunctionSetContext 45 46 2. [CeedInt nq][in] - number of quadrature points to process 47 48 3. [const CeedScalar *const *u][in] - input fields data at quadrature pts, listed in the order given by the user 49 50 4. [CeedScalar *const *v][out] - output fields data at quadrature points, again listed in order given by the user 51 52 @ref Basic 53 **/ 54 int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vlength, 55 int (*f)(void*, CeedInt, const CeedScalar *const*, CeedScalar *const*), 56 const char *focca, CeedQFunction *qf) { 57 int ierr; 58 char *focca_copy; 59 60 if (!ceed->QFunctionCreate) { 61 Ceed delegate; 62 ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr); 63 64 if (!delegate) 65 return CeedError(ceed, 1, "Backend does not support QFunctionCreate"); 66 67 ierr = CeedQFunctionCreateInterior(delegate, vlength, f, focca, qf); 68 CeedChk(ierr); 69 return 0; 70 } 71 72 ierr = CeedCalloc(1,qf); CeedChk(ierr); 73 (*qf)->ceed = ceed; 74 ceed->refcount++; 75 (*qf)->refcount = 1; 76 (*qf)->vlength = vlength; 77 (*qf)->function = f; 78 ierr = CeedCalloc(strlen(focca)+1, &focca_copy); CeedChk(ierr); 79 strcpy(focca_copy, focca); 80 (*qf)->focca = focca_copy; 81 ierr = CeedCalloc(16, &(*qf)->inputfields); CeedChk(ierr); 82 ierr = CeedCalloc(16, &(*qf)->outputfields); CeedChk(ierr); 83 ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr); 84 return 0; 85 } 86 87 /** 88 @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output 89 90 @param f CeedQFunctionField 91 @param fieldname Name of QFunction field 92 @param ncomp Number of components per quadrature node 93 @param emode \ref CEED_EVAL_NONE to use values directly, 94 \ref CEED_EVAL_INTERP to use interpolated values, 95 \ref CEED_EVAL_GRAD to use gradients. 96 97 @return An error code: 0 - success, otherwise - failure 98 99 @ref Developer 100 **/ 101 static int CeedQFunctionFieldSet(CeedQFunctionField *f,const char *fieldname, 102 CeedInt ncomp, CeedEvalMode emode) { 103 size_t len = strlen(fieldname); 104 char *tmp; 105 int ierr; 106 ierr = CeedCalloc(1,f); CeedChk(ierr); 107 108 ierr = CeedCalloc(len+1, &tmp); CeedChk(ierr); 109 memcpy(tmp, fieldname, len+1); 110 (*f)->fieldname = tmp; 111 (*f)->ncomp = ncomp; 112 (*f)->emode = emode; 113 return 0; 114 } 115 116 /** 117 @brief Add a CeedQFunction input 118 119 @param qf CeedQFunction 120 @param fieldname Name of QFunction field 121 @param ncomp Number of components per quadrature node 122 @param emode \ref CEED_EVAL_NONE to use values directly, 123 \ref CEED_EVAL_INTERP to use interpolated values, 124 \ref CEED_EVAL_GRAD to use gradients. 125 126 @return An error code: 0 - success, otherwise - failure 127 128 @ref Basic 129 **/ 130 int CeedQFunctionAddInput(CeedQFunction qf, const char *fieldname, 131 CeedInt ncomp, CeedEvalMode emode) { 132 int ierr = CeedQFunctionFieldSet(&qf->inputfields[qf->numinputfields], 133 fieldname, ncomp, emode); 134 CeedChk(ierr); 135 qf->numinputfields++; 136 return 0; 137 } 138 139 /** 140 @brief Add a CeedQFunction output 141 142 @param qf CeedQFunction 143 @param fieldname Name of QFunction field 144 @param ncomp Number of components per quadrature node 145 @param emode \ref CEED_EVAL_NONE to use values directly, 146 \ref CEED_EVAL_INTERP to use interpolated values, 147 \ref CEED_EVAL_GRAD to use gradients. 148 149 @return An error code: 0 - success, otherwise - failure 150 151 @ref Basic 152 **/ 153 int CeedQFunctionAddOutput(CeedQFunction qf, const char *fieldname, 154 CeedInt ncomp, CeedEvalMode emode) { 155 if (emode == CEED_EVAL_WEIGHT) 156 return CeedError(qf->ceed, 1, 157 "Cannot create qfunction output with CEED_EVAL_WEIGHT"); 158 int ierr = CeedQFunctionFieldSet(&qf->outputfields[qf->numoutputfields], 159 fieldname, ncomp, emode); 160 CeedChk(ierr); 161 qf->numoutputfields++; 162 return 0; 163 } 164 165 /** 166 @brief Get the Ceed associated with a CeedQFunction 167 168 @param qf CeedQFunction 169 @param[out] ceed Variable to store Ceed 170 171 @return An error code: 0 - success, otherwise - failure 172 173 @ref Advanced 174 **/ 175 176 int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) { 177 *ceed = qf->ceed; 178 return 0; 179 } 180 181 /** 182 @brief Get the vector length of a CeedQFunction 183 184 @param qf CeedQFunction 185 @param[out] veclength Variable to store vector length 186 187 @return An error code: 0 - success, otherwise - failure 188 189 @ref Advanced 190 **/ 191 192 int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vlength) { 193 *vlength = qf->vlength; 194 return 0; 195 } 196 197 /** 198 @brief Get the number of inputs and outputs to a CeedQFunction 199 200 @param qf CeedQFunction 201 @param[out] numinput Variable to store number of input fields 202 @param[out] numoutput Variable to store number of output fields 203 204 @return An error code: 0 - success, otherwise - failure 205 206 @ref Advanced 207 **/ 208 209 int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *numinput, 210 CeedInt *numoutput) { 211 if (numinput) *numinput = qf->numinputfields; 212 if (numoutput) *numoutput = qf->numoutputfields; 213 return 0; 214 } 215 216 /** 217 @brief Get the FOCCA string for a CeedQFunction 218 219 @param qf CeedQFunction 220 @param[out] focca Variable to store focca string 221 222 @return An error code: 0 - success, otherwise - failure 223 224 @ref Advanced 225 **/ 226 227 int CeedQFunctionGetFOCCA(CeedQFunction qf, char* *focca) { 228 *focca = (char*) qf->focca; 229 return 0; 230 } 231 232 /** 233 @brief Get the User Function for a CeedQFunction 234 235 @param qf CeedQFunction 236 @param[out] f Variable to store user function 237 238 @return An error code: 0 - success, otherwise - failure 239 240 @ref Advanced 241 **/ 242 243 int CeedQFunctionGetUserFunction(CeedQFunction qf, int (**f)()) { 244 *f = (int (*)())qf->function; 245 return 0; 246 } 247 248 /** 249 @brief Get global context size for a CeedQFunction 250 251 @param qf CeedQFunction 252 @param[out] ctxsize Variable to store size of context data values 253 254 @return An error code: 0 - success, otherwise - failure 255 256 @ref Advanced 257 **/ 258 259 int CeedQFunctionGetContextSize(CeedQFunction qf, size_t *ctxsize) { 260 *ctxsize = qf->ctxsize; 261 return 0; 262 } 263 264 /** 265 @brief Get global context for a CeedQFunction 266 267 @param qf CeedQFunction 268 @param[out] ctx Variable to store context data values 269 270 @return An error code: 0 - success, otherwise - failure 271 272 @ref Advanced 273 **/ 274 275 int CeedQFunctionGetContext(CeedQFunction qf, void* *ctx) { 276 *ctx = qf->ctx; 277 return 0; 278 } 279 280 /** 281 @brief Determine if Fortran interface was used 282 283 @param qf CeedQFunction 284 @param[out] fortranstatus Variable to store Fortran status 285 286 @return An error code: 0 - success, otherwise - failure 287 288 @ref Advanced 289 **/ 290 291 int CeedQFunctionGetFortranStatus(CeedQFunction qf, bool *fortranstatus) { 292 *fortranstatus = qf->fortranstatus; 293 return 0; 294 } 295 296 /** 297 @brief Get backend data of a CeedQFunction 298 299 @param qf CeedQFunction 300 @param[out] data Variable to store data 301 302 @return An error code: 0 - success, otherwise - failure 303 304 @ref Advanced 305 **/ 306 307 int CeedQFunctionGetData(CeedQFunction qf, void* *data) { 308 *data = qf->data; 309 return 0; 310 } 311 312 /** 313 @brief Set backend data of a CeedQFunction 314 315 @param[out] qf CeedQFunction 316 @param data Data to set 317 318 @return An error code: 0 - success, otherwise - failure 319 320 @ref Advanced 321 **/ 322 323 int CeedQFunctionSetData(CeedQFunction qf, void* *data) { 324 qf->data = *data; 325 return 0; 326 } 327 328 /** 329 @brief Set global context for a CeedQFunction 330 331 @param qf CeedQFunction 332 @param ctx Context data to set 333 @param ctxsize Size of context data values 334 335 @return An error code: 0 - success, otherwise - failure 336 337 @ref Basic 338 **/ 339 int CeedQFunctionSetContext(CeedQFunction qf, void *ctx, size_t ctxsize) { 340 qf->ctx = ctx; 341 qf->ctxsize = ctxsize; 342 return 0; 343 } 344 345 /** 346 @brief Apply the action of a CeedQFunction 347 348 @param qf CeedQFunction 349 @param Q Number of quadrature points 350 @param[in] u Array of input data arrays 351 @param[out] v Array of output data arrays 352 353 @return An error code: 0 - success, otherwise - failure 354 355 @ref Advanced 356 **/ 357 int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, 358 CeedVector* u, CeedVector* v) { 359 int ierr; 360 if (!qf->Apply) 361 return CeedError(qf->ceed, 1, "Backend does not support QFunctionApply"); 362 if (Q % qf->vlength) 363 return CeedError(qf->ceed, 2, 364 "Number of quadrature points %d must be a multiple of %d", 365 Q, qf->vlength); 366 ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr); 367 return 0; 368 } 369 370 /** 371 @brief Get the CeedQFunctionFields of a CeedQFunction 372 373 @param qf CeedQFunction 374 @param[out] inputfields Variable to store inputfields 375 @param[out] outputfields Variable to store outputfields 376 377 @return An error code: 0 - success, otherwise - failure 378 379 @ref Advanced 380 **/ 381 382 int CeedQFunctionGetFields(CeedQFunction qf, 383 CeedQFunctionField* *inputfields, 384 CeedQFunctionField* *outputfields) { 385 if (inputfields) *inputfields = qf->inputfields; 386 if (outputfields) *outputfields = qf->outputfields; 387 return 0; 388 } 389 390 /** 391 @brief Get the name of a CeedQFunctionField 392 393 @param qffield CeedQFunctionField 394 @param[out] fieldname Variable to store the field name 395 396 @return An error code: 0 - success, otherwise - failure 397 398 @ref Advanced 399 **/ 400 401 int CeedQFunctionFieldGetName(CeedQFunctionField qffield, 402 char* *fieldname) { 403 *fieldname = (char *)qffield->fieldname; 404 return 0; 405 } 406 407 /** 408 @brief Get the number of components of a CeedQFunctionField 409 410 @param qffield CeedQFunctionField 411 @param[out] numcomp Variable to store the number of components 412 413 @return An error code: 0 - success, otherwise - failure 414 415 @ref Advanced 416 **/ 417 418 int CeedQFunctionFieldGetNumComponents(CeedQFunctionField qffield, 419 CeedInt *numcomp) { 420 *numcomp = qffield->ncomp; 421 return 0; 422 } 423 424 /** 425 @brief Get the CeedEvalMode of a CeedQFunctionField 426 427 @param qffield CeedQFunctionField 428 @param[out] vec Variable to store the number of components 429 430 @return An error code: 0 - success, otherwise - failure 431 432 @ref Advanced 433 **/ 434 435 int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qffield, 436 CeedEvalMode *emode) { 437 *emode = qffield->emode; 438 return 0; 439 } 440 441 /** 442 @brief Destroy a CeedQFunction 443 444 @param qf CeedQFunction to destroy 445 446 @return An error code: 0 - success, otherwise - failure 447 448 @ref Basic 449 **/ 450 int CeedQFunctionDestroy(CeedQFunction *qf) { 451 int ierr; 452 453 if (!*qf || --(*qf)->refcount > 0) return 0; 454 // Backend destroy 455 if ((*qf)->Destroy) { 456 ierr = (*qf)->Destroy(*qf); CeedChk(ierr); 457 } 458 // Free fields 459 for (int i=0; i<(*qf)->numinputfields; i++) { 460 ierr = CeedFree(&(*(*qf)->inputfields[i]).fieldname); CeedChk(ierr); 461 ierr = CeedFree(&(*qf)->inputfields[i]); CeedChk(ierr); 462 } 463 for (int i=0; i<(*qf)->numoutputfields; i++) { 464 ierr = CeedFree(&(*(*qf)->outputfields[i]).fieldname); CeedChk(ierr); 465 ierr = CeedFree(&(*qf)->outputfields[i]); CeedChk(ierr); 466 } 467 ierr = CeedFree(&(*qf)->inputfields); CeedChk(ierr); 468 ierr = CeedFree(&(*qf)->outputfields); CeedChk(ierr); 469 470 ierr = CeedFree(&(*qf)->focca); CeedChk(ierr); 471 ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr); 472 ierr = CeedFree(qf); CeedChk(ierr); 473 return 0; 474 } 475 476 /// @} 477