Actual source code: veckok.kokkos.cxx

  1: /*
  2:    Implements the sequential Kokkos vectors.
  3: */
  4: #include <petsc_kokkos.hpp>
  5: #include <petscvec_kokkos.hpp>

  7: #include <petsc/private/sfimpl.h>
  8: #include <petsc/private/petscimpl.h>
  9: #include <petscmath.h>
 10: #include <petscviewer.h>
 11: #include <KokkosBlas.hpp>
 12: #include <Kokkos_Functional.hpp>

 14: #include <../src/vec/vec/impls/dvecimpl.h>
 15: #include <../src/vec/vec/impls/seq/kokkos/veckokkosimpl.hpp>

 17: template <class MemorySpace>
 18: static PetscErrorCode VecGetKokkosView_Private(Vec v, PetscScalarKokkosViewType<MemorySpace> *kv, PetscBool overwrite)
 19: {
 20:   Vec_Kokkos *veckok   = static_cast<Vec_Kokkos *>(v->spptr);
 21:   using ExecutionSpace = typename PetscScalarKokkosViewType<MemorySpace>::traits::device_type;

 23:   PetscFunctionBegin;
 24:   VecErrorIfNotKokkos(v);
 25:   if (!overwrite) { /* If overwrite=true, no need to sync the space, since caller will overwrite the data */
 26:     PetscCallCXX(veckok->v_dual.sync<ExecutionSpace>());
 27:   }
 28:   PetscCallCXX(*kv = veckok->v_dual.view<ExecutionSpace>());
 29:   PetscFunctionReturn(PETSC_SUCCESS);
 30: }

 32: template <class MemorySpace>
 33: static PetscErrorCode VecRestoreKokkosView_Private(Vec v, PetscScalarKokkosViewType<MemorySpace> *, PetscBool overwrite)
 34: {
 35:   Vec_Kokkos *veckok   = static_cast<Vec_Kokkos *>(v->spptr);
 36:   using ExecutionSpace = typename PetscScalarKokkosViewType<MemorySpace>::traits::device_type;

 38:   PetscFunctionBegin;
 39:   VecErrorIfNotKokkos(v);
 40:   if (overwrite) PetscCallCXX(veckok->v_dual.clear_sync_state()); /* If overwrite=true, clear the old sync state since user forced an overwrite */
 41:   PetscCallCXX(veckok->v_dual.modify<ExecutionSpace>());
 42:   PetscCall(PetscObjectStateIncrease((PetscObject)v));
 43:   PetscFunctionReturn(PETSC_SUCCESS);
 44: }

 46: template <class MemorySpace>
 47: PetscErrorCode VecGetKokkosView(Vec v, ConstPetscScalarKokkosViewType<MemorySpace> *kv)
 48: {
 49:   Vec_Kokkos *veckok   = static_cast<Vec_Kokkos *>(v->spptr);
 50:   using ExecutionSpace = typename PetscScalarKokkosViewType<MemorySpace>::traits::device_type;

 52:   PetscFunctionBegin;
 53:   VecErrorIfNotKokkos(v);
 54:   PetscCallCXX(veckok->v_dual.sync<ExecutionSpace>());
 55:   PetscCallCXX(*kv = veckok->v_dual.view<ExecutionSpace>());
 56:   PetscFunctionReturn(PETSC_SUCCESS);
 57: }

 59: /* Function template explicit instantiation */
 60: template PETSC_VISIBILITY_PUBLIC PetscErrorCode VecGetKokkosView(Vec, ConstPetscScalarKokkosView *);
 61: template <>
 62: PETSC_VISIBILITY_PUBLIC PetscErrorCode VecGetKokkosView(Vec v, PetscScalarKokkosView *kv)
 63: {
 64:   return VecGetKokkosView_Private(v, kv, PETSC_FALSE);
 65: }
 66: template <>
 67: PETSC_VISIBILITY_PUBLIC PetscErrorCode VecRestoreKokkosView(Vec v, PetscScalarKokkosView *kv)
 68: {
 69:   return VecRestoreKokkosView_Private(v, kv, PETSC_FALSE);
 70: }
 71: template <>
 72: PETSC_VISIBILITY_PUBLIC PetscErrorCode VecGetKokkosViewWrite(Vec v, PetscScalarKokkosView *kv)
 73: {
 74:   return VecGetKokkosView_Private(v, kv, PETSC_TRUE);
 75: }
 76: template <>
 77: PETSC_VISIBILITY_PUBLIC PetscErrorCode VecRestoreKokkosViewWrite(Vec v, PetscScalarKokkosView *kv)
 78: {
 79:   return VecRestoreKokkosView_Private(v, kv, PETSC_TRUE);
 80: }

 82: #if !defined(KOKKOS_ENABLE_UNIFIED_MEMORY) /* Get host views if the default memory space is not host space */
 83: template PETSC_VISIBILITY_PUBLIC PetscErrorCode VecGetKokkosView(Vec, ConstPetscScalarKokkosViewHost *);
 84: template <>
 85: PETSC_VISIBILITY_PUBLIC PetscErrorCode VecGetKokkosView(Vec v, PetscScalarKokkosViewHost *kv)
 86: {
 87:   return VecGetKokkosView_Private(v, kv, PETSC_FALSE);
 88: }
 89: template <>
 90: PETSC_VISIBILITY_PUBLIC PetscErrorCode VecRestoreKokkosView(Vec v, PetscScalarKokkosViewHost *kv)
 91: {
 92:   return VecRestoreKokkosView_Private(v, kv, PETSC_FALSE);
 93: }
 94: template <>
 95: PETSC_VISIBILITY_PUBLIC PetscErrorCode VecGetKokkosViewWrite(Vec v, PetscScalarKokkosViewHost *kv)
 96: {
 97:   return VecGetKokkosView_Private(v, kv, PETSC_TRUE);
 98: }
 99: template <>
100: PETSC_VISIBILITY_PUBLIC PetscErrorCode VecRestoreKokkosViewWrite(Vec v, PetscScalarKokkosViewHost *kv)
101: {
102:   return VecRestoreKokkosView_Private(v, kv, PETSC_TRUE);
103: }
104: #endif

106: PetscErrorCode VecSetRandom_SeqKokkos(Vec xin, PetscRandom r)
107: {
108:   const PetscInt n = xin->map->n;
109:   PetscScalar   *xx;

111:   PetscFunctionBegin;
112:   PetscCall(VecGetArrayWrite(xin, &xx)); /* TODO: generate randoms directly on device */
113:   for (PetscInt i = 0; i < n; i++) PetscCall(PetscRandomGetValue(r, &xx[i]));
114:   PetscCall(VecRestoreArrayWrite(xin, &xx));
115:   PetscFunctionReturn(PETSC_SUCCESS);
116: }

118: /* x = |x| */
119: PetscErrorCode VecAbs_SeqKokkos(Vec xin)
120: {
121:   PetscScalarKokkosView xv;
122:   auto                  exec = PetscGetKokkosExecutionSpace();

124:   PetscFunctionBegin;
125:   PetscCall(PetscLogGpuTimeBegin());
126:   PetscCall(VecGetKokkosView(xin, &xv));
127:   PetscCallCXX(KokkosBlas::abs(exec, xv, xv));
128:   PetscCall(VecRestoreKokkosView(xin, &xv));
129:   PetscCall(PetscLogGpuTimeEnd());
130:   PetscFunctionReturn(PETSC_SUCCESS);
131: }

133: /* x = 1/x */
134: PetscErrorCode VecReciprocal_SeqKokkos(Vec xin)
135: {
136:   PetscScalarKokkosView xv;

138:   PetscFunctionBegin;
139:   PetscCall(PetscLogGpuTimeBegin());
140:   PetscCall(VecGetKokkosView(xin, &xv));
141:   PetscCallCXX(Kokkos::parallel_for(
142:     "VecReciprocal", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, xin->map->n), KOKKOS_LAMBDA(const PetscInt &i) {
143:       if (xv(i) != (PetscScalar)0.0) xv(i) = (PetscScalar)1.0 / xv(i);
144:     }));
145:   PetscCall(VecRestoreKokkosView(xin, &xv));
146:   PetscCall(PetscLogGpuTimeEnd());
147:   PetscFunctionReturn(PETSC_SUCCESS);
148: }

150: /* x = sqrt(|x|) */
151: PetscErrorCode VecSqrtAbs_SeqKokkos(Vec xin)
152: {
153:   PetscScalarKokkosView xv;

155:   PetscFunctionBegin;
156:   PetscCall(PetscLogGpuTimeBegin());
157:   PetscCall(VecGetKokkosView(xin, &xv));
158:   PetscCallCXX(Kokkos::parallel_for("VecSqrtAbs", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, xin->map->n), KOKKOS_LAMBDA(const PetscInt &i) { xv(i) = Kokkos::sqrt(PetscAbsScalar(xv(i))); }));
159:   PetscCall(VecRestoreKokkosView(xin, &xv));
160:   PetscCall(PetscLogGpuTimeEnd());
161:   PetscFunctionReturn(PETSC_SUCCESS);
162: }

164: PetscErrorCode VecMin_SeqKokkos(Vec xin, PetscInt *p, PetscReal *val)
165: {
166:   ConstPetscScalarKokkosView                           xv;
167:   Kokkos::MinFirstLoc<PetscReal, PetscInt>::value_type result;

169:   PetscFunctionBegin;
170:   PetscCall(PetscLogGpuTimeBegin());
171:   PetscCall(VecGetKokkosView(xin, &xv));
172:   PetscCallCXX(Kokkos::parallel_reduce(
173:     "VecMin", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, xin->map->n),
174:     KOKKOS_LAMBDA(const PetscInt &i, Kokkos::MinFirstLoc<PetscReal, PetscInt>::value_type &lupdate) {
175:       if (PetscRealPart(xv(i)) < lupdate.val) {
176:         lupdate.val = PetscRealPart(xv(i));
177:         lupdate.loc = i;
178:       }
179:     },
180:     Kokkos::MinFirstLoc<PetscReal, PetscInt>(result)));
181:   *val = result.val;
182:   if (p) *p = result.loc;
183:   PetscCall(VecRestoreKokkosView(xin, &xv));
184:   PetscCall(PetscLogGpuTimeEnd());
185:   PetscFunctionReturn(PETSC_SUCCESS);
186: }

188: PetscErrorCode VecMax_SeqKokkos(Vec xin, PetscInt *p, PetscReal *val)
189: {
190:   ConstPetscScalarKokkosView                           xv;
191:   Kokkos::MaxFirstLoc<PetscReal, PetscInt>::value_type result;

193:   PetscFunctionBegin;
194:   PetscCall(PetscLogGpuTimeBegin());
195:   PetscCall(VecGetKokkosView(xin, &xv));
196:   PetscCallCXX(Kokkos::parallel_reduce(
197:     "VecMax", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, xin->map->n),
198:     KOKKOS_LAMBDA(const PetscInt &i, Kokkos::MaxFirstLoc<PetscReal, PetscInt>::value_type &lupdate) {
199:       if (PetscRealPart(xv(i)) > lupdate.val) {
200:         lupdate.val = PetscRealPart(xv(i));
201:         lupdate.loc = i;
202:       }
203:     },
204:     Kokkos::MaxFirstLoc<PetscReal, PetscInt>(result)));
205:   *val = result.val;
206:   if (p) *p = result.loc;
207:   PetscCall(VecRestoreKokkosView(xin, &xv));
208:   PetscCall(PetscLogGpuTimeEnd());
209:   PetscFunctionReturn(PETSC_SUCCESS);
210: }

212: PetscErrorCode VecSum_SeqKokkos(Vec xin, PetscScalar *sum)
213: {
214:   ConstPetscScalarKokkosView xv;

216:   PetscFunctionBegin;
217:   PetscCall(PetscLogGpuTimeBegin());
218:   PetscCall(VecGetKokkosView(xin, &xv));
219:   PetscCallCXX(*sum = KokkosBlas::sum(PetscGetKokkosExecutionSpace(), xv));
220:   PetscCall(VecRestoreKokkosView(xin, &xv));
221:   PetscCall(PetscLogGpuTimeEnd());
222:   PetscFunctionReturn(PETSC_SUCCESS);
223: }

225: PetscErrorCode VecShift_SeqKokkos(Vec xin, PetscScalar shift)
226: {
227:   PetscScalarKokkosView xv;

229:   PetscFunctionBegin;
230:   PetscCall(PetscLogGpuTimeBegin());
231:   PetscCall(VecGetKokkosView(xin, &xv));
232:   PetscCallCXX(Kokkos::parallel_for("VecShift", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, xin->map->n), KOKKOS_LAMBDA(const PetscInt &i) { xv(i) += shift; }); PetscCall(VecRestoreKokkosView(xin, &xv)));
233:   PetscCall(PetscLogGpuTimeEnd());
234:   PetscCall(PetscLogGpuFlops(xin->map->n));
235:   PetscFunctionReturn(PETSC_SUCCESS);
236: }

238: /* y = alpha x + y */
239: PetscErrorCode VecAXPY_SeqKokkos(Vec yin, PetscScalar alpha, Vec xin)
240: {
241:   PetscFunctionBegin;
242:   if (alpha == (PetscScalar)0.0) PetscFunctionReturn(PETSC_SUCCESS);
243:   if (yin == xin) PetscCall(VecScale_SeqKokkos(yin, alpha + 1));
244:   else {
245:     PetscBool xiskok, yiskok;

247:     PetscCall(PetscObjectTypeCompareAny((PetscObject)xin, &xiskok, VECSEQKOKKOS, VECMPIKOKKOS, ""));
248:     PetscCall(PetscObjectTypeCompareAny((PetscObject)yin, &yiskok, VECSEQKOKKOS, VECMPIKOKKOS, ""));
249:     if (xiskok && yiskok) {
250:       PetscScalarKokkosView      yv;
251:       ConstPetscScalarKokkosView xv;

253:       PetscCall(PetscLogGpuTimeBegin());
254:       PetscCall(VecGetKokkosView(xin, &xv));
255:       PetscCall(VecGetKokkosView(yin, &yv));
256:       PetscCallCXX(KokkosBlas::axpy(PetscGetKokkosExecutionSpace(), alpha, xv, yv));
257:       PetscCall(VecRestoreKokkosView(xin, &xv));
258:       PetscCall(VecRestoreKokkosView(yin, &yv));
259:       PetscCall(PetscLogGpuTimeEnd());
260:       PetscCall(PetscLogGpuFlops(2.0 * yin->map->n));
261:     } else {
262:       PetscCall(VecAXPY_Seq(yin, alpha, xin));
263:     }
264:   }
265:   PetscFunctionReturn(PETSC_SUCCESS);
266: }

268: /* y = x + beta y */
269: PetscErrorCode VecAYPX_SeqKokkos(Vec yin, PetscScalar beta, Vec xin)
270: {
271:   PetscFunctionBegin;
272:   /* One needs to define KOKKOSBLAS_OPTIMIZATION_LEVEL_AXPBY > 2 to have optimizations for cases alpha/beta = 0,+/-1 */
273:   PetscCall(VecAXPBY_SeqKokkos(yin, 1.0, beta, xin));
274:   PetscFunctionReturn(PETSC_SUCCESS);
275: }

277: /* z = y^T x */
278: PetscErrorCode VecTDot_SeqKokkos(Vec xin, Vec yin, PetscScalar *z)
279: {
280:   ConstPetscScalarKokkosView xv, yv;

282:   PetscFunctionBegin;
283:   PetscCall(PetscLogGpuTimeBegin());
284:   PetscCall(VecGetKokkosView(xin, &xv));
285:   PetscCall(VecGetKokkosView(yin, &yv));
286:   // Kokkos always overwrites z, so no need to init it
287:   PetscCallCXX(Kokkos::parallel_reduce("VecTDot", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, xin->map->n), KOKKOS_LAMBDA(const PetscInt &i, PetscScalar &update) { update += yv(i) * xv(i); }, *z));
288:   PetscCall(VecRestoreKokkosView(yin, &yv));
289:   PetscCall(VecRestoreKokkosView(xin, &xv));
290:   PetscCall(PetscLogGpuTimeEnd());
291:   if (xin->map->n > 0) PetscCall(PetscLogGpuFlops(2.0 * xin->map->n));
292:   PetscFunctionReturn(PETSC_SUCCESS);
293: }

295: struct TransposeDotTag { };
296: struct ConjugateDotTag { };

298: template <PetscInt ValueCount>
299: struct MDotFunctor {
300:   static_assert(ValueCount >= 1 && ValueCount <= 8, "ValueCount must be in [1, 8]");
301:   /* Note the C++ notation for an array typedef */
302:   // noted, thanks
303:   typedef PetscScalar                           value_type[];
304:   typedef ConstPetscScalarKokkosView::size_type size_type;

306:   /* Tell Kokkos the result array's number of entries. This must be a public value in the functor */
307:   static constexpr size_type value_count = ValueCount;
308:   ConstPetscScalarKokkosView xv, yv[8];

310:   MDotFunctor(ConstPetscScalarKokkosView &xv, ConstPetscScalarKokkosView &yv0, ConstPetscScalarKokkosView &yv1, ConstPetscScalarKokkosView &yv2, ConstPetscScalarKokkosView &yv3, ConstPetscScalarKokkosView &yv4, ConstPetscScalarKokkosView &yv5, ConstPetscScalarKokkosView &yv6, ConstPetscScalarKokkosView &yv7) :
311:     xv(xv)
312:   {
313:     yv[0] = yv0;
314:     yv[1] = yv1;
315:     yv[2] = yv2;
316:     yv[3] = yv3;
317:     yv[4] = yv4;
318:     yv[5] = yv5;
319:     yv[6] = yv6;
320:     yv[7] = yv7;
321:   }

323:   KOKKOS_INLINE_FUNCTION void operator()(TransposeDotTag, const size_type i, value_type sum) const
324:   {
325:     PetscScalar xval = xv(i);
326:     for (size_type j = 0; j < value_count; ++j) sum[j] += yv[j](i) * xval;
327:   }

329:   KOKKOS_INLINE_FUNCTION void operator()(ConjugateDotTag, const size_type i, value_type sum) const
330:   {
331:     PetscScalar xval = xv(i);
332:     for (size_type j = 0; j < value_count; ++j) sum[j] += PetscConj(yv[j](i)) * xval;
333:   }

335:   // Per https://kokkos.github.io/kokkos-core-wiki/API/core/parallel-dispatch/parallel_reduce.html#requirements
336:   // "when specifying a tag in the policy, the functor's potential init/join/final member functions must also be tagged"
337:   // So we have this kind of duplicated code.
338:   KOKKOS_INLINE_FUNCTION void join(TransposeDotTag, value_type dst, const value_type src) const { join(dst, src); }
339:   KOKKOS_INLINE_FUNCTION void join(ConjugateDotTag, value_type dst, const value_type src) const { join(dst, src); }

341:   KOKKOS_INLINE_FUNCTION void init(TransposeDotTag, value_type sum) const { init(sum); }
342:   KOKKOS_INLINE_FUNCTION void init(ConjugateDotTag, value_type sum) const { init(sum); }

344:   KOKKOS_INLINE_FUNCTION void join(value_type dst, const value_type src) const
345:   {
346:     for (size_type j = 0; j < value_count; j++) dst[j] += src[j];
347:   }

349:   KOKKOS_INLINE_FUNCTION void init(value_type sum) const
350:   {
351:     for (size_type j = 0; j < value_count; j++) sum[j] = 0.0;
352:   }
353: };

355: template <class WorkTag>
356: PetscErrorCode VecMultiDot_Private(Vec xin, PetscInt nv, const Vec yin[], PetscScalar *z)
357: {
358:   PetscInt                   i, j, cur = 0, ngroup = nv / 8, rem = nv % 8, N = xin->map->n;
359:   ConstPetscScalarKokkosView xv, yv[8];
360:   PetscScalarKokkosViewHost  zv(z, nv);
361:   auto                       exec = PetscGetKokkosExecutionSpace();

363:   PetscFunctionBegin;
364:   PetscCall(VecGetKokkosView(xin, &xv));
365:   for (i = 0; i < ngroup; i++) { /* 8 y's per group */
366:     for (j = 0; j < 8; j++) PetscCall(VecGetKokkosView(yin[cur + j], &yv[j]));
367:     MDotFunctor<8> mdot(xv, yv[0], yv[1], yv[2], yv[3], yv[4], yv[5], yv[6], yv[7]); /* Hope Kokkos make it asynchronous */
368:     PetscCall(PetscLogGpuTimeBegin());
369:     PetscCallCXX(Kokkos::parallel_reduce(Kokkos::RangePolicy<WorkTag>(exec, 0, N), mdot, Kokkos::subview(zv, Kokkos::pair<PetscInt, PetscInt>(cur, cur + 8))));
370:     PetscCall(PetscLogGpuTimeEnd());
371:     for (j = 0; j < 8; j++) PetscCall(VecRestoreKokkosView(yin[cur + j], &yv[j]));
372:     cur += 8;
373:   }

375:   if (rem) { /* The remaining */
376:     for (j = 0; j < rem; j++) PetscCall(VecGetKokkosView(yin[cur + j], &yv[j]));
377:     Kokkos::RangePolicy<WorkTag> policy(exec, 0, N);
378:     auto                         results = Kokkos::subview(zv, Kokkos::pair<PetscInt, PetscInt>(cur, cur + rem));
379:     // clang-format off
380:     PetscCall(PetscLogGpuTimeBegin());
381:     switch (rem) {
382:     case 1: PetscCallCXX(Kokkos::parallel_reduce(policy, MDotFunctor<1>(xv, yv[0], yv[1], yv[2], yv[3], yv[4], yv[5], yv[6], yv[7]), results)); break;
383:     case 2: PetscCallCXX(Kokkos::parallel_reduce(policy, MDotFunctor<2>(xv, yv[0], yv[1], yv[2], yv[3], yv[4], yv[5], yv[6], yv[7]), results)); break;
384:     case 3: PetscCallCXX(Kokkos::parallel_reduce(policy, MDotFunctor<3>(xv, yv[0], yv[1], yv[2], yv[3], yv[4], yv[5], yv[6], yv[7]), results)); break;
385:     case 4: PetscCallCXX(Kokkos::parallel_reduce(policy, MDotFunctor<4>(xv, yv[0], yv[1], yv[2], yv[3], yv[4], yv[5], yv[6], yv[7]), results)); break;
386:     case 5: PetscCallCXX(Kokkos::parallel_reduce(policy, MDotFunctor<5>(xv, yv[0], yv[1], yv[2], yv[3], yv[4], yv[5], yv[6], yv[7]), results)); break;
387:     case 6: PetscCallCXX(Kokkos::parallel_reduce(policy, MDotFunctor<6>(xv, yv[0], yv[1], yv[2], yv[3], yv[4], yv[5], yv[6], yv[7]), results)); break;
388:     case 7: PetscCallCXX(Kokkos::parallel_reduce(policy, MDotFunctor<7>(xv, yv[0], yv[1], yv[2], yv[3], yv[4], yv[5], yv[6], yv[7]), results)); break;
389:     }
390:     PetscCall(PetscLogGpuTimeEnd());
391:     // clang-format on
392:     for (j = 0; j < rem; j++) PetscCall(VecRestoreKokkosView(yin[cur + j], &yv[j]));
393:   }
394:   PetscCall(VecRestoreKokkosView(xin, &xv));
395:   exec.fence(); /* If reduce is async, then we need this fence to make sure z is ready for use on host */
396:   PetscFunctionReturn(PETSC_SUCCESS);
397: }

399: static PetscErrorCode VecMultiDot_Verbose(Vec xin, PetscInt nv, const Vec yin[], PetscScalar *z)
400: {
401:   PetscInt                   ngroup = nv / 8, rem = nv % 8, N = xin->map->n;
402:   ConstPetscScalarKokkosView xv, y0, y1, y2, y3, y4, y5, y6, y7;
403:   PetscScalar               *zp = z;
404:   const Vec                 *yp = yin;
405:   Kokkos::RangePolicy<>      policy(PetscGetKokkosExecutionSpace(), 0, N);

407:   // clang-format off
408:   PetscFunctionBegin;
409:   PetscCall(VecGetKokkosView(xin, &xv));
410:   for (PetscInt k = 0; k < ngroup; k++) { // 8 y's per group
411:     PetscCall(VecGetKokkosView(yp[0], &y0));
412:     PetscCall(VecGetKokkosView(yp[1], &y1));
413:     PetscCall(VecGetKokkosView(yp[2], &y2));
414:     PetscCall(VecGetKokkosView(yp[3], &y3));
415:     PetscCall(VecGetKokkosView(yp[4], &y4));
416:     PetscCall(VecGetKokkosView(yp[5], &y5));
417:     PetscCall(VecGetKokkosView(yp[6], &y6));
418:     PetscCall(VecGetKokkosView(yp[7], &y7));
419:     PetscCall(PetscLogGpuTimeBegin()); // only for GPU kernel execution
420:     Kokkos::parallel_reduce(
421:       "VecMDot8", policy,
422:       KOKKOS_LAMBDA(const PetscInt &i, PetscScalar &lsum0, PetscScalar &lsum1, PetscScalar &lsum2, PetscScalar &lsum3, PetscScalar &lsum4, PetscScalar &lsum5, PetscScalar &lsum6, PetscScalar &lsum7) {
423:         lsum0 += xv(i) * PetscConj(y0(i)); lsum1 += xv(i) * PetscConj(y1(i)); lsum2 += xv(i) * PetscConj(y2(i)); lsum3 += xv(i) * PetscConj(y3(i));
424:         lsum4 += xv(i) * PetscConj(y4(i)); lsum5 += xv(i) * PetscConj(y5(i)); lsum6 += xv(i) * PetscConj(y6(i)); lsum7 += xv(i) * PetscConj(y7(i));
425:       }, zp[0], zp[1], zp[2], zp[3], zp[4], zp[5], zp[6], zp[7]);
426:     PetscCall(PetscLogGpuTimeEnd());
427:     PetscCall(PetscLogGpuToCpu(8 * sizeof(PetscScalar))); // for copying to z[] on host
428:     PetscCall(VecRestoreKokkosView(yp[0], &y0));
429:     PetscCall(VecRestoreKokkosView(yp[1], &y1));
430:     PetscCall(VecRestoreKokkosView(yp[2], &y2));
431:     PetscCall(VecRestoreKokkosView(yp[3], &y3));
432:     PetscCall(VecRestoreKokkosView(yp[4], &y4));
433:     PetscCall(VecRestoreKokkosView(yp[5], &y5));
434:     PetscCall(VecRestoreKokkosView(yp[6], &y6));
435:     PetscCall(VecRestoreKokkosView(yp[7], &y7));
436:     yp += 8;
437:     zp += 8;
438:   }

440:   if (rem) { /* The remaining */
441:     if (rem > 0) PetscCall(VecGetKokkosView(yp[0], &y0));
442:     if (rem > 1) PetscCall(VecGetKokkosView(yp[1], &y1));
443:     if (rem > 2) PetscCall(VecGetKokkosView(yp[2], &y2));
444:     if (rem > 3) PetscCall(VecGetKokkosView(yp[3], &y3));
445:     if (rem > 4) PetscCall(VecGetKokkosView(yp[4], &y4));
446:     if (rem > 5) PetscCall(VecGetKokkosView(yp[5], &y5));
447:     if (rem > 6) PetscCall(VecGetKokkosView(yp[6], &y6));
448:     PetscCall(PetscLogGpuTimeBegin());
449:     switch (rem) {
450:     case 7:
451:       Kokkos::parallel_reduce(
452:         "VecMDot7", policy,
453:         KOKKOS_LAMBDA(const PetscInt &i, PetscScalar &lsum0, PetscScalar &lsum1, PetscScalar &lsum2, PetscScalar &lsum3, PetscScalar &lsum4, PetscScalar &lsum5, PetscScalar &lsum6) {
454:         lsum0 += xv(i) * PetscConj(y0(i)); lsum1 += xv(i) * PetscConj(y1(i)); lsum2 += xv(i) * PetscConj(y2(i)); lsum3 += xv(i) * PetscConj(y3(i));
455:         lsum4 += xv(i) * PetscConj(y4(i)); lsum5 += xv(i) * PetscConj(y5(i)); lsum6 += xv(i) * PetscConj(y6(i));
456:       }, zp[0], zp[1], zp[2], zp[3], zp[4], zp[5], zp[6]);
457:       break;
458:     case 6:
459:       Kokkos::parallel_reduce(
460:         "VecMDot6", policy,
461:         KOKKOS_LAMBDA(const PetscInt &i, PetscScalar &lsum0, PetscScalar &lsum1, PetscScalar &lsum2, PetscScalar &lsum3, PetscScalar &lsum4, PetscScalar &lsum5) {
462:         lsum0 += xv(i) * PetscConj(y0(i)); lsum1 += xv(i) * PetscConj(y1(i)); lsum2 += xv(i) * PetscConj(y2(i)); lsum3 += xv(i) * PetscConj(y3(i));
463:         lsum4 += xv(i) * PetscConj(y4(i)); lsum5 += xv(i) * PetscConj(y5(i));
464:       }, zp[0], zp[1], zp[2], zp[3], zp[4], zp[5]);
465:       break;
466:     case 5:
467:       Kokkos::parallel_reduce(
468:         "VecMDot5", policy,
469:         KOKKOS_LAMBDA(const PetscInt &i, PetscScalar &lsum0, PetscScalar &lsum1, PetscScalar &lsum2, PetscScalar &lsum3, PetscScalar &lsum4) {
470:         lsum0 += xv(i) * PetscConj(y0(i)); lsum1 += xv(i) * PetscConj(y1(i)); lsum2 += xv(i) * PetscConj(y2(i)); lsum3 += xv(i) * PetscConj(y3(i));
471:         lsum4 += xv(i) * PetscConj(y4(i));
472:       }, zp[0], zp[1], zp[2], zp[3], zp[4]);
473:       break;
474:     case 4:
475:       Kokkos::parallel_reduce(
476:         "VecMDot4", policy,
477:         KOKKOS_LAMBDA(const PetscInt &i, PetscScalar &lsum0, PetscScalar &lsum1, PetscScalar &lsum2, PetscScalar &lsum3) {
478:         lsum0 += xv(i) * PetscConj(y0(i)); lsum1 += xv(i) * PetscConj(y1(i)); lsum2 += xv(i) * PetscConj(y2(i)); lsum3 += xv(i) * PetscConj(y3(i));
479:       }, zp[0], zp[1], zp[2], zp[3]);
480:       break;
481:     case 3:
482:       Kokkos::parallel_reduce(
483:         "VecMDot3", policy,
484:         KOKKOS_LAMBDA(const PetscInt &i, PetscScalar &lsum0, PetscScalar &lsum1, PetscScalar &lsum2) {
485:         lsum0 += xv(i) * PetscConj(y0(i)); lsum1 += xv(i) * PetscConj(y1(i)); lsum2 += xv(i) * PetscConj(y2(i));
486:       }, zp[0], zp[1], zp[2]);
487:       break;
488:     case 2:
489:       Kokkos::parallel_reduce(
490:         "VecMDot2", policy,
491:         KOKKOS_LAMBDA(const PetscInt &i, PetscScalar &lsum0, PetscScalar &lsum1) {
492:         lsum0 += xv(i) * PetscConj(y0(i)); lsum1 += xv(i) * PetscConj(y1(i));
493:       }, zp[0], zp[1]);
494:       break;
495:     case 1:
496:       Kokkos::parallel_reduce(
497:         "VecMDot1", policy,
498:         KOKKOS_LAMBDA(const PetscInt &i, PetscScalar &lsum0) {
499:         lsum0 += xv(i) * PetscConj(y0(i));
500:       }, zp[0]);
501:       break;
502:     }
503:     PetscCall(PetscLogGpuTimeEnd());
504:     PetscCall(PetscLogGpuToCpu(rem * sizeof(PetscScalar))); // for copying to z[] on host
505:     if (rem > 0) PetscCall(VecRestoreKokkosView(yp[0], &y0));
506:     if (rem > 1) PetscCall(VecRestoreKokkosView(yp[1], &y1));
507:     if (rem > 2) PetscCall(VecRestoreKokkosView(yp[2], &y2));
508:     if (rem > 3) PetscCall(VecRestoreKokkosView(yp[3], &y3));
509:     if (rem > 4) PetscCall(VecRestoreKokkosView(yp[4], &y4));
510:     if (rem > 5) PetscCall(VecRestoreKokkosView(yp[5], &y5));
511:     if (rem > 6) PetscCall(VecRestoreKokkosView(yp[6], &y6));
512:   }
513:   PetscCall(VecRestoreKokkosView(xin, &xv));
514:   PetscFunctionReturn(PETSC_SUCCESS);
515:   // clang-format on
516: }

518: /* z[i] = (x,y_i) = y_i^H x */
519: PetscErrorCode VecMDot_SeqKokkos(Vec xin, PetscInt nv, const Vec yin[], PetscScalar *z)
520: {
521:   PetscFunctionBegin;
522:   // With no good reason, VecMultiDot_Private() performs much worse than VecMultiDot_Verbose() with HIP,
523:   // but they are on par with CUDA. Kokkos team is investigating this problem.
524: #if 0
525:   PetscCall(VecMultiDot_Private<ConjugateDotTag>(xin, nv, yin, z));
526: #else
527:   PetscCall(VecMultiDot_Verbose(xin, nv, yin, z));
528: #endif
529:   PetscCall(PetscLogGpuFlops(PetscMax(nv * (2.0 * xin->map->n - 1), 0.0)));
530:   PetscFunctionReturn(PETSC_SUCCESS);
531: }

533: /* z[i] = (x,y_i) = y_i^T x */
534: PetscErrorCode VecMTDot_SeqKokkos(Vec xin, PetscInt nv, const Vec yin[], PetscScalar *z)
535: {
536:   PetscFunctionBegin;
537:   PetscCall(VecMultiDot_Private<TransposeDotTag>(xin, nv, yin, z));
538:   PetscCall(PetscLogGpuFlops(PetscMax(nv * (2.0 * xin->map->n - 1), 0.0)));
539:   PetscFunctionReturn(PETSC_SUCCESS);
540: }

542: // z[i] = (x,y_i) = y_i^H x OR y_i^T x
543: static PetscErrorCode VecMultiDot_SeqKokkos_GEMV(PetscBool conjugate, Vec xin, PetscInt nv, const Vec yin[], PetscScalar *z_h)
544: {
545:   PetscInt                   i, j, nfail;
546:   ConstPetscScalarKokkosView xv, yfirst, ynext;
547:   const PetscScalar         *yarray;
548:   PetscBool                  stop  = PETSC_FALSE;
549:   PetscScalar               *z_d   = nullptr;
550:   const char                *trans = conjugate ? "C" : "T";
551:   PetscInt64                 lda   = 0;
552:   PetscInt                   m, n = xin->map->n;

554:   PetscFunctionBegin;
555:   PetscCall(VecGetKokkosView(xin, &xv));
556: #if defined(KOKKOS_ENABLE_UNIFIED_MEMORY)
557:   z_d = z_h;
558: #endif
559:   i = nfail = 0;
560:   while (i < nv) {
561:     // search a sequence of vectors with a fixed stride
562:     stop = PETSC_FALSE;
563:     PetscCall(VecGetKokkosView(yin[i], &yfirst));
564:     yarray = yfirst.data();
565:     for (j = i + 1; j < nv; j++) {
566:       PetscCall(VecGetKokkosView(yin[j], &ynext));
567:       if (j == i + 1) {
568:         lda = ynext.data() - yarray;                       // arbitrary ptrdiff could be very large
569:         if (lda < 0 || lda - n > 64) stop = PETSC_TRUE;    // avoid using arbitrary lda; 64 bytes are a big enough alignment in VecDuplicateVecs
570:       } else if (lda * (j - i) != ynext.data() - yarray) { // not in the same stride? if so, stop searching
571:         stop = PETSC_TRUE;
572:       }
573:       PetscCall(VecRestoreKokkosView(yin[j], &ynext));
574:       if (stop) break;
575:     }
576:     PetscCall(VecRestoreKokkosView(yin[i], &yfirst));

578:     // found m vectors yin[i..j) with a stride lda at address yarray
579:     m = j - i;
580:     if (m > 1) {
581:       if (!z_d) {
582:         if (nv > PetscScalarPoolSize) { // rare case
583:           PetscScalarPoolSize = nv;
584:           PetscCallCXX(PetscScalarPool = static_cast<PetscScalar *>(Kokkos::kokkos_realloc(PetscScalarPool, PetscScalarPoolSize)));
585:         }
586:         z_d = PetscScalarPool;
587:       }
588:       const auto &A  = Kokkos::View<const PetscScalar **, Kokkos::LayoutLeft>(yarray, lda, m);
589:       const auto &Y  = Kokkos::subview(A, std::pair<PetscInt, PetscInt>(0, n), Kokkos::ALL);
590:       auto        zv = PetscScalarKokkosDualView(PetscScalarKokkosView(z_d + i, m), PetscScalarKokkosViewHost(z_h + i, m));
591:       PetscCall(PetscLogGpuTimeBegin());
592:       PetscCallCXX(KokkosBlas::gemv(PetscGetKokkosExecutionSpace(), trans, 1.0, Y, xv, 0.0, zv.view_device()));
593:       PetscCall(PetscLogGpuTimeEnd());
594:       PetscCallCXX(zv.modify_device());
595:       PetscCall(KokkosDualViewSyncHost(zv, PetscGetKokkosExecutionSpace()));
596:       PetscCall(PetscLogGpuFlops(PetscMax(m * (2.0 * n - 1), 0.0)));
597:     } else {
598:       // we only allow falling back on VecDot once, to avoid doing VecMultiDot via individual VecDots
599:       if (nfail++ == 0) {
600:         if (conjugate) PetscCall(VecDot_SeqKokkos(xin, yin[i], z_h + i));
601:         else PetscCall(VecTDot_SeqKokkos(xin, yin[i], z_h + i));
602:       } else break; // break the while loop
603:     }
604:     i = j;
605:   }
606:   PetscCall(VecRestoreKokkosView(xin, &xv));
607:   if (i < nv) { // finish the remaining if any
608:     if (conjugate) PetscCall(VecMDot_SeqKokkos(xin, nv - i, yin + i, z_h + i));
609:     else PetscCall(VecMTDot_SeqKokkos(xin, nv - i, yin + i, z_h + i));
610:   }
611:   PetscFunctionReturn(PETSC_SUCCESS);
612: }

614: PetscErrorCode VecMDot_SeqKokkos_GEMV(Vec xin, PetscInt nv, const Vec yin[], PetscScalar *z)
615: {
616:   PetscFunctionBegin;
617:   PetscCall(VecMultiDot_SeqKokkos_GEMV(PETSC_TRUE, xin, nv, yin, z)); // conjugate
618:   PetscFunctionReturn(PETSC_SUCCESS);
619: }

621: PetscErrorCode VecMTDot_SeqKokkos_GEMV(Vec xin, PetscInt nv, const Vec yin[], PetscScalar *z)
622: {
623:   PetscFunctionBegin;
624:   PetscCall(VecMultiDot_SeqKokkos_GEMV(PETSC_FALSE, xin, nv, yin, z)); // transpose
625:   PetscFunctionReturn(PETSC_SUCCESS);
626: }

628: /* x[:] = alpha */
629: PetscErrorCode VecSet_SeqKokkos(Vec xin, PetscScalar alpha)
630: {
631:   PetscScalarKokkosView xv;
632:   auto                  exec = PetscGetKokkosExecutionSpace();

634:   PetscFunctionBegin;
635:   PetscCall(PetscLogGpuTimeBegin());
636:   PetscCall(VecGetKokkosViewWrite(xin, &xv));
637:   PetscCallCXX(KokkosBlas::fill(exec, xv, alpha));
638:   PetscCall(VecRestoreKokkosViewWrite(xin, &xv));
639:   PetscCall(PetscLogGpuTimeEnd());
640:   PetscFunctionReturn(PETSC_SUCCESS);
641: }

643: /* x = alpha x */
644: PetscErrorCode VecScale_SeqKokkos(Vec xin, PetscScalar alpha)
645: {
646:   auto exec = PetscGetKokkosExecutionSpace();

648:   PetscFunctionBegin;
649:   if (alpha == (PetscScalar)0.0) {
650:     PetscCall(VecSet_SeqKokkos(xin, alpha));
651:   } else if (alpha != (PetscScalar)1.0) {
652:     PetscScalarKokkosView xv;

654:     PetscCall(PetscLogGpuTimeBegin());
655:     PetscCall(VecGetKokkosView(xin, &xv));
656:     PetscCallCXX(KokkosBlas::scal(exec, xv, alpha, xv));
657:     PetscCall(VecRestoreKokkosView(xin, &xv));
658:     PetscCall(PetscLogGpuTimeEnd());
659:     PetscCall(PetscLogGpuFlops(xin->map->n));
660:   }
661:   PetscFunctionReturn(PETSC_SUCCESS);
662: }

664: /* z = y^H x */
665: PetscErrorCode VecDot_SeqKokkos(Vec xin, Vec yin, PetscScalar *z)
666: {
667:   ConstPetscScalarKokkosView xv, yv;
668:   auto                       exec = PetscGetKokkosExecutionSpace();

670:   PetscFunctionBegin;
671:   PetscCall(PetscLogGpuTimeBegin());
672:   PetscCall(VecGetKokkosView(xin, &xv));
673:   PetscCall(VecGetKokkosView(yin, &yv));
674:   PetscCallCXX(*z = KokkosBlas::dot(exec, yv, xv)); /* KokkosBlas::dot(a,b) takes conjugate of a */
675:   PetscCall(VecRestoreKokkosView(xin, &xv));
676:   PetscCall(VecRestoreKokkosView(yin, &yv));
677:   PetscCall(PetscLogGpuTimeEnd());
678:   PetscCall(PetscLogGpuFlops(PetscMax(2.0 * xin->map->n - 1, 0.0)));
679:   PetscFunctionReturn(PETSC_SUCCESS);
680: }

682: /* y = x, where x is VECKOKKOS, but y may be not */
683: PetscErrorCode VecCopy_SeqKokkos(Vec xin, Vec yin)
684: {
685:   auto exec = PetscGetKokkosExecutionSpace();

687:   PetscFunctionBegin;
688:   PetscCall(PetscLogGpuTimeBegin());
689:   if (xin != yin) {
690:     Vec_Kokkos *xkok = static_cast<Vec_Kokkos *>(xin->spptr);
691:     if (yin->offloadmask == PETSC_OFFLOAD_KOKKOS) {
692:       /* y is also a VecKokkos */
693:       Vec_Kokkos *ykok = static_cast<Vec_Kokkos *>(yin->spptr);
694:       /* Kokkos rule: if x's host has newer data, it will copy to y's host view; otherwise to y's device view
695:         In case x's host is newer, y's device is newer, it will error (though should not, I think). So we just
696:         clear y's sync state.
697:        */
698:       ykok->v_dual.clear_sync_state();
699:       PetscCallCXX(Kokkos::deep_copy(exec, ykok->v_dual, xkok->v_dual)); // either cpu2cpu or gpu2cpu, so don't log it
700:     } else {
701:       PetscScalar *yarray;
702:       PetscCall(VecGetArrayWrite(yin, &yarray));
703:       PetscScalarKokkosViewHost yv(yarray, yin->map->n);
704:       if (xkok->v_dual.need_sync_host()) {                                     // x's device has newer data
705:         PetscCallCXX(Kokkos::deep_copy(exec, yv, xkok->v_dual.view_device())); // gpu2cpu
706:         PetscCallCXX(exec.fence());                                            // finish the deep copy
707:         PetscCall(PetscLogGpuToCpu(xkok->v_dual.extent(0) * sizeof(PetscScalar)));
708:       } else {
709:         PetscCallCXX(exec.fence());                                          // make sure xkok->v_dual.view_host() in ready for use on host;  Kokkos might also call it inside deep_copy(). We do it here for safety.
710:         PetscCallCXX(Kokkos::deep_copy(exec, yv, xkok->v_dual.view_host())); // Host view to host view deep copy, done on host
711:       }
712:       PetscCall(VecRestoreArrayWrite(yin, &yarray));
713:     }
714:   }
715:   PetscCall(PetscLogGpuTimeEnd());
716:   PetscFunctionReturn(PETSC_SUCCESS);
717: }

719: /* y[i] <--> x[i] */
720: PetscErrorCode VecSwap_SeqKokkos(Vec xin, Vec yin)
721: {
722:   PetscFunctionBegin;
723:   if (xin != yin) {
724:     PetscScalarKokkosView xv, yv;

726:     PetscCall(PetscLogGpuTimeBegin());
727:     PetscCall(VecGetKokkosView(xin, &xv));
728:     PetscCall(VecGetKokkosView(yin, &yv));
729:     PetscCallCXX(Kokkos::parallel_for(
730:       Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, xin->map->n), KOKKOS_LAMBDA(const PetscInt &i) {
731:         PetscScalar tmp = xv(i);
732:         xv(i)           = yv(i);
733:         yv(i)           = tmp;
734:       }));
735:     PetscCall(VecRestoreKokkosView(xin, &xv));
736:     PetscCall(VecRestoreKokkosView(yin, &yv));
737:     PetscCall(PetscLogGpuTimeEnd());
738:   }
739:   PetscFunctionReturn(PETSC_SUCCESS);
740: }

742: /*  w = alpha x + y */
743: PetscErrorCode VecWAXPY_SeqKokkos(Vec win, PetscScalar alpha, Vec xin, Vec yin)
744: {
745:   PetscFunctionBegin;
746:   if (alpha == (PetscScalar)0.0) {
747:     PetscCall(VecCopy_SeqKokkos(yin, win));
748:   } else {
749:     ConstPetscScalarKokkosView xv, yv;
750:     PetscScalarKokkosView      wv;

752:     PetscCall(PetscLogGpuTimeBegin());
753:     PetscCall(VecGetKokkosViewWrite(win, &wv));
754:     PetscCall(VecGetKokkosView(xin, &xv));
755:     PetscCall(VecGetKokkosView(yin, &yv));
756:     PetscCallCXX(Kokkos::parallel_for(Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, win->map->n), KOKKOS_LAMBDA(const PetscInt &i) { wv(i) = alpha * xv(i) + yv(i); }));
757:     PetscCall(VecRestoreKokkosView(xin, &xv));
758:     PetscCall(VecRestoreKokkosView(yin, &yv));
759:     PetscCall(VecRestoreKokkosViewWrite(win, &wv));
760:     PetscCall(PetscLogGpuTimeEnd());
761:     PetscCall(PetscLogGpuFlops(2.0 * win->map->n));
762:   }
763:   PetscFunctionReturn(PETSC_SUCCESS);
764: }

766: template <PetscInt ValueCount>
767: struct MAXPYFunctor {
768:   static_assert(ValueCount >= 1 && ValueCount <= 8, "ValueCount must be in [1, 8]");
769:   typedef ConstPetscScalarKokkosView::size_type size_type;

771:   PetscScalarKokkosView      yv;
772:   PetscScalar                a[8];
773:   ConstPetscScalarKokkosView xv[8];

775:   MAXPYFunctor(PetscScalarKokkosView yv, PetscScalar a0, PetscScalar a1, PetscScalar a2, PetscScalar a3, PetscScalar a4, PetscScalar a5, PetscScalar a6, PetscScalar a7, ConstPetscScalarKokkosView xv0, ConstPetscScalarKokkosView xv1, ConstPetscScalarKokkosView xv2, ConstPetscScalarKokkosView xv3, ConstPetscScalarKokkosView xv4, ConstPetscScalarKokkosView xv5, ConstPetscScalarKokkosView xv6, ConstPetscScalarKokkosView xv7) :
776:     yv(yv)
777:   {
778:     a[0]  = a0;
779:     a[1]  = a1;
780:     a[2]  = a2;
781:     a[3]  = a3;
782:     a[4]  = a4;
783:     a[5]  = a5;
784:     a[6]  = a6;
785:     a[7]  = a7;
786:     xv[0] = xv0;
787:     xv[1] = xv1;
788:     xv[2] = xv2;
789:     xv[3] = xv3;
790:     xv[4] = xv4;
791:     xv[5] = xv5;
792:     xv[6] = xv6;
793:     xv[7] = xv7;
794:   }

796:   KOKKOS_INLINE_FUNCTION void operator()(const size_type i) const
797:   {
798:     for (PetscInt j = 0; j < ValueCount; ++j) yv(i) += a[j] * xv[j](i);
799:   }
800: };

802: /*  y = y + sum alpha[i] x[i] */
803: PetscErrorCode VecMAXPY_SeqKokkos(Vec yin, PetscInt nv, const PetscScalar *alpha, Vec *xin)
804: {
805:   PetscInt                   i, j, cur = 0, ngroup = nv / 8, rem = nv % 8, N = yin->map->n;
806:   PetscScalarKokkosView      yv;
807:   PetscScalar                a[8];
808:   ConstPetscScalarKokkosView xv[8];
809:   Kokkos::RangePolicy<>      policy(PetscGetKokkosExecutionSpace(), 0, N);

811:   PetscFunctionBegin;
812:   PetscCall(PetscLogGpuTimeBegin());
813:   PetscCall(VecGetKokkosView(yin, &yv));
814:   for (i = 0; i < ngroup; i++) { /* 8 x's per group */
815:     for (j = 0; j < 8; j++) {    /* Fill the parameters */
816:       a[j] = alpha[cur + j];
817:       PetscCall(VecGetKokkosView(xin[cur + j], &xv[j]));
818:     }
819:     MAXPYFunctor<8> maxpy(yv, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], xv[0], xv[1], xv[2], xv[3], xv[4], xv[5], xv[6], xv[7]);
820:     PetscCallCXX(Kokkos::parallel_for(policy, maxpy));
821:     for (j = 0; j < 8; j++) PetscCall(VecRestoreKokkosView(xin[cur + j], &xv[j]));
822:     cur += 8;
823:   }

825:   if (rem) { /* The remaining */
826:     for (j = 0; j < rem; j++) {
827:       a[j] = alpha[cur + j];
828:       PetscCall(VecGetKokkosView(xin[cur + j], &xv[j]));
829:     }
830:     // clang-format off
831:     switch (rem) {
832:     case 1: PetscCallCXX(Kokkos::parallel_for(policy, MAXPYFunctor<1>(yv, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], xv[0], xv[1], xv[2], xv[3], xv[4], xv[5], xv[6], xv[7]))); break;
833:     case 2: PetscCallCXX(Kokkos::parallel_for(policy, MAXPYFunctor<2>(yv, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], xv[0], xv[1], xv[2], xv[3], xv[4], xv[5], xv[6], xv[7]))); break;
834:     case 3: PetscCallCXX(Kokkos::parallel_for(policy, MAXPYFunctor<3>(yv, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], xv[0], xv[1], xv[2], xv[3], xv[4], xv[5], xv[6], xv[7]))); break;
835:     case 4: PetscCallCXX(Kokkos::parallel_for(policy, MAXPYFunctor<4>(yv, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], xv[0], xv[1], xv[2], xv[3], xv[4], xv[5], xv[6], xv[7]))); break;
836:     case 5: PetscCallCXX(Kokkos::parallel_for(policy, MAXPYFunctor<5>(yv, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], xv[0], xv[1], xv[2], xv[3], xv[4], xv[5], xv[6], xv[7]))); break;
837:     case 6: PetscCallCXX(Kokkos::parallel_for(policy, MAXPYFunctor<6>(yv, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], xv[0], xv[1], xv[2], xv[3], xv[4], xv[5], xv[6], xv[7]))); break;
838:     case 7: PetscCallCXX(Kokkos::parallel_for(policy, MAXPYFunctor<7>(yv, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], xv[0], xv[1], xv[2], xv[3], xv[4], xv[5], xv[6], xv[7]))); break;
839:     }
840:     // clang-format on
841:     for (j = 0; j < rem; j++) PetscCall(VecRestoreKokkosView(xin[cur + j], &xv[j]));
842:   }
843:   PetscCall(VecRestoreKokkosView(yin, &yv));
844:   PetscCall(PetscLogGpuTimeEnd());
845:   PetscCall(PetscLogGpuFlops(nv * 2.0 * yin->map->n));
846:   PetscFunctionReturn(PETSC_SUCCESS);
847: }

849: /*  y = y + sum alpha[i] x[i] */
850: PetscErrorCode VecMAXPY_SeqKokkos_GEMV(Vec yin, PetscInt nv, const PetscScalar *a_h, Vec *xin)
851: {
852:   const PetscInt             n = yin->map->n;
853:   PetscInt                   i, j, nfail;
854:   PetscScalarKokkosView      yv;
855:   ConstPetscScalarKokkosView xfirst, xnext;
856:   PetscBool                  stop = PETSC_FALSE;
857:   PetscInt                   lda  = 0, m;
858:   const PetscScalar         *xarray;
859:   PetscScalar               *a_d = nullptr;

861:   PetscFunctionBegin;
862:   PetscCall(PetscLogGpuTimeBegin());
863:   PetscCall(VecGetKokkosView(yin, &yv));
864: #if defined(KOKKOS_ENABLE_UNIFIED_MEMORY)
865:   a_d = const_cast<PetscScalar *>(a_h);
866: #endif
867:   i = nfail = 0;
868:   while (i < nv) {
869:     stop = PETSC_FALSE;
870:     PetscCall(VecGetKokkosView(xin[i], &xfirst));
871:     xarray = xfirst.data();
872:     for (j = i + 1; j < nv; j++) {
873:       PetscCall(VecGetKokkosView(xin[j], &xnext));
874:       if (j == i + 1) {
875:         lda = xnext.data() - xfirst.data();
876:         if (lda < 0 || lda - n > 64) stop = PETSC_TRUE;    // avoid using arbitrary lda; 64 bytes are a big enough alignment in VecDuplicateVecs
877:       } else if (lda * (j - i) != xnext.data() - xarray) { // not in the same stride? if so, stop here
878:         stop = PETSC_TRUE;
879:       }
880:       PetscCall(VecRestoreKokkosView(xin[j], &xnext));
881:       if (stop) break;
882:     }
883:     PetscCall(VecRestoreKokkosView(xin[i], &xfirst));

885:     m = j - i;
886:     if (m > 1) {
887:       if (!a_d) {
888:         if (nv > PetscScalarPoolSize) { // rare case
889:           PetscScalarPoolSize = nv;
890:           PetscCallCXX(PetscScalarPool = static_cast<PetscScalar *>(Kokkos::kokkos_realloc(PetscScalarPool, PetscScalarPoolSize)));
891:         }
892:         a_d = PetscScalarPool;
893:       }
894:       const auto &B  = Kokkos::View<const PetscScalar **, Kokkos::LayoutLeft>(xarray, lda, m);
895:       const auto &A  = Kokkos::subview(B, std::pair<PetscInt, PetscInt>(0, n), Kokkos::ALL);
896:       auto        av = PetscScalarKokkosDualView(PetscScalarKokkosView(a_d + i, m), PetscScalarKokkosViewHost(const_cast<PetscScalar *>(a_h) + i, m));
897:       av.modify_host();
898:       PetscCall(KokkosDualViewSyncDevice(av, PetscGetKokkosExecutionSpace()));
899:       PetscCallCXX(KokkosBlas::gemv(PetscGetKokkosExecutionSpace(), "N", 1.0, A, av.view_device(), 1.0, yv));
900:       PetscCall(PetscLogGpuFlops(m * 2.0 * n));
901:     } else {
902:       // we only allow falling back on VecAXPY once
903:       if (nfail++ == 0) PetscCall(VecAXPY_SeqKokkos(yin, a_h[i], xin[i]));
904:       else break; // break the while loop
905:     }
906:     i = j;
907:   }
908:   // finish the remaining if any
909:   PetscCall(VecRestoreKokkosView(yin, &yv));
910:   if (i < nv) PetscCall(VecMAXPY_SeqKokkos(yin, nv - i, a_h + i, xin + i));
911:   PetscCall(PetscLogGpuTimeEnd());
912:   PetscFunctionReturn(PETSC_SUCCESS);
913: }

915: /* y = alpha x + beta y */
916: PetscErrorCode VecAXPBY_SeqKokkos(Vec yin, PetscScalar alpha, PetscScalar beta, Vec xin)
917: {
918:   PetscBool xiskok, yiskok;

920:   PetscFunctionBegin;
921:   PetscCall(PetscObjectTypeCompareAny((PetscObject)xin, &xiskok, VECSEQKOKKOS, VECMPIKOKKOS, ""));
922:   PetscCall(PetscObjectTypeCompareAny((PetscObject)yin, &yiskok, VECSEQKOKKOS, VECMPIKOKKOS, ""));
923:   if (xiskok && yiskok) {
924:     ConstPetscScalarKokkosView xv;
925:     PetscScalarKokkosView      yv;

927:     PetscCall(PetscLogGpuTimeBegin());
928:     PetscCall(VecGetKokkosView(xin, &xv));
929:     PetscCall(VecGetKokkosView(yin, &yv));
930:     PetscCallCXX(KokkosBlas::axpby(PetscGetKokkosExecutionSpace(), alpha, xv, beta, yv));
931:     PetscCall(VecRestoreKokkosView(xin, &xv));
932:     PetscCall(VecRestoreKokkosView(yin, &yv));
933:     PetscCall(PetscLogGpuTimeEnd());
934:     if (alpha == (PetscScalar)0.0 || beta == (PetscScalar)0.0) {
935:       PetscCall(PetscLogGpuFlops(xin->map->n));
936:     } else if (beta == (PetscScalar)1.0 || alpha == (PetscScalar)1.0) {
937:       PetscCall(PetscLogGpuFlops(2.0 * xin->map->n));
938:     } else {
939:       PetscCall(PetscLogGpuFlops(3.0 * xin->map->n));
940:     }
941:   } else {
942:     PetscCall(VecAXPBY_Seq(yin, alpha, beta, xin));
943:   }
944:   PetscFunctionReturn(PETSC_SUCCESS);
945: }

947: /* z = alpha x + beta y + gamma z */
948: PetscErrorCode VecAXPBYPCZ_SeqKokkos(Vec zin, PetscScalar alpha, PetscScalar beta, PetscScalar gamma, Vec xin, Vec yin)
949: {
950:   ConstPetscScalarKokkosView xv, yv;
951:   PetscScalarKokkosView      zv;
952:   Kokkos::RangePolicy<>      policy(PetscGetKokkosExecutionSpace(), 0, zin->map->n);

954:   PetscFunctionBegin;
955:   PetscCall(PetscLogGpuTimeBegin());
956:   PetscCall(VecGetKokkosView(zin, &zv));
957:   PetscCall(VecGetKokkosView(xin, &xv));
958:   PetscCall(VecGetKokkosView(yin, &yv));
959:   if (gamma == (PetscScalar)0.0) { // a common case
960:     if (alpha == -beta) {
961:       PetscCallCXX(Kokkos::parallel_for( // a common case
962:         policy, KOKKOS_LAMBDA(const PetscInt &i) { zv(i) = alpha * (xv(i) - yv(i)); }));
963:     } else {
964:       PetscCallCXX(Kokkos::parallel_for(policy, KOKKOS_LAMBDA(const PetscInt &i) { zv(i) = alpha * xv(i) + beta * yv(i); }));
965:     }
966:   } else {
967:     PetscCallCXX(KokkosBlas::update(PetscGetKokkosExecutionSpace(), alpha, xv, beta, yv, gamma, zv));
968:   }
969:   PetscCall(VecRestoreKokkosView(xin, &xv));
970:   PetscCall(VecRestoreKokkosView(yin, &yv));
971:   PetscCall(VecRestoreKokkosView(zin, &zv));
972:   PetscCall(PetscLogGpuTimeEnd());
973:   PetscCall(PetscLogGpuFlops(zin->map->n * 5.0));
974:   PetscFunctionReturn(PETSC_SUCCESS);
975: }

977: /* w = x*y. Any subset of the x, y, and w may be the same vector.

979:   w is of type VecKokkos, but x, y may be not.
980: */
981: PetscErrorCode VecPointwiseMult_SeqKokkos(Vec win, Vec xin, Vec yin)
982: {
983:   PetscInt n;

985:   PetscFunctionBegin;
986:   PetscCall(PetscLogGpuTimeBegin());
987:   PetscCall(VecGetLocalSize(win, &n));
988:   if (xin->offloadmask != PETSC_OFFLOAD_KOKKOS || yin->offloadmask != PETSC_OFFLOAD_KOKKOS) {
989:     PetscScalarKokkosViewHost wv;
990:     const PetscScalar        *xp, *yp;
991:     PetscCall(VecGetArrayRead(xin, &xp));
992:     PetscCall(VecGetArrayRead(yin, &yp));
993:     PetscCall(VecGetKokkosViewWrite(win, &wv));

995:     ConstPetscScalarKokkosViewHost xv(xp, n), yv(yp, n);
996:     PetscCallCXX(Kokkos::parallel_for(Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>(0, n), KOKKOS_LAMBDA(const PetscInt &i) { wv(i) = xv(i) * yv(i); }));

998:     PetscCall(VecRestoreArrayRead(xin, &xp));
999:     PetscCall(VecRestoreArrayRead(yin, &yp));
1000:     PetscCall(VecRestoreKokkosViewWrite(win, &wv));
1001:   } else {
1002:     ConstPetscScalarKokkosView xv, yv;
1003:     PetscScalarKokkosView      wv;

1005:     PetscCall(VecGetKokkosViewWrite(win, &wv));
1006:     PetscCall(VecGetKokkosView(xin, &xv));
1007:     PetscCall(VecGetKokkosView(yin, &yv));
1008:     PetscCallCXX(Kokkos::parallel_for(Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, n), KOKKOS_LAMBDA(const PetscInt &i) { wv(i) = xv(i) * yv(i); }));
1009:     PetscCall(VecRestoreKokkosView(yin, &yv));
1010:     PetscCall(VecRestoreKokkosView(xin, &xv));
1011:     PetscCall(VecRestoreKokkosViewWrite(win, &wv));
1012:   }
1013:   PetscCall(PetscLogGpuTimeEnd());
1014:   PetscCall(PetscLogGpuFlops(n));
1015:   PetscFunctionReturn(PETSC_SUCCESS);
1016: }

1018: /* w = x/y */
1019: PetscErrorCode VecPointwiseDivide_SeqKokkos(Vec win, Vec xin, Vec yin)
1020: {
1021:   PetscInt n;

1023:   PetscFunctionBegin;
1024:   PetscCall(PetscLogGpuTimeBegin());
1025:   PetscCall(VecGetLocalSize(win, &n));
1026:   if (xin->offloadmask != PETSC_OFFLOAD_KOKKOS || yin->offloadmask != PETSC_OFFLOAD_KOKKOS) {
1027:     PetscScalarKokkosViewHost wv;
1028:     const PetscScalar        *xp, *yp;
1029:     PetscCall(VecGetArrayRead(xin, &xp));
1030:     PetscCall(VecGetArrayRead(yin, &yp));
1031:     PetscCall(VecGetKokkosViewWrite(win, &wv));

1033:     ConstPetscScalarKokkosViewHost xv(xp, n), yv(yp, n);
1034:     PetscCallCXX(Kokkos::parallel_for(
1035:       Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>(0, n), KOKKOS_LAMBDA(const PetscInt &i) {
1036:         if (yv(i) != 0.0) wv(i) = xv(i) / yv(i);
1037:         else wv(i) = ((xv(i) == 0.0) ? 1.0 : 0.0);
1038:       }));

1040:     PetscCall(VecRestoreArrayRead(xin, &xp));
1041:     PetscCall(VecRestoreArrayRead(yin, &yp));
1042:     PetscCall(VecRestoreKokkosViewWrite(win, &wv));
1043:   } else {
1044:     ConstPetscScalarKokkosView xv, yv;
1045:     PetscScalarKokkosView      wv;

1047:     PetscCall(VecGetKokkosViewWrite(win, &wv));
1048:     PetscCall(VecGetKokkosView(xin, &xv));
1049:     PetscCall(VecGetKokkosView(yin, &yv));
1050:     PetscCallCXX(Kokkos::parallel_for(
1051:       Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, n), KOKKOS_LAMBDA(const PetscInt &i) {
1052:         if (yv(i) != 0.0) wv(i) = xv(i) / yv(i);
1053:         else wv(i) = ((xv(i) == 0.0) ? 1.0 : 0.0);
1054:       }));
1055:     PetscCall(VecRestoreKokkosView(yin, &yv));
1056:     PetscCall(VecRestoreKokkosView(xin, &xv));
1057:     PetscCall(VecRestoreKokkosViewWrite(win, &wv));
1058:   }
1059:   PetscCall(PetscLogGpuTimeEnd());
1060:   PetscCall(PetscLogGpuFlops(win->map->n));
1061:   PetscFunctionReturn(PETSC_SUCCESS);
1062: }

1064: PetscErrorCode VecNorm_SeqKokkos(Vec xin, NormType type, PetscReal *z)
1065: {
1066:   const PetscInt             n = xin->map->n;
1067:   ConstPetscScalarKokkosView xv;
1068:   auto                       exec = PetscGetKokkosExecutionSpace();

1070:   PetscFunctionBegin;
1071:   if (type == NORM_1_AND_2) {
1072:     PetscCall(VecNorm_SeqKokkos(xin, NORM_1, z));
1073:     PetscCall(VecNorm_SeqKokkos(xin, NORM_2, z + 1));
1074:   } else {
1075:     PetscCall(PetscLogGpuTimeBegin());
1076:     PetscCall(VecGetKokkosView(xin, &xv));
1077:     if (type == NORM_2 || type == NORM_FROBENIUS) {
1078:       PetscCallCXX(*z = KokkosBlas::nrm2(exec, xv));
1079:       PetscCall(PetscLogGpuFlops(PetscMax(2.0 * n - 1, 0.0)));
1080:     } else if (type == NORM_1) {
1081:       PetscCallCXX(*z = KokkosBlas::nrm1(exec, xv));
1082:       PetscCall(PetscLogGpuFlops(PetscMax(n - 1.0, 0.0)));
1083:     } else if (type == NORM_INFINITY) {
1084:       PetscCallCXX(*z = KokkosBlas::nrminf(exec, xv));
1085:     }
1086:     PetscCall(VecRestoreKokkosView(xin, &xv));
1087:     PetscCall(PetscLogGpuTimeEnd());
1088:   }
1089:   PetscFunctionReturn(PETSC_SUCCESS);
1090: }

1092: PetscErrorCode VecErrorWeightedNorms_SeqKokkos(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)
1093: {
1094:   ConstPetscScalarKokkosView u, y, erra, atola, rtola;
1095:   PetscBool                  has_E = PETSC_FALSE, has_atol = PETSC_FALSE, has_rtol = PETSC_FALSE;
1096:   PetscInt                   n, n_loc = 0, na_loc = 0, nr_loc = 0;
1097:   PetscReal                  nrm = 0, nrma = 0, nrmr = 0;

1099:   PetscFunctionBegin;
1100:   PetscCall(VecGetLocalSize(U, &n));
1101:   PetscCall(VecGetKokkosView(U, &u));
1102:   PetscCall(VecGetKokkosView(Y, &y));
1103:   if (E) {
1104:     PetscCall(VecGetKokkosView(E, &erra));
1105:     has_E = PETSC_TRUE;
1106:   }
1107:   if (vatol) {
1108:     PetscCall(VecGetKokkosView(vatol, &atola));
1109:     has_atol = PETSC_TRUE;
1110:   }
1111:   if (vrtol) {
1112:     PetscCall(VecGetKokkosView(vrtol, &rtola));
1113:     has_rtol = PETSC_TRUE;
1114:   }

1116:   if (wnormtype == NORM_INFINITY) {
1117:     PetscCallCXX(Kokkos::parallel_reduce(
1118:       "VecErrorWeightedNorms_INFINITY", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, n),
1119:       KOKKOS_LAMBDA(const PetscInt &i, PetscReal &l_nrm, PetscReal &l_nrma, PetscReal &l_nrmr, PetscInt &l_n_loc, PetscInt &l_na_loc, PetscInt &l_nr_loc) {
1120:         PetscReal err, tol, tola, tolr, l_atol, l_rtol;
1121:         if (PetscAbsScalar(y(i)) >= ignore_max && PetscAbsScalar(u(i)) >= ignore_max) {
1122:           l_atol = has_atol ? PetscRealPart(atola(i)) : atol;
1123:           l_rtol = has_rtol ? PetscRealPart(rtola(i)) : rtol;
1124:           err    = has_E ? PetscAbsScalar(erra(i)) : PetscAbsScalar(y(i) - u(i));
1125:           tola   = l_atol;
1126:           tolr   = l_rtol * PetscMax(PetscAbsScalar(u(i)), PetscAbsScalar(y(i)));
1127:           tol    = tola + tolr;
1128:           if (tola > 0.) {
1129:             l_nrma = PetscMax(l_nrma, err / tola);
1130:             l_na_loc++;
1131:           }
1132:           if (tolr > 0.) {
1133:             l_nrmr = PetscMax(l_nrmr, err / tolr);
1134:             l_nr_loc++;
1135:           }
1136:           if (tol > 0.) {
1137:             l_nrm = PetscMax(l_nrm, err / tol);
1138:             l_n_loc++;
1139:           }
1140:         }
1141:       },
1142:       Kokkos::Max<PetscReal>(nrm), Kokkos::Max<PetscReal>(nrma), Kokkos::Max<PetscReal>(nrmr), n_loc, na_loc, nr_loc));
1143:   } else {
1144:     PetscCallCXX(Kokkos::parallel_reduce(
1145:       "VecErrorWeightedNorms_NORM_2", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, n),
1146:       KOKKOS_LAMBDA(const PetscInt &i, PetscReal &l_nrm, PetscReal &l_nrma, PetscReal &l_nrmr, PetscInt &l_n_loc, PetscInt &l_na_loc, PetscInt &l_nr_loc) {
1147:         PetscReal err, tol, tola, tolr, l_atol, l_rtol;
1148:         if (PetscAbsScalar(y(i)) >= ignore_max && PetscAbsScalar(u(i)) >= ignore_max) {
1149:           l_atol = has_atol ? PetscRealPart(atola(i)) : atol;
1150:           l_rtol = has_rtol ? PetscRealPart(rtola(i)) : rtol;
1151:           err    = has_E ? PetscAbsScalar(erra(i)) : PetscAbsScalar(y(i) - u(i));
1152:           tola   = l_atol;
1153:           tolr   = l_rtol * PetscMax(PetscAbsScalar(u(i)), PetscAbsScalar(y(i)));
1154:           tol    = tola + tolr;
1155:           if (tola > 0.) {
1156:             l_nrma += PetscSqr(err / tola);
1157:             l_na_loc++;
1158:           }
1159:           if (tolr > 0.) {
1160:             l_nrmr += PetscSqr(err / tolr);
1161:             l_nr_loc++;
1162:           }
1163:           if (tol > 0.) {
1164:             l_nrm += PetscSqr(err / tol);
1165:             l_n_loc++;
1166:           }
1167:         }
1168:       },
1169:       nrm, nrma, nrmr, n_loc, na_loc, nr_loc));
1170:   }

1172:   if (wnormtype == NORM_2) {
1173:     *norm  = PetscSqrtReal(nrm);
1174:     *norma = PetscSqrtReal(nrma);
1175:     *normr = PetscSqrtReal(nrmr);
1176:   } else {
1177:     *norm  = nrm;
1178:     *norma = nrma;
1179:     *normr = nrmr;
1180:   }
1181:   *norm_loc  = n_loc;
1182:   *norma_loc = na_loc;
1183:   *normr_loc = nr_loc;

1185:   if (E) PetscCall(VecRestoreKokkosView(E, &erra));
1186:   if (vatol) PetscCall(VecRestoreKokkosView(vatol, &atola));
1187:   if (vrtol) PetscCall(VecRestoreKokkosView(vrtol, &rtola));
1188:   PetscCall(VecRestoreKokkosView(U, &u));
1189:   PetscCall(VecRestoreKokkosView(Y, &y));
1190:   PetscFunctionReturn(PETSC_SUCCESS);
1191: }

1193: /* A functor for DotNorm2 so that we can compute dp and nm in one kernel */
1194: struct DotNorm2 {
1195:   typedef PetscScalar                           value_type[];
1196:   typedef ConstPetscScalarKokkosView::size_type size_type;

1198:   size_type                  value_count;
1199:   ConstPetscScalarKokkosView xv_, yv_; /* first and second vectors in VecDotNorm2. The order matters. */

1201:   DotNorm2(ConstPetscScalarKokkosView &xv, ConstPetscScalarKokkosView &yv) : value_count(2), xv_(xv), yv_(yv) { }

1203:   KOKKOS_INLINE_FUNCTION void operator()(const size_type i, value_type result) const
1204:   {
1205:     result[0] += PetscConj(yv_(i)) * xv_(i);
1206:     result[1] += PetscConj(yv_(i)) * yv_(i);
1207:   }

1209:   KOKKOS_INLINE_FUNCTION void join(value_type dst, const value_type src) const
1210:   {
1211:     dst[0] += src[0];
1212:     dst[1] += src[1];
1213:   }

1215:   KOKKOS_INLINE_FUNCTION void init(value_type result) const
1216:   {
1217:     result[0] = 0.0;
1218:     result[1] = 0.0;
1219:   }
1220: };

1222: /* dp = y^H x, nm = y^H y */
1223: PetscErrorCode VecDotNorm2_SeqKokkos(Vec xin, Vec yin, PetscScalar *dp, PetscScalar *nm)
1224: {
1225:   ConstPetscScalarKokkosView xv, yv;
1226:   PetscScalar                result[2];

1228:   PetscFunctionBegin;
1229:   PetscCall(PetscLogGpuTimeBegin());
1230:   PetscCall(VecGetKokkosView(xin, &xv));
1231:   PetscCall(VecGetKokkosView(yin, &yv));
1232:   DotNorm2 dn(xv, yv);
1233:   PetscCallCXX(Kokkos::parallel_reduce(Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, xin->map->n), dn, result));
1234:   *dp = result[0];
1235:   *nm = result[1];
1236:   PetscCall(VecRestoreKokkosView(yin, &yv));
1237:   PetscCall(VecRestoreKokkosView(xin, &xv));
1238:   PetscCall(PetscLogGpuTimeEnd());
1239:   PetscCall(PetscLogGpuFlops(4.0 * xin->map->n));
1240:   PetscFunctionReturn(PETSC_SUCCESS);
1241: }

1243: PetscErrorCode VecConjugate_SeqKokkos(Vec xin)
1244: {
1245:   PetscScalarKokkosView xv;

1247:   PetscFunctionBegin;
1248:   PetscCall(PetscLogGpuTimeBegin());
1249:   PetscCall(VecGetKokkosView(xin, &xv));
1250:   PetscCallCXX(Kokkos::parallel_for(Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, xin->map->n), KOKKOS_LAMBDA(const PetscInt &i) { xv(i) = PetscConj(xv(i)); }));
1251:   PetscCall(VecRestoreKokkosView(xin, &xv));
1252:   PetscCall(PetscLogGpuTimeEnd());
1253:   PetscFunctionReturn(PETSC_SUCCESS);
1254: }

1256: /* Temporarily replace the array in vin with a[]. Return to the original array with a call to VecResetArray() */
1257: PetscErrorCode VecPlaceArray_SeqKokkos(Vec vin, const PetscScalar *a)
1258: {
1259:   Vec_Seq    *vecseq = (Vec_Seq *)vin->data;
1260:   Vec_Kokkos *veckok = static_cast<Vec_Kokkos *>(vin->spptr);

1262:   PetscFunctionBegin;
1263:   PetscCall(VecPlaceArray_Seq(vin, a));
1264:   PetscCall(veckok->UpdateArray<HostMirrorMemorySpace>(vecseq->array));
1265:   PetscFunctionReturn(PETSC_SUCCESS);
1266: }

1268: PetscErrorCode VecResetArray_SeqKokkos(Vec vin)
1269: {
1270:   Vec_Seq    *vecseq = (Vec_Seq *)vin->data;
1271:   Vec_Kokkos *veckok = static_cast<Vec_Kokkos *>(vin->spptr);

1273:   PetscFunctionBegin;
1274:   /* User wants to unhook the provided host array. Sync it so that user can get the latest */
1275:   PetscCall(KokkosDualViewSyncHost(veckok->v_dual, PetscGetKokkosExecutionSpace()));
1276:   PetscCall(VecResetArray_Seq(vin)); /* Swap back the old host array, assuming its has the latest value */
1277:   PetscCall(veckok->UpdateArray<HostMirrorMemorySpace>(vecseq->array));
1278:   PetscFunctionReturn(PETSC_SUCCESS);
1279: }

1281: /*@
1282:   VecKokkosPlaceArray - Allows one to replace the device array in a VecKokkos vector with a
1283:   device array provided by the user. This is useful to avoid copying an array into a vector.

1285:   Logically Collective; No Fortran Support

1287:   Input Parameters:
1288: + v - the VecKokkos vector
1289: - a - the device array

1291:   Level: developer

1293:   Notes:

1295:   You can return to the original array with a call to `VecKokkosResetArray()`. `vec` does not take
1296:   ownership of `array` in any way.

1298:   The user manages the device array so PETSc doesn't care how it was allocated.

1300:   The user must free `array` themselves but be careful not to
1301:   do so before the vector has either been destroyed, had its original array restored with
1302:   `VecKokkosResetArray()` or permanently replaced with `VecReplaceArray()`.

1304: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecKokkosResetArray()`, `VecReplaceArray()`, `VecResetArray()`
1305: @*/
1306: PetscErrorCode VecKokkosPlaceArray(Vec v, PetscScalar *a)
1307: {
1308:   Vec_Kokkos *veckok = static_cast<Vec_Kokkos *>(v->spptr);
1309:   Vec_Seq    *vecseq = static_cast<Vec_Seq *>(v->data);

1311:   PetscFunctionBegin;
1312:   VecErrorIfNotKokkos(v);
1313:   // An array-less vector (see VecCreateSeqKokkosWithArray()) has no host mirror yet. Allocate it now, as
1314:   // VecCreate{Seq,MPI}KokkosWithArray() would have done at creation with a non-NULL darray
1315:   if (!std::is_same<DefaultMemorySpace, HostMirrorMemorySpace>::value && !vecseq->array && v->map->n) {
1316:     PetscCall(PetscMalloc1(v->map->n, &vecseq->array_allocated));
1317:     vecseq->array = vecseq->array_allocated;
1318:     PetscCallCXX(veckok->v_dual = PetscScalarKokkosDualView(veckok->v_dual.view_device(), PetscScalarKokkosViewHost(vecseq->array, v->map->n)));
1319:   }
1320:   // Sync the old device view before replacing it; so that when it is put back, it has the saved value.
1321:   PetscCall(KokkosDualViewSyncDevice(veckok->v_dual, PetscGetKokkosExecutionSpace()));
1322:   PetscCallCXX(veckok->unplaced_d = veckok->v_dual.view_device());
1323:   // We assume a[] contains the latest data and discard the vector's old sync state
1324:   PetscCall(veckok->UpdateArray<DefaultMemorySpace>(a));
1325:   // When host and device share the array, keep the host array in Vec_Seq consistent with the placed one for VecGetArray()
1326:   if (std::is_same<DefaultMemorySpace, HostMirrorMemorySpace>::value) vecseq->array = veckok->v_dual.view_host().data();
1327:   PetscCall(PetscObjectStateIncrease((PetscObject)v));
1328:   PetscFunctionReturn(PETSC_SUCCESS);
1329: }

1331: /*@
1332:   VecKokkosResetArray - Resets a vector to use its default memory. Call this
1333:   after the use of `VecKokkosPlaceArray()`.

1335:   Not Collective

1337:   Input Parameter:
1338: . v - the vector

1340:   Level: developer

1342:   Notes:

1344:   After the call, the original array placed in with `VecKokkosPlaceArray()` will contain the latest value of the vector.
1345:   Note that device kernels are asynchronous. Users are responsible to sync the device if they wish to have immediate access
1346:   to the data in the array. Also, after the call, `v` will contain whatever data before `VecKokkosPlaceArray()`.

1348: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecReplaceArray()`, `VecKokkosPlaceArray()`
1349: @*/
1350: PetscErrorCode VecKokkosResetArray(Vec v)
1351: {
1352:   Vec_Kokkos *veckok = static_cast<Vec_Kokkos *>(v->spptr);
1353:   Vec_Seq    *vecseq = static_cast<Vec_Seq *>(v->data);

1355:   PetscFunctionBegin;
1356:   VecErrorIfNotKokkos(v);
1357:   // User wants to unhook the provided device array. Sync it so that user can get the latest
1358:   PetscCall(KokkosDualViewSyncDevice(veckok->v_dual, PetscGetKokkosExecutionSpace()));
1359:   // Put the unplaced device array back, and set an appropriate modify flag
1360:   PetscCall(veckok->UpdateArray<DefaultMemorySpace>(veckok->unplaced_d.data()));
1361:   // Keep the host array in Vec_Seq consistent with the one put back, see VecKokkosPlaceArray()
1362:   if (std::is_same<DefaultMemorySpace, HostMirrorMemorySpace>::value) vecseq->array = veckok->v_dual.view_host().data();
1363:   PetscCall(PetscObjectStateIncrease((PetscObject)v));
1364:   PetscFunctionReturn(PETSC_SUCCESS);
1365: }

1367: /* Replace the array in vin with a[] that must be allocated by PetscMalloc. a[] is owned by vin afterwards. */
1368: PetscErrorCode VecReplaceArray_SeqKokkos(Vec vin, const PetscScalar *a)
1369: {
1370:   Vec_Seq    *vecseq = (Vec_Seq *)vin->data;
1371:   Vec_Kokkos *veckok = static_cast<Vec_Kokkos *>(vin->spptr);

1373:   PetscFunctionBegin;
1374:   /* Make sure the users array has the latest values */
1375:   if (vecseq->array != vecseq->array_allocated) PetscCall(KokkosDualViewSyncHost(veckok->v_dual, PetscGetKokkosExecutionSpace()));
1376:   PetscCall(VecReplaceArray_Seq(vin, a));
1377:   PetscCall(veckok->UpdateArray<HostMirrorMemorySpace>(vecseq->array));
1378:   PetscFunctionReturn(PETSC_SUCCESS);
1379: }

1381: /* Maps the local portion of vector v into vector w */
1382: PetscErrorCode VecGetLocalVector_SeqKokkos(Vec v, Vec w)
1383: {
1384:   PetscBool isKokkos = PETSC_FALSE;

1386:   PetscFunctionBegin;
1387:   PetscCall(PetscObjectTypeCompareAny((PetscObject)(w), &isKokkos, VECSEQKOKKOS, VECMPIKOKKOS, ""));
1388:   if (!isKokkos) {
1389:     PetscScalar *a;

1391:     PetscCall(VecGetArray(v, &a));
1392:     PetscCall(VecPlaceArray(w, a));
1393:   } else { // The whole purpose of VecGetLocalVector is to quickly put v's data pointers into w (i.e., the v_dual, array fields)
1394:     Vec_Kokkos *vkok = static_cast<Vec_Kokkos *>(v->spptr);
1395:     Vec_Kokkos *wkok = static_cast<Vec_Kokkos *>(w->spptr);
1396:     Vec_Seq    *vseq = static_cast<Vec_Seq *>(v->data);
1397:     Vec_Seq    *wseq = static_cast<Vec_Seq *>(w->data);

1399:     wkok->unplaced_dual = wkok->v_dual;
1400:     wkok->v_dual        = vkok->v_dual;
1401:     wseq->array         = vseq->array; // Need to also update w's host array pointer
1402:   }
1403:   PetscFunctionReturn(PETSC_SUCCESS);
1404: }

1406: PetscErrorCode VecRestoreLocalVector_SeqKokkos(Vec v, Vec w)
1407: {
1408:   PetscBool isKokkos = PETSC_FALSE;

1410:   PetscFunctionBegin;
1411:   PetscCall(PetscObjectTypeCompareAny((PetscObject)(w), &isKokkos, VECSEQKOKKOS, VECMPIKOKKOS, ""));
1412:   if (!isKokkos) {
1413:     PetscScalar *a;

1415:     PetscCall(VecGetArray(w, &a));
1416:     PetscCall(VecRestoreArray(v, &a));
1417:     PetscCall(VecResetArray(w));
1418:   } else {
1419:     Vec_Kokkos *vkok = static_cast<Vec_Kokkos *>(v->spptr);
1420:     Vec_Kokkos *wkok = static_cast<Vec_Kokkos *>(w->spptr);
1421:     Vec_Seq    *wseq = static_cast<Vec_Seq *>(w->data);

1423:     vkok->v_dual = wkok->v_dual;
1424:     wkok->v_dual = wkok->unplaced_dual;
1425:     wseq->array  = wkok->v_dual.view_host().data(); // restore w's host array pointer
1426:   }
1427:   PetscFunctionReturn(PETSC_SUCCESS);
1428: }

1430: /* Maps the local portion of vector v into vector w */
1431: PetscErrorCode VecGetLocalVectorRead_SeqKokkos(Vec v, Vec w)
1432: {
1433:   PetscBool isKokkos = PETSC_FALSE;

1435:   PetscFunctionBegin;
1436:   PetscCall(PetscObjectTypeCompareAny((PetscObject)(w), &isKokkos, VECSEQKOKKOS, VECMPIKOKKOS, ""));
1437:   if (!isKokkos) {
1438:     const PetscScalar *a;

1440:     PetscCall(VecGetArrayRead(v, &a));
1441:     PetscCall(VecPlaceArray(w, a));
1442:   } else {
1443:     Vec_Kokkos *vkok = static_cast<Vec_Kokkos *>(v->spptr);
1444:     Vec_Kokkos *wkok = static_cast<Vec_Kokkos *>(w->spptr);
1445:     Vec_Seq    *vseq = static_cast<Vec_Seq *>(v->data);
1446:     Vec_Seq    *wseq = static_cast<Vec_Seq *>(w->data);

1448:     wkok->unplaced_dual = wkok->v_dual;
1449:     wkok->v_dual        = vkok->v_dual;
1450:     wseq->array         = vseq->array;
1451:   }
1452:   PetscFunctionReturn(PETSC_SUCCESS);
1453: }

1455: PetscErrorCode VecRestoreLocalVectorRead_SeqKokkos(Vec v, Vec w)
1456: {
1457:   PetscBool isKokkos = PETSC_FALSE;

1459:   PetscFunctionBegin;
1460:   PetscCall(PetscObjectTypeCompareAny((PetscObject)(w), &isKokkos, VECSEQKOKKOS, VECMPIKOKKOS, ""));
1461:   if (!isKokkos) {
1462:     const PetscScalar *a;

1464:     PetscCall(VecGetArrayRead(w, &a));
1465:     PetscCall(VecRestoreArrayRead(v, &a));
1466:     PetscCall(VecResetArray(w));
1467:   } else {
1468:     Vec_Kokkos *vkok = static_cast<Vec_Kokkos *>(v->spptr);
1469:     Vec_Kokkos *wkok = static_cast<Vec_Kokkos *>(w->spptr);
1470:     Vec_Seq    *wseq = static_cast<Vec_Seq *>(w->data);

1472:     vkok->v_dual = wkok->v_dual;
1473:     wkok->v_dual = wkok->unplaced_dual;
1474:     wseq->array  = wkok->v_dual.view_host().data();
1475:   }
1476:   PetscFunctionReturn(PETSC_SUCCESS);
1477: }

1479: PetscErrorCode VecGetArray_SeqKokkos(Vec v, PetscScalar **a)
1480: {
1481:   Vec_Kokkos *veckok = static_cast<Vec_Kokkos *>(v->spptr);

1483:   PetscFunctionBegin;
1484:   PetscCall(KokkosDualViewSyncHost(veckok->v_dual, PetscGetKokkosExecutionSpace()));
1485:   *a = *((PetscScalar **)v->data);
1486:   PetscFunctionReturn(PETSC_SUCCESS);
1487: }

1489: PetscErrorCode VecRestoreArray_SeqKokkos(Vec v, PetscScalar **)
1490: {
1491:   Vec_Kokkos *veckok = static_cast<Vec_Kokkos *>(v->spptr);

1493:   PetscFunctionBegin;
1494:   PetscCallCXX(veckok->v_dual.modify_host());
1495:   PetscFunctionReturn(PETSC_SUCCESS);
1496: }

1498: /* Get array on host to overwrite, so no need to sync host. In VecRestoreArrayWrite() we will mark host is modified. */
1499: PetscErrorCode VecGetArrayWrite_SeqKokkos(Vec v, PetscScalar **a)
1500: {
1501:   Vec_Kokkos *veckok = static_cast<Vec_Kokkos *>(v->spptr);

1503:   PetscFunctionBegin;
1504:   PetscCallCXX(veckok->v_dual.clear_sync_state());
1505:   *a = veckok->v_dual.view_host().data();
1506:   PetscFunctionReturn(PETSC_SUCCESS);
1507: }

1509: PetscErrorCode VecGetArrayAndMemType_SeqKokkos(Vec v, PetscScalar **a, PetscMemType *mtype)
1510: {
1511:   Vec_Kokkos *veckok = static_cast<Vec_Kokkos *>(v->spptr);

1513:   PetscFunctionBegin;
1514:   /* Always return up-to-date in the default memory space */
1515:   PetscCall(KokkosDualViewSyncDevice(veckok->v_dual, PetscGetKokkosExecutionSpace()));
1516:   *a = veckok->v_dual.view_device().data();
1517:   if (mtype) *mtype = PETSC_MEMTYPE_KOKKOS; // Could be PETSC_MEMTYPE_HOST when Kokkos was not configured with cuda etc.
1518:   PetscFunctionReturn(PETSC_SUCCESS);
1519: }

1521: PetscErrorCode VecRestoreArrayAndMemType_SeqKokkos(Vec v, PetscScalar **)
1522: {
1523:   Vec_Kokkos *veckok = static_cast<Vec_Kokkos *>(v->spptr);

1525:   PetscFunctionBegin;
1526:   if (PetscMemTypeHost(PETSC_MEMTYPE_KOKKOS)) PetscCallCXX(veckok->v_dual.modify_host());
1527:   else PetscCallCXX(veckok->v_dual.modify_device());
1528:   PetscFunctionReturn(PETSC_SUCCESS);
1529: }

1531: PetscErrorCode VecGetArrayWriteAndMemType_SeqKokkos(Vec v, PetscScalar **a, PetscMemType *mtype)
1532: {
1533:   Vec_Kokkos *veckok = static_cast<Vec_Kokkos *>(v->spptr);

1535:   PetscFunctionBegin;
1536:   // Since the data will be overwritten, we clear the sync state to suppress potential memory copying in sync'ing
1537:   PetscCallCXX(veckok->v_dual.clear_sync_state()); // So that in restore, we can safely modify_device()
1538:   PetscCall(KokkosDualViewSyncDevice(veckok->v_dual, PetscGetKokkosExecutionSpace()));
1539:   *a = veckok->v_dual.view_device().data();
1540:   if (mtype) *mtype = PETSC_MEMTYPE_KOKKOS; // Could be PETSC_MEMTYPE_HOST when Kokkos was not configured with cuda etc.
1541:   PetscFunctionReturn(PETSC_SUCCESS);
1542: }

1544: /* Copy xin's sync state to y */
1545: static PetscErrorCode VecCopySyncState_Kokkos_Private(Vec xin, Vec yout)
1546: {
1547:   Vec_Kokkos *xkok = static_cast<Vec_Kokkos *>(xin->spptr);
1548:   Vec_Kokkos *ykok = static_cast<Vec_Kokkos *>(yout->spptr);

1550:   PetscFunctionBegin;
1551:   PetscCallCXX(ykok->v_dual.clear_sync_state());
1552:   if (xkok->v_dual.need_sync_host()) PetscCallCXX(ykok->v_dual.modify_device());
1553:   else if (xkok->v_dual.need_sync_device()) PetscCallCXX(ykok->v_dual.modify_host());
1554:   PetscFunctionReturn(PETSC_SUCCESS);
1555: }

1557: static PetscErrorCode VecCreateSeqKokkosWithArrays_Private(MPI_Comm, PetscInt, PetscInt, const PetscScalar[], const PetscScalar[], Vec *);

1559: /* Internal routine shared by VecGetSubVector_{SeqKokkos,MPIKokkos} */
1560: PetscErrorCode VecGetSubVector_Kokkos_Private(Vec x, PetscBool xIsMPI, IS is, Vec *y)
1561: {
1562:   PetscBool contig;
1563:   PetscInt  n, N, start, bs;
1564:   MPI_Comm  comm;
1565:   Vec       z;

1567:   PetscFunctionBegin;
1568:   PetscCall(PetscObjectGetComm((PetscObject)x, &comm));
1569:   PetscCall(ISGetLocalSize(is, &n));
1570:   PetscCall(ISGetSize(is, &N));
1571:   PetscCall(VecGetSubVectorContiguityAndBS_Private(x, is, &contig, &start, &bs));

1573:   if (contig) { /* We can do a no-copy (in-place) implementation with y sharing x's arrays */
1574:     Vec_Kokkos        *xkok    = static_cast<Vec_Kokkos *>(x->spptr);
1575:     const PetscScalar *array_h = xkok->v_dual.view_host().data() + start;
1576:     const PetscScalar *array_d = xkok->v_dual.view_device().data() + start;

1578:     /* These calls assume the input arrays are synced */
1579:     if (xIsMPI) PetscCall(VecCreateMPIKokkosWithArrays_Private(comm, bs, n, N, array_h, array_d, &z)); /* x could be MPI even when x's comm size = 1 */
1580:     else PetscCall(VecCreateSeqKokkosWithArrays_Private(comm, bs, n, array_h, array_d, &z));

1582:     PetscCall(VecCopySyncState_Kokkos_Private(x, z)); /* Copy x's sync state to z */

1584:     /* This is relevant only in debug mode */
1585:     PetscInt state = 0;
1586:     PetscCall(VecLockGet(x, &state));
1587:     if (state) { /* x is either in read or read/write mode, therefore z, overlapped with x, can only be in read mode */
1588:       PetscCall(VecLockReadPush(z));
1589:     }

1591:     z->ops->placearray   = NULL; /* z's arrays can't be replaced, because z does not own them */
1592:     z->ops->replacearray = NULL;

1594:   } else { /* Have to create a VecScatter and a stand-alone vector */
1595:     PetscCall(VecGetSubVectorThroughVecScatter_Private(x, is, bs, &z));
1596:   }
1597:   *y = z;
1598:   PetscFunctionReturn(PETSC_SUCCESS);
1599: }

1601: static PetscErrorCode VecGetSubVector_SeqKokkos(Vec x, IS is, Vec *y)
1602: {
1603:   PetscFunctionBegin;
1604:   PetscCall(VecGetSubVector_Kokkos_Private(x, PETSC_FALSE, is, y));
1605:   PetscFunctionReturn(PETSC_SUCCESS);
1606: }

1608: /* Restore subvector y to x */
1609: PetscErrorCode VecRestoreSubVector_SeqKokkos(Vec x, IS, Vec *y)
1610: {
1611:   VecScatter                    vscat;
1612:   PETSC_UNUSED PetscObjectState dummystate = 0;
1613:   PetscBool                     unchanged;

1615:   PetscFunctionBegin;
1616:   PetscCall(PetscObjectComposedDataGetInt((PetscObject)*y, VecGetSubVectorSavedStateId, dummystate, unchanged));
1617:   if (unchanged) PetscFunctionReturn(PETSC_SUCCESS); /* If y's state has not changed since VecGetSubVector(), we only need to destroy it */

1619:   PetscCall(PetscObjectQuery((PetscObject)*y, "VecGetSubVector_Scatter", (PetscObject *)&vscat));
1620:   if (vscat) {
1621:     PetscCall(VecScatterBegin(vscat, *y, x, INSERT_VALUES, SCATTER_REVERSE));
1622:     PetscCall(VecScatterEnd(vscat, *y, x, INSERT_VALUES, SCATTER_REVERSE));
1623:   } else { /* y and x's (host and device) arrays overlap */
1624:     Vec_Kokkos *xkok = static_cast<Vec_Kokkos *>(x->spptr);
1625:     Vec_Kokkos *ykok = static_cast<Vec_Kokkos *>((*y)->spptr);
1626:     PetscInt    state;

1628:     PetscCall(VecLockGet(x, &state));
1629:     PetscCheck(!state, PetscObjectComm((PetscObject)x), PETSC_ERR_ARG_WRONGSTATE, "Vec x is locked for read-only or read/write access");

1631:     /* The tricky part: one has to carefully sync the arrays */
1632:     auto exec = PetscGetKokkosExecutionSpace();
1633:     if (xkok->v_dual.need_sync_device()) { /* x's host has newer data */
1634:       /* Move y's latest values to host (since y is just a subset of x) */
1635:       PetscCall(KokkosDualViewSyncHost(ykok->v_dual, exec));
1636:     } else if (xkok->v_dual.need_sync_host()) {                /* x's device has newer data */
1637:       PetscCall(KokkosDualViewSyncDevice(ykok->v_dual, exec)); /* Move y's latest data to device */
1638:     } else {                                                   /* x's host and device data is already sync'ed; Copy y's sync state to x */
1639:       PetscCall(VecCopySyncState_Kokkos_Private(*y, x));
1640:     }
1641:     PetscCall(PetscObjectStateIncrease((PetscObject)x)); /* Since x is updated */
1642:   }
1643:   PetscFunctionReturn(PETSC_SUCCESS);
1644: }

1646: static PetscErrorCode VecSetPreallocationCOO_SeqKokkos(Vec x, PetscCount ncoo, const PetscInt coo_i[])
1647: {
1648:   Vec_Seq    *vecseq = static_cast<Vec_Seq *>(x->data);
1649:   Vec_Kokkos *veckok = static_cast<Vec_Kokkos *>(x->spptr);
1650:   PetscInt    m;

1652:   PetscFunctionBegin;
1653:   PetscCall(VecSetPreallocationCOO_Seq(x, ncoo, coo_i));
1654:   PetscCall(VecGetLocalSize(x, &m));
1655:   PetscCall(veckok->SetUpCOO(vecseq, m));
1656:   PetscFunctionReturn(PETSC_SUCCESS);
1657: }

1659: static PetscErrorCode VecSetValuesCOO_SeqKokkos(Vec x, const PetscScalar v[], InsertMode imode)
1660: {
1661:   Vec_Seq                    *vecseq = static_cast<Vec_Seq *>(x->data);
1662:   Vec_Kokkos                 *veckok = static_cast<Vec_Kokkos *>(x->spptr);
1663:   const PetscCountKokkosView &jmap1  = veckok->jmap1_d;
1664:   const PetscCountKokkosView &perm1  = veckok->perm1_d;
1665:   PetscScalarKokkosView       xv; /* View for vector x */
1666:   ConstPetscScalarKokkosView  vv; /* View for array v[] */
1667:   PetscInt                    m;
1668:   PetscMemType                memtype;

1670:   PetscFunctionBegin;
1671:   PetscCall(VecGetLocalSize(x, &m));
1672:   PetscCall(PetscGetMemType(v, &memtype));
1673:   if (PetscMemTypeHost(memtype)) { /* If user gave v[] in host, we might need to copy it to device if any */
1674:     PetscCallCXX(vv = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(), ConstPetscScalarKokkosViewHost(v, vecseq->coo_n)));
1675:     PetscCall(PetscLogCpuToGpu(vecseq->coo_n * sizeof(PetscScalar)));
1676:   } else {
1677:     PetscCallCXX(vv = ConstPetscScalarKokkosView(v, vecseq->coo_n)); /* Directly use v[]'s memory */
1678:   }

1680:   if (imode == INSERT_VALUES) PetscCall(VecGetKokkosViewWrite(x, &xv)); /* write vector */
1681:   else PetscCall(VecGetKokkosView(x, &xv));                             /* read & write vector */

1683:   PetscCallCXX(Kokkos::parallel_for(
1684:     Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, m), KOKKOS_LAMBDA(const PetscInt &i) {
1685:       PetscScalar sum = 0.0;
1686:       for (PetscCount k = jmap1(i); k < jmap1(i + 1); k++) sum += vv(perm1(k));
1687:       xv(i) = (imode == INSERT_VALUES ? 0.0 : xv(i)) + sum;
1688:     }));

1690:   if (imode == INSERT_VALUES) PetscCall(VecRestoreKokkosViewWrite(x, &xv));
1691:   else PetscCall(VecRestoreKokkosView(x, &xv));
1692:   PetscFunctionReturn(PETSC_SUCCESS);
1693: }

1695: static PetscErrorCode VecCreate_SeqKokkos_Common(Vec); // forward declaration

1697: /* Duplicate layout etc but not the values in the input vector */
1698: static PetscErrorCode VecDuplicate_SeqKokkos(Vec win, Vec *v)
1699: {
1700:   PetscFunctionBegin;
1701:   PetscCall(VecDuplicate_Seq(win, v)); /* It also dups ops of win */
1702:   PetscCall(VecCreate_SeqKokkos_Common(*v));
1703:   (*v)->ops[0] = win->ops[0]; // recover the ops[]. We need to always follow ops in win
1704:   PetscFunctionReturn(PETSC_SUCCESS);
1705: }

1707: static PetscErrorCode VecDestroy_SeqKokkos(Vec v)
1708: {
1709:   Vec_Kokkos *veckok = static_cast<Vec_Kokkos *>(v->spptr);
1710:   Vec_Seq    *vecseq = static_cast<Vec_Seq *>(v->data);

1712:   PetscFunctionBegin;
1713:   delete veckok;
1714:   v->spptr = NULL;
1715:   if (vecseq) PetscCall(VecDestroy_Seq(v));
1716:   PetscFunctionReturn(PETSC_SUCCESS);
1717: }

1719: PETSC_INTERN PetscErrorCode VecBindToCPU_SeqKokkos(Vec v, PetscBool flg)
1720: {
1721:   PetscFunctionBegin;
1722:   PetscCheck(v->boundtocpu == flg, PetscObjectComm((PetscObject)v), PETSC_ERR_SUP, "Changing binding of a VECKOKKOS vector is not supported yet");
1723:   PetscFunctionReturn(PETSC_SUCCESS);
1724: }

1726: // Shared by all VecCreate/Duplicate routines for VecSeqKokkos
1727: static PetscErrorCode VecCreate_SeqKokkos_Common(Vec v)
1728: {
1729:   PetscFunctionBegin;
1730:   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.
1731:   v->ops->bindtocpu       = VecBindToCPU_SeqKokkos;
1732:   v->ops->abs             = VecAbs_SeqKokkos;
1733:   v->ops->reciprocal      = VecReciprocal_SeqKokkos;
1734:   v->ops->sqrt            = VecSqrtAbs_SeqKokkos;
1735:   v->ops->pointwisemult   = VecPointwiseMult_SeqKokkos;
1736:   v->ops->min             = VecMin_SeqKokkos;
1737:   v->ops->max             = VecMax_SeqKokkos;
1738:   v->ops->sum             = VecSum_SeqKokkos;
1739:   v->ops->shift           = VecShift_SeqKokkos;
1740:   v->ops->norm            = VecNorm_SeqKokkos;
1741:   v->ops->scale           = VecScale_SeqKokkos;
1742:   v->ops->copy            = VecCopy_SeqKokkos;
1743:   v->ops->set             = VecSet_SeqKokkos;
1744:   v->ops->swap            = VecSwap_SeqKokkos;
1745:   v->ops->axpy            = VecAXPY_SeqKokkos;
1746:   v->ops->axpby           = VecAXPBY_SeqKokkos;
1747:   v->ops->axpbypcz        = VecAXPBYPCZ_SeqKokkos;
1748:   v->ops->pointwisedivide = VecPointwiseDivide_SeqKokkos;
1749:   v->ops->setrandom       = VecSetRandom_SeqKokkos;

1751:   v->ops->dot   = VecDot_SeqKokkos;
1752:   v->ops->tdot  = VecTDot_SeqKokkos;
1753:   v->ops->mdot  = VecMDot_SeqKokkos;
1754:   v->ops->mtdot = VecMTDot_SeqKokkos;

1756:   v->ops->dot_local   = VecDot_SeqKokkos;
1757:   v->ops->tdot_local  = VecTDot_SeqKokkos;
1758:   v->ops->mdot_local  = VecMDot_SeqKokkos;
1759:   v->ops->mtdot_local = VecMTDot_SeqKokkos;

1761:   v->ops->norm_local             = VecNorm_SeqKokkos;
1762:   v->ops->maxpy                  = VecMAXPY_SeqKokkos;
1763:   v->ops->aypx                   = VecAYPX_SeqKokkos;
1764:   v->ops->waxpy                  = VecWAXPY_SeqKokkos;
1765:   v->ops->dotnorm2               = VecDotNorm2_SeqKokkos;
1766:   v->ops->errorwnorm             = VecErrorWeightedNorms_SeqKokkos;
1767:   v->ops->placearray             = VecPlaceArray_SeqKokkos;
1768:   v->ops->replacearray           = VecReplaceArray_SeqKokkos;
1769:   v->ops->resetarray             = VecResetArray_SeqKokkos;
1770:   v->ops->destroy                = VecDestroy_SeqKokkos;
1771:   v->ops->duplicate              = VecDuplicate_SeqKokkos;
1772:   v->ops->conjugate              = VecConjugate_SeqKokkos;
1773:   v->ops->getlocalvector         = VecGetLocalVector_SeqKokkos;
1774:   v->ops->restorelocalvector     = VecRestoreLocalVector_SeqKokkos;
1775:   v->ops->getlocalvectorread     = VecGetLocalVectorRead_SeqKokkos;
1776:   v->ops->restorelocalvectorread = VecRestoreLocalVectorRead_SeqKokkos;
1777:   v->ops->getarraywrite          = VecGetArrayWrite_SeqKokkos;
1778:   v->ops->getarray               = VecGetArray_SeqKokkos;
1779:   v->ops->restorearray           = VecRestoreArray_SeqKokkos;

1781:   v->ops->getarrayandmemtype      = VecGetArrayAndMemType_SeqKokkos;
1782:   v->ops->restorearrayandmemtype  = VecRestoreArrayAndMemType_SeqKokkos;
1783:   v->ops->getarraywriteandmemtype = VecGetArrayWriteAndMemType_SeqKokkos;
1784:   v->ops->getsubvector            = VecGetSubVector_SeqKokkos;
1785:   v->ops->restoresubvector        = VecRestoreSubVector_SeqKokkos;

1787:   v->ops->setpreallocationcoo = VecSetPreallocationCOO_SeqKokkos;
1788:   v->ops->setvaluescoo        = VecSetValuesCOO_SeqKokkos;

1790:   v->offloadmask = PETSC_OFFLOAD_KOKKOS; // Mark this is a VECKOKKOS; We use this flag for cheap VECKOKKOS test.
1791:   PetscFunctionReturn(PETSC_SUCCESS);
1792: }

1794: /*@
1795:   VecCreateSeqKokkosWithArray - Creates a Kokkos sequential array-style vector,
1796:   where the user provides the array space to store the vector values. The array
1797:   provided must be a device array.

1799:   Collective

1801:   Input Parameters:
1802: + comm   - the communicator, should be PETSC_COMM_SELF
1803: . bs     - the block size
1804: . n      - the vector length
1805: - darray - device memory where the vector elements are to be stored.

1807:   Output Parameter:
1808: . v - the vector

1810:   Notes:
1811:   Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the
1812:   same type as an existing vector.

1814:   If `darray` is NULL, the vector is created without any array, on host or device;
1815:   `VecKokkosPlaceArray()` (with a device array) or `VecPlaceArray()` (with a host array)
1816:   must then be called before the vector is used.

1818:   PETSc does NOT free the array when the vector is destroyed via VecDestroy().
1819:   The user should not free the array until the vector is destroyed.

1821:   Level: intermediate

1823: .seealso: `VecCreateMPICUDAWithArray()`, `VecCreate()`, `VecDuplicate()`, `VecDuplicateVecs()`,
1824:           `VecCreateGhost()`, `VecCreateSeq()`, `VecCreateSeqWithArray()`,
1825:           `VecCreateMPIWithArray()`, `VecKokkosPlaceArray()`, `VecPlaceArray()`
1826: @*/
1827: PetscErrorCode VecCreateSeqKokkosWithArray(MPI_Comm comm, PetscInt bs, PetscInt n, const PetscScalar darray[], Vec *v)
1828: {
1829:   PetscMPIInt  size;
1830:   Vec          w;
1831:   Vec_Kokkos  *veckok = NULL;
1832:   PetscScalar *harray;

1834:   PetscFunctionBegin;
1835:   PetscCallMPI(MPI_Comm_size(comm, &size));
1836:   PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot create VECSEQKOKKOS on more than one process");

1838:   PetscCall(PetscKokkosInitializeCheck());
1839:   PetscCall(VecCreate(comm, &w));
1840:   PetscCall(VecSetSizes(w, n, n));
1841:   PetscCall(VecSetBlockSize(w, bs));
1842:   /* Build a VECSEQ, get its harray, and then build Vec_Kokkos along with darray */
1843:   if (std::is_same<DefaultMemorySpace, HostMirrorMemorySpace>::value || !darray) {
1844:     harray = const_cast<PetscScalar *>(darray);  /* With a NULL darray, the vector is array-less until an array is placed */
1845:     PetscCall(VecCreate_Seq_Private(w, harray)); /* Build a sequential vector with harray */
1846:   } else {
1847:     PetscCall(VecSetType(w, VECSEQ));
1848:     harray = static_cast<Vec_Seq *>(w->data)->array;
1849:   }
1850:   PetscCall(PetscObjectChangeTypeName((PetscObject)w, VECSEQKOKKOS)); /* Change it to Kokkos */
1851:   PetscCall(VecCreate_SeqKokkos_Common(w));
1852:   PetscCallCXX(veckok = new Vec_Kokkos{n, harray, const_cast<PetscScalar *>(darray)});
1853:   if (darray) PetscCallCXX(veckok->v_dual.modify_device()); /* Mark the device is modified */
1854:   w->spptr = static_cast<void *>(veckok);
1855:   *v       = w;
1856:   PetscFunctionReturn(PETSC_SUCCESS);
1857: }

1859: // Convert VECSEQ to VECSEQKOKKOS. One could provide a non-NULL array_d[] for memory on device. If that is the case, we assume
1860: // array_d[] is already synced with the host array. If array_d is NULL, then PETSc will allocate the device memory.
1861: PetscErrorCode VecConvert_Seq_SeqKokkos_inplace(Vec v, PetscScalar *array_d)
1862: {
1863:   Vec_Seq *vecseq;

1865:   PetscFunctionBegin;
1866:   PetscCall(PetscKokkosInitializeCheck());
1867:   PetscCall(PetscLayoutSetUp(v->map));
1868:   PetscCall(PetscObjectChangeTypeName((PetscObject)v, VECSEQKOKKOS));
1869:   PetscCall(VecCreate_SeqKokkos_Common(v));
1870:   PetscCheck(!v->spptr, PETSC_COMM_SELF, PETSC_ERR_PLIB, "v->spptr not NULL");
1871:   vecseq = static_cast<Vec_Seq *>(v->data);
1872:   PetscCallCXX(v->spptr = new Vec_Kokkos(v->map->n, vecseq->array, array_d));
1873:   PetscFunctionReturn(PETSC_SUCCESS);
1874: }

1876: // Create a VECSEQKOKKOS with layout and arrays
1877: static PetscErrorCode VecCreateSeqKokkosWithLayoutAndArrays_Private(PetscLayout map, const PetscScalar harray[], const PetscScalar darray[], Vec *v)
1878: {
1879:   Vec w;

1881:   PetscFunctionBegin;
1882:   if (map->n > 0) PetscCheck(darray, map->comm, PETSC_ERR_ARG_WRONG, "darray cannot be NULL");
1883: #if defined(KOKKOS_ENABLE_UNIFIED_MEMORY)
1884:   PetscCheck(harray == darray, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "harray and darray must be the same");
1885: #endif
1886:   PetscCall(VecCreateSeqWithLayoutAndArray_Private(map, harray, &w));
1887:   PetscCall(PetscObjectChangeTypeName((PetscObject)w, VECSEQKOKKOS)); // Change it to VECKOKKOS
1888:   PetscCall(VecCreate_SeqKokkos_Common(w));
1889:   PetscCallCXX(w->spptr = new Vec_Kokkos(map->n, const_cast<PetscScalar *>(harray), const_cast<PetscScalar *>(darray)));
1890:   *v = w;
1891:   PetscFunctionReturn(PETSC_SUCCESS);
1892: }

1894: /*
1895:    VecCreateSeqKokkosWithArrays_Private - Creates a Kokkos sequential array-style vector
1896:    with user-provided arrays on host and device.

1898:    Collective

1900:    Input Parameter:
1901: +  comm - the communicator, should be PETSC_COMM_SELF
1902: .  bs - the block size
1903: .  n - the vector length
1904: .  harray - host memory where the vector elements are to be stored.
1905: -  darray - device memory where the vector elements are to be stored.

1907:    Output Parameter:
1908: .  v - the vector

1910:    Notes:
1911:    Unlike VecCreate{Seq,MPI}CUDAWithArrays(), this routine is private since we do not expect users to use it directly.

1913:    If there is no device, then harray and darray must be the same.
1914:    If n is not zero, then harray and darray must be allocated.
1915:    After the call, the created vector is supposed to be in a synchronized state, i.e.,
1916:    we suppose harray and darray have the same data.

1918:    PETSc does NOT free the array when the vector is destroyed via VecDestroy().
1919:    Caller should not free the array until the vector is destroyed.
1920: */
1921: static PetscErrorCode VecCreateSeqKokkosWithArrays_Private(MPI_Comm comm, PetscInt bs, PetscInt n, const PetscScalar harray[], const PetscScalar darray[], Vec *v)
1922: {
1923:   PetscMPIInt size;
1924:   PetscLayout map;

1926:   PetscFunctionBegin;
1927:   PetscCall(PetscKokkosInitializeCheck());
1928:   PetscCallMPI(MPI_Comm_size(comm, &size));
1929:   PetscCheck(size == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot create VECSEQKOKKOS on more than one process");
1930:   PetscCall(PetscLayoutCreateFromSizes(comm, n, n, bs, &map));
1931:   PetscCall(VecCreateSeqKokkosWithLayoutAndArrays_Private(map, harray, darray, v));
1932:   PetscCall(PetscLayoutDestroy(&map));
1933:   PetscFunctionReturn(PETSC_SUCCESS);
1934: }

1936: /* TODO: ftn-auto generates veckok.kokkosf.c */
1937: /*@
1938:   VecCreateSeqKokkos - Creates a standard, sequential array-style vector.

1940:   Collective

1942:   Input Parameters:
1943: + comm - the communicator, should be `PETSC_COMM_SELF`
1944: - n    - the vector length

1946:   Output Parameter:
1947: . v - the vector

1949:   Notes:
1950:   Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the
1951:   same type as an existing vector.

1953:   Level: intermediate

1955: .seealso: `VecCreateMPI()`, `VecCreate()`, `VecDuplicate()`, `VecDuplicateVecs()`, `VecCreateGhost()`
1956:  @*/
1957: PetscErrorCode VecCreateSeqKokkos(MPI_Comm comm, PetscInt n, Vec *v)
1958: {
1959:   PetscFunctionBegin;
1960:   PetscCall(PetscKokkosInitializeCheck());
1961:   PetscCall(VecCreate(comm, v));
1962:   PetscCall(VecSetSizes(*v, n, n));
1963:   PetscCall(VecSetType(*v, VECSEQKOKKOS)); /* Calls VecCreate_SeqKokkos */
1964:   PetscFunctionReturn(PETSC_SUCCESS);
1965: }

1967: // Duplicate a VECSEQKOKKOS
1968: static PetscErrorCode VecDuplicateVecs_SeqKokkos_GEMV(Vec w, PetscInt m, Vec *V[])
1969: {
1970:   PetscInt64                lda; // use 64-bit as we will do "m * lda"
1971:   PetscScalar              *array_h, *array_d;
1972:   PetscLayout               map;
1973:   PetscScalarKokkosDualView w_dual;

1975:   PetscFunctionBegin;
1976:   PetscCall(PetscKokkosInitializeCheck()); // as we'll call kokkos_malloc()
1977:   PetscCall(PetscMalloc1(m, V));
1978:   PetscCall(VecGetLayout(w, &map));
1979:   VecGetLocalSizeAligned(w, 64, &lda); // get in lda the 64-bytes aligned local size
1980:   // See comments in VecCreate_SeqKokkos() on why we use DualView to allocate the memory
1981:   PetscCallCXX(w_dual = PetscScalarKokkosDualView("VecDuplicateVecs", m * lda)); // Kokkos init's w_dual to zero

1983:   // create the m vectors with raw arrays from v_dual
1984:   array_h = w_dual.view_host().data();
1985:   array_d = w_dual.view_device().data();
1986:   for (PetscInt i = 0; i < m; i++) {
1987:     Vec v;
1988:     PetscCall(VecCreateSeqKokkosWithLayoutAndArrays_Private(map, &array_h[i * lda], &array_d[i * lda], &v));
1989:     PetscCall(PetscObjectListDuplicate(((PetscObject)w)->olist, &((PetscObject)v)->olist));
1990:     PetscCall(PetscFunctionListDuplicate(((PetscObject)w)->qlist, &((PetscObject)v)->qlist));
1991:     v->ops[0] = w->ops[0];
1992:     (*V)[i]   = v;
1993:   }

1995:   // let the first vector own the long DualView, so when it is destroyed it will free the v_dual
1996:   if (m) {
1997:     Vec v = (*V)[0];

1999:     static_cast<Vec_Kokkos *>(v->spptr)->w_dual = w_dual; // stash the memory
2000:     // disable replacearray of the first vector, as freeing its memory also frees others in the group.
2001:     // But replacearray of others is ok, as they don't own their array.
2002:     if (m > 1) v->ops->replacearray = VecReplaceArray_Default_GEMV_Error;
2003:   }
2004:   PetscFunctionReturn(PETSC_SUCCESS);
2005: }

2007: /*MC
2008:    VECSEQKOKKOS - VECSEQKOKKOS = "seqkokkos" - The basic sequential vector, modified to use Kokkos

2010:    Options Database Keys:
2011: . -vec_type seqkokkos - sets the vector type to VECSEQKOKKOS during a call to VecSetFromOptions()

2013:   Level: beginner

2015: .seealso: `VecCreate()`, `VecSetType()`, `VecSetFromOptions()`, `VecCreateMPIWithArray()`, `VECMPI`, `VecType`, `VecCreateMPI()`
2016: M*/
2017: PetscErrorCode VecCreate_SeqKokkos(Vec v)
2018: {
2019:   PetscBool                 mdot_use_gemv  = PETSC_TRUE;
2020:   PetscBool                 maxpy_use_gemv = PETSC_FALSE; // default is false as we saw bad performance with vendors' GEMV with tall skinny matrices.
2021:   PetscScalarKokkosDualView v_dual;

2023:   PetscFunctionBegin;
2024:   PetscCall(PetscKokkosInitializeCheck());
2025:   PetscCall(PetscLayoutSetUp(v->map));

2027:   // Use DualView to allocate both the host array and the device array.
2028:   // DualView first allocates the device array and then mirrors it to host.
2029:   // With unified memory (e.g., on AMD MI300A APU), the two arrays are the same, with the host array
2030:   // sharing the device array allocated by hipMalloc(), but not the other way around if we call
2031:   // VecCreate_Seq() first and let the device array share the host array allocated by malloc().
2032:   // hipMalloc() has an advantage over malloc() as it gives great binding and page size settings automatically, see
2033:   // https://hpc.llnl.gov/documentation/user-guides/using-el-capitan-systems/introduction-and-quickstart/pro-tips
2034:   PetscCallCXX(v_dual = PetscScalarKokkosDualView("v_dual", v->map->n)); // Kokkos init's v_dual to zero

2036:   PetscCall(VecCreate_Seq_Private(v, v_dual.view_host().data()));
2037:   PetscCall(PetscObjectChangeTypeName((PetscObject)v, VECSEQKOKKOS));
2038:   PetscCall(VecCreate_SeqKokkos_Common(v));
2039:   PetscCheck(!v->spptr, PETSC_COMM_SELF, PETSC_ERR_PLIB, "v->spptr not NULL");
2040:   PetscCallCXX(v->spptr = new Vec_Kokkos(v_dual));

2042:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-vec_mdot_use_gemv", &mdot_use_gemv, NULL));
2043:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-vec_maxpy_use_gemv", &maxpy_use_gemv, NULL));

2045:   // allocate multiple vectors together
2046:   if (mdot_use_gemv || maxpy_use_gemv) v->ops[0].duplicatevecs = VecDuplicateVecs_SeqKokkos_GEMV;

2048:   if (mdot_use_gemv) {
2049:     v->ops[0].mdot        = VecMDot_SeqKokkos_GEMV;
2050:     v->ops[0].mtdot       = VecMTDot_SeqKokkos_GEMV;
2051:     v->ops[0].mdot_local  = VecMDot_SeqKokkos_GEMV;
2052:     v->ops[0].mtdot_local = VecMTDot_SeqKokkos_GEMV;
2053:   }
2054:   if (maxpy_use_gemv) v->ops[0].maxpy = VecMAXPY_SeqKokkos_GEMV;
2055:   PetscFunctionReturn(PETSC_SUCCESS);
2056: }