xref: /petsc/include/petscvec_kokkos.hpp (revision a02b07ed3db079d23118a0427f32d5760d7944b7)
1152b3e56SJunchao Zhang #if !defined(PETSCVEC_KOKKOS_HPP)
2152b3e56SJunchao Zhang #define PETSCVEC_KOKKOS_HPP
3152b3e56SJunchao Zhang 
411d22bbfSJunchao Zhang #include <petscconf.h>
511d22bbfSJunchao Zhang 
611d22bbfSJunchao Zhang #if defined(PETSC_HAVE_KOKKOS)
711d22bbfSJunchao Zhang   #if defined(petsccomplexlib)
811d22bbfSJunchao Zhang     #error "Error: You must include petscvec_kokkos.hpp before other petsc headers in this C++ file to use petsc complex with Kokkos"
911d22bbfSJunchao Zhang   #endif
1011d22bbfSJunchao Zhang 
1111d22bbfSJunchao Zhang   #define PETSC_DESIRE_KOKKOS_COMPLEX   1   /* To control the definition of petsccomplexlib in petscsystypes.h */
1211d22bbfSJunchao Zhang #endif
1311d22bbfSJunchao Zhang 
14152b3e56SJunchao Zhang #include <petscvec.h>
15152b3e56SJunchao Zhang 
16152b3e56SJunchao Zhang #if defined(PETSC_HAVE_KOKKOS)
17152b3e56SJunchao Zhang   #include <Kokkos_Core.hpp>
18152b3e56SJunchao Zhang 
1921bad809SJunchao Zhang /*@C
2021bad809SJunchao Zhang      VecGetKokkosView - Returns a constant Kokkos View that contains up-to-date data of a vector in the specified memory space.
21152b3e56SJunchao Zhang 
2221bad809SJunchao Zhang    Synopsis:
2321bad809SJunchao Zhang    #include <petscvec_kokkos.hpp>
2421bad809SJunchao Zhang    PetscErrorCode VecGetKokkosView  (Vec v,Kokkos::View<const PetscScalar*,MemorySpace>* kv);
2521bad809SJunchao Zhang    PetscErrorCode VecGetKokkosView  (Vec v,Kokkos::View<PetscScalar*,MemorySpace>* kv);
2621bad809SJunchao Zhang 
2721bad809SJunchao Zhang    Logically Collective on Vec
2821bad809SJunchao Zhang 
2921bad809SJunchao Zhang    Input Parameter:
3021bad809SJunchao Zhang .  v - the vector in type of VECKOKKOS
3121bad809SJunchao Zhang 
3221bad809SJunchao Zhang    Output Parameter:
3321bad809SJunchao Zhang .  kv - the Kokkos View with a user-specified template parameter MemorySpace
3421bad809SJunchao Zhang 
3521bad809SJunchao Zhang    Notes:
3621bad809SJunchao Zhang    If the vector is not of type VECKOKKOS, an error will be raised.
3721bad809SJunchao Zhang    The functions are similar to VecGetArrayRead() and VecGetArray() respectively. One can read-only or read/write the returned Kokkos View.
3821bad809SJunchao Zhang    Note that passing in a const View enables read-only access.
3921bad809SJunchao Zhang    One must return the View by a matching VecRestoreKokkosView() after finishing using the View. Currently, only two memory
4021bad809SJunchao Zhang    spaces are supported: Kokkos::HostSpace and Kokkos::DefaultExecutionSpace::memory_space.
4121bad809SJunchao Zhang    If needed, a memory copy will be internally called to copy the latest vector data to the specified memory space.
4221bad809SJunchao Zhang 
4321bad809SJunchao Zhang    Level: beginner
4421bad809SJunchao Zhang 
4521bad809SJunchao Zhang .seealso: VecRestoreKokkosView(), VecRestoreArray(), VecGetKokkosViewWrite(), VecGetArrayRead(), VecGetArrays(), VecGetArrayF90(), VecGetArrayReadF90(), VecPlaceArray(), VecGetArray2d(),
4621bad809SJunchao Zhang           VecGetArrayPair(), VecRestoreArrayPair(), VecGetArrayWrite(), VecRestoreArrayWrite()
4721bad809SJunchao Zhang @*/
48152b3e56SJunchao Zhang   template<class MemorySpace> PetscErrorCode VecGetKokkosView (Vec,Kokkos::View<const PetscScalar*,MemorySpace>*);
49152b3e56SJunchao Zhang   template<class MemorySpace> PetscErrorCode VecGetKokkosView (Vec,Kokkos::View<PetscScalar*,MemorySpace>*);
5021bad809SJunchao Zhang 
5121bad809SJunchao Zhang /*@C
5221bad809SJunchao Zhang    VecRestoreKokkosView - Returns a Kokkos View gotten by VecGetKokkosView().
5321bad809SJunchao Zhang 
5421bad809SJunchao Zhang    Synopsis:
5521bad809SJunchao Zhang    #include <petscvec_kokkos.hpp>
5621bad809SJunchao Zhang    PetscErrorCode VecRestoreKokkosView  (Vec v,Kokkos::View<const PetscScalar*,MemorySpace>* kv);
5721bad809SJunchao Zhang    PetscErrorCode VecRestoreKokkosView  (Vec v,Kokkos::View<PetscScalar*,MemorySpace>* kv);
5821bad809SJunchao Zhang 
5921bad809SJunchao Zhang    Logically Collective on Vec
6021bad809SJunchao Zhang 
61f1a722f8SMatthew G. Knepley    Input Parameters:
6221bad809SJunchao Zhang +  v  - the vector in type of VECKOKKOS
6321bad809SJunchao Zhang -  kv - the Kokkos View with a user-specified template parameter MemorySpace
6421bad809SJunchao Zhang 
6521bad809SJunchao Zhang    Notes:
6621bad809SJunchao Zhang    If the vector is not of type VECKOKKOS, an error will be raised.
6721bad809SJunchao Zhang    The functions are similar to VecRestoreArrayRead() and VecRestoreArray() respectively. They are the counterpart of VecGetKokkosView().
6821bad809SJunchao Zhang 
6921bad809SJunchao Zhang    Level: beginner
7021bad809SJunchao Zhang 
7121bad809SJunchao Zhang .seealso: VecGetKokkosView(), VecRestoreKokkosViewWrite(), VecRestoreArray(), VecGetArrayRead(), VecGetArrays(), VecGetArrayF90(), VecGetArrayReadF90(), VecPlaceArray(), VecGetArray2d(),
7221bad809SJunchao Zhang           VecGetArrayPair(), VecRestoreArrayPair(), VecGetArrayWrite(), VecRestoreArrayWrite()
7321bad809SJunchao Zhang @*/
7421bad809SJunchao Zhang   template<class MemorySpace> PetscErrorCode VecRestoreKokkosView(Vec,Kokkos::View<const PetscScalar*,MemorySpace>*){return 0;}
75152b3e56SJunchao Zhang   template<class MemorySpace> PetscErrorCode VecRestoreKokkosView(Vec,Kokkos::View<PetscScalar*,MemorySpace>*);
76152b3e56SJunchao Zhang 
7721bad809SJunchao Zhang 
7821bad809SJunchao Zhang /*@C
7921bad809SJunchao Zhang    VecGetKokkosViewWrite - Returns a Kokkos View that contains the array of a vector in the specified memory space.
8021bad809SJunchao Zhang 
8121bad809SJunchao Zhang    Synopsis:
8221bad809SJunchao Zhang    #include <petscvec_kokkos.hpp>
8321bad809SJunchao Zhang    PetscErrorCode VecGetKokkosViewWrite  (Vec v,Kokkos::View<PetscScalar*,MemorySpace>* kv);
8421bad809SJunchao Zhang 
8521bad809SJunchao Zhang    Logically Collective on Vec
8621bad809SJunchao Zhang 
8721bad809SJunchao Zhang    Input Parameter:
8821bad809SJunchao Zhang .  v - the vector in type of VECKOKKOS
8921bad809SJunchao Zhang 
9021bad809SJunchao Zhang    Output Parameter:
9121bad809SJunchao Zhang .  kv - the Kokkos View with a user-specified template parameter MemorySpace
9221bad809SJunchao Zhang 
9321bad809SJunchao Zhang    Notes:
9421bad809SJunchao Zhang    If the vector is not of type VECKOKKOS, an error will be raised.
9521bad809SJunchao Zhang    The functions is similar to VecGetArrayWrite(). The returned view might contain garbage data or stale data and one is not
9621bad809SJunchao Zhang    expected to read data from the View. Instead, one is expected to overwrite all data in the View.
9721bad809SJunchao Zhang    One must return the View by a matching VecRestoreKokkosViewWrite() after finishing using the View.
9821bad809SJunchao Zhang   Currently, only two memory spaces are supported: Kokkos::HostSpace and Kokkos::DefaultExecutionSpace::memory_space.
9921bad809SJunchao Zhang 
10021bad809SJunchao Zhang    Level: beginner
10121bad809SJunchao Zhang 
10221bad809SJunchao Zhang .seealso: VecRestoreKokkosViewWrite(), VecRestoreKokkosView(), VecGetKokkosView(), VecRestoreArray(), VecGetArrayRead(), VecGetArrays(), VecGetArrayF90(), VecGetArrayReadF90(), VecPlaceArray(), VecGetArray2d(),
10321bad809SJunchao Zhang           VecGetArrayPair(), VecRestoreArrayPair(), VecGetArrayWrite(), VecRestoreArrayWrite()
10421bad809SJunchao Zhang @*/
105152b3e56SJunchao Zhang   template<class MemorySpace> PetscErrorCode VecGetKokkosViewWrite    (Vec,Kokkos::View<PetscScalar*,MemorySpace>*);
10621bad809SJunchao Zhang 
10721bad809SJunchao Zhang /*@C
10821bad809SJunchao Zhang    VecRestoreKokkosViewWrite - Returns a Kokkos View gotten by VecGetKokkosViewWrite().
10921bad809SJunchao Zhang 
11021bad809SJunchao Zhang    Synopsis:
11121bad809SJunchao Zhang    #include <petscvec_kokkos.hpp>
11221bad809SJunchao Zhang    PetscErrorCode VecRestoreKokkosViewWrite  (Vec v,Kokkos::View<PetscScalar*,MemorySpace>* kv);
11321bad809SJunchao Zhang 
11421bad809SJunchao Zhang    Logically Collective on Vec
11521bad809SJunchao Zhang 
116f1a722f8SMatthew G. Knepley    Input Parameters:
11721bad809SJunchao Zhang +  v  - the vector in type of VECKOKKOS
11821bad809SJunchao Zhang -  kv - the Kokkos View with a user-specified template parameter MemorySpace
11921bad809SJunchao Zhang 
12021bad809SJunchao Zhang    Notes:
12121bad809SJunchao Zhang    If the vector is not of type VECKOKKOS, an error will be raised.
12221bad809SJunchao Zhang    The function is similar to VecRestoreArrayWrite(). It is the counterpart of VecGetKokkosViewWrite().
12321bad809SJunchao Zhang 
12421bad809SJunchao Zhang    Level: beginner
12521bad809SJunchao Zhang 
12621bad809SJunchao Zhang .seealso: VecGetKokkosViewWrite(), VecGetKokkosView(), VecGetKokkosView(), VecRestoreArray(), VecGetArrayRead(), VecGetArrays(), VecGetArrayF90(), VecGetArrayReadF90(), VecPlaceArray(), VecGetArray2d(),
12721bad809SJunchao Zhang           VecGetArrayPair(), VecRestoreArrayPair(), VecGetArrayWrite(), VecRestoreArrayWrite()
12821bad809SJunchao Zhang @*/
129152b3e56SJunchao Zhang   template<class MemorySpace> PetscErrorCode VecRestoreKokkosViewWrite(Vec,Kokkos::View<PetscScalar*,MemorySpace>*);
130152b3e56SJunchao Zhang 
131*a02b07edSJunchao Zhang  #if defined(PETSC_HAVE_COMPLEX) && defined(PETSC_USE_COMPLEX)
132*a02b07edSJunchao Zhang   static_assert(std::alignment_of<Kokkos::complex<PetscReal>>::value == std::alignment_of<std::complex<PetscReal>>::value,
133*a02b07edSJunchao Zhang    "Alignment of Kokkos::complex<PetscReal> and std::complex<PetscReal> mismatch. Reconfigure your Kokkos with -DKOKKOS_ENABLE_COMPLEX_ALIGN=OFF, or let PETSc install Kokkos for you with --download-kokkos --download-kokkos-kernels");
134*a02b07edSJunchao Zhang  #endif
135*a02b07edSJunchao Zhang 
136152b3e56SJunchao Zhang #endif
137152b3e56SJunchao Zhang 
138152b3e56SJunchao Zhang #endif
139