#pragma once /* types used by all PETSc Kokkos code */ #include #include #include #include // the pool is defined in veckok.kokkos.cxx as it is currently only used there PETSC_SINGLE_LIBRARY_INTERN PetscScalar *PetscScalarPool; PETSC_SINGLE_LIBRARY_INTERN PetscInt PetscScalarPoolSize; using DefaultExecutionSpace = Kokkos::DefaultExecutionSpace; using DefaultMemorySpace = Kokkos::DefaultExecutionSpace::memory_space; using HostMirrorMemorySpace = Kokkos::DualView::host_mirror_space::memory_space; /* Define a macro if DefaultMemorySpace and HostMirrorMemorySpace are the same */ #if defined(KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_SERIAL) || defined(KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_OPENMP) || defined(KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_THREADS) || defined(KOKKOS_ENABLE_DEFAULT_DEVICE_TYPE_HPX) || defined(KOKKOS_ENABLE_IMPL_CUDA_UNIFIED_MEMORY) || defined(KOKKOS_IMPL_HIP_UNIFIED_MEMORY) #define KOKKOS_ENABLE_UNIFIED_MEMORY #endif /* 1 to 4D PetscScalar Kokkos Views */ template using PetscScalarKokkosViewType = Kokkos::View; template using PetscScalarKokkosView1DType = Kokkos::View; template using PetscScalarKokkosView2DType = Kokkos::View; template using PetscScalarKokkosView3DType = Kokkos::View; template using PetscScalarKokkosView4DType = Kokkos::View; template using ConstPetscScalarKokkosViewType = Kokkos::View; template using ConstPetscScalarKokkosView1DType = Kokkos::View; template using ConstPetscScalarKokkosView2DType = Kokkos::View; template using ConstPetscScalarKokkosView3DType = Kokkos::View; template using ConstPetscScalarKokkosView4DType = Kokkos::View; /* 1 to 4D PetscScalar Kokkos OffsetViews */ template using PetscScalarKokkosOffsetViewType = Kokkos::Experimental::OffsetView; template using PetscScalarKokkosOffsetView1DType = Kokkos::Experimental::OffsetView; template using PetscScalarKokkosOffsetView2DType = Kokkos::Experimental::OffsetView; template using PetscScalarKokkosOffsetView3DType = Kokkos::Experimental::OffsetView; template using PetscScalarKokkosOffsetView4DType = Kokkos::Experimental::OffsetView; template using ConstPetscScalarKokkosOffsetViewType = Kokkos::Experimental::OffsetView; template using ConstPetscScalarKokkosOffsetView1DType = Kokkos::Experimental::OffsetView; template using ConstPetscScalarKokkosOffsetView2DType = Kokkos::Experimental::OffsetView; template using ConstPetscScalarKokkosOffsetView3DType = Kokkos::Experimental::OffsetView; template using ConstPetscScalarKokkosOffsetView4DType = Kokkos::Experimental::OffsetView; using PetscScalarKokkosDualView = Kokkos::DualView; /* Shortcut types for Views in the default space and host space */ using PetscScalarKokkosView = PetscScalarKokkosViewType; using PetscScalarKokkosView1D = PetscScalarKokkosView1DType; using PetscScalarKokkosView2D = PetscScalarKokkosView2DType; using PetscScalarKokkosView3D = PetscScalarKokkosView3DType; using PetscScalarKokkosView4D = PetscScalarKokkosView4DType; using PetscScalarKokkosViewHost = PetscScalarKokkosViewType; using PetscScalarKokkosView1DHost = PetscScalarKokkosView1DType; using PetscScalarKokkosView2DHost = PetscScalarKokkosView2DType; using PetscScalarKokkosView3DHost = PetscScalarKokkosView3DType; using PetscScalarKokkosView4DHost = PetscScalarKokkosView4DType; using ConstPetscScalarKokkosView = ConstPetscScalarKokkosViewType; using ConstPetscScalarKokkosView1D = ConstPetscScalarKokkosView1DType; using ConstPetscScalarKokkosView2D = ConstPetscScalarKokkosView2DType; using ConstPetscScalarKokkosView3D = ConstPetscScalarKokkosView3DType; using ConstPetscScalarKokkosView4D = ConstPetscScalarKokkosView4DType; using ConstPetscScalarKokkosViewHost = ConstPetscScalarKokkosViewType; using ConstPetscScalarKokkosView1DHost = ConstPetscScalarKokkosView1DType; using ConstPetscScalarKokkosView2DHost = ConstPetscScalarKokkosView2DType; using ConstPetscScalarKokkosView3DHost = ConstPetscScalarKokkosView3DType; using ConstPetscScalarKokkosView4DHost = ConstPetscScalarKokkosView4DType; /* Shortcut types for OffsetViews in the default space and host space */ using PetscScalarKokkosOffsetView = PetscScalarKokkosOffsetViewType; using PetscScalarKokkosOffsetView1D = PetscScalarKokkosOffsetView1DType; using PetscScalarKokkosOffsetView2D = PetscScalarKokkosOffsetView2DType; using PetscScalarKokkosOffsetView3D = PetscScalarKokkosOffsetView3DType; using PetscScalarKokkosOffsetView4D = PetscScalarKokkosOffsetView4DType; using PetscScalarKokkosOffsetViewHost = PetscScalarKokkosOffsetViewType; using PetscScalarKokkosOffsetView1DHost = PetscScalarKokkosOffsetView1DType; using PetscScalarKokkosOffsetView2DHost = PetscScalarKokkosOffsetView2DType; using PetscScalarKokkosOffsetView3DHost = PetscScalarKokkosOffsetView3DType; using PetscScalarKokkosOffsetView4DHost = PetscScalarKokkosOffsetView4DType; using ConstPetscScalarKokkosOffsetView = ConstPetscScalarKokkosOffsetViewType; using ConstPetscScalarKokkosOffsetView1D = ConstPetscScalarKokkosOffsetView1DType; using ConstPetscScalarKokkosOffsetView2D = ConstPetscScalarKokkosOffsetView2DType; using ConstPetscScalarKokkosOffsetView3D = ConstPetscScalarKokkosOffsetView3DType; using ConstPetscScalarKokkosOffsetView4D = ConstPetscScalarKokkosOffsetView4DType; using ConstPetscScalarKokkosOffsetViewHost = ConstPetscScalarKokkosOffsetViewType; using ConstPetscScalarKokkosOffsetView1DHost = ConstPetscScalarKokkosOffsetView1DType; using ConstPetscScalarKokkosOffsetView2DHost = ConstPetscScalarKokkosOffsetView2DType; using ConstPetscScalarKokkosOffsetView3DHost = ConstPetscScalarKokkosOffsetView3DType; using ConstPetscScalarKokkosOffsetView4DHost = ConstPetscScalarKokkosOffsetView4DType; using PetscIntKokkosView = Kokkos::View; using PetscIntKokkosViewHost = Kokkos::View; using PetscIntKokkosDualView = Kokkos::DualView; using PetscCountKokkosView = Kokkos::View; using PetscCountKokkosViewHost = Kokkos::View; // Sync a Kokkos::DualView to HostMirrorMemorySpace in execution space // If is HostMirrorMemorySpace, fence the exec so that the data on host is immediately available. template static PetscErrorCode KokkosDualViewSyncHost(Kokkos::DualView &v_dual, const Kokkos::DefaultExecutionSpace &exec) { size_t bytes = v_dual.extent(0) * sizeof(Type); PetscFunctionBegin; if (v_dual.need_sync_host()) { PetscCallCXX(v_dual.sync_host(exec)); if (!std::is_same_v) PetscCall(PetscLogGpuToCpu(bytes)); } // even if v_d and v_h share the same memory (as on AMD MI300A) and thus we don't need to sync_host, // we still need to fence the execution space as v_d might being populated by some async kernel, // and we need to finish it. PetscCallCXX(exec.fence()); PetscFunctionReturn(PETSC_SUCCESS); } template static PetscErrorCode KokkosDualViewSyncDevice(Kokkos::DualView &v_dual, const Kokkos::DefaultExecutionSpace &exec) { size_t bytes = v_dual.extent(0) * sizeof(Type); PetscFunctionBegin; if (v_dual.need_sync_device()) { PetscCallCXX(v_dual.sync_device(exec)); if (!std::is_same_v) PetscCall(PetscLogCpuToGpu(bytes)); } PetscFunctionReturn(PETSC_SUCCESS); }