Actual source code: mpikok.kokkos.cxx
1: /*
2: This file contains routines for Parallel vector operations.
3: */
4: #include <petsc_kokkos.hpp>
5: #include <petscvec_kokkos.hpp>
6: #include <petsc/private/deviceimpl.h>
7: #include <petsc/private/vecimpl.h>
8: #include <../src/vec/vec/impls/mpi/pvecimpl.h>
9: #include <../src/vec/vec/impls/seq/kokkos/veckokkosimpl.hpp>
10: #include <petscsf.h>
12: static PetscErrorCode VecDestroy_MPIKokkos(Vec v)
13: {
14: PetscFunctionBegin;
15: delete static_cast<Vec_Kokkos *>(v->spptr);
16: PetscCall(VecDestroy_MPI(v));
17: PetscFunctionReturn(PETSC_SUCCESS);
18: }
20: static PetscErrorCode VecNorm_MPIKokkos(Vec xin, NormType type, PetscReal *z)
21: {
22: PetscFunctionBegin;
23: PetscCall(VecNorm_MPI_Default(xin, type, z, VecNorm_SeqKokkos));
24: PetscFunctionReturn(PETSC_SUCCESS);
25: }
27: static PetscErrorCode VecErrorWeightedNorms_MPIKokkos(Vec U, Vec Y, Vec E, NormType wnormtype, PetscReal atol, Vec vatol, PetscReal rtol, Vec vrtol, PetscReal ignore_max, PetscReal *norm, PetscInt *norm_loc, PetscReal *norma, PetscInt *norma_loc, PetscReal *normr, PetscInt *normr_loc)
28: {
29: PetscFunctionBegin;
30: PetscCall(VecErrorWeightedNorms_MPI_Default(U, Y, E, wnormtype, atol, vatol, rtol, vrtol, ignore_max, norm, norm_loc, norma, norma_loc, normr, normr_loc, VecErrorWeightedNorms_SeqKokkos));
31: PetscFunctionReturn(PETSC_SUCCESS);
32: }
34: /* z = y^H x */
35: static PetscErrorCode VecDot_MPIKokkos(Vec xin, Vec yin, PetscScalar *z)
36: {
37: PetscFunctionBegin;
38: PetscCall(VecXDot_MPI_Default(xin, yin, z, VecDot_SeqKokkos));
39: PetscFunctionReturn(PETSC_SUCCESS);
40: }
42: /* z = y^T x */
43: static PetscErrorCode VecTDot_MPIKokkos(Vec xin, Vec yin, PetscScalar *z)
44: {
45: PetscFunctionBegin;
46: PetscCall(VecXDot_MPI_Default(xin, yin, z, VecTDot_SeqKokkos));
47: PetscFunctionReturn(PETSC_SUCCESS);
48: }
50: static PetscErrorCode VecMDot_MPIKokkos(Vec xin, PetscInt nv, const Vec y[], PetscScalar *z)
51: {
52: PetscFunctionBegin;
53: PetscCall(VecMXDot_MPI_Default(xin, nv, y, z, VecMDot_SeqKokkos));
54: PetscFunctionReturn(PETSC_SUCCESS);
55: }
57: static PetscErrorCode VecMTDot_MPIKokkos(Vec xin, PetscInt nv, const Vec y[], PetscScalar *z)
58: {
59: PetscFunctionBegin;
60: PetscCall(VecMXDot_MPI_Default(xin, nv, y, z, VecMTDot_SeqKokkos));
61: PetscFunctionReturn(PETSC_SUCCESS);
62: }
64: static PetscErrorCode VecMDot_MPIKokkos_GEMV(Vec xin, PetscInt nv, const Vec y[], PetscScalar *z)
65: {
66: PetscFunctionBegin;
67: PetscCall(VecMXDot_MPI_Default(xin, nv, y, z, VecMDot_SeqKokkos_GEMV));
68: PetscFunctionReturn(PETSC_SUCCESS);
69: }
71: static PetscErrorCode VecMTDot_MPIKokkos_GEMV(Vec xin, PetscInt nv, const Vec y[], PetscScalar *z)
72: {
73: PetscFunctionBegin;
74: PetscCall(VecMXDot_MPI_Default(xin, nv, y, z, VecMTDot_SeqKokkos_GEMV));
75: PetscFunctionReturn(PETSC_SUCCESS);
76: }
78: static PetscErrorCode VecMax_MPIKokkos(Vec xin, PetscInt *idx, PetscReal *z)
79: {
80: const MPI_Op ops[] = {MPIU_MAXLOC, MPIU_MAX};
82: PetscFunctionBegin;
83: PetscCall(VecMinMax_MPI_Default(xin, idx, z, VecMax_SeqKokkos, ops));
84: PetscFunctionReturn(PETSC_SUCCESS);
85: }
87: static PetscErrorCode VecMin_MPIKokkos(Vec xin, PetscInt *idx, PetscReal *z)
88: {
89: const MPI_Op ops[] = {MPIU_MINLOC, MPIU_MIN};
91: PetscFunctionBegin;
92: PetscCall(VecMinMax_MPI_Default(xin, idx, z, VecMin_SeqKokkos, ops));
93: PetscFunctionReturn(PETSC_SUCCESS);
94: }
96: static PetscErrorCode VecCreate_MPIKokkos_Common(Vec); // forward declaration
98: static PetscErrorCode VecDuplicate_MPIKokkos(Vec win, Vec *vv)
99: {
100: Vec v;
101: Vec_Kokkos *veckok;
102: Vec_MPI *wdata = (Vec_MPI *)win->data;
104: PetscScalarKokkosDualView w_dual;
106: PetscFunctionBegin;
107: PetscCallCXX(w_dual = PetscScalarKokkosDualView("w_dual", win->map->n + wdata->nghost)); // Kokkos init's v_dual to zero
109: /* Reuse VecDuplicate_MPI, which contains a lot of stuff */
110: PetscCall(VecDuplicateWithArray_MPI(win, w_dual.view_host().data(), &v)); /* after the call, v is a VECMPI */
111: PetscCall(PetscObjectChangeTypeName((PetscObject)v, VECMPIKOKKOS));
112: PetscCall(VecCreate_MPIKokkos_Common(v));
113: v->ops[0] = win->ops[0]; // always follow ops[] in win
115: /* Build the Vec_Kokkos struct */
116: veckok = new Vec_Kokkos(v->map->n, w_dual.view_host().data(), w_dual.view_device().data());
117: veckok->w_dual = w_dual;
118: v->spptr = veckok;
119: *vv = v;
120: PetscFunctionReturn(PETSC_SUCCESS);
121: }
123: static PetscErrorCode VecDotNorm2_MPIKokkos(Vec s, Vec t, PetscScalar *dp, PetscScalar *nm)
124: {
125: PetscFunctionBegin;
126: PetscCall(VecDotNorm2_MPI_Default(s, t, dp, nm, VecDotNorm2_SeqKokkos));
127: PetscFunctionReturn(PETSC_SUCCESS);
128: }
130: static PetscErrorCode VecGetSubVector_MPIKokkos(Vec x, IS is, Vec *y)
131: {
132: PetscFunctionBegin;
133: PetscCall(VecGetSubVector_Kokkos_Private(x, PETSC_TRUE, is, y));
134: PetscFunctionReturn(PETSC_SUCCESS);
135: }
137: static PetscErrorCode VecSetPreallocationCOO_MPIKokkos(Vec x, PetscCount ncoo, const PetscInt coo_i[])
138: {
139: const auto vecmpi = static_cast<Vec_MPI *>(x->data);
140: const auto veckok = static_cast<Vec_Kokkos *>(x->spptr);
141: PetscInt m;
143: PetscFunctionBegin;
144: PetscCall(VecGetLocalSize(x, &m));
145: PetscCall(VecSetPreallocationCOO_MPI(x, ncoo, coo_i));
146: PetscCall(veckok->SetUpCOO(vecmpi, m));
147: PetscFunctionReturn(PETSC_SUCCESS);
148: }
150: static PetscErrorCode VecSetValuesCOO_MPIKokkos(Vec x, const PetscScalar v[], InsertMode imode)
151: {
152: const auto vecmpi = static_cast<Vec_MPI *>(x->data);
153: const auto veckok = static_cast<Vec_Kokkos *>(x->spptr);
154: const PetscCountKokkosView &jmap1 = veckok->jmap1_d;
155: const PetscCountKokkosView &perm1 = veckok->perm1_d;
156: const PetscCountKokkosView &imap2 = veckok->imap2_d;
157: const PetscCountKokkosView &jmap2 = veckok->jmap2_d;
158: const PetscCountKokkosView &perm2 = veckok->perm2_d;
159: const PetscCountKokkosView &Cperm = veckok->Cperm_d;
160: PetscScalarKokkosView &sendbuf = veckok->sendbuf_d;
161: PetscScalarKokkosView &recvbuf = veckok->recvbuf_d;
162: PetscScalarKokkosView xv;
163: ConstPetscScalarKokkosView vv;
164: PetscMemType memtype;
165: PetscInt m;
167: PetscFunctionBegin;
168: PetscCall(VecGetLocalSize(x, &m));
169: PetscCall(PetscGetMemType(v, &memtype));
170: if (PetscMemTypeHost(memtype)) { /* If user gave v[] in host, we might need to copy it to device if any */
171: vv = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(), PetscScalarKokkosViewHost(const_cast<PetscScalar *>(v), vecmpi->coo_n));
172: } else {
173: vv = ConstPetscScalarKokkosView(v, vecmpi->coo_n); /* Directly use v[]'s memory */
174: }
176: /* Pack entries to be sent to remote */
177: Kokkos::parallel_for(Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, vecmpi->sendlen), KOKKOS_LAMBDA(const PetscCount i) { sendbuf(i) = vv(Cperm(i)); });
178: PetscCall(PetscSFReduceWithMemTypeBegin(vecmpi->coo_sf, MPIU_SCALAR, PETSC_MEMTYPE_KOKKOS, sendbuf.data(), PETSC_MEMTYPE_KOKKOS, recvbuf.data(), MPI_REPLACE));
180: if (imode == INSERT_VALUES) PetscCall(VecGetKokkosViewWrite(x, &xv)); /* write vector */
181: else PetscCall(VecGetKokkosView(x, &xv)); /* read & write vector */
183: Kokkos::parallel_for(
184: Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, m), KOKKOS_LAMBDA(const PetscCount i) {
185: PetscScalar sum = 0.0;
186: for (PetscCount k = jmap1(i); k < jmap1(i + 1); k++) sum += vv(perm1(k));
187: xv(i) = (imode == INSERT_VALUES ? 0.0 : xv(i)) + sum;
188: });
190: PetscCall(PetscSFReduceEnd(vecmpi->coo_sf, MPIU_SCALAR, sendbuf.data(), recvbuf.data(), MPI_REPLACE));
192: /* Add received remote entries */
193: Kokkos::parallel_for(
194: Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, vecmpi->nnz2), KOKKOS_LAMBDA(PetscCount i) {
195: for (PetscCount k = jmap2(i); k < jmap2(i + 1); k++) xv(imap2(i)) += recvbuf(perm2(k));
196: });
198: if (imode == INSERT_VALUES) PetscCall(VecRestoreKokkosViewWrite(x, &xv));
199: else PetscCall(VecRestoreKokkosView(x, &xv));
200: PetscFunctionReturn(PETSC_SUCCESS);
201: }
203: // Shared by all VecCreate/Duplicate routines for VecMPIKokkos
204: static PetscErrorCode VecCreate_MPIKokkos_Common(Vec v)
205: {
206: PetscFunctionBegin;
207: v->boundtocpu = PetscDefined(HAVE_KOKKOS_WITHOUT_GPU) ? PETSC_TRUE : PETSC_FALSE; // VECKOKKOS has yet to support CPU binding. But in this case, we deem it is bound to CPU.
208: v->ops->bindtocpu = VecBindToCPU_SeqKokkos;
209: v->ops->abs = VecAbs_SeqKokkos;
210: v->ops->reciprocal = VecReciprocal_SeqKokkos;
211: v->ops->pointwisemult = VecPointwiseMult_SeqKokkos;
212: v->ops->setrandom = VecSetRandom_SeqKokkos;
213: v->ops->dotnorm2 = VecDotNorm2_MPIKokkos;
214: v->ops->waxpy = VecWAXPY_SeqKokkos;
215: v->ops->norm = VecNorm_MPIKokkos;
216: v->ops->min = VecMin_MPIKokkos;
217: v->ops->max = VecMax_MPIKokkos;
218: v->ops->sum = VecSum_SeqKokkos;
219: v->ops->shift = VecShift_SeqKokkos;
220: v->ops->scale = VecScale_SeqKokkos;
221: v->ops->copy = VecCopy_SeqKokkos;
222: v->ops->set = VecSet_SeqKokkos;
223: v->ops->swap = VecSwap_SeqKokkos;
224: v->ops->axpy = VecAXPY_SeqKokkos;
225: v->ops->axpby = VecAXPBY_SeqKokkos;
226: v->ops->maxpy = VecMAXPY_SeqKokkos;
227: v->ops->aypx = VecAYPX_SeqKokkos;
228: v->ops->axpbypcz = VecAXPBYPCZ_SeqKokkos;
229: v->ops->pointwisedivide = VecPointwiseDivide_SeqKokkos;
230: v->ops->placearray = VecPlaceArray_SeqKokkos;
231: v->ops->replacearray = VecReplaceArray_SeqKokkos;
232: v->ops->resetarray = VecResetArray_SeqKokkos;
234: v->ops->dot = VecDot_MPIKokkos;
235: v->ops->tdot = VecTDot_MPIKokkos;
236: v->ops->mdot = VecMDot_MPIKokkos;
237: v->ops->mtdot = VecMTDot_MPIKokkos;
239: v->ops->dot_local = VecDot_SeqKokkos;
240: v->ops->tdot_local = VecTDot_SeqKokkos;
241: v->ops->mdot_local = VecMDot_SeqKokkos;
242: v->ops->mtdot_local = VecMTDot_SeqKokkos;
244: v->ops->norm_local = VecNorm_SeqKokkos;
245: v->ops->duplicate = VecDuplicate_MPIKokkos;
246: v->ops->destroy = VecDestroy_MPIKokkos;
247: v->ops->getlocalvector = VecGetLocalVector_SeqKokkos;
248: v->ops->restorelocalvector = VecRestoreLocalVector_SeqKokkos;
249: v->ops->getlocalvectorread = VecGetLocalVector_SeqKokkos;
250: v->ops->restorelocalvectorread = VecRestoreLocalVector_SeqKokkos;
251: v->ops->getarraywrite = VecGetArrayWrite_SeqKokkos;
252: v->ops->getarray = VecGetArray_SeqKokkos;
253: v->ops->restorearray = VecRestoreArray_SeqKokkos;
254: v->ops->getarrayandmemtype = VecGetArrayAndMemType_SeqKokkos;
255: v->ops->restorearrayandmemtype = VecRestoreArrayAndMemType_SeqKokkos;
256: v->ops->getarraywriteandmemtype = VecGetArrayWriteAndMemType_SeqKokkos;
257: v->ops->getsubvector = VecGetSubVector_MPIKokkos;
258: v->ops->restoresubvector = VecRestoreSubVector_SeqKokkos;
260: v->ops->setpreallocationcoo = VecSetPreallocationCOO_MPIKokkos;
261: v->ops->setvaluescoo = VecSetValuesCOO_MPIKokkos;
263: v->ops->errorwnorm = VecErrorWeightedNorms_MPIKokkos;
265: v->offloadmask = PETSC_OFFLOAD_KOKKOS; // Mark this is a VECKOKKOS; We use this flag for cheap VECKOKKOS test.
266: PetscFunctionReturn(PETSC_SUCCESS);
267: }
269: PETSC_INTERN PetscErrorCode VecConvert_MPI_MPIKokkos_inplace(Vec v)
270: {
271: Vec_MPI *vecmpi;
273: PetscFunctionBegin;
274: PetscCall(PetscKokkosInitializeCheck());
275: PetscCall(PetscLayoutSetUp(v->map));
276: PetscCall(PetscObjectChangeTypeName((PetscObject)v, VECMPIKOKKOS));
277: PetscCall(VecCreate_MPIKokkos_Common(v));
278: PetscCheck(!v->spptr, PETSC_COMM_SELF, PETSC_ERR_PLIB, "v->spptr not NULL");
279: vecmpi = static_cast<Vec_MPI *>(v->data);
280: PetscCallCXX(v->spptr = new Vec_Kokkos(v->map->n, vecmpi->array, NULL));
281: PetscFunctionReturn(PETSC_SUCCESS);
282: }
284: // Duplicate a VECMPIKOKKOS
285: static PetscErrorCode VecDuplicateVecs_MPIKokkos_GEMV(Vec w, PetscInt m, Vec *V[])
286: {
287: PetscInt64 lda; // use 64-bit as we will do "m * lda"
288: PetscScalar *array_h, *array_d;
289: PetscLayout map;
290: Vec_MPI *wmpi = (Vec_MPI *)w->data;
291: PetscScalarKokkosDualView w_dual;
293: PetscFunctionBegin;
294: PetscCall(PetscKokkosInitializeCheck()); // as we'll call kokkos_malloc()
295: if (wmpi->nghost) { // currently only do GEMV optimization for vectors without ghosts
296: w->ops->duplicatevecs = VecDuplicateVecs_Default;
297: PetscCall(VecDuplicateVecs(w, m, V));
298: } else {
299: PetscCall(PetscMalloc1(m, V));
300: PetscCall(VecGetLayout(w, &map));
301: VecGetLocalSizeAligned(w, 64, &lda); // get in lda the 64-bytes aligned local size
303: // See comments in VecCreate_SeqKokkos() on why we use DualView to allocate the memory
304: PetscCallCXX(w_dual = PetscScalarKokkosDualView("VecDuplicateVecs", m * lda)); // Kokkos init's w_dual to zero
306: // create the m vectors with raw arrays
307: array_h = w_dual.view_host().data();
308: array_d = w_dual.view_device().data();
309: for (PetscInt i = 0; i < m; i++) {
310: Vec v;
311: PetscCall(VecCreateMPIKokkosWithLayoutAndArrays_Private(map, &array_h[i * lda], &array_d[i * lda], &v));
312: PetscCallCXX(static_cast<Vec_Kokkos *>(v->spptr)->v_dual.modify_host()); // as we only init'ed array_h
313: PetscCall(PetscObjectListDuplicate(((PetscObject)w)->olist, &((PetscObject)v)->olist));
314: PetscCall(PetscFunctionListDuplicate(((PetscObject)w)->qlist, &((PetscObject)v)->qlist));
315: v->ops[0] = w->ops[0];
316: v->stash.donotstash = w->stash.donotstash;
317: v->stash.ignorenegidx = w->stash.ignorenegidx;
318: v->stash.bs = w->stash.bs;
319: v->bstash.bs = w->bstash.bs;
320: (*V)[i] = v;
321: }
323: // let the first vector own the raw arrays, so when it is destroyed it will free the arrays
324: if (m) {
325: Vec v = (*V)[0];
327: static_cast<Vec_Kokkos *>(v->spptr)->w_dual = w_dual; // stash the memory
328: // disable replacearray of the first vector, as freeing its memory also frees others in the group.
329: // But replacearray of others is ok, as they don't own their array.
330: if (m > 1) v->ops->replacearray = VecReplaceArray_Default_GEMV_Error;
331: }
332: }
333: PetscFunctionReturn(PETSC_SUCCESS);
334: }
336: /*MC
337: VECMPIKOKKOS - VECMPIKOKKOS = "mpikokkos" - The basic parallel vector, modified to use Kokkos
339: Options Database Keys:
340: . -vec_type mpikokkos - sets the vector type to VECMPIKOKKOS during a call to VecSetFromOptions()
342: Level: beginner
344: .seealso: `VecCreate()`, `VecSetType()`, `VecSetFromOptions()`, `VecCreateMPIKokkosWithArray()`, `VECMPI`, `VecType`, `VecCreateMPI()`
345: M*/
346: PetscErrorCode VecCreate_MPIKokkos(Vec v)
347: {
348: PetscBool mdot_use_gemv = PETSC_TRUE;
349: PetscBool maxpy_use_gemv = PETSC_FALSE; // default is false as we saw bad performance with vendors' GEMV with tall skinny matrices.
350: PetscScalarKokkosDualView v_dual;
352: PetscFunctionBegin;
353: PetscCall(PetscKokkosInitializeCheck());
354: PetscCall(PetscLayoutSetUp(v->map));
356: PetscCallCXX(v_dual = PetscScalarKokkosDualView("v_dual", v->map->n)); // Kokkos init's v_dual to zero
357: PetscCall(VecCreate_MPI_Private(v, PETSC_FALSE, 0, v_dual.view_host().data()));
359: PetscCall(PetscObjectChangeTypeName((PetscObject)v, VECMPIKOKKOS));
360: PetscCall(VecCreate_MPIKokkos_Common(v));
361: PetscCheck(!v->spptr, PETSC_COMM_SELF, PETSC_ERR_PLIB, "v->spptr not NULL");
362: PetscCallCXX(v->spptr = new Vec_Kokkos(v_dual));
363: PetscCall(PetscOptionsGetBool(NULL, NULL, "-vec_mdot_use_gemv", &mdot_use_gemv, NULL));
364: PetscCall(PetscOptionsGetBool(NULL, NULL, "-vec_maxpy_use_gemv", &maxpy_use_gemv, NULL));
366: // allocate multiple vectors together
367: if (mdot_use_gemv || maxpy_use_gemv) v->ops[0].duplicatevecs = VecDuplicateVecs_MPIKokkos_GEMV;
369: if (mdot_use_gemv) {
370: v->ops[0].mdot = VecMDot_MPIKokkos_GEMV;
371: v->ops[0].mtdot = VecMTDot_MPIKokkos_GEMV;
372: v->ops[0].mdot_local = VecMDot_SeqKokkos_GEMV;
373: v->ops[0].mtdot_local = VecMTDot_SeqKokkos_GEMV;
374: }
376: if (maxpy_use_gemv) v->ops[0].maxpy = VecMAXPY_SeqKokkos_GEMV;
377: PetscFunctionReturn(PETSC_SUCCESS);
378: }
380: // Create a VECMPIKOKKOS with layout and arrays
381: PetscErrorCode VecCreateMPIKokkosWithLayoutAndArrays_Private(PetscLayout map, const PetscScalar harray[], const PetscScalar darray[], Vec *v)
382: {
383: Vec w;
385: PetscFunctionBegin;
386: if (map->n > 0) PetscCheck(darray, map->comm, PETSC_ERR_ARG_WRONG, "darray cannot be NULL");
387: #if defined(KOKKOS_ENABLE_UNIFIED_MEMORY)
388: PetscCheck(harray == darray, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "harray and darray must be the same");
389: #endif
390: PetscCall(VecCreateMPIWithLayoutAndArray_Private(map, harray, &w));
391: PetscCall(PetscObjectChangeTypeName((PetscObject)w, VECMPIKOKKOS)); // Change it to VECKOKKOS
392: PetscCall(VecCreate_MPIKokkos_Common(w));
393: PetscCallCXX(w->spptr = new Vec_Kokkos(map->n, const_cast<PetscScalar *>(harray), const_cast<PetscScalar *>(darray)));
394: *v = w;
395: PetscFunctionReturn(PETSC_SUCCESS);
396: }
398: /*@C
399: VecCreateMPIKokkosWithArray - Creates a parallel, array-style vector,
400: where the user provides the GPU array space to store the vector values.
402: Collective
404: Input Parameters:
405: + comm - the MPI communicator to use
406: . bs - block size, same meaning as VecSetBlockSize()
407: . n - local vector length, cannot be PETSC_DECIDE
408: . N - global vector length (or PETSC_DECIDE to have calculated)
409: - darray - the user provided GPU array to store the vector values
411: Output Parameter:
412: . v - the vector
414: Notes:
415: Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the
416: same type as an existing vector.
418: If the user-provided array is NULL, then VecKokkosPlaceArray() can be used
419: at a later stage to SET the array for storing the vector values.
421: PETSc does NOT free the array when the vector is destroyed via VecDestroy().
422: The user should not free the array until the vector is destroyed.
424: Level: intermediate
426: .seealso: `VecCreateSeqKokkosWithArray()`, `VecCreateMPIWithArray()`, `VecCreateSeqWithArray()`,
427: `VecCreate()`, `VecDuplicate()`, `VecDuplicateVecs()`, `VecCreateGhost()`,
428: `VecCreateMPI()`, `VecCreateGhostWithArray()`, `VecPlaceArray()`
430: @*/
431: PetscErrorCode VecCreateMPIKokkosWithArray(MPI_Comm comm, PetscInt bs, PetscInt n, PetscInt N, const PetscScalar darray[], Vec *v)
432: {
433: Vec w;
434: Vec_Kokkos *veckok;
435: Vec_MPI *vecmpi;
436: PetscScalar *harray;
438: PetscFunctionBegin;
439: PetscCheck(n != PETSC_DECIDE, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must set local size of vector");
440: PetscCall(PetscKokkosInitializeCheck());
441: PetscCall(PetscSplitOwnership(comm, &n, &N));
442: PetscCall(VecCreate(comm, &w));
443: PetscCall(VecSetSizes(w, n, N));
444: PetscCall(VecSetBlockSize(w, bs));
445: PetscCall(PetscLayoutSetUp(w->map));
447: if (std::is_same<DefaultMemorySpace, HostMirrorMemorySpace>::value) {
448: harray = const_cast<PetscScalar *>(darray);
449: } else PetscCall(PetscMalloc1(w->map->n, &harray)); /* If device is not the same as host, allocate the host array ourselves */
451: PetscCall(VecCreate_MPI_Private(w, PETSC_FALSE /*alloc*/, 0 /*nghost*/, harray)); /* Build a sequential vector with provided data */
452: vecmpi = static_cast<Vec_MPI *>(w->data);
454: if (!std::is_same<DefaultMemorySpace, HostMirrorMemorySpace>::value) vecmpi->array_allocated = harray; /* The host array was allocated by PETSc */
456: PetscCall(PetscObjectChangeTypeName((PetscObject)w, VECMPIKOKKOS));
457: PetscCall(VecCreate_MPIKokkos_Common(w));
458: veckok = new Vec_Kokkos(n, harray, const_cast<PetscScalar *>(darray));
459: veckok->v_dual.modify_device(); /* Mark the device is modified */
460: w->spptr = static_cast<void *>(veckok);
461: *v = w;
462: PetscFunctionReturn(PETSC_SUCCESS);
463: }
465: /*
466: VecCreateMPIKokkosWithArrays_Private - Creates a Kokkos parallel, array-style vector
467: with user-provided arrays on host and device.
469: Collective
471: Input Parameter:
472: + comm - the communicator
473: . bs - the block size
474: . n - the local vector length
475: . N - the global vector length
476: - harray - host memory where the vector elements are to be stored.
477: - darray - device memory where the vector elements are to be stored.
479: Output Parameter:
480: . v - the vector
482: Notes:
483: If there is no device, then harray and darray must be the same.
484: If n is not zero, then harray and darray must be allocated.
485: After the call, the created vector is supposed to be in a synchronized state, i.e.,
486: we suppose harray and darray have the same data.
488: PETSc does NOT free the array when the vector is destroyed via VecDestroy().
489: The user should not free the array until the vector is destroyed.
490: */
491: PetscErrorCode VecCreateMPIKokkosWithArrays_Private(MPI_Comm comm, PetscInt bs, PetscInt n, PetscInt N, const PetscScalar harray[], const PetscScalar darray[], Vec *v)
492: {
493: Vec w;
495: PetscFunctionBegin;
496: PetscCall(PetscKokkosInitializeCheck());
497: if (n) {
498: PetscAssertPointer(harray, 5);
499: PetscCheck(darray, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "darray cannot be NULL");
500: }
501: if (std::is_same<DefaultMemorySpace, HostMirrorMemorySpace>::value) PetscCheck(harray == darray, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "harray and darray must be the same");
502: PetscCall(VecCreateMPIWithArray(comm, bs, n, N, harray, &w));
503: PetscCall(PetscObjectChangeTypeName((PetscObject)w, VECMPIKOKKOS)); /* Change it to Kokkos */
504: PetscCall(VecCreate_MPIKokkos_Common(w));
505: PetscCallCXX(w->spptr = new Vec_Kokkos(n, const_cast<PetscScalar *>(harray), const_cast<PetscScalar *>(darray)));
506: *v = w;
507: PetscFunctionReturn(PETSC_SUCCESS);
508: }
510: /*MC
511: VECKOKKOS - VECKOKKOS = "kokkos" - The basic vector, modified to use Kokkos
513: Options Database Keys:
514: . -vec_type kokkos - sets the vector type to VECKOKKOS during a call to VecSetFromOptions()
516: Level: beginner
518: .seealso: `VecCreate()`, `VecSetType()`, `VecSetFromOptions()`, `VecCreateMPIKokkosWithArray()`, `VECMPI`, `VecType`, `VecCreateMPI()`
519: M*/
520: PetscErrorCode VecCreate_Kokkos(Vec v)
521: {
522: PetscMPIInt size;
524: PetscFunctionBegin;
525: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)v), &size));
526: if (size == 1) PetscCall(VecSetType(v, VECSEQKOKKOS));
527: else PetscCall(VecSetType(v, VECMPIKOKKOS));
528: PetscFunctionReturn(PETSC_SUCCESS);
529: }