1 #include <petsc/private/deviceimpl.h> 2 #include <Kokkos_Core.hpp> 3 4 PetscBool PetscKokkosInitialized = PETSC_FALSE; 5 6 PetscErrorCode PetscKokkosFinalize_Private(void) 7 { 8 PetscFunctionBegin; 9 Kokkos::finalize(); 10 PetscFunctionReturn(0); 11 } 12 13 PetscErrorCode PetscKokkosIsInitialized_Private(PetscBool *isInitialized) 14 { 15 PetscFunctionBegin; 16 *isInitialized = Kokkos::is_initialized() ? PETSC_TRUE : PETSC_FALSE; 17 PetscFunctionReturn(0); 18 } 19 20 /* Initialize Kokkos if not yet */ 21 PetscErrorCode PetscKokkosInitializeCheck(void) 22 { 23 PetscFunctionBegin; 24 if (!Kokkos::is_initialized()) { 25 auto args = Kokkos::InitArguments{}; /* use default constructor */ 26 27 #if (defined(KOKKOS_ENABLE_CUDA) && PetscDefined(HAVE_CUDA)) || (defined(KOKKOS_ENABLE_HIP) && PetscDefined(HAVE_HIP)) || (defined(KOKKOS_ENABLE_SYCL) && PetscDefined(HAVE_SYCL)) 28 /* Kokkos does not support CUDA and HIP at the same time (but we do :)) */ 29 PetscDeviceContext dctx; 30 31 PetscCall(PetscDeviceContextGetCurrentContext(&dctx)); 32 PetscCall(PetscMPIIntCast(dctx->device->deviceId,&args.device_id)); 33 #endif 34 35 args.disable_warnings = !PetscDefined(HAVE_KOKKOS_INIT_WARNINGS); 36 37 /* To use PetscNumOMPThreads, one has to configure petsc --with-openmp. 38 Otherwise, let's keep the default value (-1) of args.num_threads. 39 */ 40 #if defined(KOKKOS_ENABLE_OPENMP) && PetscDefined(HAVE_OPENMP) 41 args.num_threads = PetscNumOMPThreads; 42 #endif 43 44 Kokkos::initialize(args); 45 PetscBeganKokkos = PETSC_TRUE; 46 } 47 PetscKokkosInitialized = PETSC_TRUE; 48 PetscFunctionReturn(0); 49 } 50