Actual source code: matdensecupmimpl.h

  1: #pragma once

  3: #define PETSC_SKIP_IMMINTRIN_H_CUDAWORKAROUND 1
  4: #include <petsc/private/matimpl.h>

  6: #if defined(__cplusplus)
  7: #include <petsc/private/deviceimpl.h>
  8: #include <petsc/private/cupmsolverinterface.hpp>
  9: #include <petsc/private/cupmobject.hpp>

 11:   #include "../src/sys/objects/device/impls/cupm/cupmthrustutility.hpp"
 12:   #include "../src/sys/objects/device/impls/cupm/kernels.hpp"

 14:   #include <thrust/device_vector.h>
 15:   #include <thrust/device_ptr.h>
 16:   #include <thrust/iterator/counting_iterator.h>
 17:   #include <thrust/iterator/transform_iterator.h>
 18:   #include <thrust/iterator/permutation_iterator.h>
 19:   #include <thrust/transform.h>
 20:   #include <thrust/copy.h>

 22: namespace Petsc
 23: {

 25: namespace vec
 26: {

 28: namespace cupm
 29: {

 31: namespace impl
 32: {

 34: template <device::cupm::DeviceType>
 35: class VecSeq_CUPM;
 36: template <device::cupm::DeviceType>
 37: class VecMPI_CUPM;

 39: } // namespace impl

 41: } // namespace cupm

 43: } // namespace vec

 45: namespace mat
 46: {

 48: namespace cupm
 49: {

 51: namespace impl
 52: {

 54: // ==========================================================================================
 55: // MatDense_CUPM_Base
 56: //
 57: // A base class to separate out the CRTP code from the common CUPM stuff (like the composed
 58: // function names).
 59: // ==========================================================================================

 61: template <device::cupm::DeviceType T>
 62: class PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL MatDense_CUPM_Base : protected device::cupm::impl::CUPMObject<T> {
 63: public:
 64:   PETSC_CUPMOBJECT_HEADER(T);

 66:   #define MatDenseCUPMComposedOpDecl(OP_NAME) \
 67:     PETSC_NODISCARD static constexpr const char *PetscConcat(MatDenseCUPM, OP_NAME)() noexcept \
 68:     { \
 69:       return T == device::cupm::DeviceType::CUDA ? PetscStringize(PetscConcat(MatDenseCUDA, OP_NAME)) : PetscStringize(PetscConcat(MatDenseHIP, OP_NAME)); \
 70:     }

 72:   // clang-format off
 73:   MatDenseCUPMComposedOpDecl(GetArray_C)
 74:   MatDenseCUPMComposedOpDecl(GetArrayRead_C)
 75:   MatDenseCUPMComposedOpDecl(GetArrayWrite_C)
 76:   MatDenseCUPMComposedOpDecl(RestoreArray_C)
 77:   MatDenseCUPMComposedOpDecl(RestoreArrayRead_C)
 78:   MatDenseCUPMComposedOpDecl(RestoreArrayWrite_C)
 79:   MatDenseCUPMComposedOpDecl(PlaceArray_C)
 80:   MatDenseCUPMComposedOpDecl(ReplaceArray_C)
 81:   MatDenseCUPMComposedOpDecl(ResetArray_C)
 82:   MatDenseCUPMComposedOpDecl(SetPreallocation_C)
 83:   // clang-format on

 85:   #undef MatDenseCUPMComposedOpDecl

 87:     PETSC_NODISCARD static constexpr MatType MATSEQDENSECUPM() noexcept;
 88:   PETSC_NODISCARD static constexpr MatType       MATMPIDENSECUPM() noexcept;
 89:   PETSC_NODISCARD static constexpr MatType       MATDENSECUPM() noexcept;
 90:   PETSC_NODISCARD static constexpr MatSolverType MATSOLVERCUPM() noexcept;

 92:   static PetscErrorCode GetCurrentMemType(Mat A, PetscMemType *mtype) noexcept
 93:   {
 94:     PetscFunctionBegin;
 95:     *mtype = A->boundtocpu ? PETSC_MEMTYPE_HOST : PETSC_MEMTYPE_CUPM();
 96:     PetscFunctionReturn(PETSC_SUCCESS);
 97:   }
 98: };

100: // ==========================================================================================
101: // MatDense_CUPM_Base -- Public API
102: // ==========================================================================================

104: template <device::cupm::DeviceType T>
105: inline constexpr MatType MatDense_CUPM_Base<T>::MATSEQDENSECUPM() noexcept
106: {
107:   return T == device::cupm::DeviceType::CUDA ? MATSEQDENSECUDA : MATSEQDENSEHIP;
108: }

110: template <device::cupm::DeviceType T>
111: inline constexpr MatType MatDense_CUPM_Base<T>::MATMPIDENSECUPM() noexcept
112: {
113:   return T == device::cupm::DeviceType::CUDA ? MATMPIDENSECUDA : MATMPIDENSEHIP;
114: }

116: template <device::cupm::DeviceType T>
117: inline constexpr MatType MatDense_CUPM_Base<T>::MATDENSECUPM() noexcept
118: {
119:   return T == device::cupm::DeviceType::CUDA ? MATDENSECUDA : MATDENSEHIP;
120: }

122: template <device::cupm::DeviceType T>
123: inline constexpr MatSolverType MatDense_CUPM_Base<T>::MATSOLVERCUPM() noexcept
124: {
125:   return T == device::cupm::DeviceType::CUDA ? MATSOLVERCUDA : MATSOLVERHIP;
126: }

128:   #define MATDENSECUPM_BASE_HEADER(T) \
129:     PETSC_CUPMOBJECT_HEADER(T); \
130:     using VecSeq_CUPM = ::Petsc::vec::cupm::impl::VecSeq_CUPM<T>; \
131:     using VecMPI_CUPM = ::Petsc::vec::cupm::impl::VecMPI_CUPM<T>; \
132:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MATSEQDENSECUPM; \
133:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MATMPIDENSECUPM; \
134:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MATDENSECUPM; \
135:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MATSOLVERCUPM; \
136:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::GetCurrentMemType; \
137:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MatDenseCUPMGetArray_C; \
138:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MatDenseCUPMGetArrayRead_C; \
139:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MatDenseCUPMGetArrayWrite_C; \
140:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MatDenseCUPMRestoreArray_C; \
141:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MatDenseCUPMRestoreArrayRead_C; \
142:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MatDenseCUPMRestoreArrayWrite_C; \
143:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MatDenseCUPMPlaceArray_C; \
144:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MatDenseCUPMReplaceArray_C; \
145:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MatDenseCUPMResetArray_C; \
146:     using ::Petsc::mat::cupm::impl::MatDense_CUPM_Base<T>::MatDenseCUPMSetPreallocation_C

148: // forward declare
149: template <device::cupm::DeviceType>
150: class MatDense_Seq_CUPM;
151: template <device::cupm::DeviceType>
152: class MatDense_MPI_CUPM;

154: // ==========================================================================================
155: // MatDense_CUPM
156: //
157: // The true "base" class for MatDenseCUPM. The reason MatDense_CUPM and MatDense_CUPM_Base
158: // exist is to separate out the CRTP code from the non-crtp code so that the generic functions
159: // can be called via templates below.
160: // ==========================================================================================

162: template <device::cupm::DeviceType T, typename Derived>
163: class PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL MatDense_CUPM : protected MatDense_CUPM_Base<T> {
164: private:
165:   static PetscErrorCode CheckSaneSequentialMatSizes_(Mat) noexcept;

167: protected:
168:   MATDENSECUPM_BASE_HEADER(T);

170:   template <PetscMemType, PetscMemoryAccessMode>
171:   class MatrixArray;

173:   // Cast the Mat to its host struct, i.e. return the result of (Mat_SeqDense *)m->data
174:   template <typename U = Derived>
175:   PETSC_NODISCARD static constexpr auto    MatIMPLCast(Mat m) noexcept PETSC_DECLTYPE_AUTO_RETURNS(U::MatIMPLCast_(m))
176:   PETSC_NODISCARD static constexpr MatType MATIMPLCUPM() noexcept;

178:   static PetscErrorCode CreateIMPLDenseCUPM(MPI_Comm, PetscInt, PetscInt, PetscInt, PetscInt, PetscScalar *, Mat *, PetscDeviceContext, bool) noexcept;
179:   static PetscErrorCode SetPreallocation(Mat, PetscDeviceContext, PetscScalar *) noexcept;

181:   template <typename F>
182:   static PetscErrorCode DiagonalUnaryTransform(Mat, PetscDeviceContext, F &&) noexcept;

184:   static PetscErrorCode Shift(Mat, PetscScalar) noexcept;
185:   static PetscErrorCode GetDiagonal(Mat, Vec) noexcept;

187:   PETSC_NODISCARD static auto DeviceArrayRead(PetscDeviceContext dctx, Mat m) noexcept PETSC_DECLTYPE_AUTO_RETURNS(MatrixArray<PETSC_MEMTYPE_DEVICE, PETSC_MEMORY_ACCESS_READ>{dctx, m})
188:   PETSC_NODISCARD static auto DeviceArrayWrite(PetscDeviceContext dctx, Mat m) noexcept PETSC_DECLTYPE_AUTO_RETURNS(MatrixArray<PETSC_MEMTYPE_DEVICE, PETSC_MEMORY_ACCESS_WRITE>{dctx, m})
189:   PETSC_NODISCARD static auto DeviceArrayReadWrite(PetscDeviceContext dctx, Mat m) noexcept PETSC_DECLTYPE_AUTO_RETURNS(MatrixArray<PETSC_MEMTYPE_DEVICE, PETSC_MEMORY_ACCESS_READ_WRITE>{dctx, m})
190:   PETSC_NODISCARD static auto HostArrayRead(PetscDeviceContext dctx, Mat m) noexcept PETSC_DECLTYPE_AUTO_RETURNS(MatrixArray<PETSC_MEMTYPE_HOST, PETSC_MEMORY_ACCESS_READ>{dctx, m})
191:   PETSC_NODISCARD static auto HostArrayWrite(PetscDeviceContext dctx, Mat m) noexcept PETSC_DECLTYPE_AUTO_RETURNS(MatrixArray<PETSC_MEMTYPE_HOST, PETSC_MEMORY_ACCESS_WRITE>{dctx, m})
192:   PETSC_NODISCARD static auto HostArrayReadWrite(PetscDeviceContext dctx, Mat m) noexcept PETSC_DECLTYPE_AUTO_RETURNS(MatrixArray<PETSC_MEMTYPE_HOST, PETSC_MEMORY_ACCESS_READ_WRITE>{dctx, m})
193: };

195: // ==========================================================================================
196: // MatDense_CUPM::MatrixArray
197: // ==========================================================================================

199: template <device::cupm::DeviceType T, typename D>
200: template <PetscMemType MT, PetscMemoryAccessMode MA>
201: class PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL MatDense_CUPM<T, D>::MatrixArray : public device::cupm::impl::RestoreableArray<T, MT, MA> {
202:   using base_type = device::cupm::impl::RestoreableArray<T, MT, MA>;

204: public:
205:   MatrixArray(PetscDeviceContext, Mat) noexcept;
206:   ~MatrixArray() noexcept;

208:   // must declare move constructor since we declare a destructor
209:   constexpr MatrixArray(MatrixArray &&) noexcept;

211: private:
212:   Mat m_ = nullptr;
213: };

215: // ==========================================================================================
216: // MatDense_CUPM::MatrixArray -- Public API
217: // ==========================================================================================

219: template <device::cupm::DeviceType T, typename D>
220: template <PetscMemType MT, PetscMemoryAccessMode MA>
221: inline MatDense_CUPM<T, D>::MatrixArray<MT, MA>::MatrixArray(PetscDeviceContext dctx, Mat m) noexcept : base_type{dctx}, m_{m}
222: {
223:   PetscFunctionBegin;
224:   PetscCallAbort(PETSC_COMM_SELF, D::template GetArray<MT, MA>(m, &this->ptr_, dctx));
225:   PetscFunctionReturnVoid();
226: }

228: template <device::cupm::DeviceType T, typename D>
229: template <PetscMemType MT, PetscMemoryAccessMode MA>
230: inline MatDense_CUPM<T, D>::MatrixArray<MT, MA>::~MatrixArray() noexcept
231: {
232:   PetscFunctionBegin;
233:   PetscCallAbort(PETSC_COMM_SELF, D::template RestoreArray<MT, MA>(m_, &this->ptr_, this->dctx_));
234:   PetscFunctionReturnVoid();
235: }

237: template <device::cupm::DeviceType T, typename D>
238: template <PetscMemType MT, PetscMemoryAccessMode MA>
239: inline constexpr MatDense_CUPM<T, D>::MatrixArray<MT, MA>::MatrixArray(MatrixArray &&other) noexcept : base_type{std::move(other)}, m_{util::exchange(other.m_, nullptr)}
240: {
241: }

243: // ==========================================================================================
244: // MatDense_CUPM -- Private API
245: // ==========================================================================================

247: template <device::cupm::DeviceType T, typename D>
248: inline PetscErrorCode MatDense_CUPM<T, D>::CheckSaneSequentialMatSizes_(Mat A) noexcept
249: {
250:   PetscFunctionBegin;
251:   if (PetscDefined(USE_DEBUG)) {
252:     PetscBool isseq;

254:     PetscCall(PetscObjectTypeCompare(PetscObjectCast(A), D::MATSEQDENSECUPM(), &isseq));
255:     if (isseq) {
256:       // doing this check allows both sequential and parallel implementations to just pass in
257:       // A, otherwise they would need to specify rstart, rend, and cols separately!
258:       PetscCheck(A->rmap->rstart == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Sequential matrix row start %" PetscInt_FMT " != 0?", A->rmap->rstart);
259:       PetscCheck(A->rmap->rend == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Sequential matrix row end %" PetscInt_FMT " != number of rows %" PetscInt_FMT, A->rmap->rend, A->rmap->n);
260:       PetscCheck(A->cmap->n == A->cmap->N, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Sequential matrix number of local columns %" PetscInt_FMT " != number of global columns %" PetscInt_FMT, A->cmap->n, A->cmap->n);
261:     }
262:   }
263:   PetscFunctionReturn(PETSC_SUCCESS);
264: }

266: // ==========================================================================================
267: // MatDense_CUPM -- Protected API
268: // ==========================================================================================

270: template <device::cupm::DeviceType T, typename D>
271: inline constexpr MatType MatDense_CUPM<T, D>::MATIMPLCUPM() noexcept
272: {
273:   return D::MATIMPLCUPM_();
274: }

276: // Common core for MatCreateSeqDenseCUPM() and MatCreateMPIDenseCUPM()
277: template <device::cupm::DeviceType T, typename D>
278: inline PetscErrorCode MatDense_CUPM<T, D>::CreateIMPLDenseCUPM(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt M, PetscInt N, PetscScalar *data, Mat *A, PetscDeviceContext dctx, bool preallocate) noexcept
279: {
280:   Mat mat;

282:   PetscFunctionBegin;
283:   PetscAssertPointer(A, 7);
284:   PetscCall(MatCreate(comm, &mat));
285:   PetscCall(MatSetSizes(mat, m, n, M, N));
286:   PetscCall(MatSetType(mat, D::MATIMPLCUPM()));
287:   if (preallocate) PetscCall(D::SetPreallocation(mat, dctx, data));
288:   *A = mat;
289:   PetscFunctionReturn(PETSC_SUCCESS);
290: }

292: template <device::cupm::DeviceType T, typename D>
293: inline PetscErrorCode MatDense_CUPM<T, D>::SetPreallocation(Mat A, PetscDeviceContext dctx, PetscScalar *device_array) noexcept
294: {
295:   PetscFunctionBegin;
297:   // might be the local (sequential) matrix of a MatMPIDense_CUPM. Since this would be called
298:   // from the MPI matrix'es impl MATIMPLCUPM() would return MATMPIDENSECUPM().
300:   PetscCheckTypeNames(A, D::MATSEQDENSECUPM(), D::MATMPIDENSECUPM());
301:   PetscCall(PetscLayoutSetUp(A->rmap));
302:   PetscCall(PetscLayoutSetUp(A->cmap));
303:   if (!dctx) PetscCall(GetHandles_(&dctx));
305:   PetscCall(D::SetPreallocation_(A, dctx, device_array));
306:   A->preallocated = PETSC_TRUE;
307:   A->assembled    = PETSC_TRUE;
308:   PetscFunctionReturn(PETSC_SUCCESS);
309: }

311: namespace detail
312: {

314:   #if CCCL_VERSION >= 3000000
315: template <class T>
316: using iter_difference_t = cuda::std::iter_difference_t<T>;
317:   #else
318: template <class T>
319: using iter_difference_t = typename thrust::iterator_difference<T>::type;
320:   #endif

322: // ==========================================================================================
323: // MatrixIteratorBase
324: //
325: // A base class for creating thrust iterators over the local sub-matrix. This will set up the
326: // proper iterator definitions so thrust knows how to handle things properly. Template
327: // parameters are as follows:
328: //
329: // - Iterator:
330: // The type of the primary array iterator. Usually this is
331: // thrust::device_pointer::iterator.
332: //
333: // - IndexFunctor:
334: // This should be a functor which contains an operator() that when called with an index `i`,
335: // returns the i'th permuted index into the array. For example, it could return the i'th
336: // diagonal entry.
337: // ==========================================================================================
338: template <typename Iterator, typename IndexFunctor>
339: class MatrixIteratorBase {
340: public:
341:   using array_iterator_type = Iterator;
342:   using index_functor_type  = IndexFunctor;

344:   using difference_type     = iter_difference_t<array_iterator_type>;
345:   using CountingIterator    = thrust::counting_iterator<difference_type>;
346:   using TransformIterator   = thrust::transform_iterator<index_functor_type, CountingIterator>;
347:   using PermutationIterator = thrust::permutation_iterator<array_iterator_type, TransformIterator>;
348:   using iterator            = PermutationIterator; // type of the begin/end iterator

350:   constexpr MatrixIteratorBase(array_iterator_type first, array_iterator_type last, index_functor_type idx_func) noexcept : first{std::move(first)}, last{std::move(last)}, func{std::move(idx_func)} { }

352:   PETSC_NODISCARD iterator begin() const noexcept
353:   {
354:     return PermutationIterator{
355:       first, TransformIterator{CountingIterator{0}, func}
356:     };
357:   }

359: protected:
360:   array_iterator_type first;
361:   array_iterator_type last;
362:   index_functor_type  func;
363: };

365: // ==========================================================================================
366: // StridedIndexFunctor
367: //
368: // Iterator which permutes a linear index range into strided matrix indices. Usually used to
369: // get the diagonal.
370: // ==========================================================================================
371: template <typename T>
372: struct StridedIndexFunctor {
373:   PETSC_NODISCARD PETSC_HOSTDEVICE_INLINE_DECL constexpr T operator()(const T &i) const noexcept { return stride * i; }

375:   T stride;
376: };

378: template <typename Iterator>
379: class DiagonalIterator : public MatrixIteratorBase<Iterator, StridedIndexFunctor<iter_difference_t<Iterator>>> {
380: public:
381:   using base_type = MatrixIteratorBase<Iterator, StridedIndexFunctor<iter_difference_t<Iterator>>>;

383:   using difference_type = typename base_type::difference_type;
384:   using iterator        = typename base_type::iterator;

386:   constexpr DiagonalIterator(Iterator first, Iterator last, difference_type stride) noexcept : base_type{std::move(first), std::move(last), {stride}} { }

388:   PETSC_NODISCARD iterator end() const noexcept { return this->begin() + (this->last - this->first + this->func.stride - 1) / this->func.stride; }
389: };

391: template <typename T>
392: inline DiagonalIterator<typename thrust::device_vector<T>::iterator> MakeDiagonalIterator(T *data, PetscInt rstart, PetscInt rend, PetscInt cols, PetscInt lda) noexcept
393: {
394:   const auto        rend2 = std::min(rend, cols);
395:   const std::size_t begin = rstart * lda;
396:   const std::size_t end   = rend2 - rstart + rend2 * lda;
397:   const auto        dptr  = thrust::device_pointer_cast(data);

399:   return {dptr + begin, dptr + end, lda + 1};
400: }

402: } // namespace detail

404: template <device::cupm::DeviceType T, typename D>
405: template <typename F>
406: inline PetscErrorCode MatDense_CUPM<T, D>::DiagonalUnaryTransform(Mat A, PetscDeviceContext dctx, F &&functor) noexcept
407: {
408:   const auto rstart = A->rmap->rstart;
409:   const auto rend   = A->rmap->rend;
410:   const auto gcols  = A->cmap->N;
411:   const auto rend2  = std::min(rend, gcols);

413:   PetscFunctionBegin;
414:   PetscCall(CheckSaneSequentialMatSizes_(A));
415:   if (rend2 > rstart) {
416:     const auto   da = D::DeviceArrayReadWrite(dctx, A);
417:     cupmStream_t stream;
418:     PetscInt     lda;

420:     PetscCall(MatDenseGetLDA(A, &lda));
421:     PetscCall(D::GetHandlesFrom_(dctx, &stream));
422:     {
423:       auto diagonal = detail::MakeDiagonalIterator(da.data(), rstart, rend, gcols, lda);

425:       // clang-format off
426:       PetscCallThrust(
427:         THRUST_CALL(
428:           thrust::transform,
429:           stream,
430:           diagonal.begin(), diagonal.end(), diagonal.begin(),
431:           std::forward<F>(functor)
432:         )
433:       );
434:       // clang-format on
435:     }
436:     PetscCall(PetscLogGpuFlops(rend2 - rstart));
437:   }
438:   PetscFunctionReturn(PETSC_SUCCESS);
439: }

441: template <device::cupm::DeviceType T, typename D>
442: inline PetscErrorCode MatDense_CUPM<T, D>::Shift(Mat A, PetscScalar alpha) noexcept
443: {
444:   PetscDeviceContext dctx;

446:   PetscFunctionBegin;
447:   PetscCall(GetHandles_(&dctx));
448:   PetscCall(DiagonalUnaryTransform(A, dctx, device::cupm::functors::make_plus_equals(alpha)));
449:   PetscFunctionReturn(PETSC_SUCCESS);
450: }

452: template <device::cupm::DeviceType T, typename D>
453: inline PetscErrorCode MatDense_CUPM<T, D>::GetDiagonal(Mat A, Vec v) noexcept
454: {
455:   const auto         rstart = A->rmap->rstart;
456:   const auto         rend   = A->rmap->rend;
457:   const auto         gcols  = A->cmap->N;
458:   PetscInt           lda;
459:   PetscDeviceContext dctx;

461:   PetscFunctionBegin;
462:   PetscCall(CheckSaneSequentialMatSizes_(A));
463:   PetscCall(GetHandles_(&dctx));
464:   PetscCall(MatDenseGetLDA(A, &lda));
465:   {
466:     auto dv       = VecSeq_CUPM::DeviceArrayWrite(dctx, v);
467:     auto da       = D::DeviceArrayRead(dctx, A);
468:     auto diagonal = detail::MakeDiagonalIterator(da.data(), rstart, rend, gcols, lda);
469:     // We must have this cast outside of THRUST_CALL(). Without it, GCC 6.4 - 7.5, and 11.3.0
470:     // throw spurious warnings:
471:     //
472:     // warning: 'MatDense_CUPM<...>::GetDiagonal(Mat, Vec)::' declared with greater
473:     // visibility than the type of its field 'MatDense_CUPM<...>::GetDiagonal(Mat,
474:     // Vec)::::' [-Wattributes]
475:     // 460 |     PetscCallThrust(
476:     //     |     ^~~~~~~~~~~~~~~~
477:     auto         dvp = thrust::device_pointer_cast(dv.data());
478:     cupmStream_t stream;

480:     PetscCall(GetHandlesFrom_(dctx, &stream));
481:     PetscCallThrust(THRUST_CALL(thrust::copy, stream, diagonal.begin(), diagonal.end(), dvp));
482:   }
483:   PetscFunctionReturn(PETSC_SUCCESS);
484: }

486:   #define MatComposeOp_CUPM(use_host, pobj, op_str, op_host, ...) \
487:     do { \
488:       if (use_host) { \
489:         PetscCall(PetscObjectComposeFunction(pobj, op_str, op_host)); \
490:       } else { \
491:         PetscCall(PetscObjectComposeFunction(pobj, op_str, __VA_ARGS__)); \
492:       } \
493:     } while (0)

495:   #define MatSetOp_CUPM(use_host, mat, op_name, op_host, ...) \
496:     do { \
497:       if (use_host) { \
498:         (mat)->ops->op_name = op_host; \
499:       } else { \
500:         (mat)->ops->op_name = __VA_ARGS__; \
501:       } \
502:     } while (0)

504:   #define MATDENSECUPM_HEADER(T, ...) \
505:     MATDENSECUPM_BASE_HEADER(T); \
506:     friend class ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>; \
507:     using ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>::MatIMPLCast; \
508:     using ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>::MATIMPLCUPM; \
509:     using ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>::CreateIMPLDenseCUPM; \
510:     using ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>::SetPreallocation; \
511:     using ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>::DeviceArrayRead; \
512:     using ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>::DeviceArrayWrite; \
513:     using ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>::DeviceArrayReadWrite; \
514:     using ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>::HostArrayRead; \
515:     using ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>::HostArrayWrite; \
516:     using ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>::HostArrayReadWrite; \
517:     using ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>::DiagonalUnaryTransform; \
518:     using ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>::Shift; \
519:     using ::Petsc::mat::cupm::impl::MatDense_CUPM<T, __VA_ARGS__>::GetDiagonal

521: } // namespace impl

523: // ==========================================================================================
524: // MatDense_CUPM -- Implementations
525: // ==========================================================================================

527: template <device::cupm::DeviceType T, PetscMemoryAccessMode access>
528: inline PetscErrorCode MatDenseCUPMGetArray_Private(Mat A, PetscScalar **array) noexcept
529: {
530:   PetscFunctionBegin;
532:   PetscAssertPointer(array, 2);
533:   switch (access) {
534:   case PETSC_MEMORY_ACCESS_READ:
535:     PetscUseMethod(A, impl::MatDense_CUPM_Base<T>::MatDenseCUPMGetArrayRead_C(), (Mat, PetscScalar **), (A, array));
536:     break;
537:   case PETSC_MEMORY_ACCESS_WRITE:
538:     PetscUseMethod(A, impl::MatDense_CUPM_Base<T>::MatDenseCUPMGetArrayWrite_C(), (Mat, PetscScalar **), (A, array));
539:     break;
540:   case PETSC_MEMORY_ACCESS_READ_WRITE:
541:     PetscUseMethod(A, impl::MatDense_CUPM_Base<T>::MatDenseCUPMGetArray_C(), (Mat, PetscScalar **), (A, array));
542:     break;
543:   }
544:   if (PetscMemoryAccessWrite(access)) PetscCall(PetscObjectStateIncrease(PetscObjectCast(A)));
545:   PetscFunctionReturn(PETSC_SUCCESS);
546: }

548: template <device::cupm::DeviceType T, PetscMemoryAccessMode access>
549: inline PetscErrorCode MatDenseCUPMRestoreArray_Private(Mat A, PetscScalar **array) noexcept
550: {
551:   PetscFunctionBegin;
553:   if (array) PetscAssertPointer(array, 2);
554:   switch (access) {
555:   case PETSC_MEMORY_ACCESS_READ:
556:     PetscUseMethod(A, impl::MatDense_CUPM_Base<T>::MatDenseCUPMRestoreArrayRead_C(), (Mat, PetscScalar **), (A, array));
557:     break;
558:   case PETSC_MEMORY_ACCESS_WRITE:
559:     PetscUseMethod(A, impl::MatDense_CUPM_Base<T>::MatDenseCUPMRestoreArrayWrite_C(), (Mat, PetscScalar **), (A, array));
560:     break;
561:   case PETSC_MEMORY_ACCESS_READ_WRITE:
562:     PetscUseMethod(A, impl::MatDense_CUPM_Base<T>::MatDenseCUPMRestoreArray_C(), (Mat, PetscScalar **), (A, array));
563:     break;
564:   }
565:   if (PetscMemoryAccessWrite(access)) {
566:     PetscCall(PetscObjectStateIncrease(PetscObjectCast(A)));
567:     A->offloadmask = PETSC_OFFLOAD_GPU;
568:   }
569:   if (array) *array = nullptr;
570:   PetscFunctionReturn(PETSC_SUCCESS);
571: }

573: template <device::cupm::DeviceType T>
574: inline PetscErrorCode MatDenseCUPMGetArray(Mat A, PetscScalar **array) noexcept
575: {
576:   PetscFunctionBegin;
577:   PetscCall(MatDenseCUPMGetArray_Private<T, PETSC_MEMORY_ACCESS_READ_WRITE>(A, array));
578:   PetscFunctionReturn(PETSC_SUCCESS);
579: }

581: template <device::cupm::DeviceType T>
582: inline PetscErrorCode MatDenseCUPMGetArrayRead(Mat A, const PetscScalar **array) noexcept
583: {
584:   PetscFunctionBegin;
585:   PetscCall(MatDenseCUPMGetArray_Private<T, PETSC_MEMORY_ACCESS_READ>(A, const_cast<PetscScalar **>(array)));
586:   PetscFunctionReturn(PETSC_SUCCESS);
587: }

589: template <device::cupm::DeviceType T>
590: inline PetscErrorCode MatDenseCUPMGetArrayWrite(Mat A, PetscScalar **array) noexcept
591: {
592:   PetscFunctionBegin;
593:   PetscCall(MatDenseCUPMGetArray_Private<T, PETSC_MEMORY_ACCESS_WRITE>(A, array));
594:   PetscFunctionReturn(PETSC_SUCCESS);
595: }

597: template <device::cupm::DeviceType T>
598: inline PetscErrorCode MatDenseCUPMRestoreArray(Mat A, PetscScalar **array) noexcept
599: {
600:   PetscFunctionBegin;
601:   PetscCall(MatDenseCUPMRestoreArray_Private<T, PETSC_MEMORY_ACCESS_READ_WRITE>(A, array));
602:   PetscFunctionReturn(PETSC_SUCCESS);
603: }

605: template <device::cupm::DeviceType T>
606: inline PetscErrorCode MatDenseCUPMRestoreArrayRead(Mat A, const PetscScalar **array) noexcept
607: {
608:   PetscFunctionBegin;
609:   PetscCall(MatDenseCUPMRestoreArray_Private<T, PETSC_MEMORY_ACCESS_READ>(A, const_cast<PetscScalar **>(array)));
610:   PetscFunctionReturn(PETSC_SUCCESS);
611: }

613: template <device::cupm::DeviceType T>
614: inline PetscErrorCode MatDenseCUPMRestoreArrayWrite(Mat A, PetscScalar **array) noexcept
615: {
616:   PetscFunctionBegin;
617:   PetscCall(MatDenseCUPMRestoreArray_Private<T, PETSC_MEMORY_ACCESS_WRITE>(A, array));
618:   PetscFunctionReturn(PETSC_SUCCESS);
619: }

621: template <device::cupm::DeviceType T>
622: inline PetscErrorCode MatDenseCUPMPlaceArray(Mat A, const PetscScalar *array) noexcept
623: {
624:   PetscFunctionBegin;
626:   PetscUseMethod(A, impl::MatDense_CUPM_Base<T>::MatDenseCUPMPlaceArray_C(), (Mat, const PetscScalar *), (A, array));
627:   PetscCall(PetscObjectStateIncrease(PetscObjectCast(A)));
628:   A->offloadmask = PETSC_OFFLOAD_GPU;
629:   PetscFunctionReturn(PETSC_SUCCESS);
630: }

632: template <device::cupm::DeviceType T>
633: inline PetscErrorCode MatDenseCUPMReplaceArray(Mat A, const PetscScalar *array) noexcept
634: {
635:   PetscFunctionBegin;
637:   PetscUseMethod(A, impl::MatDense_CUPM_Base<T>::MatDenseCUPMReplaceArray_C(), (Mat, const PetscScalar *), (A, array));
638:   PetscCall(PetscObjectStateIncrease(PetscObjectCast(A)));
639:   A->offloadmask = PETSC_OFFLOAD_GPU;
640:   PetscFunctionReturn(PETSC_SUCCESS);
641: }

643: template <device::cupm::DeviceType T>
644: inline PetscErrorCode MatDenseCUPMResetArray(Mat A) noexcept
645: {
646:   PetscFunctionBegin;
648:   PetscUseMethod(A, impl::MatDense_CUPM_Base<T>::MatDenseCUPMResetArray_C(), (Mat), (A));
649:   PetscCall(PetscObjectStateIncrease(PetscObjectCast(A)));
650:   PetscFunctionReturn(PETSC_SUCCESS);
651: }

653: template <device::cupm::DeviceType T>
654: inline PetscErrorCode MatDenseCUPMSetPreallocation(Mat A, PetscScalar *device_data, PetscDeviceContext dctx = nullptr) noexcept
655: {
656:   PetscFunctionBegin;
658:   PetscUseMethod(A, impl::MatDense_CUPM_Base<T>::MatDenseCUPMSetPreallocation_C(), (Mat, PetscDeviceContext, PetscScalar *), (A, dctx, device_data));
659:   PetscFunctionReturn(PETSC_SUCCESS);
660: }

662: } // namespace cupm

664: } // namespace mat

666: } // namespace Petsc

668: #endif // __cplusplus