xref: /petsc/include/petscvec_kokkos.hpp (revision 09b68a49ed2854d1e4985cc2aa6af33c7c4e69b3)
1a4963045SJacob Faibussowitsch #pragma once
2152b3e56SJunchao Zhang 
311d22bbfSJunchao Zhang #include <petscconf.h>
411d22bbfSJunchao Zhang 
5ac09b921SBarry Smith /* SUBMANSEC = Vec */
6ac09b921SBarry Smith 
711d22bbfSJunchao Zhang #if defined(PETSC_HAVE_KOKKOS)
811d22bbfSJunchao Zhang   #if defined(petsccomplexlib)
9*f0b74427SPierre Jolivet     #error "Error: You must include petscvec_kokkos.hpp before other PETSc headers in this C++ file to use PetscComplex with Kokkos"
1011d22bbfSJunchao Zhang   #endif
1111d22bbfSJunchao Zhang 
1211d22bbfSJunchao Zhang   #define PETSC_DESIRE_KOKKOS_COMPLEX 1 /* To control the definition of petsccomplexlib in petscsystypes.h */
1311d22bbfSJunchao Zhang #endif
1411d22bbfSJunchao Zhang 
15152b3e56SJunchao Zhang #include <petscvec.h>
16152b3e56SJunchao Zhang 
17152b3e56SJunchao Zhang #if defined(PETSC_HAVE_KOKKOS)
18152b3e56SJunchao Zhang   #include <Kokkos_Core.hpp>
19152b3e56SJunchao Zhang 
2021bad809SJunchao Zhang /*@C
2121bad809SJunchao Zhang      VecGetKokkosView - Returns a constant Kokkos View that contains up-to-date data of a vector in the specified memory space.
22152b3e56SJunchao Zhang 
2321bad809SJunchao Zhang    Synopsis:
2421bad809SJunchao Zhang    #include <petscvec_kokkos.hpp>
2521bad809SJunchao Zhang    PetscErrorCode VecGetKokkosView  (Vec v,Kokkos::View<const PetscScalar*,MemorySpace>* kv);
2621bad809SJunchao Zhang    PetscErrorCode VecGetKokkosView  (Vec v,Kokkos::View<PetscScalar*,MemorySpace>* kv);
2721bad809SJunchao Zhang 
28cc4c1da9SBarry Smith    Logically Collective, No Fortran Support
2921bad809SJunchao Zhang 
3021bad809SJunchao Zhang    Input Parameter:
3187497f52SBarry Smith .  v - the vector in type of `VECKOKKOS`
3221bad809SJunchao Zhang 
3321bad809SJunchao Zhang    Output Parameter:
3421bad809SJunchao Zhang .  kv - the Kokkos View with a user-specified template parameter MemorySpace
3521bad809SJunchao Zhang 
3695bd0b28SBarry Smith    Level: beginner
3795bd0b28SBarry Smith 
3821bad809SJunchao Zhang    Notes:
3987497f52SBarry Smith    If the vector is not of type `VECKOKKOS`, an error will be raised.
4087497f52SBarry Smith 
4187497f52SBarry Smith    The functions are similar to `VecGetArrayRead()` and `VecGetArray()` respectively. One can read-only or read/write the returned Kokkos View.
4287497f52SBarry Smith 
4387497f52SBarry Smith    Passing in a const View enables read-only access.
4487497f52SBarry Smith 
4587497f52SBarry Smith    One must return the View by a matching `VecRestoreKokkosView()` after finishing using the View. Currently, only two memory
4645402d8aSJunchao Zhang    spaces are supported: HostMirrorMemorySpace and Kokkos::DefaultExecutionSpace::memory_space.
4721bad809SJunchao Zhang    If needed, a memory copy will be internally called to copy the latest vector data to the specified memory space.
4821bad809SJunchao Zhang 
49ce78bad3SBarry Smith .seealso: `VecRestoreKokkosView()`, `VecRestoreArray()`, `VecGetKokkosViewWrite()`, `VecGetArrayRead()`, `VecGetArrays()`, `VecPlaceArray()`, `VecGetArray2d()`,
50db781477SPatrick Sanan           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`, `VecRestoreArrayWrite()`
5121bad809SJunchao Zhang @*/
529371c9d4SSatish Balay template <class MemorySpace>
539371c9d4SSatish Balay PetscErrorCode VecGetKokkosView(Vec, Kokkos::View<const PetscScalar *, MemorySpace> *);
549371c9d4SSatish Balay template <class MemorySpace>
559371c9d4SSatish Balay PetscErrorCode VecGetKokkosView(Vec, Kokkos::View<PetscScalar *, MemorySpace> *);
5621bad809SJunchao Zhang 
5721bad809SJunchao Zhang /*@C
5887497f52SBarry Smith    VecRestoreKokkosView - Returns a Kokkos View gotten by `VecGetKokkosView()`.
5921bad809SJunchao Zhang 
6021bad809SJunchao Zhang    Synopsis:
6121bad809SJunchao Zhang    #include <petscvec_kokkos.hpp>
6221bad809SJunchao Zhang    PetscErrorCode VecRestoreKokkosView  (Vec v,Kokkos::View<const PetscScalar*,MemorySpace>* kv);
6321bad809SJunchao Zhang    PetscErrorCode VecRestoreKokkosView  (Vec v,Kokkos::View<PetscScalar*,MemorySpace>* kv);
6421bad809SJunchao Zhang 
65cc4c1da9SBarry Smith    Logically Collective, No Fortran Support
6621bad809SJunchao Zhang 
67f1a722f8SMatthew G. Knepley    Input Parameters:
6887497f52SBarry Smith +  v  - the vector in type of `VECKOKKOS`
6921bad809SJunchao Zhang -  kv - the Kokkos View with a user-specified template parameter MemorySpace
7021bad809SJunchao Zhang 
7195bd0b28SBarry Smith    Level: beginner
7295bd0b28SBarry Smith 
7395bd0b28SBarry Smith    Note:
7487497f52SBarry Smith    If the vector is not of type `VECKOKKOS`, an error will be raised.
7587497f52SBarry Smith    The functions are similar to `VecRestoreArrayRead()` and `VecRestoreArray()` respectively. They are the counterpart of `VecGetKokkosView()`.
7621bad809SJunchao Zhang 
77ce78bad3SBarry Smith .seealso: `VecGetKokkosView()`, `VecRestoreKokkosViewWrite()`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecGetArrays()`, `VecPlaceArray()`, `VecGetArray2d()`,
78db781477SPatrick Sanan           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`, `VecRestoreArrayWrite()`
7921bad809SJunchao Zhang @*/
809371c9d4SSatish Balay template <class MemorySpace>
VecRestoreKokkosView(Vec,Kokkos::View<const PetscScalar *,MemorySpace> *)81d71ae5a4SJacob Faibussowitsch PetscErrorCode VecRestoreKokkosView(Vec, Kokkos::View<const PetscScalar *, MemorySpace> *)
82d71ae5a4SJacob Faibussowitsch {
833ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
849371c9d4SSatish Balay }
859371c9d4SSatish Balay template <class MemorySpace>
869371c9d4SSatish Balay PetscErrorCode VecRestoreKokkosView(Vec, Kokkos::View<PetscScalar *, MemorySpace> *);
8721bad809SJunchao Zhang 
8821bad809SJunchao Zhang /*@C
8921bad809SJunchao Zhang    VecGetKokkosViewWrite - Returns a Kokkos View that contains the array of a vector in the specified memory space.
9021bad809SJunchao Zhang 
9121bad809SJunchao Zhang    Synopsis:
9221bad809SJunchao Zhang    #include <petscvec_kokkos.hpp>
9321bad809SJunchao Zhang    PetscErrorCode VecGetKokkosViewWrite  (Vec v,Kokkos::View<PetscScalar*,MemorySpace>* kv);
9421bad809SJunchao Zhang 
95cc4c1da9SBarry Smith    Logically Collective, No Fortran Support
9621bad809SJunchao Zhang 
9721bad809SJunchao Zhang    Input Parameter:
9887497f52SBarry Smith .  v - the vector in type of `VECKOKKOS`
9921bad809SJunchao Zhang 
10021bad809SJunchao Zhang    Output Parameter:
10121bad809SJunchao Zhang .  kv - the Kokkos View with a user-specified template parameter MemorySpace
10221bad809SJunchao Zhang 
10395bd0b28SBarry Smith    Level: beginner
10495bd0b28SBarry Smith 
10521bad809SJunchao Zhang    Notes:
10687497f52SBarry Smith    If the vector is not of type `VECKOKKOS`, an error will be raised.
10787497f52SBarry Smith 
10887497f52SBarry Smith    The functions is similar to `VecGetArrayWrite()`. The returned view might contain garbage data or stale data and one is not
10921bad809SJunchao Zhang    expected to read data from the View. Instead, one is expected to overwrite all data in the View.
11087497f52SBarry Smith    One must return the View by a matching `VecRestoreKokkosViewWrite()` after finishing using the View.
11187497f52SBarry Smith 
11245402d8aSJunchao Zhang    Currently, only two memory spaces are supported: HostMirrorMemorySpace and Kokkos::DefaultExecutionSpace::memory_space.
11321bad809SJunchao Zhang 
114ce78bad3SBarry Smith .seealso: `VecRestoreKokkosViewWrite()`, `VecRestoreKokkosView()`, `VecGetKokkosView()`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecGetArrays()`, `VecPlaceArray()`, `VecGetArray2d()`,
115db781477SPatrick Sanan           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`, `VecRestoreArrayWrite()`
11621bad809SJunchao Zhang @*/
1179371c9d4SSatish Balay template <class MemorySpace>
1189371c9d4SSatish Balay PetscErrorCode VecGetKokkosViewWrite(Vec, Kokkos::View<PetscScalar *, MemorySpace> *);
11921bad809SJunchao Zhang 
12021bad809SJunchao Zhang /*@C
12187497f52SBarry Smith    VecRestoreKokkosViewWrite - Returns a Kokkos View gotten with `VecGetKokkosViewWrite()`.
12221bad809SJunchao Zhang 
12321bad809SJunchao Zhang    Synopsis:
12421bad809SJunchao Zhang    #include <petscvec_kokkos.hpp>
12521bad809SJunchao Zhang    PetscErrorCode VecRestoreKokkosViewWrite  (Vec v,Kokkos::View<PetscScalar*,MemorySpace>* kv);
12621bad809SJunchao Zhang 
127cc4c1da9SBarry Smith    Logically Collective, No Fortran Support
12821bad809SJunchao Zhang 
129f1a722f8SMatthew G. Knepley    Input Parameters:
13087497f52SBarry Smith +  v  - the vector in type of `VECKOKKOS`
13121bad809SJunchao Zhang -  kv - the Kokkos View with a user-specified template parameter MemorySpace
13221bad809SJunchao Zhang 
13395bd0b28SBarry Smith    Level: beginner
13495bd0b28SBarry Smith 
13521bad809SJunchao Zhang    Notes:
13687497f52SBarry Smith    If the vector is not of type `VECKOKKOS`, an error will be raised.
13787497f52SBarry Smith 
13887497f52SBarry Smith    The function is similar to `VecRestoreArrayWrite()`. It is the counterpart of `VecGetKokkosViewWrite()`.
13921bad809SJunchao Zhang 
140ce78bad3SBarry Smith .seealso: `VecGetKokkosViewWrite()`, `VecGetKokkosView()`, `VecGetKokkosView()`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecGetArrays()`, `VecPlaceArray()`, `VecGetArray2d()`,
141db781477SPatrick Sanan           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`, `VecRestoreArrayWrite()`
14221bad809SJunchao Zhang @*/
1439371c9d4SSatish Balay template <class MemorySpace>
1449371c9d4SSatish Balay PetscErrorCode VecRestoreKokkosViewWrite(Vec, Kokkos::View<PetscScalar *, MemorySpace> *);
145152b3e56SJunchao Zhang 
146a02b07edSJunchao Zhang   #if defined(PETSC_HAVE_COMPLEX) && defined(PETSC_USE_COMPLEX)
147a02b07edSJunchao Zhang static_assert(std::alignment_of<Kokkos::complex<PetscReal>>::value == std::alignment_of<std::complex<PetscReal>>::value,
148a02b07edSJunchao 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");
149a02b07edSJunchao Zhang   #endif
150a02b07edSJunchao Zhang 
151152b3e56SJunchao Zhang #endif
152