Actual source code: ex324.c

  1: static const char help[] = "Test CUDA/HIP AIJ, SELL, and dense operations and array copies on the current device context.\n";

  3: #include <petscmat.h>
  4: #include <petscdevice.h>
  5: #include <petsc/private/deviceimpl.h>
  6: #if PetscDefined(HAVE_CUDA)
  7: #include <petscdevice_cuda.h>
  8: #endif
  9: #if PetscDefined(HAVE_HIP)
 10: #include <petscdevice_hip.h>
 11: #endif

 13: static PetscErrorCode WriteValues(Mat A, PetscDeviceType type, PetscDeviceContext dctx, const PetscScalar input[], PetscBool delay)
 14: {
 15:   PetscScalar *a;
 16:   void        *stream;

 18:   PetscFunctionBeginUser;
 19:   PetscCall(PetscDeviceContextGetStreamHandle(dctx, &stream));
 20: #if PetscDefined(HAVE_CUDA)
 21:   if (type == PETSC_DEVICE_CUDA) {
 22:     PetscCall(MatSeqAIJCUSPARSEGetArray(A, &a));
 23:     if (delay) PetscCall(PetscDeviceContextDelay(dctx, 0.05));
 24:     PetscCallCUDA(cudaMemcpyAsync(a, input, 4 * sizeof(*a), cudaMemcpyHostToDevice, *(cudaStream_t *)stream));
 25:     PetscCall(MatSeqAIJCUSPARSERestoreArray(A, &a));
 26:   }
 27: #endif
 28: #if PetscDefined(HAVE_HIP)
 29:   if (type == PETSC_DEVICE_HIP) {
 30:     PetscCall(MatSeqAIJHIPSPARSEGetArray(A, &a));
 31:     if (delay) PetscCall(PetscDeviceContextDelay(dctx, 0.05));
 32:     PetscCallHIP(hipMemcpyAsync(a, input, 4 * sizeof(*a), hipMemcpyHostToDevice, *(hipStream_t *)stream));
 33:     PetscCall(MatSeqAIJHIPSPARSERestoreArray(A, &a));
 34:   }
 35: #endif
 36:   PetscFunctionReturn(PETSC_SUCCESS);
 37: }

 39: static PetscErrorCode TestCopies(Mat A, PetscDeviceType type, PetscDeviceContext dctx, const PetscScalar input[])
 40: {
 41:   PetscErrorCode (*copy)(Mat, PetscInt, const PetscInt[], PetscScalar[]) = NULL;
 42:   const PetscInt     idx[]                                               = {3, 1, 0, 2};
 43:   const PetscScalar *a;
 44:   PetscScalar       *host, *device;
 45:   PetscStreamType    streamtype;

 47:   PetscFunctionBeginUser;
 48:   PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqAIJCopySubArray_C", &copy));
 49:   PetscCheck(copy, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing device subarray copy implementation");
 50:   PetscCall(PetscDeviceContextGetStreamType(dctx, &streamtype));
 51:   PetscCall(PetscDeviceMalloc(dctx, PETSC_MEMTYPE_HOST, 4, PETSC_DECIDE, &host));
 52:   PetscCall(PetscDeviceMalloc(dctx, PETSC_MEMTYPE_DEVICE, 4, sizeof(PetscScalar), &device));

 54:   PetscCall(MatZeroEntries(A));
 55:   PetscCall(PetscDeviceContextSynchronize(dctx));
 56:   PetscCall(WriteValues(A, type, dctx, input, PETSC_TRUE));
 57:   PetscCall(MatSeqAIJGetArrayRead(A, &a));
 58:   for (PetscInt i = 0; i < 4; ++i) PetscCheck(a[i] == input[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Host readback overtook the device write at entry %" PetscInt_FMT, i);
 59:   PetscCall(MatSeqAIJRestoreArrayRead(A, &a));

 61:   for (PetscInt d = 0; d < 2; ++d) {
 62:     for (PetscInt indexed = 0; indexed < 2; ++indexed) {
 63:       PetscCall(MatZeroEntries(A));
 64:       PetscCall(PetscDeviceContextSynchronize(dctx));
 65:       PetscCall(WriteValues(A, type, dctx, input, PETSC_TRUE));
 66:       PetscCall((*copy)(A, 4, indexed ? idx : NULL, d ? device : host));
 67:       if (streamtype == PETSC_STREAM_NONBLOCKING_WITH_BARRIER) {
 68:         PetscBool idle;

 70:         PetscCall(PetscDeviceContextQueryIdle(dctx, &idle));
 71:         PetscCheck(idle, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Subarray copy returned with work pending on a barrier context");
 72:       }
 73:       if (d) {
 74:         PetscCall(PetscDeviceArrayCopy(dctx, host, device, 4));
 75:         PetscCall(PetscDeviceContextSynchronize(dctx));
 76:       }
 77:       for (PetscInt i = 0; i < 4; ++i)
 78:         PetscCheck(host[i] == input[indexed ? idx[i] : i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Subarray copy (device %" PetscInt_FMT ", indexed %" PetscInt_FMT ") overtook the device write at entry %" PetscInt_FMT, d, indexed, i);
 79:       PetscCall((*copy)(A, 0, NULL, d ? device : host));
 80:     }
 81:   }
 82:   PetscCall(PetscDeviceFree(dctx, device));
 83:   PetscCall(PetscDeviceFree(dctx, host));
 84:   PetscCall(PetscDeviceContextSynchronize(dctx));
 85:   PetscFunctionReturn(PETSC_SUCCESS);
 86: }

 88: static PetscErrorCode TestGetDiagonal(Mat A, PetscDeviceType type, PetscDeviceContext dctx, const PetscScalar input[])
 89: {
 90:   Vec                diag;
 91:   const PetscScalar *a;
 92:   PetscStreamType    streamtype;

 94:   PetscFunctionBeginUser;
 95:   PetscCall(PetscDeviceContextGetStreamType(dctx, &streamtype));
 96:   PetscCall(MatCreateVecs(A, &diag, NULL));
 97:   // Clear A so a stale diagonal differs from the values written after the delay.
 98:   PetscCall(MatZeroEntries(A));
 99:   // Warm the allocation and kernel before delaying the matrix values.
100:   PetscCall(MatGetDiagonal(A, diag));
101:   PetscCall(PetscDeviceContextSynchronize(dctx));
102:   PetscCall(WriteValues(A, type, dctx, input, PETSC_TRUE));
103:   PetscCall(MatGetDiagonal(A, diag));
104:   if (streamtype == PETSC_STREAM_NONBLOCKING_WITH_BARRIER) {
105:     PetscBool idle;

107:     PetscCall(PetscDeviceContextQueryIdle(dctx, &idle));
108:     PetscCheck(idle, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatGetDiagonal() returned with work pending on a barrier context");
109:   }
110:   PetscCall(PetscDeviceContextSynchronize(dctx));
111:   PetscCall(VecGetArrayRead(diag, &a));
112:   for (PetscInt i = 0; i < 4; ++i) PetscCheck(a[i] == input[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Incorrect diagonal entry %" PetscInt_FMT, i);
113:   PetscCall(VecRestoreArrayRead(diag, &a));
114:   PetscCall(VecDestroy(&diag));
115:   PetscFunctionReturn(PETSC_SUCCESS);
116: }

118: static PetscErrorCode TestDiagonalScale(Mat A, PetscDeviceType type, PetscDeviceContext dctx, const PetscScalar input[])
119: {
120:   Vec                left, right;
121:   const PetscScalar *a;
122:   PetscStreamType    streamtype;

124:   PetscFunctionBeginUser;
125:   PetscCall(PetscDeviceContextGetStreamType(dctx, &streamtype));
126:   PetscCall(MatCreateVecs(A, &right, &left));
127:   PetscCall(VecSet(left, 2));
128:   PetscCall(VecSet(right, 3));
129:   for (PetscInt mode = 0; mode < 3; ++mode) {
130:     PetscScalar factor = mode == 0 ? 2 : (mode == 1 ? 3 : 6);
131:     Vec         ll = mode == 1 ? NULL : left, rr = mode == 0 ? NULL : right;

133:     // Warm the scaling kernels before delaying the matrix values.
134:     PetscCall(MatDiagonalScale(A, ll, rr));
135:     PetscCall(PetscDeviceContextSynchronize(dctx));
136:     PetscCall(WriteValues(A, type, dctx, input, PETSC_TRUE));
137:     PetscCall(MatDiagonalScale(A, ll, rr));
138:     if (streamtype == PETSC_STREAM_NONBLOCKING_WITH_BARRIER) {
139:       PetscBool idle;

141:       PetscCall(PetscDeviceContextQueryIdle(dctx, &idle));
142:       PetscCheck(idle, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatDiagonalScale() mode %" PetscInt_FMT " returned with work pending on a barrier context", mode);
143:     }
144:     PetscCall(PetscDeviceContextSynchronize(dctx));
145:     PetscCall(MatSeqAIJGetArrayRead(A, &a));
146:     for (PetscInt i = 0; i < 4; ++i) PetscCheck(a[i] == factor * input[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatDiagonalScale() mode %" PetscInt_FMT ": incorrect entry %" PetscInt_FMT, mode, i);
147:     PetscCall(MatSeqAIJRestoreArrayRead(A, &a));
148:   }
149:   PetscCall(VecDestroy(&left));
150:   PetscCall(VecDestroy(&right));
151:   PetscFunctionReturn(PETSC_SUCCESS);
152: }

154: static PetscErrorCode TestGetIJ(PetscDeviceType type, PetscDeviceContext dctx)
155: {
156:   Mat             A;
157:   const PetscInt *i, *j;
158:   PetscInt       *rows, *cols;
159:   PetscBool       idle;
160:   void           *stream;

162:   PetscFunctionBeginUser;
163:   PetscCall(MatCreateSeqAIJ(PETSC_COMM_SELF, 4, 4, 1, NULL, &A));
164:   PetscCall(MatSetValue(A, 1, 2, 7, INSERT_VALUES));
165:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
166:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
167:   PetscCall(MatConvert(A, type == PETSC_DEVICE_CUDA ? MATSEQAIJCUSPARSE : MATSEQAIJHIPSPARSE, MAT_INPLACE_MATRIX, &A));
168:   PetscCall(PetscDeviceMalloc(dctx, PETSC_MEMTYPE_HOST, 5, PETSC_DECIDE, &rows));
169:   PetscCall(PetscDeviceMalloc(dctx, PETSC_MEMTYPE_HOST, 1, PETSC_DECIDE, &cols));
170:   PetscCall(PetscDeviceContextGetStreamHandle(dctx, &stream));
171:   // Build the compressed device matrix before testing the full row-offset upload.
172: #if PetscDefined(HAVE_CUDA)
173:   if (type == PETSC_DEVICE_CUDA) {
174:     PetscCall(MatSeqAIJCUSPARSEGetIJ(A, PETSC_TRUE, &i, &j));
175:     PetscCall(MatSeqAIJCUSPARSERestoreIJ(A, PETSC_TRUE, &i, &j));
176:   }
177: #endif
178: #if PetscDefined(HAVE_HIP)
179:   if (type == PETSC_DEVICE_HIP) {
180:     PetscCall(MatSeqAIJHIPSPARSEGetIJ(A, PETSC_TRUE, &i, &j));
181:     PetscCall(MatSeqAIJHIPSPARSERestoreIJ(A, PETSC_TRUE, &i, &j));
182:   }
183: #endif
184:   PetscCall(PetscDeviceContextSynchronize(dctx));
185:   PetscCall(PetscDeviceContextDelay(dctx, 0.2));
186: #if PetscDefined(HAVE_CUDA)
187:   if (type == PETSC_DEVICE_CUDA) PetscCall(MatSeqAIJCUSPARSEGetIJ(A, PETSC_FALSE, &i, &j));
188: #endif
189: #if PetscDefined(HAVE_HIP)
190:   if (type == PETSC_DEVICE_HIP) PetscCall(MatSeqAIJHIPSPARSEGetIJ(A, PETSC_FALSE, &i, &j));
191: #endif
192:   PetscCall(PetscDeviceContextQueryIdle(dctx, &idle));
193:   PetscCheck(idle, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatSeqAIJGetIJ() returned before its current-context upload completed");
194: #if PetscDefined(HAVE_CUDA)
195:   if (type == PETSC_DEVICE_CUDA) {
196:     PetscCallCUDA(cudaMemcpyAsync(rows, i, 5 * sizeof(*rows), cudaMemcpyDeviceToHost, *(cudaStream_t *)stream));
197:     PetscCallCUDA(cudaMemcpyAsync(cols, j, sizeof(*cols), cudaMemcpyDeviceToHost, *(cudaStream_t *)stream));
198:   }
199: #endif
200: #if PetscDefined(HAVE_HIP)
201:   if (type == PETSC_DEVICE_HIP) {
202:     PetscCallHIP(hipMemcpyAsync(rows, i, 5 * sizeof(*rows), hipMemcpyDeviceToHost, *(hipStream_t *)stream));
203:     PetscCallHIP(hipMemcpyAsync(cols, j, sizeof(*cols), hipMemcpyDeviceToHost, *(hipStream_t *)stream));
204:   }
205: #endif
206:   PetscCall(PetscDeviceContextSynchronize(dctx));
207:   for (PetscInt k = 0; k < 5; ++k) PetscCheck(rows[k] == (k > 1), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Incorrect row offset at entry %" PetscInt_FMT, k);
208:   PetscCheck(cols[0] == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Incorrect column index");
209: #if PetscDefined(HAVE_CUDA)
210:   if (type == PETSC_DEVICE_CUDA) PetscCall(MatSeqAIJCUSPARSERestoreIJ(A, PETSC_FALSE, &i, &j));
211: #endif
212: #if PetscDefined(HAVE_HIP)
213:   if (type == PETSC_DEVICE_HIP) PetscCall(MatSeqAIJHIPSPARSERestoreIJ(A, PETSC_FALSE, &i, &j));
214: #endif
215:   PetscCall(PetscDeviceFree(dctx, cols));
216:   PetscCall(PetscDeviceFree(dctx, rows));
217:   PetscCall(MatDestroy(&A));
218:   PetscFunctionReturn(PETSC_SUCCESS);
219: }

221: static PetscErrorCode SparsePointerMode(PetscDeviceType type, PetscDeviceContext dctx, PetscBool set, PetscBool device_mode)
222: {
223:   PetscFunctionBeginUser;
224: #if PetscDefined(HAVE_CUDA)
225:   if (type == PETSC_DEVICE_CUDA) {
226:     cusparseHandle_t      handle;
227:     cusparsePointerMode_t mode = device_mode ? CUSPARSE_POINTER_MODE_DEVICE : CUSPARSE_POINTER_MODE_HOST;

229:     PetscCall(PetscDeviceContextGetSPARSEHandle_Internal(dctx, &handle));
230:     if (set) PetscCallCUSPARSE(cusparseSetPointerMode(handle, mode));
231:     else {
232:       cusparsePointerMode_t actual;

234:       PetscCallCUSPARSE(cusparseGetPointerMode(handle, &actual));
235:       PetscCheck(actual == mode, PETSC_COMM_SELF, PETSC_ERR_PLIB, "cuSPARSE pointer mode was not restored");
236:     }
237:   }
238: #endif
239: #if PetscDefined(HAVE_HIP)
240:   if (type == PETSC_DEVICE_HIP) {
241:     hipsparseHandle_t      handle;
242:     hipsparsePointerMode_t mode = device_mode ? HIPSPARSE_POINTER_MODE_DEVICE : HIPSPARSE_POINTER_MODE_HOST;

244:     PetscCall(PetscDeviceContextGetSPARSEHandle_Internal(dctx, &handle));
245:     if (set) PetscCallHIPSPARSE(hipsparseSetPointerMode(handle, mode));
246:     else {
247:       hipsparsePointerMode_t actual;

249:       PetscCallHIPSPARSE(hipsparseGetPointerMode(handle, &actual));
250:       PetscCheck(actual == mode, PETSC_COMM_SELF, PETSC_ERR_PLIB, "hipSPARSE pointer mode was not restored");
251:     }
252:   }
253: #endif
254:   PetscFunctionReturn(PETSC_SUCCESS);
255: }

257: static PetscErrorCode TestSparseOperations(Mat A, PetscDeviceType type, PetscDeviceContext dctx, const PetscScalar input[])
258: {
259:   const PetscScalar  ones[] = {1, 1, 1, 1};
260:   const PetscScalar *a;
261:   PetscStreamType    streamtype;
262:   PetscBool          idle;
263:   Vec                x, y;
264:   Mat                B, C;

266:   PetscFunctionBeginUser;
267:   PetscCall(PetscDeviceContextGetStreamType(dctx, &streamtype));
268:   PetscCall(MatCreateVecs(A, &x, &y));
269:   PetscCall(VecSet(x, 1));
270:   PetscCall(SparsePointerMode(type, dctx, PETSC_TRUE, PETSC_FALSE));
271:   PetscCall(WriteValues(A, type, dctx, ones, PETSC_FALSE));
272:   PetscCall(MatMult(A, x, y));
273:   PetscCall(SparsePointerMode(type, dctx, PETSC_FALSE, PETSC_FALSE));
274:   PetscCall(PetscDeviceContextSynchronize(dctx));

276:   PetscCall(WriteValues(A, type, dctx, input, PETSC_TRUE));
277:   PetscCall(MatMult(A, x, y));
278:   if (streamtype == PETSC_STREAM_NONBLOCKING_WITH_BARRIER) {
279:     PetscCall(PetscDeviceContextQueryIdle(dctx, &idle));
280:     PetscCheck(idle, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatMult() returned with work pending on a barrier context");
281:   }
282:   PetscCall(PetscDeviceContextSynchronize(dctx));
283:   PetscCall(VecGetArrayRead(y, &a));
284:   for (PetscInt i = 0; i < 4; ++i) PetscCheck(a[i] == input[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatMult() returned an incorrect entry %" PetscInt_FMT, i);
285:   PetscCall(VecRestoreArrayRead(y, &a));

287:   PetscCall(MatSetOption(A, MAT_FORM_EXPLICIT_TRANSPOSE, PETSC_TRUE));
288:   PetscCall(MatMultTranspose(A, x, y));
289:   PetscCall(PetscDeviceContextSynchronize(dctx));
290:   PetscCall(VecSet(x, 0));
291:   PetscCall(PetscDeviceContextSynchronize(dctx));
292:   PetscCall(PetscDeviceContextDelay(dctx, 0.05));
293:   PetscCall(VecSet(x, 2));
294:   PetscCall(MatMultTranspose(A, x, y));
295:   if (streamtype == PETSC_STREAM_NONBLOCKING_WITH_BARRIER) {
296:     PetscCall(PetscDeviceContextQueryIdle(dctx, &idle));
297:     PetscCheck(idle, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatMultTranspose() returned with work pending on a barrier context");
298:   }
299:   PetscCall(PetscDeviceContextSynchronize(dctx));
300:   PetscCall(VecGetArrayRead(y, &a));
301:   for (PetscInt i = 0; i < 4; ++i) PetscCheck(a[i] == 2 * input[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatMultTranspose() returned an incorrect entry %" PetscInt_FMT, i);
302:   PetscCall(VecRestoreArrayRead(y, &a));
303:   PetscCall(WriteValues(A, type, dctx, ones, PETSC_TRUE));
304:   PetscCall(MatMultTranspose(A, x, y));
305:   if (streamtype == PETSC_STREAM_NONBLOCKING_WITH_BARRIER) {
306:     PetscCall(PetscDeviceContextQueryIdle(dctx, &idle));
307:     PetscCheck(idle, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatMultTranspose() returned with a stale transpose update pending on a barrier context");
308:   }
309:   PetscCall(PetscDeviceContextSynchronize(dctx));
310:   PetscCall(VecGetArrayRead(y, &a));
311:   for (PetscInt i = 0; i < 4; ++i) PetscCheck(a[i] == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatMultTranspose() used stale matrix values at entry %" PetscInt_FMT, i);
312:   PetscCall(VecRestoreArrayRead(y, &a));
313:   PetscCall(MatSetOption(A, MAT_FORM_EXPLICIT_TRANSPOSE, PETSC_FALSE));
314:   PetscCall(VecSet(x, 1));
315:   PetscCall(WriteValues(A, type, dctx, input, PETSC_FALSE));

317:   PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &B));
318:   PetscCall(WriteValues(B, type, dctx, ones, PETSC_FALSE));
319:   PetscCall(MatMatMult(A, B, MAT_INITIAL_MATRIX, 1.0, &C));
320:   PetscCall(PetscDeviceContextSynchronize(dctx));
321:   PetscCall(WriteValues(B, type, dctx, input, PETSC_TRUE));
322:   PetscCall(MatMatMult(A, B, MAT_REUSE_MATRIX, 1.0, &C));
323:   PetscCall(SparsePointerMode(type, dctx, PETSC_FALSE, PETSC_FALSE));
324:   if (streamtype == PETSC_STREAM_NONBLOCKING_WITH_BARRIER) {
325:     PetscCall(PetscDeviceContextQueryIdle(dctx, &idle));
326:     PetscCheck(idle, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatMatMult() returned with work pending on a barrier context");
327:   }
328:   PetscCall(PetscDeviceContextSynchronize(dctx));
329:   PetscCall(MatSeqAIJGetArrayRead(C, &a));
330:   for (PetscInt i = 0; i < 4; ++i) PetscCheck(a[i] == input[i] * input[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatMatMult() returned an incorrect entry %" PetscInt_FMT, i);
331:   PetscCall(MatSeqAIJRestoreArrayRead(C, &a));
332:   PetscCall(MatDestroy(&C));
333:   PetscCall(MatDestroy(&B));
334:   PetscCall(VecDestroy(&y));
335:   PetscCall(VecDestroy(&x));
336:   PetscFunctionReturn(PETSC_SUCCESS);
337: }

339: static PetscErrorCode TestSparseSolve(PetscDeviceType type, PetscDeviceContext saved, PetscDeviceContext dctx)
340: {
341:   Mat                A, F;
342:   IS                 perm;
343:   Vec                b, x;
344:   MatFactorInfo      info;
345:   PetscStreamType    streamtype;
346:   PetscBool          idle;
347:   const PetscScalar *a;

349:   PetscFunctionBeginUser;
350:   PetscCall(PetscDeviceContextSetCurrentContext(saved));
351:   PetscCall(MatCreateSeqAIJ(PETSC_COMM_SELF, 4, 4, 1, NULL, &A));
352:   for (PetscInt i = 0; i < 4; ++i) PetscCall(MatSetValue(A, i, i, 2, INSERT_VALUES));
353:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
354:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
355:   PetscCall(MatConvert(A, type == PETSC_DEVICE_CUDA ? MATSEQAIJCUSPARSE : MATSEQAIJHIPSPARSE, MAT_INPLACE_MATRIX, &A));
356:   PetscCall(ISCreateStride(PETSC_COMM_SELF, 4, 0, 1, &perm));
357:   PetscCall(MatCreateVecs(A, &b, &x));
358:   PetscCall(MatFactorInfoInitialize(&info));
359:   PetscCall(PetscDeviceContextGetStreamType(dctx, &streamtype));
360:   for (PetscInt kind = 0; kind < 2; ++kind) {
361:     MatFactorType factortype = kind ? MAT_FACTOR_CHOLESKY : MAT_FACTOR_LU;

363:     PetscCall(PetscDeviceContextSetCurrentContext(saved));
364:     PetscCall(MatGetFactor(A, type == PETSC_DEVICE_CUDA ? MATSOLVERCUSPARSE : MATSOLVERHIPSPARSE, factortype, &F));
365:     PetscCall(SparsePointerMode(type, saved, PETSC_TRUE, PETSC_TRUE));
366:     if (kind) {
367:       PetscCall(MatCholeskyFactorSymbolic(F, A, perm, &info));
368:       PetscCall(MatCholeskyFactorNumeric(F, A, &info));
369:     } else {
370:       PetscCall(MatLUFactorSymbolic(F, A, perm, perm, &info));
371:       PetscCall(MatLUFactorNumeric(F, A, &info));
372:     }
373:     PetscCall(SparsePointerMode(type, saved, PETSC_FALSE, PETSC_TRUE));
374:     PetscCall(SparsePointerMode(type, saved, PETSC_TRUE, PETSC_FALSE));
375:     PetscCall(PetscDeviceContextSynchronize(saved));
376:     PetscCall(PetscDeviceContextSetCurrentContext(dctx));
377:     for (PetscInt solve = 0; solve < 3; ++solve) {
378:       PetscCall(VecSet(b, 0));
379:       PetscCall(VecSet(x, 0));
380:       PetscCall(PetscDeviceContextSynchronize(dctx));
381:       PetscCall(PetscDeviceContextDelay(dctx, 0.05));
382:       PetscCall(VecSet(b, 2));
383:       PetscCall(SparsePointerMode(type, dctx, PETSC_TRUE, PETSC_TRUE));
384:       if (solve == 1) PetscCall(MatSolveTranspose(F, b, x));
385:       else PetscCall(MatSolve(F, b, x));
386:       PetscCall(SparsePointerMode(type, dctx, PETSC_FALSE, PETSC_TRUE));
387:       PetscCall(SparsePointerMode(type, dctx, PETSC_TRUE, PETSC_FALSE));
388:       if (streamtype == PETSC_STREAM_NONBLOCKING_WITH_BARRIER) {
389:         PetscCall(PetscDeviceContextQueryIdle(dctx, &idle));
390:         PetscCheck(idle, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatSolve() returned with work pending on a barrier context");
391:       }
392:       PetscCall(PetscDeviceContextSynchronize(dctx));
393:       PetscCall(VecGetArrayRead(x, &a));
394:       for (PetscInt i = 0; i < 4; ++i) PetscCheck(PetscAbsScalar(a[i] - 1) < 100 * PETSC_MACHINE_EPSILON, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatSolve() returned an incorrect entry %" PetscInt_FMT, i);
395:       PetscCall(VecRestoreArrayRead(x, &a));
396:     }
397:     PetscCall(MatDestroy(&F));
398:   }
399:   PetscCall(VecDestroy(&x));
400:   PetscCall(VecDestroy(&b));
401:   PetscCall(ISDestroy(&perm));
402:   PetscCall(MatDestroy(&A));
403:   PetscFunctionReturn(PETSC_SUCCESS);
404: }

406: static PetscErrorCode WriteVector(Vec x, PetscDeviceType type, PetscDeviceContext dctx, const PetscScalar input[])
407: {
408:   PetscScalar *a;
409:   PetscInt     n;
410:   PetscMemType mtype;
411:   void        *stream;

413:   PetscFunctionBeginUser;
414:   PetscCall(VecGetLocalSize(x, &n));
415:   PetscCall(VecGetArrayWriteAndMemType(x, &a, &mtype));
416:   PetscCheck(PetscMemTypeDevice(mtype), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expected a device array");
417:   PetscCall(PetscDeviceContextGetStreamHandle(dctx, &stream));
418:   PetscCall(PetscDeviceContextDelay(dctx, 0.05));
419: #if PetscDefined(HAVE_CUDA)
420:   if (type == PETSC_DEVICE_CUDA) PetscCallCUDA(cudaMemcpyAsync(a, input, n * sizeof(*a), cudaMemcpyHostToDevice, *(cudaStream_t *)stream));
421: #endif
422: #if PetscDefined(HAVE_HIP)
423:   if (type == PETSC_DEVICE_HIP) PetscCallHIP(hipMemcpyAsync(a, input, n * sizeof(*a), hipMemcpyHostToDevice, *(hipStream_t *)stream));
424: #endif
425:   PetscCall(VecRestoreArrayWriteAndMemType(x, &a));
426:   PetscFunctionReturn(PETSC_SUCCESS);
427: }

429: static PetscErrorCode CheckBarrier(PetscDeviceContext dctx)
430: {
431:   PetscStreamType streamtype;
432:   PetscBool       idle;

434:   PetscFunctionBeginUser;
435:   PetscCall(PetscDeviceContextGetStreamType(dctx, &streamtype));
436:   if (streamtype == PETSC_STREAM_NONBLOCKING_WITH_BARRIER) {
437:     PetscCall(PetscDeviceContextQueryIdle(dctx, &idle));
438:     PetscCheck(idle, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Operation returned with work pending on a barrier context");
439:   }
440:   PetscFunctionReturn(PETSC_SUCCESS);
441: }

443: static PetscErrorCode TestSELL(PetscDeviceType type, PetscDeviceContext dctx)
444: {
445:   Mat                A, B;
446:   Vec                x, y, z;
447:   PetscScalar       *input;
448:   const PetscScalar *a;
449:   const PetscScalar  expected[] = {3, 3, 5, 9, 5, 8, 10};
450: #if PetscDefined(USE_LOG) && PetscDefined(HAVE_DEVICE)
451:   PetscLogEvent      event;
452:   PetscEventPerfInfo before, after;
453:   MatInfo            info;
454: #endif

456:   PetscFunctionBeginUser;
457: #if PetscDefined(USE_LOG) && PetscDefined(HAVE_DEVICE)
458:   PetscCall(PetscLogDefaultBegin());
459:   PetscCall(PetscLogEventRegister("SELLUpdate", MAT_CLASSID, &event));
460: #endif
461:   PetscCall(PetscDeviceMalloc(dctx, PETSC_MEMTYPE_HOST, 32, PETSC_DECIDE, &input));
462:   PetscCall(PetscDeviceContextSynchronize(dctx));
463:   for (PetscInt i = 0; i < 32; ++i) input[i] = i + 1;
464:   PetscCall(MatCreate(PETSC_COMM_SELF, &A));
465:   PetscCall(MatSetSizes(A, 32, 32, 32, 32));
466:   PetscCall(MatSetType(A, MATSEQSELL));
467:   PetscCall(MatSetUp(A));
468:   for (PetscInt i = 0; i < 32; ++i) PetscCall(MatSetValue(A, i, i, 2, INSERT_VALUES));
469:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
470:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
471:   PetscCall(MatConvert(A, type == PETSC_DEVICE_CUDA ? MATSEQSELLCUDA : MATSEQSELLHIP, MAT_INPLACE_MATRIX, &A));
472:   PetscCall(MatSetFromOptions(A));
473:   PetscCall(MatCreateVecs(A, &x, &y));
474:   PetscCall(VecDuplicate(y, &z));
475:   PetscCall(VecSet(x, 1));
476:   PetscCall(VecSet(y, 1));
477:   // Destroying a duplicate before its first upload must release its backend data.
478:   PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &B));
479:   PetscCall(MatDestroy(&B));
480:   // MatMultAdd() must also work before any multiply has initialized the device arrays.
481:   PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &B));
482:   PetscCall(MatSetFromOptions(B));
483:   for (size_t stage = 0; stage < PETSC_STATIC_ARRAY_LENGTH(expected); ++stage) {
484:     Mat C = stage == 0 ? A : B;

486: #if PetscDefined(USE_LOG) && PetscDefined(HAVE_DEVICE)
487:     PetscCall(PetscLogEventGetPerfInfo(PETSC_DETERMINE, event, &before));
488:     PetscCall(PetscLogEventBegin(event, C, 0, 0, 0));
489: #endif
490:     if (stage == 2) {
491:       PetscCall(MatScale(A, 2));
492:       PetscCall(MatCopy(A, B, SAME_NONZERO_PATTERN));
493:       PetscCall(MatScale(A, 0.5));
494:     } else if (stage == 3) {
495:       PetscCall(MatSetOption(C, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE));
496:       PetscCall(MatStoreValues(C));
497:       PetscCall(MatScale(C, 2));
498:     } else if (stage == 4) PetscCall(MatRetrieveValues(C));
499:     else if (stage == 5) {
500:       PetscCall(MatSetOption(C, MAT_NEW_NONZERO_LOCATIONS, PETSC_TRUE));
501:       PetscCall(MatSetOption(C, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
502:       for (PetscInt i = 0; i < 32; ++i) PetscCall(MatSetValue(C, i, (i + 1) % 32, 3, INSERT_VALUES));
503:       PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
504:       PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
505:     } else if (stage == 6) {
506:       for (PetscInt i = 0; i < 32; ++i) PetscCall(MatSetValue(C, i, i, 6, INSERT_VALUES));
507:       PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
508:       PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
509:     }
510:     PetscCall(MatMultAdd(C, x, y, z));
511:     PetscCall(CheckBarrier(dctx));
512:     PetscCall(PetscDeviceContextSynchronize(dctx));
513: #if PetscDefined(USE_LOG) && PetscDefined(HAVE_DEVICE)
514:     PetscCall(PetscLogEventEnd(event, C, 0, 0, 0));
515:     if (stage == 2 || stage == 3 || stage == 4 || stage == 6) {
516:       PetscCall(PetscLogEventGetPerfInfo(PETSC_DETERMINE, event, &after));
517:       PetscCall(MatGetInfo(C, MAT_LOCAL, &info));
518:       PetscCheck(after.CpuToGpuSize - before.CpuToGpuSize == info.nz_used * sizeof(PetscScalar), PETSC_COMM_SELF, PETSC_ERR_PLIB, "SELL values-only update stage %zu uploaded %g bytes instead of %g", stage, after.CpuToGpuSize - before.CpuToGpuSize, info.nz_used * sizeof(PetscScalar));
519:     }
520: #endif
521:     PetscCall(VecGetArrayRead(z, &a));
522:     for (PetscInt i = 0; i < 32; ++i) PetscCheck(a[i] == expected[stage], PETSC_COMM_SELF, PETSC_ERR_PLIB, "SELL MatMultAdd() stage %zu returned an incorrect entry %" PetscInt_FMT, stage, i);
523:     PetscCall(VecRestoreArrayRead(z, &a));
524:   }
525:   PetscCall(MatDestroy(&B));
526:   PetscCall(MatMult(A, x, z));
527:   PetscCall(PetscDeviceContextSynchronize(dctx));

529:   for (PetscInt op = 0; op < 4; ++op) {
530:     PetscCall(VecSet(x, 0));
531:     PetscCall(PetscDeviceContextSynchronize(dctx));
532:     PetscCall(WriteVector(x, type, dctx, input));
533:     if (op == 1) PetscCall(MatMultAdd(A, x, y, z));
534:     else PetscCall(MatMult(A, x, z));
535:     PetscCall(CheckBarrier(dctx));
536:     if (op == 2) {
537:       // Upload new values after a queued multiply has consumed the old values.
538:       for (PetscInt i = 0; i < 32; ++i) PetscCall(MatSetValue(A, i, i, 3, INSERT_VALUES));
539:       PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
540:       PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
541:     } else if (op == 3) {
542:       PetscCall(PetscDeviceContextDelay(dctx, 0.05));
543:       PetscCall(MatZeroEntries(A));
544:       PetscCall(CheckBarrier(dctx));
545:     }
546:     PetscCall(PetscDeviceContextSynchronize(dctx));
547:     PetscCall(VecGetArrayRead(z, &a));
548:     for (PetscInt i = 0; i < 32; ++i) PetscCheck(a[i] == (op == 3 ? 3 : 2) * input[i] + (op == 1), PETSC_COMM_SELF, PETSC_ERR_PLIB, "SELL operation %" PetscInt_FMT " returned an incorrect entry %" PetscInt_FMT, op, i);
549:     PetscCall(VecRestoreArrayRead(z, &a));
550:   }
551:   PetscCall(MatMult(A, x, z));
552:   PetscCall(PetscDeviceContextSynchronize(dctx));
553:   PetscCall(VecGetArrayRead(z, &a));
554:   for (PetscInt i = 0; i < 32; ++i) PetscCheck(a[i] == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "SELL zeroing left a nonzero entry %" PetscInt_FMT, i);
555:   PetscCall(VecRestoreArrayRead(z, &a));
556:   // An empty matrix still needs device slice offsets before values-only updates.
557:   PetscCall(MatCreateSeqSELL(PETSC_COMM_SELF, 32, 32, 0, NULL, &B));
558:   PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
559:   PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
560:   PetscCall(MatConvert(B, type == PETSC_DEVICE_CUDA ? MATSEQSELLCUDA : MATSEQSELLHIP, MAT_INPLACE_MATRIX, &B));
561:   PetscCall(MatSetFromOptions(B));
562:   for (PetscInt stage = 0; stage < 3; ++stage) {
563:     if (stage == 1) PetscCall(MatScale(B, 2));
564:     else if (stage == 2) {
565:       PetscCall(MatSetOption(B, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
566:       for (PetscInt i = 0; i < 32; ++i) PetscCall(MatSetValue(B, i, i, 2, INSERT_VALUES));
567:       PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
568:       PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
569:     }
570:     PetscCall(MatMult(B, x, z));
571:     PetscCall(CheckBarrier(dctx));
572:     PetscCall(PetscDeviceContextSynchronize(dctx));
573:     PetscCall(VecGetArrayRead(z, &a));
574:     for (PetscInt i = 0; i < 32; ++i) PetscCheck(a[i] == (stage == 2 ? 2 * input[i] : 0), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Initially empty SELL matrix stage %" PetscInt_FMT " returned an incorrect entry %" PetscInt_FMT, stage, i);
575:     PetscCall(VecRestoreArrayRead(z, &a));
576:   }
577:   PetscCall(MatDestroy(&B));
578:   PetscCall(VecDestroy(&z));
579:   PetscCall(VecDestroy(&y));
580:   PetscCall(VecDestroy(&x));
581:   PetscCall(MatDestroy(&A));
582:   PetscCall(PetscDeviceFree(dctx, input));
583:   PetscCall(PetscDeviceContextSynchronize(dctx));
584:   PetscFunctionReturn(PETSC_SUCCESS);
585: }

587: static PetscErrorCode TestDense(MPI_Comm comm, PetscDeviceType type, PetscDeviceContext dctx, const PetscScalar input[])
588: {
589:   PetscErrorCode (*mult)(Mat, Vec, Vec, PetscInt, PetscInt) = NULL;
590:   Mat                A;
591:   Vec                x, y;
592:   PetscScalar       *a;
593:   const PetscScalar *b;
594:   PetscMPIInt        rank, size;
595:   PetscInt           n;

597:   PetscFunctionBeginUser;
598:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
599:   PetscCallMPI(MPI_Comm_size(comm, &size));
600:   PetscCall(MatCreate(comm, &A));
601:   PetscCall(MatSetSizes(A, 4, rank ? 0 : 4, PETSC_DECIDE, 4));
602:   PetscCall(MatSetType(A, type == PETSC_DEVICE_CUDA ? MATDENSECUDA : MATDENSEHIP));
603:   PetscCall(MatSetUp(A));
604:   PetscCall(MatDenseGetArrayWrite(A, &a));
605:   for (PetscInt j = 0; j < 4; ++j)
606:     for (PetscInt i = 0; i < 4; ++i) a[4 * j + i] = i == j ? 2 : 0;
607:   PetscCall(MatDenseRestoreArrayWrite(A, &a));
608:   PetscCall(MatCreateVecs(A, &y, &x));
609:   PetscCall(VecSet(x, 1));
610:   PetscCall(VecSet(y, 0));
611:   PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMultHermitianTransposeColumnRange_C", &mult));
612:   PetscCheck(mult, comm, PETSC_ERR_PLIB, "Missing dense column-range multiply implementation");
613:   PetscCall((*mult)(A, x, y, 1, 3));
614:   PetscCall(PetscDeviceContextSynchronize(dctx));
615:   PetscCall(WriteVector(y, type, dctx, input));
616:   PetscCall((*mult)(A, x, y, 1, 3));
617:   PetscCall(PetscDeviceContextSynchronize(dctx));
618:   PetscCall(VecGetLocalSize(y, &n));
619:   PetscCall(VecGetArrayRead(y, &b));
620:   for (PetscInt i = 0; i < n; ++i) PetscCheck(b[i] == ((i == 1 || i == 2) ? 2 * size : input[i]), comm, PETSC_ERR_PLIB, "Dense column-range multiply returned an incorrect entry %" PetscInt_FMT, i);
621:   PetscCall(VecRestoreArrayRead(y, &b));
622:   PetscCall(MatDenseGetArrayRead(A, &b));
623:   for (PetscInt j = 0; j < 4; ++j)
624:     for (PetscInt i = 0; i < 4; ++i) PetscCheck(b[4 * j + i] == (i == j ? 2 : 0), comm, PETSC_ERR_PLIB, "Incorrect dense array entry");
625:   PetscCall(MatDenseRestoreArrayRead(A, &b));
626:   if (size == 1) {
627:     Mat B, X;
628:     IS  perm;

630:     PetscCall(ISCreateStride(comm, 4, 0, 1, &perm));
631:     PetscCall(MatLUFactor(A, perm, perm, NULL));
632:     PetscCall(MatCreateSeqDense(comm, 4, 2, NULL, &B));
633:     PetscCall(MatDuplicate(B, MAT_DO_NOT_COPY_VALUES, &X));
634:     PetscCall(MatDenseGetArrayWrite(B, &a));
635:     for (PetscInt j = 0; j < 2; ++j)
636:       for (PetscInt i = 0; i < 4; ++i) a[4 * j + i] = (j + 1) * input[i];
637:     PetscCall(MatDenseRestoreArrayWrite(B, &a));
638:     // Host output requires a temporary device array on the solve's stream.
639:     PetscCall(MatMatSolve(A, B, X));
640:     PetscCall(PetscDeviceContextSynchronize(dctx));
641:     // Change the right-hand side so a stale solution differs from the expected one.
642:     PetscCall(MatScale(B, 2));
643:     PetscCall(PetscDeviceContextDelay(dctx, 0.05));
644:     PetscCall(MatMatSolve(A, B, X));
645:     PetscCall(PetscDeviceContextSynchronize(dctx));
646:     PetscCall(MatDenseGetArrayRead(X, &b));
647:     for (PetscInt j = 0; j < 2; ++j)
648:       for (PetscInt i = 0; i < 4; ++i) PetscCheck(PetscAbsScalar(b[4 * j + i] - (j + 1) * input[i]) < PETSC_SMALL, comm, PETSC_ERR_PLIB, "Incorrect dense solve entry");
649:     PetscCall(MatDenseRestoreArrayRead(X, &b));
650:     PetscCall(MatDestroy(&X));
651:     PetscCall(MatDestroy(&B));
652:     PetscCall(ISDestroy(&perm));
653:   }
654:   PetscCall(VecDestroy(&y));
655:   PetscCall(VecDestroy(&x));
656:   PetscCall(MatDestroy(&A));
657:   PetscFunctionReturn(PETSC_SUCCESS);
658: }

660: #if !PetscDefined(USE_COMPLEX)
661: static PetscErrorCode TestSELLChunks(PetscDeviceType type)
662: {
663:   Mat                A, B, C;
664:   Vec                x, y, z, x_ref, y_ref;
665:   const PetscScalar *a, *a_ref;
666:   PetscReal          avgwidth;
667:   PetscInt           maxwidth;

669:   PetscFunctionBeginUser;
670:   PetscCall(MatCreateSeqAIJ(PETSC_COMM_SELF, 512, 512, 0, NULL, &A));
671:   PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
672:   for (PetscInt i = 0; i < 512; ++i) PetscCall(MatSetValue(A, i, i, 2, INSERT_VALUES));
673:   // One wide slice makes the default kernel selection use the chunk map.
674:   for (PetscInt j = 1; j < 256; ++j) PetscCall(MatSetValue(A, 0, j, 1, INSERT_VALUES));
675:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
676:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
677:   PetscCall(MatConvert(A, type == PETSC_DEVICE_CUDA ? MATSEQSELLCUDA : MATSEQSELLHIP, MAT_INITIAL_MATRIX, &B));
678:   PetscCall(MatSeqSELLGetMaxSliceWidth(B, &maxwidth));
679:   PetscCall(MatSeqSELLGetAvgSliceWidth(B, &avgwidth));
680:   PetscCheck(maxwidth / avgwidth > 12 && maxwidth / avgwidth / 512 > 0.001, PETSC_COMM_SELF, PETSC_ERR_PLIB, "SELL regression must select the chunked kernel");
681:   PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &C));
682:   PetscCall(MatDestroy(&B));
683:   PetscCall(MatSetOption(C, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
684:   PetscCall(MatCreateVecs(C, &x, &y));
685:   PetscCall(VecDuplicate(y, &z));
686:   PetscCall(MatCreateVecs(A, &x_ref, &y_ref));
687:   PetscCall(VecSet(x, 1));
688:   PetscCall(VecSet(x_ref, 1));
689:   for (PetscInt stage = 0; stage < 3; ++stage) {
690:     if (stage) {
691:       PetscCall(MatSetValue(A, 16, 17, stage, INSERT_VALUES));
692:       PetscCall(MatSetValue(C, 16, 17, stage, INSERT_VALUES));
693:       PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
694:       PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
695:       PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
696:       PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
697:     }
698:     PetscCall(MatMult(A, x_ref, y_ref));
699:     PetscCall(MatMult(C, x, y));
700:     PetscCall(MatMultAdd(C, x, y, z));
701:     PetscCall(VecGetArrayRead(y_ref, &a_ref));
702:     for (PetscInt add = 0; add < 2; ++add) {
703:       PetscCall(VecGetArrayRead(add ? z : y, &a));
704:       for (PetscInt i = 0; i < 512; ++i) PetscCheck(a[i] == (add + 1) * a_ref[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "SELL chunked multiply stage %" PetscInt_FMT " add %" PetscInt_FMT " returned an incorrect entry %" PetscInt_FMT, stage, add, i);
705:       PetscCall(VecRestoreArrayRead(add ? z : y, &a));
706:     }
707:     PetscCall(VecRestoreArrayRead(y_ref, &a_ref));
708:   }
709:   PetscCall(VecDestroy(&x_ref));
710:   PetscCall(VecDestroy(&y_ref));
711:   PetscCall(VecDestroy(&x));
712:   PetscCall(VecDestroy(&y));
713:   PetscCall(VecDestroy(&z));
714:   PetscCall(MatDestroy(&A));
715:   PetscCall(MatDestroy(&C));
716:   PetscFunctionReturn(PETSC_SUCCESS);
717: }
718: #endif

720: static PetscErrorCode TestSparseSolves(PetscDeviceType type)
721: {
722:   Mat                A, F;
723:   Vec                b, x;
724:   IS                 perm;
725:   MatFactorInfo      info;
726:   const PetscScalar *a;

728:   PetscFunctionBeginUser;
729:   PetscCall(MatCreateSeqAIJ(PETSC_COMM_SELF, 4, 4, 1, NULL, &A));
730:   for (PetscInt i = 0; i < 4; ++i) PetscCall(MatSetValue(A, i, i, 2, INSERT_VALUES));
731:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
732:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
733:   PetscCall(MatConvert(A, type == PETSC_DEVICE_CUDA ? MATSEQAIJCUSPARSE : MATSEQAIJHIPSPARSE, MAT_INPLACE_MATRIX, &A));
734:   PetscCall(MatCreateVecs(A, &b, &x));
735:   PetscCall(VecSet(b, 2));
736:   PetscCall(ISCreateStride(PETSC_COMM_SELF, 4, 0, 1, &perm));
737:   PetscCall(MatFactorInfoInitialize(&info));
738:   for (PetscInt kind = 0; kind < 2; ++kind) {
739:     PetscCall(MatGetFactor(A, type == PETSC_DEVICE_CUDA ? MATSOLVERCUSPARSE : MATSOLVERHIPSPARSE, kind ? MAT_FACTOR_CHOLESKY : MAT_FACTOR_LU, &F));
740:     if (kind) {
741:       PetscCall(MatCholeskyFactorSymbolic(F, A, perm, &info));
742:       PetscCall(MatCholeskyFactorNumeric(F, A, &info));
743:     } else {
744:       PetscCall(MatLUFactorSymbolic(F, A, perm, perm, &info));
745:       PetscCall(MatLUFactorNumeric(F, A, &info));
746:     }
747:     for (PetscInt transpose = 0; transpose < 2; ++transpose) {
748:       if (transpose) PetscCall(MatSolveTranspose(F, b, x));
749:       else PetscCall(MatSolve(F, b, x));
750:       PetscCall(VecGetArrayRead(x, &a));
751:       for (PetscInt i = 0; i < 4; ++i) PetscCheck(PetscAbsScalar(a[i] - 1) <= 10 * PETSC_MACHINE_EPSILON, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Incorrect sparse solve at entry %" PetscInt_FMT, i);
752:       PetscCall(VecRestoreArrayRead(x, &a));
753:     }
754:     PetscCall(MatDestroy(&F));
755:   }
756:   PetscCall(ISDestroy(&perm));
757:   PetscCall(VecDestroy(&x));
758:   PetscCall(VecDestroy(&b));
759:   PetscCall(MatDestroy(&A));
760:   PetscFunctionReturn(PETSC_SUCCESS);
761: }

763: static PetscErrorCode TestTransposeUpdate(PetscDeviceType type)
764: {
765:   Mat                A;
766:   Vec                x, y;
767:   PetscScalar       *values;
768:   const PetscScalar *a;
769:   const PetscScalar  ones[] = {1, 1, 1, 1};

771:   PetscFunctionBeginUser;
772:   PetscCall(MatCreateSeqAIJ(PETSC_COMM_SELF, 4, 4, 1, NULL, &A));
773:   for (PetscInt i = 0; i < 4; ++i) PetscCall(MatSetValue(A, i, i, 2, INSERT_VALUES));
774:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
775:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
776:   PetscCall(MatConvert(A, type == PETSC_DEVICE_CUDA ? MATSEQAIJCUSPARSE : MATSEQAIJHIPSPARSE, MAT_INPLACE_MATRIX, &A));
777:   PetscCall(MatSetOption(A, MAT_FORM_EXPLICIT_TRANSPOSE, PETSC_TRUE));
778:   PetscCall(MatCreateVecs(A, &x, &y));
779:   PetscCall(VecSet(x, 2));
780:   PetscCall(MatMultTranspose(A, x, y));
781: #if PetscDefined(HAVE_CUDA)
782:   if (type == PETSC_DEVICE_CUDA) {
783:     PetscCall(MatSeqAIJCUSPARSEGetArray(A, &values));
784:     PetscCallCUDA(cudaMemcpy(values, ones, sizeof(ones), cudaMemcpyHostToDevice));
785:     PetscCall(MatSeqAIJCUSPARSERestoreArray(A, &values));
786:   }
787: #endif
788: #if PetscDefined(HAVE_HIP)
789:   if (type == PETSC_DEVICE_HIP) {
790:     PetscCall(MatSeqAIJHIPSPARSEGetArray(A, &values));
791:     PetscCallHIP(hipMemcpy(values, ones, sizeof(ones), hipMemcpyHostToDevice));
792:     PetscCall(MatSeqAIJHIPSPARSERestoreArray(A, &values));
793:   }
794: #endif
795:   PetscCall(MatMultTranspose(A, x, y));
796:   PetscCall(VecGetArrayRead(y, &a));
797:   for (PetscInt i = 0; i < 4; ++i) PetscCheck(a[i] == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Transpose multiply used stale values at entry %" PetscInt_FMT, i);
798:   PetscCall(VecRestoreArrayRead(y, &a));
799:   PetscCall(VecDestroy(&y));
800:   PetscCall(VecDestroy(&x));
801:   PetscCall(MatDestroy(&A));
802:   PetscFunctionReturn(PETSC_SUCCESS);
803: }

805: int main(int argc, char **argv)
806: {
807:   Mat                   A, B;
808:   PetscDeviceContext    saved, current;
809:   PetscDevice           device;
810:   PetscDeviceType       type;
811:   PetscScalar          *input;
812:   const PetscScalar    *a;
813:   const PetscStreamType streams[] = {PETSC_STREAM_NONBLOCKING, PETSC_STREAM_NONBLOCKING_WITH_BARRIER};

815:   PetscFunctionBeginUser;
816:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
817:   PetscCall(PetscDeviceContextGetCurrentContext(&saved));
818:   PetscCall(PetscDeviceContextGetDevice(saved, &device));
819:   PetscCall(PetscDeviceGetType(device, &type));
820: #if !PetscDefined(USE_COMPLEX)
821:   PetscCall(TestSELLChunks(type));
822: #endif
823:   PetscCall(TestSparseSolves(type));
824:   PetscCall(TestTransposeUpdate(type));
825:   PetscCall(PetscDeviceMalloc(saved, PETSC_MEMTYPE_HOST, 4, PETSC_DECIDE, &input));
826:   PetscCall(PetscDeviceContextSynchronize(saved));
827:   for (PetscInt i = 0; i < 4; ++i) input[i] = i + 1;
828:   PetscCall(MatCreateSeqAIJ(PETSC_COMM_SELF, 4, 4, 1, NULL, &A));
829:   for (PetscInt i = 0; i < 4; ++i) PetscCall(MatSetValue(A, i, i, 1, INSERT_VALUES));
830:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
831:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
832:   PetscCall(MatConvert(A, type == PETSC_DEVICE_CUDA ? MATSEQAIJCUSPARSE : MATSEQAIJHIPSPARSE, MAT_INPLACE_MATRIX, &A));
833:   PetscCall(WriteValues(A, type, saved, input, PETSC_FALSE));
834:   PetscCall(PetscDeviceContextSynchronize(saved));
835:   PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &B));
836:   PetscCall(PetscDeviceContextSynchronize(saved));

838:   for (size_t k = 0; k < PETSC_STATIC_ARRAY_LENGTH(streams); ++k) {
839:     PetscCall(PetscDeviceContextDuplicate(saved, &current));
840:     PetscCall(PetscDeviceContextSetStreamType(current, streams[k]));
841:     PetscCall(PetscDeviceContextSetUp(current));
842:     PetscCall(PetscDeviceContextSetCurrentContext(current));
843:     // Warm the zeroing operation before delaying the input write.
844:     PetscCall(MatZeroEntries(A));
845:     PetscCall(PetscDeviceContextSynchronize(current));
846:     PetscCall(WriteValues(A, type, current, input, PETSC_TRUE));
847:     PetscCall(MatZeroEntries(A));
848:     PetscCall(PetscDeviceContextSynchronize(current));
849:     PetscCall(MatSeqAIJGetArrayRead(A, &a));
850:     for (PetscInt i = 0; i < 4; ++i) PetscCheck(a[i] == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Stream type %s: CSR entry %" PetscInt_FMT " is %g instead of zero", PetscStreamTypes[streams[k]], i, (double)PetscRealPart(a[i]));
851:     PetscCall(MatSeqAIJRestoreArrayRead(A, &a));

853:     // Warm the BLAS handle and scaling kernel before checking completion on return.
854:     PetscCall(MatScale(A, 2));
855:     PetscCall(PetscDeviceContextSynchronize(current));
856:     PetscCall(WriteValues(A, type, current, input, PETSC_TRUE));
857:     PetscCall(MatScale(A, 2));
858:     if (streams[k] == PETSC_STREAM_NONBLOCKING_WITH_BARRIER) {
859:       PetscBool idle;

861:       PetscCall(PetscDeviceContextQueryIdle(current, &idle));
862:       PetscCheck(idle, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatScale() returned with work pending on a barrier context");
863:     }
864:     PetscCall(PetscDeviceContextSynchronize(current));
865:     PetscCall(MatSeqAIJGetArrayRead(A, &a));
866:     for (PetscInt i = 0; i < 4; ++i) PetscCheck(a[i] == 2 * input[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Stream type %s: incorrectly scaled CSR entry %" PetscInt_FMT, PetscStreamTypes[streams[k]], i);
867:     PetscCall(MatSeqAIJRestoreArrayRead(A, &a));

869:     // Warm AXPY before delaying the update of its source matrix.
870:     PetscCall(MatAXPY(A, 2, B, SAME_NONZERO_PATTERN));
871:     // Clear B so an AXPY that overtakes the delayed write produces a wrong result.
872:     PetscCall(MatZeroEntries(B));
873:     PetscCall(PetscDeviceContextSynchronize(current));
874:     PetscCall(WriteValues(A, type, current, input, PETSC_FALSE));
875:     PetscCall(WriteValues(B, type, current, input, PETSC_TRUE));
876:     PetscCall(MatAXPY(A, 2, B, SAME_NONZERO_PATTERN));
877:     if (streams[k] == PETSC_STREAM_NONBLOCKING_WITH_BARRIER) {
878:       PetscBool idle;

880:       PetscCall(PetscDeviceContextQueryIdle(current, &idle));
881:       PetscCheck(idle, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatAXPY() returned with work pending on a barrier context");
882:     }
883:     PetscCall(PetscDeviceContextSynchronize(current));
884:     PetscCall(MatSeqAIJGetArrayRead(A, &a));
885:     for (PetscInt i = 0; i < 4; ++i) PetscCheck(a[i] == 3 * input[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Stream type %s: incorrect CSR entry %" PetscInt_FMT " after MatAXPY()", PetscStreamTypes[streams[k]], i);
886:     PetscCall(MatSeqAIJRestoreArrayRead(A, &a));
887:     PetscCall(TestCopies(A, type, current, input));
888:     PetscCall(TestGetDiagonal(A, type, current, input));
889:     PetscCall(TestDiagonalScale(A, type, current, input));
890:     PetscCall(TestGetIJ(type, current));
891:     PetscCall(TestSparseOperations(A, type, current, input));
892:     PetscCall(TestSparseSolve(type, saved, current));
893:     PetscCall(TestSELL(type, current));
894:     PetscCall(TestDense(PETSC_COMM_SELF, type, current, input));
895:     PetscCall(TestDense(PETSC_COMM_WORLD, type, current, input));
896:     PetscCall(PetscDeviceContextSetCurrentContext(saved));
897:     PetscCall(PetscDeviceContextDestroy(&current));
898:   }

900:   PetscCall(MatDestroy(&B));
901:   PetscCall(MatDestroy(&A));
902:   PetscCall(PetscDeviceFree(saved, input));
903:   PetscCall(PetscDeviceContextSynchronize(saved));
904:   PetscCall(PetscFinalize());
905:   return 0;
906: }

908: /*TEST

910:   testset:
911:     output_file: output/empty.out
912:     nsize: {{1 2}}

914:     test:
915:       suffix: cuda
916:       requires: cuda
917:       args: -mat_sell_spmv_cuda_kernel 1

919:     test:
920:       suffix: hip
921:       requires: hip
922:       args: -mat_sell_spmv_hip_kernel 1

924: TEST*/