xref: /petsc/include/petscvec_kokkos.hpp (revision c3339decea92175325d9368fa13196bcd0e0e58b)
16524c165SJacob Faibussowitsch #ifndef PETSCVEC_KOKKOS_HPP
2152b3e56SJunchao Zhang #define PETSCVEC_KOKKOS_HPP
3152b3e56SJunchao Zhang 
411d22bbfSJunchao Zhang #include <petscconf.h>
511d22bbfSJunchao Zhang 
6ac09b921SBarry Smith /* SUBMANSEC = Vec */
7ac09b921SBarry Smith 
811d22bbfSJunchao Zhang #if defined(PETSC_HAVE_KOKKOS)
911d22bbfSJunchao Zhang   #if defined(petsccomplexlib)
1011d22bbfSJunchao Zhang     #error "Error: You must include petscvec_kokkos.hpp before other petsc headers in this C++ file to use petsc complex with Kokkos"
1111d22bbfSJunchao Zhang   #endif
1211d22bbfSJunchao Zhang 
1311d22bbfSJunchao Zhang   #define PETSC_DESIRE_KOKKOS_COMPLEX 1 /* To control the definition of petsccomplexlib in petscsystypes.h */
1411d22bbfSJunchao Zhang #endif
1511d22bbfSJunchao Zhang 
16152b3e56SJunchao Zhang #include <petscvec.h>
17152b3e56SJunchao Zhang 
18152b3e56SJunchao Zhang #if defined(PETSC_HAVE_KOKKOS)
19152b3e56SJunchao Zhang   #include <Kokkos_Core.hpp>
20152b3e56SJunchao Zhang 
2121bad809SJunchao Zhang /*@C
2221bad809SJunchao Zhang      VecGetKokkosView - Returns a constant Kokkos View that contains up-to-date data of a vector in the specified memory space.
23152b3e56SJunchao Zhang 
2421bad809SJunchao Zhang    Synopsis:
2521bad809SJunchao Zhang    #include <petscvec_kokkos.hpp>
2621bad809SJunchao Zhang    PetscErrorCode VecGetKokkosView  (Vec v,Kokkos::View<const PetscScalar*,MemorySpace>* kv);
2721bad809SJunchao Zhang    PetscErrorCode VecGetKokkosView  (Vec v,Kokkos::View<PetscScalar*,MemorySpace>* kv);
2821bad809SJunchao Zhang 
29*c3339decSBarry Smith    Logically Collective
3021bad809SJunchao Zhang 
3121bad809SJunchao Zhang    Input Parameter:
3287497f52SBarry Smith .  v - the vector in type of `VECKOKKOS`
3321bad809SJunchao Zhang 
3421bad809SJunchao Zhang    Output Parameter:
3521bad809SJunchao Zhang .  kv - the Kokkos View with a user-specified template parameter MemorySpace
3621bad809SJunchao Zhang 
3721bad809SJunchao Zhang    Notes:
3887497f52SBarry Smith    If the vector is not of type `VECKOKKOS`, an error will be raised.
3987497f52SBarry Smith 
4087497f52SBarry Smith    The functions are similar to `VecGetArrayRead()` and `VecGetArray()` respectively. One can read-only or read/write the returned Kokkos View.
4187497f52SBarry Smith 
4287497f52SBarry Smith    Passing in a const View enables read-only access.
4387497f52SBarry Smith 
4487497f52SBarry Smith    One must return the View by a matching `VecRestoreKokkosView()` after finishing using the View. Currently, only two memory
4521bad809SJunchao Zhang    spaces are supported: Kokkos::HostSpace and Kokkos::DefaultExecutionSpace::memory_space.
4621bad809SJunchao Zhang    If needed, a memory copy will be internally called to copy the latest vector data to the specified memory space.
4721bad809SJunchao Zhang 
4821bad809SJunchao Zhang    Level: beginner
4921bad809SJunchao Zhang 
50db781477SPatrick Sanan .seealso: `VecRestoreKokkosView()`, `VecRestoreArray()`, `VecGetKokkosViewWrite()`, `VecGetArrayRead()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecGetArrayReadF90()`, `VecPlaceArray()`, `VecGetArray2d()`,
51db781477SPatrick Sanan           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`, `VecRestoreArrayWrite()`
5221bad809SJunchao Zhang @*/
539371c9d4SSatish Balay template <class MemorySpace>
549371c9d4SSatish Balay PetscErrorCode VecGetKokkosView(Vec, Kokkos::View<const PetscScalar *, MemorySpace> *);
559371c9d4SSatish Balay template <class MemorySpace>
569371c9d4SSatish Balay PetscErrorCode VecGetKokkosView(Vec, Kokkos::View<PetscScalar *, MemorySpace> *);
5721bad809SJunchao Zhang 
5821bad809SJunchao Zhang /*@C
5987497f52SBarry Smith    VecRestoreKokkosView - Returns a Kokkos View gotten by `VecGetKokkosView()`.
6021bad809SJunchao Zhang 
6121bad809SJunchao Zhang    Synopsis:
6221bad809SJunchao Zhang    #include <petscvec_kokkos.hpp>
6321bad809SJunchao Zhang    PetscErrorCode VecRestoreKokkosView  (Vec v,Kokkos::View<const PetscScalar*,MemorySpace>* kv);
6421bad809SJunchao Zhang    PetscErrorCode VecRestoreKokkosView  (Vec v,Kokkos::View<PetscScalar*,MemorySpace>* kv);
6521bad809SJunchao Zhang 
66*c3339decSBarry Smith    Logically Collective
6721bad809SJunchao Zhang 
68f1a722f8SMatthew G. Knepley    Input Parameters:
6987497f52SBarry Smith +  v  - the vector in type of `VECKOKKOS`
7021bad809SJunchao Zhang -  kv - the Kokkos View with a user-specified template parameter MemorySpace
7121bad809SJunchao Zhang 
7221bad809SJunchao Zhang    Notes:
7387497f52SBarry Smith    If the vector is not of type `VECKOKKOS`, an error will be raised.
7487497f52SBarry Smith    The functions are similar to `VecRestoreArrayRead()` and `VecRestoreArray()` respectively. They are the counterpart of `VecGetKokkosView()`.
7521bad809SJunchao Zhang 
7621bad809SJunchao Zhang    Level: beginner
7721bad809SJunchao Zhang 
78db781477SPatrick Sanan .seealso: `VecGetKokkosView()`, `VecRestoreKokkosViewWrite()`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecGetArrayReadF90()`, `VecPlaceArray()`, `VecGetArray2d()`,
79db781477SPatrick Sanan           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`, `VecRestoreArrayWrite()`
8021bad809SJunchao Zhang @*/
819371c9d4SSatish Balay template <class MemorySpace>
82d71ae5a4SJacob Faibussowitsch PetscErrorCode VecRestoreKokkosView(Vec, Kokkos::View<const PetscScalar *, MemorySpace> *)
83d71ae5a4SJacob Faibussowitsch {
849371c9d4SSatish Balay   return 0;
859371c9d4SSatish Balay }
869371c9d4SSatish Balay template <class MemorySpace>
879371c9d4SSatish Balay PetscErrorCode VecRestoreKokkosView(Vec, Kokkos::View<PetscScalar *, MemorySpace> *);
8821bad809SJunchao Zhang 
8921bad809SJunchao Zhang /*@C
9021bad809SJunchao Zhang    VecGetKokkosViewWrite - Returns a Kokkos View that contains the array of a vector in the specified memory space.
9121bad809SJunchao Zhang 
9221bad809SJunchao Zhang    Synopsis:
9321bad809SJunchao Zhang    #include <petscvec_kokkos.hpp>
9421bad809SJunchao Zhang    PetscErrorCode VecGetKokkosViewWrite  (Vec v,Kokkos::View<PetscScalar*,MemorySpace>* kv);
9521bad809SJunchao Zhang 
96*c3339decSBarry Smith    Logically Collective
9721bad809SJunchao Zhang 
9821bad809SJunchao Zhang    Input Parameter:
9987497f52SBarry Smith .  v - the vector in type of `VECKOKKOS`
10021bad809SJunchao Zhang 
10121bad809SJunchao Zhang    Output Parameter:
10221bad809SJunchao Zhang .  kv - the Kokkos View with a user-specified template parameter MemorySpace
10321bad809SJunchao Zhang 
10421bad809SJunchao Zhang    Notes:
10587497f52SBarry Smith    If the vector is not of type `VECKOKKOS`, an error will be raised.
10687497f52SBarry Smith 
10787497f52SBarry Smith    The functions is similar to `VecGetArrayWrite()`. The returned view might contain garbage data or stale data and one is not
10821bad809SJunchao Zhang    expected to read data from the View. Instead, one is expected to overwrite all data in the View.
10987497f52SBarry Smith    One must return the View by a matching `VecRestoreKokkosViewWrite()` after finishing using the View.
11087497f52SBarry Smith 
11121bad809SJunchao Zhang    Currently, only two memory spaces are supported: Kokkos::HostSpace and Kokkos::DefaultExecutionSpace::memory_space.
11221bad809SJunchao Zhang 
11321bad809SJunchao Zhang    Level: beginner
11421bad809SJunchao Zhang 
115db781477SPatrick Sanan .seealso: `VecRestoreKokkosViewWrite()`, `VecRestoreKokkosView()`, `VecGetKokkosView()`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecGetArrayReadF90()`, `VecPlaceArray()`, `VecGetArray2d()`,
116db781477SPatrick Sanan           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`, `VecRestoreArrayWrite()`
11721bad809SJunchao Zhang @*/
1189371c9d4SSatish Balay template <class MemorySpace>
1199371c9d4SSatish Balay PetscErrorCode VecGetKokkosViewWrite(Vec, Kokkos::View<PetscScalar *, MemorySpace> *);
12021bad809SJunchao Zhang 
12121bad809SJunchao Zhang /*@C
12287497f52SBarry Smith    VecRestoreKokkosViewWrite - Returns a Kokkos View gotten with `VecGetKokkosViewWrite()`.
12321bad809SJunchao Zhang 
12421bad809SJunchao Zhang    Synopsis:
12521bad809SJunchao Zhang    #include <petscvec_kokkos.hpp>
12621bad809SJunchao Zhang    PetscErrorCode VecRestoreKokkosViewWrite  (Vec v,Kokkos::View<PetscScalar*,MemorySpace>* kv);
12721bad809SJunchao Zhang 
128*c3339decSBarry Smith    Logically Collective
12921bad809SJunchao Zhang 
130f1a722f8SMatthew G. Knepley    Input Parameters:
13187497f52SBarry Smith +  v  - the vector in type of `VECKOKKOS`
13221bad809SJunchao Zhang -  kv - the Kokkos View with a user-specified template parameter MemorySpace
13321bad809SJunchao Zhang 
13421bad809SJunchao Zhang    Notes:
13587497f52SBarry Smith    If the vector is not of type `VECKOKKOS`, an error will be raised.
13687497f52SBarry Smith 
13787497f52SBarry Smith    The function is similar to `VecRestoreArrayWrite()`. It is the counterpart of `VecGetKokkosViewWrite()`.
13821bad809SJunchao Zhang 
13921bad809SJunchao Zhang    Level: beginner
14021bad809SJunchao Zhang 
141db781477SPatrick Sanan .seealso: `VecGetKokkosViewWrite()`, `VecGetKokkosView()`, `VecGetKokkosView()`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecGetArrayReadF90()`, `VecPlaceArray()`, `VecGetArray2d()`,
142db781477SPatrick Sanan           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`, `VecRestoreArrayWrite()`
14321bad809SJunchao Zhang @*/
1449371c9d4SSatish Balay template <class MemorySpace>
1459371c9d4SSatish Balay PetscErrorCode VecRestoreKokkosViewWrite(Vec, Kokkos::View<PetscScalar *, MemorySpace> *);
146152b3e56SJunchao Zhang 
147a02b07edSJunchao Zhang   #if defined(PETSC_HAVE_COMPLEX) && defined(PETSC_USE_COMPLEX)
148a02b07edSJunchao Zhang static_assert(std::alignment_of<Kokkos::complex<PetscReal>>::value == std::alignment_of<std::complex<PetscReal>>::value,
149a02b07edSJunchao 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");
150a02b07edSJunchao Zhang   #endif
151a02b07edSJunchao Zhang 
152152b3e56SJunchao Zhang #endif
153152b3e56SJunchao Zhang 
154152b3e56SJunchao Zhang #endif
155