Actual source code: mpiaijcupm.hpp
1: #pragma once
3: /* Shared CUPM (CUDA/HIP) implementations for MPIAIJCUSPARSE and MPIAIJHIPSPARSE
4: that do not depend on the cuSPARSE/hipSPARSE library proper.
6: Include ordering requirement: the vendor-specific MPI impl header
7: (mpicusparsematimpl.h or mpihipsparsematimpl.h) must be included before
8: this header so that Mat_MPIAIJCUSPARSE/Mat_MPIAIJHIPSPARSE types are visible.
10: Instantiated by:
11: mpiaijcusparse.cu (DeviceType::CUDA, using MatMPIAIJCUSPARSE_Policy)
12: mpiaijhipsparse.hip.cxx (DeviceType::HIP, using MatMPIAIJHIPSPARSE_Policy) */
14: #include <petsc/private/cupmobject.hpp>
15: #include <petsc/private/matimpl.h>
16: #include <../src/mat/impls/aij/mpi/mpiaij.h>
17: #include <petscsf.h>
19: namespace Petsc
20: {
22: namespace mat
23: {
25: namespace aij
26: {
28: namespace cupm
29: {
31: namespace impl
32: {
34: /* --------------------------------------------------------------------------
35: Shared __global__ kernel: pack entries to be sent to remote.
36: -------------------------------------------------------------------------- */
37: __global__ static void MatPackCOOValues_MPI(const PetscScalar kv[], PetscCount nnz, const PetscCount perm[], PetscScalar buf[])
38: {
39: PetscCount i = blockIdx.x * blockDim.x + threadIdx.x;
40: const PetscCount grid_size = gridDim.x * blockDim.x;
41: for (; i < nnz; i += grid_size) buf[i] = kv[perm[i]];
42: }
44: /* --------------------------------------------------------------------------
45: Shared __global__ kernel: add local COO values to diagonal and off-diagonal.
46: -------------------------------------------------------------------------- */
47: __global__ static void MatAddLocalCOOValues_MPI(const PetscScalar kv[], InsertMode imode, PetscCount Annz, const PetscCount Ajmap1[], const PetscCount Aperm1[], PetscScalar Aa[], PetscCount Bnnz, const PetscCount Bjmap1[], const PetscCount Bperm1[], PetscScalar Ba[])
48: {
49: PetscCount i = blockIdx.x * blockDim.x + threadIdx.x;
50: const PetscCount grid_size = gridDim.x * blockDim.x;
51: for (; i < Annz + Bnnz; i += grid_size) {
52: PetscScalar sum = 0.0;
53: if (i < Annz) {
54: for (PetscCount k = Ajmap1[i]; k < Ajmap1[i + 1]; k++) sum += kv[Aperm1[k]];
55: Aa[i] = (imode == INSERT_VALUES ? 0.0 : Aa[i]) + sum;
56: } else {
57: i -= Annz;
58: for (PetscCount k = Bjmap1[i]; k < Bjmap1[i + 1]; k++) sum += kv[Bperm1[k]];
59: Ba[i] = (imode == INSERT_VALUES ? 0.0 : Ba[i]) + sum;
60: }
61: }
62: }
64: /* --------------------------------------------------------------------------
65: Shared __global__ kernel: add remote COO values to diagonal and off-diagonal.
66: -------------------------------------------------------------------------- */
67: __global__ static void MatAddRemoteCOOValues_MPI(const PetscScalar kv[], PetscCount Annz2, const PetscCount Aimap2[], const PetscCount Ajmap2[], const PetscCount Aperm2[], PetscScalar Aa[], PetscCount Bnnz2, const PetscCount Bimap2[], const PetscCount Bjmap2[], const PetscCount Bperm2[], PetscScalar Ba[])
68: {
69: PetscCount i = blockIdx.x * blockDim.x + threadIdx.x;
70: const PetscCount grid_size = gridDim.x * blockDim.x;
71: for (; i < Annz2 + Bnnz2; i += grid_size) {
72: if (i < Annz2) {
73: for (PetscCount k = Ajmap2[i]; k < Ajmap2[i + 1]; k++) Aa[Aimap2[i]] += kv[Aperm2[k]];
74: } else {
75: i -= Annz2;
76: for (PetscCount k = Bjmap2[i]; k < Bjmap2[i + 1]; k++) Ba[Bimap2[i]] += kv[Bperm2[k]];
77: }
78: }
79: }
81: /* ==========================================================================
82: MatMPIAIJCUSPARSE_CUPM<T, Policy>
84: Policy (C++11 traits class) requirements - all static:
86: typedef ... mat_struct_type; // Mat_MPIAIJCUSPARSE / Mat_MPIAIJHIPSPARSE
88: static const char *mpi_mat_type; // MATMPIAIJCUSPARSE / MATMPIAIJHIPSPARSE
89: static const char *seq_mat_type; // MATSEQAIJCUSPARSE / MATSEQAIJHIPSPARSE
90: static const char *vec_seq_type; // VECSEQCUDA / VECSEQHIP
92: // Seq sub-matrix device copy
93: static PetscErrorCode CopyToGPU(Mat);
95: // Seq sub-matrix merge (for GetLocalMatMerge)
96: static PetscErrorCode MergeMats(Mat, Mat, MatReuse, Mat *);
98: // Seq sub-matrix device array access (for SetValuesCOO)
99: static PetscErrorCode GetArray (Mat, PetscScalar **);
100: static PetscErrorCode GetArrayWrite(Mat, PetscScalar **);
101: static PetscErrorCode RestoreArray (Mat, PetscScalar **);
102: static PetscErrorCode RestoreArrayWrite(Mat, PetscScalar **);
104: // Set cuSPARSE/hipSPARSE storage format on both sub-matrices
105: static PetscErrorCode SetSubMatFormats(Mat, Mat, mat_struct_type *);
107: // Compose-function keys that differ between CUDA and HIP
108: static const char *set_format_c; // "MatCUSPARSESetFormat_C" / "MatHIPSPARSESetFormat_C"
109: static const char *mpi_convert_hypre_c; // "MatConvert_mpiaijcusparse_hypre_C" / "_mpiaijhipsparse_hypre_C"
110: ========================================================================== */
112: template <device::cupm::DeviceType T, typename Policy>
113: struct MatMPIAIJCUSPARSE_CUPM : device::cupm::impl::CUPMObject<T> {
114: PETSC_CUPMOBJECT_HEADER(T);
116: typedef typename Policy::mat_struct_type MatStructType;
118: /* MatCOOStructDestroy: release all device-side COO arrays */
119: static PetscErrorCode COOStructDestroy(PetscCtxRt data) noexcept
120: {
121: MatCOOStruct_MPIAIJ *coo = *(MatCOOStruct_MPIAIJ **)data;
123: PetscFunctionBegin;
124: PetscCall(PetscSFDestroy(&coo->sf));
125: PetscCallCUPM(cupmFree(coo->Ajmap1));
126: PetscCallCUPM(cupmFree(coo->Aperm1));
127: PetscCallCUPM(cupmFree(coo->Bjmap1));
128: PetscCallCUPM(cupmFree(coo->Bperm1));
129: PetscCallCUPM(cupmFree(coo->Aimap2));
130: PetscCallCUPM(cupmFree(coo->Ajmap2));
131: PetscCallCUPM(cupmFree(coo->Aperm2));
132: PetscCallCUPM(cupmFree(coo->Bimap2));
133: PetscCallCUPM(cupmFree(coo->Bjmap2));
134: PetscCallCUPM(cupmFree(coo->Bperm2));
135: PetscCallCUPM(cupmFree(coo->Cperm1));
136: PetscCallCUPM(cupmFree(coo->sendbuf));
137: PetscCallCUPM(cupmFree(coo->recvbuf));
138: PetscCall(PetscFree(coo));
139: PetscFunctionReturn(PETSC_SUCCESS);
140: }
142: /* MatSetPreallocationCOO: copy MPIAIJ COO bookkeeping struct to device */
143: static PetscErrorCode SetPreallocationCOO(Mat mat, PetscCount coo_n, PetscInt coo_i[], PetscInt coo_j[]) noexcept
144: {
145: Mat_MPIAIJ *mpiaij = (Mat_MPIAIJ *)mat->data;
146: PetscBool dev_ij = PETSC_FALSE;
147: PetscMemType mtype = PETSC_MEMTYPE_HOST;
148: PetscInt *i, *j;
149: PetscContainer container_h;
150: MatCOOStruct_MPIAIJ *coo_h, *coo_d;
151: cupmStream_t stream;
152: PetscDeviceContext dctx;
154: PetscFunctionBegin;
155: PetscCall(GetHandles_(&dctx, &stream));
156: PetscCall(PetscFree(mpiaij->garray));
157: PetscCall(VecDestroy(&mpiaij->lvec));
158: #if PetscDefined(USE_CTABLE)
159: PetscCall(PetscHMapIDestroy(&mpiaij->colmap));
160: #else
161: PetscCall(PetscFree(mpiaij->colmap));
162: #endif
163: PetscCall(VecScatterDestroy(&mpiaij->Mvctx));
164: mat->assembled = PETSC_FALSE;
165: mat->was_assembled = PETSC_FALSE;
166: PetscCall(PetscGetMemType(coo_i, &mtype));
167: if (PetscMemTypeDevice(mtype)) {
168: dev_ij = PETSC_TRUE;
169: PetscCall(PetscMalloc2(coo_n, &i, coo_n, &j));
170: PetscCallCUPM(cupmMemcpyAsync(i, coo_i, coo_n * sizeof(PetscInt), cupmMemcpyDeviceToHost, stream));
171: PetscCallCUPM(cupmMemcpyAsync(j, coo_j, coo_n * sizeof(PetscInt), cupmMemcpyDeviceToHost, stream));
172: PetscCall(PetscDeviceContextSynchronize(dctx));
173: } else {
174: i = coo_i;
175: j = coo_j;
176: }
177: PetscCall(MatSetPreallocationCOO_MPIAIJ(mat, coo_n, i, j));
178: if (dev_ij) PetscCall(PetscFree2(i, j));
179: mat->offloadmask = PETSC_OFFLOAD_CPU;
180: /* Create the GPU memory */
181: PetscCall(Policy::CopyToGPU(mpiaij->A));
182: PetscCall(Policy::CopyToGPU(mpiaij->B));
184: /* Copy the COO struct to device */
185: PetscCall(PetscObjectQuery((PetscObject)mat, "__PETSc_MatCOOStruct_Host", (PetscObject *)&container_h));
186: PetscCall(PetscContainerGetPointer(container_h, (void **)&coo_h));
187: PetscCall(PetscMalloc1(1, &coo_d));
188: *coo_d = *coo_h; /* shallow copy; device fields amended below */
189: PetscCall(PetscObjectReference((PetscObject)coo_d->sf));
190: PetscCallCUPM(cupmMalloc((void **)&coo_d->Ajmap1, (coo_h->Annz + 1) * sizeof(PetscCount)));
191: PetscCallCUPM(cupmMalloc((void **)&coo_d->Aperm1, coo_h->Atot1 * sizeof(PetscCount)));
192: PetscCallCUPM(cupmMalloc((void **)&coo_d->Bjmap1, (coo_h->Bnnz + 1) * sizeof(PetscCount)));
193: PetscCallCUPM(cupmMalloc((void **)&coo_d->Bperm1, coo_h->Btot1 * sizeof(PetscCount)));
194: PetscCallCUPM(cupmMalloc((void **)&coo_d->Aimap2, coo_h->Annz2 * sizeof(PetscCount)));
195: PetscCallCUPM(cupmMalloc((void **)&coo_d->Ajmap2, (coo_h->Annz2 + 1) * sizeof(PetscCount)));
196: PetscCallCUPM(cupmMalloc((void **)&coo_d->Aperm2, coo_h->Atot2 * sizeof(PetscCount)));
197: PetscCallCUPM(cupmMalloc((void **)&coo_d->Bimap2, coo_h->Bnnz2 * sizeof(PetscCount)));
198: PetscCallCUPM(cupmMalloc((void **)&coo_d->Bjmap2, (coo_h->Bnnz2 + 1) * sizeof(PetscCount)));
199: PetscCallCUPM(cupmMalloc((void **)&coo_d->Bperm2, coo_h->Btot2 * sizeof(PetscCount)));
200: PetscCallCUPM(cupmMalloc((void **)&coo_d->Cperm1, coo_h->sendlen * sizeof(PetscCount)));
201: PetscCallCUPM(cupmMalloc((void **)&coo_d->sendbuf, coo_h->sendlen * sizeof(PetscScalar)));
202: PetscCallCUPM(cupmMalloc((void **)&coo_d->recvbuf, coo_h->recvlen * sizeof(PetscScalar)));
203: PetscCallCUPM(cupmMemcpyAsync(coo_d->Ajmap1, coo_h->Ajmap1, (coo_h->Annz + 1) * sizeof(PetscCount), cupmMemcpyHostToDevice, stream));
204: PetscCallCUPM(cupmMemcpyAsync(coo_d->Aperm1, coo_h->Aperm1, coo_h->Atot1 * sizeof(PetscCount), cupmMemcpyHostToDevice, stream));
205: PetscCallCUPM(cupmMemcpyAsync(coo_d->Bjmap1, coo_h->Bjmap1, (coo_h->Bnnz + 1) * sizeof(PetscCount), cupmMemcpyHostToDevice, stream));
206: PetscCallCUPM(cupmMemcpyAsync(coo_d->Bperm1, coo_h->Bperm1, coo_h->Btot1 * sizeof(PetscCount), cupmMemcpyHostToDevice, stream));
207: PetscCallCUPM(cupmMemcpyAsync(coo_d->Aimap2, coo_h->Aimap2, coo_h->Annz2 * sizeof(PetscCount), cupmMemcpyHostToDevice, stream));
208: PetscCallCUPM(cupmMemcpyAsync(coo_d->Ajmap2, coo_h->Ajmap2, (coo_h->Annz2 + 1) * sizeof(PetscCount), cupmMemcpyHostToDevice, stream));
209: PetscCallCUPM(cupmMemcpyAsync(coo_d->Aperm2, coo_h->Aperm2, coo_h->Atot2 * sizeof(PetscCount), cupmMemcpyHostToDevice, stream));
210: PetscCallCUPM(cupmMemcpyAsync(coo_d->Bimap2, coo_h->Bimap2, coo_h->Bnnz2 * sizeof(PetscCount), cupmMemcpyHostToDevice, stream));
211: PetscCallCUPM(cupmMemcpyAsync(coo_d->Bjmap2, coo_h->Bjmap2, (coo_h->Bnnz2 + 1) * sizeof(PetscCount), cupmMemcpyHostToDevice, stream));
212: PetscCallCUPM(cupmMemcpyAsync(coo_d->Bperm2, coo_h->Bperm2, coo_h->Btot2 * sizeof(PetscCount), cupmMemcpyHostToDevice, stream));
213: PetscCallCUPM(cupmMemcpyAsync(coo_d->Cperm1, coo_h->Cperm1, coo_h->sendlen * sizeof(PetscCount), cupmMemcpyHostToDevice, stream));
214: PetscCall(PetscDeviceContextSynchronize(dctx));
215: /* Put the COO struct in a container and attach it to the matrix */
216: PetscCall(PetscObjectContainerCompose((PetscObject)mat, "__PETSc_MatCOOStruct_Device", coo_d, MatMPIAIJCUSPARSE_CUPM::COOStructDestroy));
217: PetscFunctionReturn(PETSC_SUCCESS);
218: }
220: /* MatSetValuesCOO: launch CUPM kernels for packing and adding local/remote COO values */
221: static PetscErrorCode SetValuesCOO(Mat mat, const PetscScalar v[], InsertMode imode) noexcept
222: {
223: Mat_MPIAIJ *mpiaij = static_cast<Mat_MPIAIJ *>(mat->data);
224: Mat A = mpiaij->A, B = mpiaij->B;
225: PetscScalar *Aa, *Ba;
226: const PetscScalar *v1 = v;
227: PetscMemType memtype;
228: PetscContainer container;
229: MatCOOStruct_MPIAIJ *coo;
230: cupmStream_t stream;
231: PetscDeviceContext dctx;
233: PetscFunctionBegin;
234: PetscCall(MatMPIAIJCheckCOONonzeroState(mat));
235: PetscCall(GetHandles_(&dctx, &stream));
236: PetscCall(PetscObjectQuery((PetscObject)mat, "__PETSc_MatCOOStruct_Device", (PetscObject *)&container));
237: PetscCheck(container, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "The matrix is missing the device COO struct built by MatSetPreallocationCOO()");
238: PetscCall(PetscContainerGetPointer(container, (void **)&coo));
240: const auto &Annz = coo->Annz;
241: const auto &Annz2 = coo->Annz2;
242: const auto &Bnnz = coo->Bnnz;
243: const auto &Bnnz2 = coo->Bnnz2;
244: const auto &vsend = coo->sendbuf;
245: const auto &v2 = coo->recvbuf;
246: const auto &Ajmap1 = coo->Ajmap1;
247: const auto &Ajmap2 = coo->Ajmap2;
248: const auto &Aimap2 = coo->Aimap2;
249: const auto &Bjmap1 = coo->Bjmap1;
250: const auto &Bjmap2 = coo->Bjmap2;
251: const auto &Bimap2 = coo->Bimap2;
252: const auto &Aperm1 = coo->Aperm1;
253: const auto &Aperm2 = coo->Aperm2;
254: const auto &Bperm1 = coo->Bperm1;
255: const auto &Bperm2 = coo->Bperm2;
256: const auto &Cperm1 = coo->Cperm1;
258: PetscCall(PetscGetMemType(v, &memtype));
259: if (PetscMemTypeHost(memtype)) {
260: PetscCallCUPM(cupmMalloc((void **)&v1, coo->n * sizeof(PetscScalar)));
261: PetscCallCUPM(cupmMemcpyAsync((void *)v1, v, coo->n * sizeof(PetscScalar), cupmMemcpyHostToDevice, stream));
262: // The caller may reuse the host values after this call returns.
263: PetscCall(PetscDeviceContextSynchronize(dctx));
264: PetscCall(PetscLogCpuToGpu(coo->n * sizeof(PetscScalar)));
265: }
267: if (imode == INSERT_VALUES) {
268: PetscCall(Policy::GetArrayWrite(A, &Aa));
269: PetscCall(Policy::GetArrayWrite(B, &Ba));
270: } else {
271: PetscCall(Policy::GetArray(A, &Aa));
272: PetscCall(Policy::GetArray(B, &Ba));
273: }
275: PetscCall(PetscLogGpuTimeBegin());
276: /* Pack entries to be sent to remote */
277: if (coo->sendlen) {
278: PetscCallCUPM(cupmLaunchKernel(MatPackCOOValues_MPI, (unsigned int)((coo->sendlen + 255) / 256), 256u, (size_t)0, stream, v1, (PetscCount)coo->sendlen, Cperm1, vsend));
279: PetscCallCUPM(cupmGetLastError());
280: }
281: /* Send remote entries and overlap communication with local computation */
282: PetscCall(PetscSFReduceWithMemTypeBegin(coo->sf, MPIU_SCALAR, PETSC_MEMTYPE_CUPM(), vsend, PETSC_MEMTYPE_CUPM(), v2, MPI_REPLACE));
283: /* Add local entries to A and B */
284: if (Annz + Bnnz > 0) {
285: PetscCallCUPM(cupmLaunchKernel(MatAddLocalCOOValues_MPI, (unsigned int)((Annz + Bnnz + 255) / 256), 256u, (size_t)0, stream, v1, imode, Annz, Ajmap1, Aperm1, Aa, Bnnz, Bjmap1, Bperm1, Ba));
286: PetscCallCUPM(cupmGetLastError());
287: }
288: PetscCall(PetscSFReduceEnd(coo->sf, MPIU_SCALAR, vsend, v2, MPI_REPLACE));
289: /* Add received remote entries to A and B */
290: if (Annz2 + Bnnz2 > 0) {
291: PetscCallCUPM(cupmLaunchKernel(MatAddRemoteCOOValues_MPI, (unsigned int)((Annz2 + Bnnz2 + 255) / 256), 256u, (size_t)0, stream, v2, Annz2, Aimap2, Ajmap2, Aperm2, Aa, Bnnz2, Bimap2, Bjmap2, Bperm2, Ba));
292: PetscCallCUPM(cupmGetLastError());
293: }
294: PetscCall(PetscLogGpuTimeEnd());
296: if (imode == INSERT_VALUES) {
297: PetscCall(Policy::RestoreArrayWrite(A, &Aa));
298: PetscCall(Policy::RestoreArrayWrite(B, &Ba));
299: } else {
300: PetscCall(Policy::RestoreArray(A, &Aa));
301: PetscCall(Policy::RestoreArray(B, &Ba));
302: }
303: if (PetscMemTypeHost(memtype)) {
304: void *v1_device = (void *)v1;
306: PetscCallCUPM(cupmFreeAsync(v1_device, stream));
307: }
308: PetscCall(PetscDeviceContextSynchronizeIfWithBarrier_Internal(dctx));
309: mat->offloadmask = PETSC_OFFLOAD_GPU;
310: PetscFunctionReturn(PETSC_SUCCESS);
311: }
313: /* MatMPIAIJGetLocalMatMerge */
314: static PetscErrorCode GetLocalMatMerge(Mat A, MatReuse scall, IS *glob, Mat *A_loc) noexcept
315: {
316: Mat Ad, Ao;
317: const PetscInt *cmap;
319: PetscFunctionBegin;
320: PetscCall(MatMPIAIJGetSeqAIJ(A, &Ad, &Ao, &cmap));
321: PetscCall(Policy::MergeMats(Ad, Ao, scall, A_loc));
322: if (glob) {
323: PetscInt cst, i, dn, on, *gidx;
325: PetscCall(MatGetLocalSize(Ad, NULL, &dn));
326: PetscCall(MatGetLocalSize(Ao, NULL, &on));
327: PetscCall(MatGetOwnershipRangeColumn(A, &cst, NULL));
328: PetscCall(PetscMalloc1(dn + on, &gidx));
329: for (i = 0; i < dn; i++) gidx[i] = cst + i;
330: for (i = 0; i < on; i++) gidx[i + dn] = cmap[i];
331: PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)Ad), dn + on, gidx, PETSC_OWN_POINTER, glob));
332: }
333: PetscFunctionReturn(PETSC_SUCCESS);
334: }
336: /* MatMPIAIJSetPreallocation */
337: static PetscErrorCode SetPreallocation(Mat B, PetscInt d_nz, const PetscInt d_nnz[], PetscInt o_nz, const PetscInt o_nnz[]) noexcept
338: {
339: Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
340: MatStructType *spptr = (MatStructType *)b->spptr;
341: PetscInt i;
343: PetscFunctionBegin;
344: if (B->hash_active) {
345: B->ops[0] = b->cops;
346: B->hash_active = PETSC_FALSE;
347: }
348: PetscCall(PetscLayoutSetUp(B->rmap));
349: PetscCall(PetscLayoutSetUp(B->cmap));
350: if (PetscDefined(USE_DEBUG) && d_nnz) {
351: for (i = 0; i < B->rmap->n; i++) PetscCheck(d_nnz[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "d_nnz cannot be less than 0: local row %" PetscInt_FMT " value %" PetscInt_FMT, i, d_nnz[i]);
352: }
353: if (PetscDefined(USE_DEBUG) && o_nnz) {
354: for (i = 0; i < B->rmap->n; i++) PetscCheck(o_nnz[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "o_nnz cannot be less than 0: local row %" PetscInt_FMT " value %" PetscInt_FMT, i, o_nnz[i]);
355: }
356: #if PetscDefined(USE_CTABLE)
357: PetscCall(PetscHMapIDestroy(&b->colmap));
358: #else
359: PetscCall(PetscFree(b->colmap));
360: #endif
361: PetscCall(PetscFree(b->garray));
362: PetscCall(VecDestroy(&b->lvec));
363: PetscCall(VecScatterDestroy(&b->Mvctx));
364: /* Because the B will have been resized we simply destroy it and create a new one each time */
365: PetscCall(MatDestroy(&b->B));
366: if (!b->A) {
367: PetscCall(MatCreate(PETSC_COMM_SELF, &b->A));
368: PetscCall(MatSetSizes(b->A, B->rmap->n, B->cmap->n, B->rmap->n, B->cmap->n));
369: }
370: if (!b->B) {
371: PetscMPIInt size;
373: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &size));
374: PetscCall(MatCreate(PETSC_COMM_SELF, &b->B));
375: PetscCall(MatSetSizes(b->B, B->rmap->n, size > 1 ? B->cmap->N : 0, B->rmap->n, size > 1 ? B->cmap->N : 0));
376: }
377: PetscCall(MatSetType(b->A, Policy::seq_mat_type));
378: PetscCall(MatSetType(b->B, Policy::seq_mat_type));
379: PetscCall(MatBindToCPU(b->A, B->boundtocpu));
380: PetscCall(MatBindToCPU(b->B, B->boundtocpu));
381: PetscCall(MatSeqAIJSetPreallocation(b->A, d_nz, d_nnz));
382: PetscCall(MatSeqAIJSetPreallocation(b->B, o_nz, o_nnz));
383: PetscCall(Policy::SetSubMatFormats(b->A, b->B, spptr));
384: B->preallocated = PETSC_TRUE;
385: B->was_assembled = PETSC_FALSE;
386: B->assembled = PETSC_FALSE;
387: PetscFunctionReturn(PETSC_SUCCESS);
388: }
390: /* MatMult: identical in both CUDA and HIP */
391: static PetscErrorCode Mult(Mat A, Vec xx, Vec yy) noexcept
392: {
393: Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
395: PetscFunctionBegin;
396: PetscCall(VecScatterBegin(a->Mvctx, xx, a->lvec, INSERT_VALUES, SCATTER_FORWARD));
397: PetscUseTypeMethod(a->A, mult, xx, yy);
398: PetscCall(VecScatterEnd(a->Mvctx, xx, a->lvec, INSERT_VALUES, SCATTER_FORWARD));
399: PetscUseTypeMethod(a->B, multadd, a->lvec, yy, yy);
400: PetscFunctionReturn(PETSC_SUCCESS);
401: }
403: /* MatZeroEntries: identical in both CUDA and HIP */
404: static PetscErrorCode ZeroEntries(Mat A) noexcept
405: {
406: Mat_MPIAIJ *l = (Mat_MPIAIJ *)A->data;
408: PetscFunctionBegin;
409: PetscCall(MatZeroEntries(l->A));
410: PetscCall(MatZeroEntries(l->B));
411: PetscFunctionReturn(PETSC_SUCCESS);
412: }
414: /* MatMultAdd: identical in both CUDA and HIP */
415: static PetscErrorCode MultAdd(Mat A, Vec xx, Vec yy, Vec zz) noexcept
416: {
417: Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
419: PetscFunctionBegin;
420: PetscCall(VecScatterBegin(a->Mvctx, xx, a->lvec, INSERT_VALUES, SCATTER_FORWARD));
421: PetscUseTypeMethod(a->A, multadd, xx, yy, zz);
422: PetscCall(VecScatterEnd(a->Mvctx, xx, a->lvec, INSERT_VALUES, SCATTER_FORWARD));
423: PetscUseTypeMethod(a->B, multadd, a->lvec, zz, zz);
424: PetscFunctionReturn(PETSC_SUCCESS);
425: }
427: /* MatMultTranspose: identical in both CUDA and HIP */
428: static PetscErrorCode MultTranspose(Mat A, Vec xx, Vec yy) noexcept
429: {
430: Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
432: PetscFunctionBegin;
433: PetscUseTypeMethod(a->B, multtranspose, xx, a->lvec);
434: PetscUseTypeMethod(a->A, multtranspose, xx, yy);
435: PetscCall(VecScatterBegin(a->Mvctx, a->lvec, yy, ADD_VALUES, SCATTER_REVERSE));
436: PetscCall(VecScatterEnd(a->Mvctx, a->lvec, yy, ADD_VALUES, SCATTER_REVERSE));
437: PetscFunctionReturn(PETSC_SUCCESS);
438: }
440: /* MatAssemblyEnd: set lvec type to vendor-appropriate VECSEQ type */
441: static PetscErrorCode AssemblyEnd(Mat A, MatAssemblyType mode) noexcept
442: {
443: Mat_MPIAIJ *mpiaij = (Mat_MPIAIJ *)A->data;
445: PetscFunctionBegin;
446: PetscCall(MatAssemblyEnd_MPIAIJ(A, mode));
447: if (mpiaij->lvec) PetscCall(VecSetType(mpiaij->lvec, Policy::vec_seq_type));
448: PetscFunctionReturn(PETSC_SUCCESS);
449: }
451: /* MatCreateAIJ: allocate and preallocate a parallel sparse matrix of this type */
452: static PetscErrorCode CreateAIJ(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt M, PetscInt N, PetscInt d_nz, const PetscInt d_nnz[], PetscInt o_nz, const PetscInt o_nnz[], Mat *A) noexcept
453: {
454: PetscMPIInt size;
456: PetscFunctionBegin;
457: PetscCall(MatCreate(comm, A));
458: PetscCall(MatSetSizes(*A, m, n, M, N));
459: PetscCallMPI(MPI_Comm_size(comm, &size));
460: if (size > 1) {
461: PetscCall(MatSetType(*A, Policy::mpi_mat_type));
462: PetscCall(MatMPIAIJSetPreallocation(*A, d_nz, d_nnz, o_nz, o_nnz));
463: } else {
464: PetscCall(MatSetType(*A, Policy::seq_mat_type));
465: PetscCall(MatSeqAIJSetPreallocation(*A, d_nz, d_nnz));
466: }
467: PetscFunctionReturn(PETSC_SUCCESS);
468: }
470: /* MatDestroy: free vendor-specific state, deregister composed functions */
471: static PetscErrorCode Destroy(Mat A) noexcept
472: {
473: Mat_MPIAIJ *aij = (Mat_MPIAIJ *)A->data;
474: MatStructType *mpiStruct = (MatStructType *)aij->spptr;
476: PetscFunctionBegin;
477: PetscCheck(mpiStruct, PETSC_COMM_SELF, PETSC_ERR_COR, "Missing spptr");
478: PetscCallCXX(delete mpiStruct);
479: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMPIAIJSetPreallocation_C", NULL));
480: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMPIAIJGetLocalMatMerge_C", NULL));
481: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSetPreallocationCOO_C", NULL));
482: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSetValuesCOO_C", NULL));
483: PetscCall(PetscObjectComposeFunction((PetscObject)A, Policy::set_format_c, NULL));
484: PetscCall(PetscObjectComposeFunction((PetscObject)A, Policy::mpi_convert_hypre_c, NULL));
485: PetscCall(MatDestroy_MPIAIJ(A));
486: PetscFunctionReturn(PETSC_SUCCESS);
487: }
488: };
490: } // namespace impl
492: } // namespace cupm
494: } // namespace aij
496: } // namespace mat
498: } // namespace Petsc