Actual source code: ex254.c

  1: static char help[] = "Test MatSetValuesCOO() for MPIAIJ and its subclasses \n\n";

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

 13: static PetscErrorCode PreallocateDeviceCOO(Mat A, PetscInt n, const PetscInt i[], const PetscInt j[])
 14: {
 15:   PetscDeviceContext saved, current;
 16:   PetscDevice        device;
 17:   PetscDeviceType    type;
 18:   PetscInt          *hi, *hj, *di, *dj;
 19:   void              *stream;

 21:   PetscFunctionBeginUser;
 22:   PetscCall(PetscDeviceContextGetCurrentContext(&saved));
 23:   PetscCall(PetscDeviceContextDuplicate(saved, &current));
 24:   PetscCall(PetscDeviceContextSetStreamType(current, PETSC_STREAM_NONBLOCKING));
 25:   PetscCall(PetscDeviceContextSetUp(current));
 26:   PetscCall(PetscDeviceContextSetCurrentContext(current));
 27:   PetscCall(PetscDeviceContextGetDevice(current, &device));
 28:   PetscCall(PetscDeviceGetType(device, &type));
 29:   PetscCall(PetscDeviceContextGetStreamHandle(current, &stream));
 30:   // Keep valid device pointers on ranks with no COO entries.
 31:   PetscCall(PetscDeviceMalloc(current, PETSC_MEMTYPE_HOST, n + 1, PETSC_DECIDE, &hi));
 32:   PetscCall(PetscDeviceMalloc(current, PETSC_MEMTYPE_HOST, n + 1, PETSC_DECIDE, &hj));
 33:   PetscCall(PetscDeviceCalloc(current, PETSC_MEMTYPE_DEVICE, n + 1, PETSC_DECIDE, &di));
 34:   PetscCall(PetscDeviceCalloc(current, PETSC_MEMTYPE_DEVICE, n + 1, PETSC_DECIDE, &dj));
 35:   PetscCall(PetscDeviceContextSynchronize(current));
 36:   for (PetscInt k = 0; k < n; ++k) {
 37:     hi[k] = i[k];
 38:     hj[k] = j[k];
 39:   }
 40:   // Preallocation must read the updated indices, not the initial zeros.
 41:   PetscCall(PetscDeviceContextDelay(current, 0.05));
 42:   #if PetscDefined(HAVE_CUDA)
 43:   if (type == PETSC_DEVICE_CUDA) {
 44:     PetscCallCUDA(cudaMemcpyAsync(di, hi, n * sizeof(*di), cudaMemcpyHostToDevice, *(cudaStream_t *)stream));
 45:     PetscCallCUDA(cudaMemcpyAsync(dj, hj, n * sizeof(*dj), cudaMemcpyHostToDevice, *(cudaStream_t *)stream));
 46:   }
 47:   #endif
 48:   #if PetscDefined(HAVE_HIP)
 49:   if (type == PETSC_DEVICE_HIP) {
 50:     PetscCallHIP(hipMemcpyAsync(di, hi, n * sizeof(*di), hipMemcpyHostToDevice, *(hipStream_t *)stream));
 51:     PetscCallHIP(hipMemcpyAsync(dj, hj, n * sizeof(*dj), hipMemcpyHostToDevice, *(hipStream_t *)stream));
 52:   }
 53:   #endif
 54:   PetscCall(MatSetPreallocationCOO(A, n, di, dj));
 55:   PetscCall(PetscDeviceFree(current, di));
 56:   PetscCall(PetscDeviceFree(current, dj));
 57:   PetscCall(PetscDeviceFree(current, hi));
 58:   PetscCall(PetscDeviceFree(current, hj));
 59:   PetscCall(PetscDeviceContextSynchronize(current));
 60:   PetscCall(PetscDeviceContextSetCurrentContext(saved));
 61:   PetscCall(PetscDeviceContextDestroy(&current));
 62:   PetscFunctionReturn(PETSC_SUCCESS);
 63: }

 65: static PetscErrorCode TestCOOStreams(Mat ref, Mat A, PetscInt n, const PetscScalar values[])
 66: {
 67:   Mat                   twice;
 68:   PetscDeviceContext    saved, current;
 69:   PetscDevice           device;
 70:   PetscDeviceType       type;
 71:   PetscScalar          *host, *pinned, *gpu;
 72:   const PetscStreamType streams[] = {PETSC_STREAM_NONBLOCKING, PETSC_STREAM_NONBLOCKING_WITH_BARRIER};
 73:   void                 *stream;

 75:   PetscFunctionBeginUser;
 76:   PetscCall(MatDuplicate(ref, MAT_COPY_VALUES, &twice));
 77:   PetscCall(MatScale(twice, 2));
 78:   PetscCall(PetscDeviceContextGetCurrentContext(&saved));
 79:   PetscCall(PetscDeviceContextGetDevice(saved, &device));
 80:   PetscCall(PetscDeviceGetType(device, &type));
 81:   PetscCall(PetscMalloc1(n + 1, &host));
 82:   PetscCall(PetscDeviceMalloc(saved, PETSC_MEMTYPE_HOST, n + 1, PETSC_DECIDE, &pinned));
 83:   PetscCall(PetscDeviceMalloc(saved, PETSC_MEMTYPE_DEVICE, n + 1, PETSC_DECIDE, &gpu));
 84:   PetscCall(PetscDeviceContextSynchronize(saved));
 85:   for (size_t k = 0; k < PETSC_STATIC_ARRAY_LENGTH(streams); ++k) {
 86:     PetscCall(PetscDeviceContextDuplicate(saved, &current));
 87:     PetscCall(PetscDeviceContextSetStreamType(current, streams[k]));
 88:     PetscCall(PetscDeviceContextSetUp(current));
 89:     PetscCall(PetscDeviceContextSetCurrentContext(current));
 90:     PetscCall(PetscDeviceContextGetStreamHandle(current, &stream));
 91:     for (PetscInt memory = 0; memory < 3; ++memory) {
 92:       for (PetscInt add = 0; add < 2; ++add) {
 93:         PetscBool equal, idle;

 95:         // Warm allocations and communication before delaying the next update.
 96:         PetscCall(MatSetValuesCOO(A, values, INSERT_VALUES));
 97:         for (PetscInt i = 0; i < n; ++i) host[i] = pinned[i] = values[i];
 98:         // Clear the device input so a stale read differs from the delayed upload.
 99:         if (memory == 2) PetscCall(PetscDeviceArrayZero(current, gpu, n));
100:         PetscCall(PetscDeviceContextSynchronize(current));
101:         PetscCall(PetscDeviceContextDelay(current, 0.05));
102:   #if PetscDefined(HAVE_CUDA)
103:         if (type == PETSC_DEVICE_CUDA && memory == 2) PetscCallCUDA(cudaMemcpyAsync(gpu, pinned, n * sizeof(*gpu), cudaMemcpyHostToDevice, *(cudaStream_t *)stream));
104:   #endif
105:   #if PetscDefined(HAVE_HIP)
106:         if (type == PETSC_DEVICE_HIP && memory == 2) PetscCallHIP(hipMemcpyAsync(gpu, pinned, n * sizeof(*gpu), hipMemcpyHostToDevice, *(hipStream_t *)stream));
107:   #endif
108:         PetscCall(MatSetValuesCOO(A, memory == 2 ? gpu : (memory == 1 ? pinned : host), add ? ADD_VALUES : INSERT_VALUES));
109:         if (streams[k] == PETSC_STREAM_NONBLOCKING_WITH_BARRIER) {
110:           PetscCall(PetscDeviceContextQueryIdle(current, &idle));
111:           PetscCheck(idle, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatSetValuesCOO() returned with work pending on a barrier context");
112:         }
113:         // Host inputs must be reusable on return, even if device work is pending.
114:         if (memory != 2)
115:           for (PetscInt i = 0; i < n; ++i) host[i] = pinned[i] = 0;
116:         PetscCall(PetscDeviceContextSynchronize(current));
117:         PetscCall(PetscDeviceContextSetCurrentContext(saved));
118:         PetscCall(MatMultEqual(add ? twice : ref, A, 10, &equal));
119:         PetscCheck(equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Incorrect COO values (stream %s, memory %" PetscInt_FMT ", add %" PetscInt_FMT ")", PetscStreamTypes[streams[k]], memory, add);
120:         PetscCall(PetscDeviceContextSetCurrentContext(current));
121:       }
122:     }
123:     PetscCall(PetscDeviceContextSetCurrentContext(saved));
124:     PetscCall(PetscDeviceContextDestroy(&current));
125:   }
126:   PetscCall(MatZeroEntries(A));
127:   PetscCall(PetscFree(host));
128:   PetscCall(PetscDeviceFree(saved, gpu));
129:   PetscCall(PetscDeviceFree(saved, pinned));
130:   PetscCall(PetscDeviceContextSynchronize(saved));
131:   PetscCall(MatDestroy(&twice));
132:   PetscFunctionReturn(PETSC_SUCCESS);
133: }
134: #endif

136: /*
137:   Checks that MatSetValuesCOO() can still be called after MatShift() on a matrix with m local rows and n local columns, whose COO
138:   pattern already contains every diagonal entry so the shift creates no new nonzeros
139: */
140: static PetscErrorCode TestShiftCOO(PetscInt m, PetscInt n)
141: {
142:   Mat          D;
143:   PetscInt     rstart, rend, N, ncoo;
144:   PetscInt    *coo_i, *coo_j;
145:   PetscScalar  readback, expected;
146:   PetscScalar *coo_v;

148:   PetscFunctionBeginUser;
149:   PetscCallMPI(MPIU_Allreduce(&n, &N, 1, MPIU_INT, MPI_SUM, PETSC_COMM_WORLD));
150:   PetscCall(MatCreate(PETSC_COMM_WORLD, &D));
151:   PetscCall(MatSetSizes(D, m, n, PETSC_DETERMINE, PETSC_DETERMINE));
152:   PetscCall(MatSetFromOptions(D));
153:   PetscCall(MatGetOwnershipRange(D, &rstart, &rend));
154:   ncoo = 2 * (rend - rstart);
155:   PetscCall(PetscMalloc3(ncoo, &coo_i, ncoo, &coo_j, ncoo, &coo_v));
156:   for (PetscInt r = rstart, k = 0; r < rend; r++) {
157:     coo_i[k]   = r;
158:     coo_j[k++] = r % N;
159:     coo_i[k]   = r;
160:     coo_j[k++] = (r + 1) % N;
161:   }
162:   for (PetscInt k = 0; k < ncoo; k++) coo_v[k] = 1.0;
163:   PetscCall(MatSetPreallocationCOO(D, ncoo, coo_i, coo_j));
164:   PetscCall(MatSetValuesCOO(D, coo_v, INSERT_VALUES));
165:   PetscCall(MatShift(D, 2.0));
166:   PetscCall(MatShift(D, 2.0));
167:   PetscCall(MatSetValuesCOO(D, coo_v, ADD_VALUES));
168:   for (PetscInt r = rstart; r < rend; r++) {
169:     PetscCall(MatGetValue(D, r, r % N, &readback));
170:     expected = r < N ? 6.0 : 2.0;
171:     if (readback != expected) PetscCall(PetscPrintf(PETSC_COMM_SELF, "MatSetValuesCOO() after MatShift() gave %g in row %" PetscInt_FMT ", expected %g\n", (double)PetscRealPart(readback), r, (double)PetscRealPart(expected)));
172:   }
173:   PetscCall(PetscFree3(coo_i, coo_j, coo_v));
174:   PetscCall(MatDestroy(&D));
175:   PetscFunctionReturn(PETSC_SUCCESS);
176: }

178: int main(int argc, char **args)
179: {
180:   Mat            A, B, C;
181:   const PetscInt M = 18, N = 18;
182:   PetscBool      equal, isHypre;
183:   PetscScalar   *vals;
184:   PetscBool      flg = PETSC_FALSE, freecoo = PETSC_FALSE, missing_diagonal = PETSC_FALSE, device_indices = PETSC_FALSE, coo_streams = PETSC_FALSE;
185:   PetscInt       ncoos = 1;
186:   PetscMPIInt    size;

188:   // clang-format off
189:   /* Construct 18 x 18 matrices, which are big enough to have complex communication patterns but still small enough for debugging */
190:   // i0/j0[] has a dense diagonal
191:   PetscInt i0[] = {7, 7, 8, 8, 9, 16, 17,  9, 10, 1, 1, -2, 2, 3, 3, 14, 4, 5, 10, 13,  9,  9, 10, 1, 0, 0, 5,  5,  6, 6, 13, 13, 14, -14, 4, 4, 5, 11, 11, 12, 15, 15, 16};
192:   PetscInt j0[] = {7, 6, 8, 4, 9, 16, 17, 16, 10, 2, 1,  3, 2, 4, 3, 14, 4, 5, 15, 13, 10, 16, 11, 2, 0, 1, 5, -11, 0, 6, 15, 17, 11,  13, 4, 8, 2, 11, 17, 12,  3, 15,  9};

194:   // i0/j0[] miss some diagonals
195:   PetscInt i1[] = {8, 5, 15, 16, 6, 13, 4, 17, 8,  9, 9,  10, -6, 12, 7, 3, -4, 1, 1, 2, 5,  5, 6, 14, 17, 8,  9,  9, 10, 4,  5, 10, 11, 1, 2};
196:   PetscInt j1[] = {2, 3, 16,  9, 5, 17, 1, 13, 4, 10, 16, 11, -5, 12, 1, 7, -1, 2, 7, 3, 6, 11, 0, 11, 13, 4, 10, 16, 11, 8, -2, 15, 12, 7, 3};

198:   PetscInt i2[] = {3, 4, 1, 10, 0, 1, 1, 2, 1, 1, 2, 2, 3, 3, 4, 4, 1, 2, 5,  5, 6, 4, 17, 0, 1, 1, 8, 5,  5, 6, 4, 7, 8, 5};
199:   PetscInt j2[] = {7, 1, 2, 11, 5, 2, 7, 3, 2, 7, 3, 8, 4, 9, 3, 5, 7, 3, 6, 11, 0, 1, 13, 5, 2, 7, 4, 6, 11, 0, 1, 3, 4, 2};
200:   // clang-format on

202:   typedef struct {
203:     PetscInt *i, *j, n;
204:   } coo_data;

206:   coo_data coos[3] = {
207:     {i0, j0, PETSC_STATIC_ARRAY_LENGTH(i0)},
208:     {i1, j1, PETSC_STATIC_ARRAY_LENGTH(i1)},
209:     {i2, j2, PETSC_STATIC_ARRAY_LENGTH(i2)}
210:   };
211:   coo_data mycoo;

213:   PetscFunctionBeginUser;
214:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
215:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
216:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-ignore_remote", &flg, NULL));
217:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-ncoos", &ncoos, NULL));
218:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-missing_diagonal", &missing_diagonal, NULL));
219:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-device_indices", &device_indices, NULL));

221:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-coo_streams", &coo_streams, NULL));

223:   mycoo.n = 0;
224:   if (ncoos > 1) {
225:     PetscLayout map;

227:     freecoo = PETSC_TRUE;
228:     PetscCall(PetscLayoutCreate(PETSC_COMM_WORLD, &map));
229:     PetscCall(PetscLayoutSetSize(map, ncoos));
230:     PetscCall(PetscLayoutSetUp(map));
231:     PetscCall(PetscLayoutGetLocalSize(map, &ncoos));
232:     for (PetscInt i = 0; i < ncoos; i++) mycoo.n += coos[i % 3].n;
233:     PetscCall(PetscMalloc2(mycoo.n, &mycoo.i, mycoo.n, &mycoo.j));
234:     mycoo.n = 0;
235:     for (PetscInt i = 0; i < ncoos; i++) {
236:       PetscCall(PetscArraycpy(mycoo.i + mycoo.n, coos[i % 3].i, coos[i % 3].n));
237:       PetscCall(PetscArraycpy(mycoo.j + mycoo.n, coos[i % 3].j, coos[i % 3].n));
238:       mycoo.n += coos[i % 3].n;
239:     }
240:     PetscCall(PetscLayoutDestroy(&map));
241:   } else if (ncoos == 1 && PetscGlobalRank < 3) mycoo = coos[PetscGlobalRank];

243:   if (missing_diagonal && PetscGlobalRank == 0) mycoo = coos[1];

245:   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
246:   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, M, N));
247:   PetscCall(MatSetType(A, MATAIJ));
248:   // Do not preallocate A to also test MatHash with MAT_IGNORE_OFF_PROC_ENTRIES
249:   // PetscCall(MatSeqAIJSetPreallocation(A, 2, NULL));
250:   // PetscCall(MatMPIAIJSetPreallocation(A, 2, NULL, 2, NULL));
251:   PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
252:   PetscCall(MatSetOption(A, MAT_IGNORE_OFF_PROC_ENTRIES, flg));

254:   PetscCall(PetscMalloc1(mycoo.n, &vals));
255:   for (PetscInt k = 0; k < mycoo.n; k++) {
256:     vals[k] = mycoo.j[k];
257:     PetscCall(MatSetValue(A, mycoo.i[k], mycoo.j[k], vals[k], ADD_VALUES));
258:   }
259:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
260:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
261:   PetscCall(MatViewFromOptions(A, NULL, "-a_view"));

263:   PetscCall(MatCreate(PETSC_COMM_WORLD, &B));
264:   PetscCall(MatSetSizes(B, PETSC_DECIDE, PETSC_DECIDE, M, N));
265:   PetscCall(MatSetFromOptions(B));
266:   PetscCall(MatSetOption(B, MAT_IGNORE_OFF_PROC_ENTRIES, flg));
267: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
268:   if (device_indices) PetscCall(PreallocateDeviceCOO(B, mycoo.n, mycoo.n ? mycoo.i : NULL, mycoo.n ? mycoo.j : NULL));
269:   else
270: #endif
271:     PetscCall(MatSetPreallocationCOO(B, mycoo.n, mycoo.i, mycoo.j));

273: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
274:   if (coo_streams) PetscCall(TestCOOStreams(A, B, mycoo.n, vals));
275: #endif

277:   /* Test with ADD_VALUES on a zeroed matrix */
278:   PetscCall(MatSetValuesCOO(B, vals, ADD_VALUES));
279:   PetscCall(MatMultEqual(A, B, 10, &equal));
280:   if (!equal) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "MatSetValuesCOO() failed\n"));
281:   PetscCall(MatViewFromOptions(B, NULL, "-b_view"));

283:   /* Test with MatDuplicate on a zeroed matrix */
284:   PetscCall(MatDuplicate(B, MAT_DO_NOT_COPY_VALUES, &C));
285:   PetscCall(MatSetValuesCOO(C, vals, ADD_VALUES));
286:   PetscCall(MatMultEqual(A, C, 10, &equal));
287:   if (!equal) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "MatSetValuesCOO() on duplicated matrix failed\n"));
288:   PetscCall(MatViewFromOptions(C, NULL, "-c_view"));

290:   PetscCall(PetscObjectTypeCompare((PetscObject)B, MATHYPRE, &isHypre));

292:   /* Test MatSetValues() on a matrix preallocated with MatSetPreallocationCOO() with no intervening
293:      MatSetValuesCOO() that would perform matrix assembly.

295:      MATHYPRE is left out because this sequence trips a separate defect in hypre's device IJ
296:      assemble, which reads the off-diagonal column map without mirroring it to the device
297:      (hypre-space/hypre#1625). */
298:   if (!isHypre) {
299:     Mat          D;
300:     PetscInt     rstart, rend, remote_col, ncoo;
301:     PetscInt    *coo_i, *coo_j;
302:     PetscScalar  readback;
303:     PetscScalar *coo_v;
304:     PetscBool    isHostAIJ;

306:     PetscCall(MatCreate(PETSC_COMM_WORLD, &D));
307:     PetscCall(MatSetSizes(D, PETSC_DECIDE, PETSC_DECIDE, M, N));
308:     PetscCall(MatSetFromOptions(D));
309:     PetscCall(MatGetOwnershipRange(D, &rstart, &rend));

311:     /* A diagonal entry and one in the first column past this rank's rows, which another rank owns
312:        whenever there is more than one */
313:     remote_col = rend % N;
314:     ncoo       = 2 * (rend - rstart);
315:     PetscCall(PetscMalloc2(ncoo, &coo_i, ncoo, &coo_j));
316:     for (PetscInt r = rstart, k = 0; r < rend; r++) {
317:       coo_i[k] = r;
318:       coo_j[k] = r;
319:       k++;
320:       coo_i[k] = r;
321:       coo_j[k] = remote_col;
322:       k++;
323:     }
324:     PetscCall(MatSetPreallocationCOO(D, ncoo, coo_i, coo_j));
325:     PetscCall(PetscFree2(coo_i, coo_j));

327:     for (PetscInt r = rstart; r < rend; r++) {
328:       PetscCall(MatSetValue(D, r, r, 2.0, INSERT_VALUES));
329:       PetscCall(MatSetValue(D, r, remote_col, 5.0, INSERT_VALUES));
330:     }
331:     PetscCall(MatAssemblyBegin(D, MAT_FINAL_ASSEMBLY));
332:     PetscCall(MatAssemblyEnd(D, MAT_FINAL_ASSEMBLY));

334:     PetscCall(MatGetValue(D, rstart, remote_col, &readback));
335:     if (readback != 5.0) PetscCall(PetscPrintf(PETSC_COMM_SELF, "MatSetValues() after MatSetPreallocationCOO() gave %g, expected 5\n", (double)PetscRealPart(readback)));

337:     /* MatSetValues() above only wrote into the preallocated nonzero pattern, so MatSetValuesCOO() remains usable */
338:     PetscCall(PetscMalloc1(ncoo, &coo_v));
339:     for (PetscInt k = 0; k < ncoo; k++) coo_v[k] = (k % 2) ? 7.0 : 0.0;
340:     PetscCall(MatSetValuesCOO(D, coo_v, INSERT_VALUES));
341:     PetscCall(MatGetValue(D, rstart, remote_col, &readback));
342:     if (readback != 7.0) PetscCall(PetscPrintf(PETSC_COMM_SELF, "MatSetValuesCOO() after MatSetValues() gave %g, expected 7\n", (double)PetscRealPart(readback)));

344:     /* Zeroing no rows leaves the nonzero pattern, and so MatSetValuesCOO(), intact */
345:     PetscCall(MatZeroRows(D, 0, NULL, 1.0, NULL, NULL));
346:     PetscCall(MatSetValuesCOO(D, coo_v, ADD_VALUES));
347:     PetscCall(MatGetValue(D, rstart, remote_col, &readback));
348:     if (readback != 14.0) PetscCall(PetscPrintf(PETSC_COMM_SELF, "MatSetValuesCOO() after an empty MatZeroRows() gave %g, expected 14\n", (double)PetscRealPart(readback)));

350:     /* MatAXPY() with DIFFERENT_NONZERO_PATTERN of a matrix whose nonzeros are a subset of those of D leaves the nonzero pattern,
351:        and so MatSetValuesCOO(), intact. Device implementations of MatAXPY(), such as that of MATAIJKOKKOS, replace the matrix,
352:        so only the host types MATSEQAIJ and MATMPIAIJ are tested. */
353:     PetscCall(PetscObjectTypeCompareAny((PetscObject)D, &isHostAIJ, MATSEQAIJ, MATMPIAIJ, ""));
354:     if (isHostAIJ) {
355:       Mat Diag;

357:       PetscCall(MatCreateAIJ(PETSC_COMM_WORLD, rend - rstart, rend - rstart, M, N, 1, NULL, 0, NULL, &Diag));
358:       for (PetscInt r = rstart; r < rend; r++) PetscCall(MatSetValue(Diag, r, r, 1.0, INSERT_VALUES));
359:       PetscCall(MatAssemblyBegin(Diag, MAT_FINAL_ASSEMBLY));
360:       PetscCall(MatAssemblyEnd(Diag, MAT_FINAL_ASSEMBLY));
361:       PetscCall(MatAXPY(D, 1.0, Diag, DIFFERENT_NONZERO_PATTERN));
362:       PetscCall(MatDestroy(&Diag));
363:       PetscCall(MatSetValuesCOO(D, coo_v, INSERT_VALUES));
364:       PetscCall(MatGetValue(D, rstart, remote_col, &readback));
365:       if (readback != 7.0) PetscCall(PetscPrintf(PETSC_COMM_SELF, "MatSetValuesCOO() after MatAXPY() gave %g, expected 7\n", (double)PetscRealPart(readback)));
366:     }
367:     PetscCall(PetscFree(coo_v));
368:     PetscCall(MatDestroy(&D));
369:   }

371:   /* Test that aij->diag is set up correctly on COO matrices */
372:   if (!isHypre) { // TODO: MATHYPRE currently does not support MatSetValues
373:     PetscCall(MatShift(A, 2.0));
374:     PetscCall(MatShift(B, 2.0));
375:     PetscCall(MatMultEqual(A, B, 10, &equal));
376:     if (!equal) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "MatShift() on a duplicated COO matrix failed\n"));

378:     /* A square matrix whose last process owns no rows, which on one process is a 0 x 0 matrix */
379:     PetscCall(TestShiftCOO(PetscGlobalRank == size - 1 ? 0 : 3, PetscGlobalRank == size - 1 ? 0 : 3));
380:     /* A matrix with more rows than columns */
381:     PetscCall(TestShiftCOO(3, 2));
382:   }

384:   PetscCall(PetscFree(vals));
385:   if (freecoo) PetscCall(PetscFree2(mycoo.i, mycoo.j));
386:   PetscCall(MatDestroy(&A));
387:   PetscCall(MatDestroy(&B));
388:   PetscCall(MatDestroy(&C));

390:   PetscCall(PetscFinalize());
391:   return 0;
392: }

394: /*TEST

396:   testset:
397:     output_file: output/empty.out
398:     nsize: {{1 2 3}}
399:     args: -ignore_remote {{0 1}} -missing_diagonal {{0 1}}
400:     filter: grep -v type | grep -v "Mat Object"

402:     test:
403:       suffix: kokkos
404:       requires: kokkos_kernels
405:       args: -mat_type aijkokkos

407:     test:
408:       suffix: cuda
409:       requires: cuda
410:       args: -mat_type aijcusparse

412:     test:
413:       suffix: hip
414:       requires: hip
415:       args: -mat_type aijhipsparse

417:     test:
418:       suffix: aij
419:       args: -mat_type aij

421:     test:
422:       suffix: hypre
423:       requires: hypre
424:       args: -mat_type hypre

426:   testset:
427:     output_file: output/empty.out
428:     nsize: 1
429:     args: -ncoos 3
430:     filter: grep -v type | grep -v "Mat Object"

432:     test:
433:       suffix: 2_kokkos
434:       requires: kokkos_kernels
435:       args: -mat_type aijkokkos

437:     test:
438:       suffix: 2_cuda
439:       requires: cuda
440:       args: -mat_type aijcusparse

442:     test:
443:       suffix: 2_hip
444:       requires: hip
445:       args: -mat_type aijhipsparse

447:     test:
448:       suffix: 2_aij
449:       args: -mat_type aij

451:     test:
452:       suffix: 2_hypre
453:       requires: hypre
454:       args: -mat_type hypre

456:   testset:
457:     output_file: output/empty.out
458:     nsize: {{1 2 4}}
459:     args: -device_indices
460:     test:
461:       suffix: cuda_device_indices
462:       requires: cuda
463:       args: -mat_type aijcusparse

465:     test:
466:       suffix: hip_device_indices
467:       requires: hip
468:       args: -mat_type aijhipsparse

470:   testset:
471:     output_file: output/empty.out
472:     nsize: {{1 2 4}}
473:     args: -coo_streams
474:     test:
475:       suffix: cuda_streams
476:       requires: cuda
477:       args: -mat_type aijcusparse

479:     test:
480:       suffix: hip_streams
481:       requires: hip
482:       args: -mat_type aijhipsparse

484: TEST*/