@@ -659,14 +659,24 @@ int CeedGetOperatorFallbackCeed(Ceed ceed, Ceed *fallback_ceed) {
659659 fallback_ceed -> Error = ceed -> Error ;
660660 ceed -> op_fallback_ceed = fallback_ceed ;
661661 {
662- const char * * jit_source_dirs ;
663- CeedInt num_jit_source_dirs = 0 ;
662+ const char * * jit_source_roots ;
663+ CeedInt num_jit_source_roots = 0 ;
664664
665- CeedCall (CeedGetJitSourceRoots (ceed , & num_jit_source_dirs , & jit_source_dirs ));
666- for (CeedInt i = 0 ; i < num_jit_source_dirs ; i ++ ) {
667- CeedCall (CeedAddJitSourceRoot (fallback_ceed , jit_source_dirs [i ]));
665+ CeedCall (CeedGetJitSourceRoots (ceed , & num_jit_source_roots , & jit_source_roots ));
666+ for (CeedInt i = 0 ; i < num_jit_source_roots ; i ++ ) {
667+ CeedCall (CeedAddJitSourceRoot (fallback_ceed , jit_source_roots [i ]));
668668 }
669- CeedCall (CeedRestoreJitSourceRoots (ceed , & jit_source_dirs ));
669+ CeedCall (CeedRestoreJitSourceRoots (ceed , & jit_source_roots ));
670+ }
671+ {
672+ const char * * jit_defines ;
673+ CeedInt num_jit_defines = 0 ;
674+
675+ CeedCall (CeedGetJitDefines (ceed , & num_jit_defines , & jit_defines ));
676+ for (CeedInt i = 0 ; i < num_jit_defines ; i ++ ) {
677+ CeedCall (CeedAddJitSourceRoot (fallback_ceed , jit_defines [i ]));
678+ }
679+ CeedCall (CeedRestoreJitDefines (ceed , & jit_defines ));
670680 }
671681 }
672682 * fallback_ceed = ceed -> op_fallback_ceed ;
@@ -874,7 +884,7 @@ int CeedRestoreWorkVector(Ceed ceed, CeedVector *vec) {
874884}
875885
876886/**
877- @brief Retrieve list ofadditional JiT source roots from `Ceed` context.
887+ @brief Retrieve list of additional JiT source roots from `Ceed` context.
878888
879889 Note: The caller is responsible for restoring `jit_source_roots` with @ref CeedRestoreJitSourceRoots().
880890
@@ -892,6 +902,7 @@ int CeedGetJitSourceRoots(Ceed ceed, CeedInt *num_source_roots, const char ***ji
892902 CeedCall (CeedGetParent (ceed , & ceed_parent ));
893903 * num_source_roots = ceed_parent -> num_jit_source_roots ;
894904 * jit_source_roots = (const char * * )ceed_parent -> jit_source_roots ;
905+ ceed_parent -> num_jit_source_roots_readers ++ ;
895906 return CEED_ERROR_SUCCESS ;
896907}
897908
@@ -906,7 +917,53 @@ int CeedGetJitSourceRoots(Ceed ceed, CeedInt *num_source_roots, const char ***ji
906917 @ref Backend
907918**/
908919int CeedRestoreJitSourceRoots (Ceed ceed , const char * * * jit_source_roots ) {
920+ Ceed ceed_parent ;
921+
922+ CeedCall (CeedGetParent (ceed , & ceed_parent ));
909923 * jit_source_roots = NULL ;
924+ ceed_parent -> num_jit_source_roots_readers -- ;
925+ return CEED_ERROR_SUCCESS ;
926+ }
927+
928+ /**
929+ @brief Retrieve list of additional JiT defines from `Ceed` context.
930+
931+ Note: The caller is responsible for restoring `jit_defines` with @ref CeedRestoreJitDefines().
932+
933+ @param[in] ceed `Ceed` context
934+ @param[out] num_jit_defines Number of JiT defines
935+ @param[out] jit_defines Strings such as `foo=bar`, used as `-Dfoo=bar` in JiT
936+
937+ @return An error code: 0 - success, otherwise - failure
938+
939+ @ref Backend
940+ **/
941+ int CeedGetJitDefines (Ceed ceed , CeedInt * num_defines , const char * * * jit_defines ) {
942+ Ceed ceed_parent ;
943+
944+ CeedCall (CeedGetParent (ceed , & ceed_parent ));
945+ * num_defines = ceed_parent -> num_jit_defines ;
946+ * jit_defines = (const char * * )ceed_parent -> jit_defines ;
947+ ceed_parent -> num_jit_defines_readers ++ ;
948+ return CEED_ERROR_SUCCESS ;
949+ }
950+
951+ /**
952+ @brief Restore list of additional JiT defines from with @ref CeedGetJitDefines()
953+
954+ @param[in] ceed `Ceed` context
955+ @param[out] jit_defines String such as `foo=bar`, used as `-Dfoo=bar` in JiT
956+
957+ @return An error code: 0 - success, otherwise - failure
958+
959+ @ref Backend
960+ **/
961+ int CeedRestoreJitDefines (Ceed ceed , const char * * * jit_defines ) {
962+ Ceed ceed_parent ;
963+
964+ CeedCall (CeedGetParent (ceed , & ceed_parent ));
965+ * jit_defines = NULL ;
966+ ceed_parent -> num_jit_defines_readers -- ;
910967 return CEED_ERROR_SUCCESS ;
911968}
912969
@@ -1290,17 +1347,52 @@ int CeedAddJitSourceRoot(Ceed ceed, const char *jit_source_root) {
12901347 Ceed ceed_parent ;
12911348
12921349 CeedCall (CeedGetParent (ceed , & ceed_parent ));
1350+ CeedCheck (!ceed_parent -> num_jit_source_roots_readers , ceed , CEED_ERROR_ACCESS , "Cannot add JiT source root, read access has not been restored" );
12931351
12941352 CeedInt index = ceed_parent -> num_jit_source_roots ;
12951353 size_t path_length = strlen (jit_source_root );
12961354
1297- CeedCall (CeedRealloc (index + 1 , & ceed_parent -> jit_source_roots ));
1355+ if (ceed_parent -> num_jit_source_roots == ceed_parent -> max_jit_source_roots ) {
1356+ if (ceed_parent -> max_jit_source_roots == 0 ) ceed_parent -> max_jit_source_roots = 1 ;
1357+ ceed_parent -> max_jit_source_roots *= 2 ;
1358+ CeedCall (CeedRealloc (ceed_parent -> max_jit_source_roots , & ceed_parent -> jit_source_roots ));
1359+ }
12981360 CeedCall (CeedCalloc (path_length + 1 , & ceed_parent -> jit_source_roots [index ]));
12991361 memcpy (ceed_parent -> jit_source_roots [index ], jit_source_root , path_length );
13001362 ceed_parent -> num_jit_source_roots ++ ;
13011363 return CEED_ERROR_SUCCESS ;
13021364}
13031365
1366+ /**
1367+ @brief Set additional JiT compiler define for `Ceed` context
1368+
1369+ @param[in,out] ceed `Ceed` context
1370+ @param[in] jit_define String such as `foo=bar`, used as `-Dfoo=bar` in JiT
1371+
1372+ @return An error code: 0 - success, otherwise - failure
1373+
1374+ @ref User
1375+ **/
1376+ int CeedAddJitDefine (Ceed ceed , const char * jit_define ) {
1377+ Ceed ceed_parent ;
1378+
1379+ CeedCall (CeedGetParent (ceed , & ceed_parent ));
1380+ CeedCheck (!ceed_parent -> num_jit_defines_readers , ceed , CEED_ERROR_ACCESS , "Cannot add JiT define, read access has not been restored" );
1381+
1382+ CeedInt index = ceed_parent -> num_jit_defines ;
1383+ size_t define_length = strlen (jit_define );
1384+
1385+ if (ceed_parent -> num_jit_defines == ceed_parent -> max_jit_defines ) {
1386+ if (ceed_parent -> max_jit_defines == 0 ) ceed_parent -> max_jit_defines = 1 ;
1387+ ceed_parent -> max_jit_defines *= 2 ;
1388+ CeedCall (CeedRealloc (ceed_parent -> max_jit_defines , & ceed_parent -> jit_defines ));
1389+ }
1390+ CeedCall (CeedCalloc (define_length + 1 , & ceed_parent -> jit_defines [index ]));
1391+ memcpy (ceed_parent -> jit_defines [index ], jit_define , define_length );
1392+ ceed_parent -> num_jit_defines ++ ;
1393+ return CEED_ERROR_SUCCESS ;
1394+ }
1395+
13041396/**
13051397 @brief View a `Ceed`
13061398
@@ -1338,6 +1430,11 @@ int CeedDestroy(Ceed *ceed) {
13381430 * ceed = NULL ;
13391431 return CEED_ERROR_SUCCESS ;
13401432 }
1433+
1434+ CeedCheck (!(* ceed )-> num_jit_source_roots_readers , * ceed , CEED_ERROR_ACCESS ,
1435+ "Cannot destroy ceed context, read access for JiT source roots has been granted" );
1436+ CeedCheck (!(* ceed )-> num_jit_defines_readers , * ceed , CEED_ERROR_ACCESS , "Cannot add JiT source root, read access for JiT defines has been granted" );
1437+
13411438 if ((* ceed )-> delegate ) CeedCall (CeedDestroy (& (* ceed )-> delegate ));
13421439
13431440 if ((* ceed )-> obj_delegate_count > 0 ) {
@@ -1355,6 +1452,11 @@ int CeedDestroy(Ceed *ceed) {
13551452 }
13561453 CeedCall (CeedFree (& (* ceed )-> jit_source_roots ));
13571454
1455+ for (CeedInt i = 0 ; i < (* ceed )-> num_jit_defines ; i ++ ) {
1456+ CeedCall (CeedFree (& (* ceed )-> jit_defines [i ]));
1457+ }
1458+ CeedCall (CeedFree (& (* ceed )-> jit_defines ));
1459+
13581460 CeedCall (CeedFree (& (* ceed )-> f_offsets ));
13591461 CeedCall (CeedFree (& (* ceed )-> resource ));
13601462 CeedCall (CeedDestroy (& (* ceed )-> op_fallback_ceed ));
0 commit comments