Actual source code: ex23.c

  1: static char help[] = "Tests the use of interface functions for MATIS matrices and conversion routines.\n";

  3: #include <petscmat.h>

  5: PetscErrorCode TestMatZeroRows(Mat, Mat, PetscBool, IS, PetscScalar, PetscBool);
  6: PetscErrorCode CheckMat(Mat, Mat, PetscBool, const char *);
  7: PetscErrorCode CheckVariableBlockSizes(Mat);
  8: PetscErrorCode ISL2GMapNoNeg(ISLocalToGlobalMapping, IS, IS *);

 10: int main(int argc, char **args)
 11: {
 12:   Mat                    A, B, A2, B2, T;
 13:   Mat                    Aee, Aeo, Aoe, Aoo;
 14:   Mat                   *mats, *Asub, *Bsub;
 15:   Vec                    x, y;
 16:   MatInfo                info;
 17:   ISLocalToGlobalMapping cmap, rmap;
 18:   IS                     is, lis, is2, reven, rodd, ceven, codd;
 19:   IS                    *rows, *cols;
 20:   IS                     irow[2], icol[2];
 21:   PetscLayout            rlayout, clayout;
 22:   const PetscInt        *rrange, *crange, *idxs1, *idxs2;
 23:   MatType                lmtype;
 24:   PetscScalar            diag = 2., *vals;
 25:   PetscInt               n, m, i, lm, ln;
 26:   PetscInt               rst, ren, cst, cen, nr, nc, rbs = 1, cbs = 1;
 27:   PetscMPIInt            rank, size, lrank, rrank;
 28:   PetscBool              testT, squaretest, isaij;
 29:   PetscBool              permute = PETSC_FALSE, negmap = PETSC_FALSE, repmap = PETSC_FALSE, allow_repeated = PETSC_TRUE;
 30:   PetscBool              diffmap = PETSC_TRUE, symmetric = PETSC_FALSE, issymmetric, test_matlab = PETSC_FALSE, test_setvalues = PETSC_TRUE;
 31:   PetscBool              test_variableblocksizes = PETSC_FALSE;

 33:   PetscFunctionBeginUser;
 34:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 35:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 36:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 37:   m = n = 2 * size;
 38:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-symmetric", &symmetric, NULL));
 39:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
 40:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
 41:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-negmap", &negmap, NULL));
 42:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-repmap", &repmap, NULL));
 43:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-permmap", &permute, NULL));
 44:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-diffmap", &diffmap, NULL));
 45:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-allow_repeated", &allow_repeated, NULL));
 46:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_matlab", &test_matlab, NULL));
 47:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_setvalues", &test_setvalues, NULL));
 48:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_variableblocksizes", &test_variableblocksizes, NULL));
 49:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-rbs", &rbs, NULL));
 50:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-cbs", &cbs, NULL));
 51:   PetscCheck(size == 1 || m >= 4, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Number of rows should be larger or equal 4 for parallel runs");
 52:   PetscCheck(size != 1 || m >= 2, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Number of rows should be larger or equal 2 for uniprocessor runs");
 53:   PetscCheck(n >= 2, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Number of cols should be larger or equal 2");

 55:   /* create a MATIS matrix */
 56:   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
 57:   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, m, n));
 58:   PetscCall(MatSetType(A, MATIS));
 59:   PetscCall(MatSetFromOptions(A));
 60:   if (!negmap && !repmap) {
 61:     /* This is not the proper setting for MATIS for finite elements, it is just used to test the routines
 62:        Here we use a one-to-one correspondence between local row/column spaces and global row/column spaces
 63:        Equivalent to passing NULL for the mapping */
 64:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, n, 0, 1, &is));
 65:   } else if (negmap && !repmap) { /* non repeated but with negative indices */
 66:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, n + 2, -2, 1, &is));
 67:   } else if (!negmap && repmap) { /* non negative but repeated indices */
 68:     IS isl[2];

 70:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, n, 0, 1, &isl[0]));
 71:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, n, n - 1, -1, &isl[1]));
 72:     PetscCall(ISConcatenate(PETSC_COMM_WORLD, 2, isl, &is));
 73:     PetscCall(ISDestroy(&isl[0]));
 74:     PetscCall(ISDestroy(&isl[1]));
 75:   } else { /* negative and repeated indices */
 76:     IS isl[2];

 78:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, n + 1, -1, 1, &isl[0]));
 79:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, n + 1, n - 1, -1, &isl[1]));
 80:     PetscCall(ISConcatenate(PETSC_COMM_WORLD, 2, isl, &is));
 81:     PetscCall(ISDestroy(&isl[0]));
 82:     PetscCall(ISDestroy(&isl[1]));
 83:   }
 84:   PetscCall(ISLocalToGlobalMappingCreateIS(is, &cmap));
 85:   PetscCall(ISDestroy(&is));

 87:   if (m != n || diffmap) {
 88:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, m, permute ? m - 1 : 0, permute ? -1 : 1, &is));
 89:     PetscCall(ISLocalToGlobalMappingCreateIS(is, &rmap));
 90:     PetscCall(ISDestroy(&is));
 91:   } else {
 92:     PetscCall(PetscObjectReference((PetscObject)cmap));
 93:     rmap = cmap;
 94:   }

 96:   PetscCall(MatISSetAllowRepeated(A, allow_repeated));
 97:   PetscCall(MatSetLocalToGlobalMapping(A, rmap, cmap));
 98:   PetscCall(MatSetBlockSizes(A, rbs, cbs));
 99:   PetscCall(MatISStoreL2L(A, PETSC_FALSE));
100:   PetscCall(MatISSetPreallocation(A, 3, NULL, 3, NULL));
101:   PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, (PetscBool)!(repmap || negmap))); /* I do not want to precompute the pattern */
102:   PetscCall(ISLocalToGlobalMappingGetSize(rmap, &lm));
103:   PetscCall(ISLocalToGlobalMappingGetSize(cmap, &ln));
104:   for (i = 0; i < lm; i++) {
105:     PetscScalar v[3];
106:     PetscInt    cols[3];

108:     cols[0] = (i - 1 + n) % n;
109:     cols[1] = i % n;
110:     cols[2] = (i + 1) % n;
111:     v[0]    = -1. * (symmetric ? PetscMin(i + 1, cols[0] + 1) : i + 1);
112:     v[1]    = 2. * (symmetric ? PetscMin(i + 1, cols[1] + 1) : i + 1);
113:     v[2]    = -1. * (symmetric ? PetscMin(i + 1, cols[2] + 1) : i + 1);
114:     PetscCall(ISGlobalToLocalMappingApply(cmap, IS_GTOLM_MASK, 3, cols, NULL, cols));
115:     PetscCall(MatSetValuesLocal(A, 1, &i, 3, cols, v, ADD_VALUES));
116:   }
117:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
118:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));

120:   /* activate tests for square matrices with same maps only */
121:   PetscCall(MatHasCongruentLayouts(A, &squaretest));
122:   if (squaretest && rmap != cmap) {
123:     PetscInt nr, nc;

125:     PetscCall(ISLocalToGlobalMappingGetSize(rmap, &nr));
126:     PetscCall(ISLocalToGlobalMappingGetSize(cmap, &nc));
127:     if (nr != nc) squaretest = PETSC_FALSE;
128:     else {
129:       PetscCall(ISLocalToGlobalMappingGetIndices(rmap, &idxs1));
130:       PetscCall(ISLocalToGlobalMappingGetIndices(cmap, &idxs2));
131:       PetscCall(PetscArraycmp(idxs1, idxs2, nr, &squaretest));
132:       PetscCall(ISLocalToGlobalMappingRestoreIndices(rmap, &idxs1));
133:       PetscCall(ISLocalToGlobalMappingRestoreIndices(cmap, &idxs2));
134:     }
135:     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &squaretest, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)A)));
136:   }
137:   if (negmap && repmap) squaretest = PETSC_FALSE;
138:   PetscCheck(squaretest || !test_variableblocksizes, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Variable block size test only for square matrices");

140:   /* test MatISGetLocalMat */
141:   PetscCall(MatISGetLocalMat(A, &B));
142:   PetscCall(MatGetType(B, &lmtype));
143:   if (test_variableblocksizes) {
144:     PetscInt bsizes[2] = {rbs, lm - rbs};

146:     PetscCall(MatSetVariableBlockSizes(B, PETSC_STATIC_ARRAY_LENGTH(bsizes), bsizes));
147:   }

149:   /* test MatGetInfo */
150:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatGetInfo\n"));
151:   PetscCall(MatGetInfo(A, MAT_LOCAL, &info));
152:   PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
153:   PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "Process  %2d: %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", PetscGlobalRank, (PetscInt)info.nz_used, (PetscInt)info.nz_allocated,
154:                                                (PetscInt)info.nz_unneeded, (PetscInt)info.assemblies, (PetscInt)info.mallocs));
155:   PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
156:   PetscCall(MatGetInfo(A, MAT_GLOBAL_MAX, &info));
157:   PetscCall(PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "GlobalMax  : %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", (PetscInt)info.nz_used, (PetscInt)info.nz_allocated, (PetscInt)info.nz_unneeded,
158:                                    (PetscInt)info.assemblies, (PetscInt)info.mallocs));
159:   PetscCall(MatGetInfo(A, MAT_GLOBAL_SUM, &info));
160:   PetscCall(PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "GlobalSum  : %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", (PetscInt)info.nz_used, (PetscInt)info.nz_allocated, (PetscInt)info.nz_unneeded,
161:                                    (PetscInt)info.assemblies, (PetscInt)info.mallocs));

163:   /* test MatIsSymmetric */
164:   PetscCall(MatIsSymmetric(A, 0.0, &issymmetric));
165:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatIsSymmetric: %d\n", issymmetric));

167:   /* Create a MPIAIJ matrix, same as A */
168:   PetscCall(MatCreate(PETSC_COMM_WORLD, &B));
169:   PetscCall(MatSetSizes(B, PETSC_DECIDE, PETSC_DECIDE, m, n));
170:   PetscCall(MatSetBlockSizes(B, rbs, cbs));
171:   PetscCall(MatSetType(B, MATAIJ));
172:   PetscCall(MatSetFromOptions(B));
173:   PetscCall(MatSetLocalToGlobalMapping(B, rmap, cmap));
174:   PetscCall(MatMPIAIJSetPreallocation(B, 3, NULL, 3, NULL));
175:   PetscCall(MatMPIBAIJSetPreallocation(B, 1, 3, NULL, 3, NULL));
176: #if PetscDefined(HAVE_HYPRE)
177:   PetscCall(MatHYPRESetPreallocation(B, 3, NULL, 3, NULL));
178: #endif
179:   PetscCall(MatISSetPreallocation(B, 3, NULL, 3, NULL));
180:   PetscCall(MatSetOption(B, MAT_NEW_NONZERO_ALLOCATION_ERR, (PetscBool)!(repmap || negmap))); /* I do not want to precompute the pattern */
181:   for (i = 0; i < lm; i++) {
182:     PetscScalar v[3];
183:     PetscInt    cols[3];

185:     cols[0] = (i - 1 + n) % n;
186:     cols[1] = i % n;
187:     cols[2] = (i + 1) % n;
188:     v[0]    = -1. * (symmetric ? PetscMin(i + 1, cols[0] + 1) : i + 1);
189:     v[1]    = 2. * (symmetric ? PetscMin(i + 1, cols[1] + 1) : i + 1);
190:     v[2]    = -1. * (symmetric ? PetscMin(i + 1, cols[2] + 1) : i + 1);
191:     PetscCall(ISGlobalToLocalMappingApply(cmap, IS_GTOLM_MASK, 3, cols, NULL, cols));
192:     PetscCall(MatSetValuesLocal(B, 1, &i, 3, cols, v, ADD_VALUES));
193:   }
194:   PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
195:   PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));

197:   /* test MatView */
198:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatView\n"));
199:   PetscCall(MatView(A, NULL));
200:   PetscCall(MatView(B, NULL));

202:   /* test MATLAB ASCII view */
203:   if (test_matlab) { /* output is different when using real or complex numbers */
204:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatView ASCII MATLAB\n"));
205:     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_MATLAB));
206:     PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));
207:     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
208:   }

210:   /* test CheckMat */
211:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test CheckMat\n"));
212:   PetscCall(CheckMat(A, B, PETSC_FALSE, "CheckMat"));

214:   /* test binary MatView/MatLoad */
215:   {
216:     PetscMPIInt color = rank % 2;
217:     MPI_Comm    comm;
218:     char        name[PETSC_MAX_PATH_LEN];
219:     PetscViewer wview, cview, sview, view;
220:     Mat         A2;

222:     PetscCallMPI(MPI_Comm_split(PETSC_COMM_WORLD, color, rank, &comm));

224:     PetscCall(PetscSNPrintf(name, PETSC_STATIC_ARRAY_LENGTH(name), "world_is"));
225:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, name, FILE_MODE_WRITE, &wview));
226:     PetscCall(PetscSNPrintf(name, PETSC_STATIC_ARRAY_LENGTH(name), "seq_is_%d", rank));
227:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_SELF, name, FILE_MODE_WRITE, &sview));
228:     PetscCall(PetscSNPrintf(name, PETSC_STATIC_ARRAY_LENGTH(name), "color_is_%d", color));
229:     PetscCall(PetscViewerBinaryOpen(comm, name, FILE_MODE_WRITE, &cview));
230:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatView on binary world\n"));
231:     PetscCall(MatView(A, wview));
232:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatView on binary self\n"));
233:     PetscCall(MatView(A, sview));
234:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatView on binary subcomm\n"));
235:     PetscCall(MatView(A, cview));
236:     PetscCall(PetscViewerDestroy(&wview));
237:     PetscCall(PetscViewerDestroy(&cview));
238:     PetscCall(PetscViewerDestroy(&sview));

240:     /* Load a world matrix */
241:     PetscCall(MatCreate(PETSC_COMM_WORLD, &A2));
242:     PetscCall(MatSetType(A2, MATIS));
243:     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_INFO_DETAIL));

245:     /* Read back the same matrix and check */
246:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatLoad from world\n"));
247:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "world_is", FILE_MODE_READ, &view));
248:     PetscCall(MatLoad(A2, view));
249:     if (test_variableblocksizes) PetscCall(CheckVariableBlockSizes(A2));
250:     PetscCall(CheckMat(A, A2, PETSC_TRUE, "Load"));
251:     PetscCall(MatView(A2, PETSC_VIEWER_STDOUT_WORLD));
252:     PetscCall(PetscViewerDestroy(&view));

254:     /* Read the matrix from rank 0 only */
255:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatLoad from self\n"));
256:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "seq_is_0", FILE_MODE_READ, &view));
257:     PetscCall(MatLoad(A2, view));
258:     if (test_variableblocksizes) PetscCall(CheckVariableBlockSizes(A2));
259:     PetscCall(MatView(A2, PETSC_VIEWER_STDOUT_WORLD));
260:     PetscCall(PetscViewerDestroy(&view));

262:     /* Read the matrix from subcomm */
263:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatLoad from subcomm\n"));
264:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "color_is_0", FILE_MODE_READ, &view));
265:     PetscCall(MatLoad(A2, view));
266:     if (test_variableblocksizes) PetscCall(CheckVariableBlockSizes(A2));
267:     PetscCall(MatView(A2, PETSC_VIEWER_STDOUT_WORLD));
268:     PetscCall(PetscViewerDestroy(&view));

270:     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
271:     PetscCall(MatDestroy(&A2));

273:     /* now load the original matrix from color 0 only processes */
274:     if (!color) {
275:       PetscCall(PetscPrintf(comm, "Test subcomm MatLoad from world\n"));
276:       PetscCall(MatCreate(comm, &A2));
277:       PetscCall(MatSetType(A2, MATIS));
278:       PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(comm), PETSC_VIEWER_ASCII_INFO_DETAIL));
279:       PetscCall(PetscViewerBinaryOpen(comm, "world_is", FILE_MODE_READ, &view));
280:       PetscCall(MatLoad(A2, view));
281:       if (test_variableblocksizes) PetscCall(CheckVariableBlockSizes(A2));
282:       PetscCall(MatView(A2, PETSC_VIEWER_STDOUT_(comm)));
283:       PetscCall(PetscViewerDestroy(&view));
284:       PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(comm)));
285:       PetscCall(MatDestroy(&A2));
286:     }

288:     PetscCallMPI(MPI_Comm_free(&comm));
289:   }

291:   /* test MatDuplicate and MatAXPY */
292:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatDuplicate and MatAXPY\n"));
293:   PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &A2));
294:   PetscCall(CheckMat(A, A2, PETSC_FALSE, "MatDuplicate and MatAXPY"));

296:   /* test MatConvert */
297:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatConvert_IS_XAIJ\n"));
298:   PetscCall(MatConvert(A2, MATAIJ, MAT_INITIAL_MATRIX, &B2));
299:   PetscCall(CheckMat(B, B2, PETSC_TRUE, "MatConvert_IS_XAIJ MAT_INITIAL_MATRIX"));
300:   PetscCall(MatConvert(A2, MATAIJ, MAT_REUSE_MATRIX, &B2));
301:   PetscCall(CheckMat(B, B2, PETSC_TRUE, "MatConvert_IS_XAIJ MAT_REUSE_MATRIX"));
302:   PetscCall(MatConvert(A2, MATAIJ, MAT_INPLACE_MATRIX, &A2));
303:   PetscCall(CheckMat(B, A2, PETSC_TRUE, "MatConvert_IS_XAIJ MAT_INPLACE_MATRIX"));
304:   PetscCall(MatDestroy(&A2));
305:   PetscCall(MatDestroy(&B2));
306:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatConvert_XAIJ_IS\n"));
307:   PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &B2));
308:   PetscCall(MatConvert(B2, MATIS, MAT_INITIAL_MATRIX, &A2));
309:   PetscCall(CheckMat(A, A2, PETSC_TRUE, "MatConvert_XAIJ_IS MAT_INITIAL_MATRIX"));
310:   PetscCall(MatConvert(B2, MATIS, MAT_REUSE_MATRIX, &A2));
311:   PetscCall(CheckMat(A, A2, PETSC_TRUE, "MatConvert_XAIJ_IS MAT_REUSE_MATRIX"));
312:   PetscCall(MatConvert(B2, MATIS, MAT_INPLACE_MATRIX, &B2));
313:   PetscCall(CheckMat(A, B2, PETSC_TRUE, "MatConvert_XAIJ_IS MAT_INPLACE_MATRIX"));
314:   PetscCall(MatDestroy(&A2));
315:   PetscCall(MatDestroy(&B2));
316:   PetscCall(PetscStrcmp(lmtype, MATSEQAIJ, &isaij));
317:   if (size == 1 && isaij) { /* tests special code paths in MatConvert_IS_XAIJ */
318:     PetscInt               ri, ci, rr[3] = {0, 1, 0}, cr[4] = {1, 2, 0, 1}, rk[3] = {0, 2, 1}, ck[4] = {1, 0, 3, 2};
319:     ISLocalToGlobalMapping tcmap, trmap;

321:     for (ri = 0; ri < 2; ri++) {
322:       PetscInt *r;

324:       r = (PetscInt *)(ri == 0 ? rr : rk);
325:       for (ci = 0; ci < 2; ci++) {
326:         PetscInt *c, rb, cb;

328:         c = (PetscInt *)(ci == 0 ? cr : ck);
329:         for (rb = 1; rb < 4; rb++) {
330:           PetscCall(ISCreateBlock(PETSC_COMM_SELF, rb, 3, r, PETSC_COPY_VALUES, &is));
331:           PetscCall(ISLocalToGlobalMappingCreateIS(is, &trmap));
332:           PetscCall(ISDestroy(&is));
333:           for (cb = 1; cb < 4; cb++) {
334:             Mat  T, lT, T2;
335:             char testname[256];

337:             PetscCall(PetscSNPrintf(testname, sizeof(testname), "MatConvert_IS_XAIJ special case (%" PetscInt_FMT " %" PetscInt_FMT ", bs %" PetscInt_FMT " %" PetscInt_FMT ")", ri, ci, rb, cb));
338:             PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test %s\n", testname));

340:             PetscCall(ISCreateBlock(PETSC_COMM_SELF, cb, 4, c, PETSC_COPY_VALUES, &is));
341:             PetscCall(ISLocalToGlobalMappingCreateIS(is, &tcmap));
342:             PetscCall(ISDestroy(&is));

344:             PetscCall(MatCreate(PETSC_COMM_SELF, &T));
345:             PetscCall(MatSetSizes(T, PETSC_DECIDE, PETSC_DECIDE, rb * 3, cb * 4));
346:             PetscCall(MatSetType(T, MATIS));
347:             PetscCall(MatSetLocalToGlobalMapping(T, trmap, tcmap));
348:             PetscCall(ISLocalToGlobalMappingDestroy(&tcmap));
349:             PetscCall(MatISGetLocalMat(T, &lT));
350:             PetscCall(MatSetType(lT, MATSEQAIJ));
351:             PetscCall(MatSeqAIJSetPreallocation(lT, cb * 4, NULL));
352:             PetscCall(MatSetRandom(lT, NULL));
353:             PetscCall(MatConvert(lT, lmtype, MAT_INPLACE_MATRIX, &lT));
354:             PetscCall(MatISRestoreLocalMat(T, &lT));
355:             PetscCall(MatAssemblyBegin(T, MAT_FINAL_ASSEMBLY));
356:             PetscCall(MatAssemblyEnd(T, MAT_FINAL_ASSEMBLY));

358:             PetscCall(MatConvert(T, MATAIJ, MAT_INITIAL_MATRIX, &T2));
359:             PetscCall(CheckMat(T, T2, PETSC_TRUE, "MAT_INITIAL_MATRIX"));
360:             PetscCall(MatConvert(T, MATAIJ, MAT_REUSE_MATRIX, &T2));
361:             PetscCall(CheckMat(T, T2, PETSC_TRUE, "MAT_REUSE_MATRIX"));
362:             PetscCall(MatDestroy(&T2));
363:             PetscCall(MatDuplicate(T, MAT_COPY_VALUES, &T2));
364:             PetscCall(MatConvert(T2, MATAIJ, MAT_INPLACE_MATRIX, &T2));
365:             PetscCall(CheckMat(T, T2, PETSC_TRUE, "MAT_INPLACE_MATRIX"));
366:             PetscCall(MatDestroy(&T));
367:             PetscCall(MatDestroy(&T2));
368:           }
369:           PetscCall(ISLocalToGlobalMappingDestroy(&trmap));
370:         }
371:       }
372:     }
373:   }

375:   /* test MatDiagonalScale */
376:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatDiagonalScale\n"));
377:   PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &A2));
378:   PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &B2));
379:   PetscCall(MatCreateVecs(A, &x, &y));
380:   PetscCall(VecSetRandom(x, NULL));
381:   if (issymmetric) {
382:     PetscCall(VecCopy(x, y));
383:   } else {
384:     PetscCall(VecSetRandom(y, NULL));
385:     PetscCall(VecScale(y, 8.));
386:   }
387:   PetscCall(MatDiagonalScale(A2, y, x));
388:   PetscCall(MatDiagonalScale(B2, y, x));
389:   PetscCall(CheckMat(A2, B2, PETSC_FALSE, "MatDiagonalScale"));
390:   PetscCall(MatDestroy(&A2));
391:   PetscCall(MatDestroy(&B2));
392:   PetscCall(VecDestroy(&x));
393:   PetscCall(VecDestroy(&y));

395:   /* test MatPtAP (A IS and B AIJ) */
396:   if (isaij && m == n) {
397:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatPtAP\n"));
398:     /* There's a bug in MatCreateSubMatrices_MPIAIJ I cannot figure out */
399:     if (!allow_repeated || !repmap || size == 1) {
400:       PetscCall(MatISStoreL2L(A, PETSC_TRUE));
401:       PetscCall(MatPtAP(A, B, MAT_INITIAL_MATRIX, PETSC_DETERMINE, &A2));
402:       PetscCall(MatPtAP(B, B, MAT_INITIAL_MATRIX, PETSC_DETERMINE, &B2));
403:       PetscCall(CheckMat(A2, B2, PETSC_FALSE, "MatPtAP MAT_INITIAL_MATRIX"));
404:       PetscCall(MatPtAP(A, B, MAT_REUSE_MATRIX, PETSC_DETERMINE, &A2));
405:       PetscCall(CheckMat(A2, B2, PETSC_FALSE, "MatPtAP MAT_REUSE_MATRIX"));
406:       PetscCall(MatDestroy(&A2));
407:       PetscCall(MatDestroy(&B2));
408:     }
409:   }

411:   /* test MatGetLocalSubMatrix */
412:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatGetLocalSubMatrix\n"));
413:   PetscCall(MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, &A2));
414:   PetscCall(ISCreateStride(PETSC_COMM_SELF, lm / 2 + lm % 2, 0, 2, &reven));
415:   PetscCall(ISComplement(reven, 0, lm, &rodd));
416:   PetscCall(ISCreateStride(PETSC_COMM_SELF, ln / 2 + ln % 2, 0, 2, &ceven));
417:   PetscCall(ISComplement(ceven, 0, ln, &codd));
418:   PetscCall(MatGetLocalSubMatrix(A2, reven, ceven, &Aee));
419:   PetscCall(MatGetLocalSubMatrix(A2, reven, codd, &Aeo));
420:   PetscCall(MatGetLocalSubMatrix(A2, rodd, ceven, &Aoe));
421:   PetscCall(MatGetLocalSubMatrix(A2, rodd, codd, &Aoo));
422:   for (i = 0; i < lm; i++) {
423:     PetscInt    j, je, jo, colse[3], colso[3];
424:     PetscScalar ve[3], vo[3];
425:     PetscScalar v[3];
426:     PetscInt    cols[3];
427:     PetscInt    row = i / 2;

429:     cols[0] = (i - 1 + n) % n;
430:     cols[1] = i % n;
431:     cols[2] = (i + 1) % n;
432:     v[0]    = -1. * (symmetric ? PetscMin(i + 1, cols[0] + 1) : i + 1);
433:     v[1]    = 2. * (symmetric ? PetscMin(i + 1, cols[1] + 1) : i + 1);
434:     v[2]    = -1. * (symmetric ? PetscMin(i + 1, cols[2] + 1) : i + 1);
435:     PetscCall(ISGlobalToLocalMappingApply(cmap, IS_GTOLM_MASK, 3, cols, NULL, cols));
436:     for (j = 0, je = 0, jo = 0; j < 3; j++) {
437:       if (cols[j] % 2) {
438:         vo[jo]      = v[j];
439:         colso[jo++] = cols[j] / 2;
440:       } else {
441:         ve[je]      = v[j];
442:         colse[je++] = cols[j] / 2;
443:       }
444:     }
445:     if (i % 2) {
446:       PetscCall(MatSetValuesLocal(Aoe, 1, &row, je, colse, ve, ADD_VALUES));
447:       PetscCall(MatSetValuesBlockedLocal(Aoo, 1, &row, jo, colso, vo, ADD_VALUES));
448:     } else {
449:       PetscCall(MatSetValuesLocal(Aee, 1, &row, je, colse, ve, ADD_VALUES));
450:       PetscCall(MatSetValuesBlockedLocal(Aeo, 1, &row, jo, colso, vo, ADD_VALUES));
451:     }
452:   }
453:   PetscCall(MatRestoreLocalSubMatrix(A2, reven, ceven, &Aee));
454:   PetscCall(MatRestoreLocalSubMatrix(A2, reven, codd, &Aeo));
455:   PetscCall(MatRestoreLocalSubMatrix(A2, rodd, ceven, &Aoe));
456:   PetscCall(MatRestoreLocalSubMatrix(A2, rodd, codd, &Aoo));
457:   PetscCall(ISDestroy(&reven));
458:   PetscCall(ISDestroy(&ceven));
459:   PetscCall(ISDestroy(&rodd));
460:   PetscCall(ISDestroy(&codd));
461:   PetscCall(MatAssemblyBegin(A2, MAT_FINAL_ASSEMBLY));
462:   PetscCall(MatAssemblyEnd(A2, MAT_FINAL_ASSEMBLY));
463:   PetscCall(MatAXPY(A2, -1., A, SAME_NONZERO_PATTERN));
464:   PetscCall(CheckMat(A2, NULL, PETSC_FALSE, "MatGetLocalSubMatrix"));
465:   PetscCall(MatDestroy(&A2));

467:   /* test MatConvert_Nest_IS */
468:   testT = PETSC_FALSE;
469:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_trans", &testT, NULL));

471:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatConvert_Nest_IS\n"));
472:   nr = 2;
473:   nc = 2;
474:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-nr", &nr, NULL));
475:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-nc", &nc, NULL));
476:   if (testT) {
477:     PetscCall(MatGetOwnershipRange(A, &cst, &cen));
478:     PetscCall(MatGetOwnershipRangeColumn(A, &rst, &ren));
479:   } else {
480:     PetscCall(MatGetOwnershipRange(A, &rst, &ren));
481:     PetscCall(MatGetOwnershipRangeColumn(A, &cst, &cen));
482:   }
483:   PetscCall(PetscMalloc3(nr, &rows, nc, &cols, 2 * nr * nc, &mats));
484:   for (i = 0; i < nr * nc; i++) {
485:     if (testT) {
486:       PetscCall(MatCreateTranspose(A, &mats[i]));
487:       PetscCall(MatTranspose(B, MAT_INITIAL_MATRIX, &mats[i + nr * nc]));
488:     } else {
489:       PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &mats[i]));
490:       PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &mats[i + nr * nc]));
491:     }
492:   }
493:   for (i = 0; i < nr; i++) PetscCall(ISCreateStride(PETSC_COMM_WORLD, ren - rst, i + rst, nr, &rows[i]));
494:   for (i = 0; i < nc; i++) PetscCall(ISCreateStride(PETSC_COMM_WORLD, cen - cst, i + cst, nc, &cols[i]));
495:   PetscCall(MatCreateNest(PETSC_COMM_WORLD, nr, rows, nc, cols, mats, &A2));
496:   PetscCall(MatCreateNest(PETSC_COMM_WORLD, nr, rows, nc, cols, mats + nr * nc, &B2));
497:   for (i = 0; i < nr; i++) PetscCall(ISDestroy(&rows[i]));
498:   for (i = 0; i < nc; i++) PetscCall(ISDestroy(&cols[i]));
499:   for (i = 0; i < 2 * nr * nc; i++) PetscCall(MatDestroy(&mats[i]));
500:   PetscCall(PetscFree3(rows, cols, mats));
501:   PetscCall(MatConvert(B2, MATAIJ, MAT_INITIAL_MATRIX, &T));
502:   PetscCall(MatDestroy(&B2));
503:   PetscCall(MatConvert(A2, MATIS, MAT_INITIAL_MATRIX, &B2));
504:   PetscCall(CheckMat(B2, T, PETSC_TRUE, "MatConvert_Nest_IS MAT_INITIAL_MATRIX"));
505:   PetscCall(MatConvert(A2, MATIS, MAT_REUSE_MATRIX, &B2));
506:   PetscCall(CheckMat(B2, T, PETSC_TRUE, "MatConvert_Nest_IS MAT_REUSE_MATRIX"));
507:   PetscCall(MatDestroy(&B2));
508:   PetscCall(MatConvert(A2, MATIS, MAT_INPLACE_MATRIX, &A2));
509:   PetscCall(CheckMat(A2, T, PETSC_TRUE, "MatConvert_Nest_IS MAT_INPLACE_MATRIX"));
510:   PetscCall(MatDestroy(&T));
511:   PetscCall(MatDestroy(&A2));

513:   /* test MatCreateSubMatrix */
514:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatCreateSubMatrix\n"));
515:   if (rank == 0) {
516:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, 1, 1, 1, &is));
517:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, 2, 0, 1, &is2));
518:   } else if (rank == 1) {
519:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, 1, 0, 1, &is));
520:     if (n > 3) {
521:       PetscCall(ISCreateStride(PETSC_COMM_WORLD, 1, 3, 1, &is2));
522:     } else {
523:       PetscCall(ISCreateStride(PETSC_COMM_WORLD, 0, 0, 1, &is2));
524:     }
525:   } else if (rank == 2 && n > 4) {
526:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, 0, 0, 1, &is));
527:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, n - 4, 4, 1, &is2));
528:   } else {
529:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, 0, 0, 1, &is));
530:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, 0, 0, 1, &is2));
531:   }
532:   PetscCall(MatCreateSubMatrix(A, is, is, MAT_INITIAL_MATRIX, &A2));
533:   PetscCall(MatCreateSubMatrix(B, is, is, MAT_INITIAL_MATRIX, &B2));
534:   PetscCall(CheckMat(A2, B2, PETSC_TRUE, "first MatCreateSubMatrix"));

536:   PetscCall(MatCreateSubMatrix(A, is, is, MAT_REUSE_MATRIX, &A2));
537:   PetscCall(MatCreateSubMatrix(B, is, is, MAT_REUSE_MATRIX, &B2));
538:   PetscCall(CheckMat(A2, B2, PETSC_FALSE, "reuse MatCreateSubMatrix"));
539:   PetscCall(MatDestroy(&A2));
540:   PetscCall(MatDestroy(&B2));

542:   if (!issymmetric) {
543:     PetscCall(MatCreateSubMatrix(A, is, is2, MAT_INITIAL_MATRIX, &A2));
544:     PetscCall(MatCreateSubMatrix(B, is, is2, MAT_INITIAL_MATRIX, &B2));
545:     PetscCall(MatCreateSubMatrix(A, is, is2, MAT_REUSE_MATRIX, &A2));
546:     PetscCall(MatCreateSubMatrix(B, is, is2, MAT_REUSE_MATRIX, &B2));
547:     PetscCall(CheckMat(A2, B2, PETSC_FALSE, "second MatCreateSubMatrix"));
548:   }

550:   PetscCall(MatDestroy(&A2));
551:   PetscCall(MatDestroy(&B2));
552:   PetscCall(ISDestroy(&is));
553:   PetscCall(ISDestroy(&is2));

555:   /* test MatCreateSubMatrices */
556:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatCreateSubMatrices\n"));
557:   PetscCall(MatGetLayouts(A, &rlayout, &clayout));
558:   PetscCall(PetscLayoutGetRanges(rlayout, &rrange));
559:   PetscCall(PetscLayoutGetRanges(clayout, &crange));
560:   lrank = (size + rank - 1) % size;
561:   rrank = (rank + 1) % size;
562:   PetscCall(ISCreateStride(PETSC_COMM_SELF, rrange[lrank + 1] - rrange[lrank], rrange[lrank], 1, &irow[0]));
563:   PetscCall(ISCreateStride(PETSC_COMM_SELF, crange[rrank + 1] - crange[rrank], crange[rrank], 1, &icol[0]));
564:   PetscCall(ISCreateStride(PETSC_COMM_SELF, rrange[rrank + 1] - rrange[rrank], rrange[rrank], 1, &irow[1]));
565:   PetscCall(ISCreateStride(PETSC_COMM_SELF, crange[lrank + 1] - crange[lrank], crange[lrank], 1, &icol[1]));
566:   PetscCall(MatCreateSubMatrices(A, 2, irow, icol, MAT_INITIAL_MATRIX, &Asub));
567:   PetscCall(MatCreateSubMatrices(B, 2, irow, icol, MAT_INITIAL_MATRIX, &Bsub));
568:   PetscCall(CheckMat(Asub[0], Bsub[0], PETSC_FALSE, "MatCreateSubMatrices[0]"));
569:   PetscCall(CheckMat(Asub[1], Bsub[1], PETSC_FALSE, "MatCreateSubMatrices[1]"));
570:   PetscCall(MatCreateSubMatrices(A, 2, irow, icol, MAT_REUSE_MATRIX, &Asub));
571:   PetscCall(MatCreateSubMatrices(B, 2, irow, icol, MAT_REUSE_MATRIX, &Bsub));
572:   PetscCall(CheckMat(Asub[0], Bsub[0], PETSC_FALSE, "MatCreateSubMatrices[0]"));
573:   PetscCall(CheckMat(Asub[1], Bsub[1], PETSC_FALSE, "MatCreateSubMatrices[1]"));
574:   PetscCall(MatDestroySubMatrices(2, &Asub));
575:   PetscCall(MatDestroySubMatrices(2, &Bsub));
576:   PetscCall(ISDestroy(&irow[0]));
577:   PetscCall(ISDestroy(&irow[1]));
578:   PetscCall(ISDestroy(&icol[0]));
579:   PetscCall(ISDestroy(&icol[1]));

581:   /* Create an IS required by MatZeroRows(): just rank zero provides the rows to be eliminated */
582:   if (size > 1) {
583:     if (rank == 0) {
584:       PetscInt st, len;

586:       st  = (m + 1) / 2;
587:       len = PetscMin(m / 2, PetscMax(m - (m + 1) / 2 - 1, 0));
588:       PetscCall(ISCreateStride(PETSC_COMM_WORLD, len, st, 1, &is));
589:     } else {
590:       PetscCall(ISCreateStride(PETSC_COMM_WORLD, 0, 0, 1, &is));
591:     }
592:   } else {
593:     PetscCall(ISCreateStride(PETSC_COMM_WORLD, 1, 0, 1, &is));
594:   }
595:   /* local IS for local zero operations */
596:   PetscCall(ISLocalToGlobalMappingGetSize(rmap, &lm));
597:   PetscCall(ISCreateStride(PETSC_COMM_WORLD, lm ? 1 : 0, 0, 1, &lis));

599:   if (squaretest) { /* tests for square matrices only, with same maps for rows and columns */
600:     PetscInt *idx0, *idx1, n0, n1;
601:     IS        Ais[2], Bis[2];

603:     /* test MatDiagonalSet */
604:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatDiagonalSet\n"));
605:     PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &A2));
606:     PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &B2));
607:     PetscCall(MatCreateVecs(A, NULL, &x));
608:     PetscCall(VecSetRandom(x, NULL));
609:     PetscCall(MatDiagonalSet(A2, x, allow_repeated ? ADD_VALUES : INSERT_VALUES));
610:     PetscCall(MatDiagonalSet(B2, x, allow_repeated ? ADD_VALUES : INSERT_VALUES));
611:     PetscCall(CheckMat(A2, B2, PETSC_FALSE, "MatDiagonalSet"));
612:     PetscCall(VecDestroy(&x));
613:     PetscCall(MatDestroy(&A2));
614:     PetscCall(MatDestroy(&B2));

616:     /* test MatShift (MatShift_IS internally uses MatDiagonalSet_IS with ADD_VALUES) */
617:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatShift\n"));
618:     PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &A2));
619:     PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &B2));
620:     PetscCall(MatShift(A2, 2.0));
621:     PetscCall(MatShift(B2, 2.0));
622:     PetscCall(CheckMat(A2, B2, PETSC_FALSE, "MatShift"));
623:     PetscCall(MatDestroy(&A2));
624:     PetscCall(MatDestroy(&B2));

626:     /* nonzero diag value is supported for square matrices only */
627:     PetscCall(TestMatZeroRows(A, B, PETSC_TRUE, is, diag, PETSC_FALSE));
628:     PetscCall(TestMatZeroRows(A, B, PETSC_TRUE, lis, diag, PETSC_TRUE));

630:     /* test MatIncreaseOverlap */
631:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatIncreaseOverlap\n"));
632:     PetscCall(MatGetOwnershipRange(A, &rst, &ren));
633:     n0 = (ren - rst) / 2;
634:     n1 = (ren - rst) / 3;
635:     PetscCall(PetscMalloc1(n0, &idx0));
636:     PetscCall(PetscMalloc1(n1, &idx1));
637:     for (i = 0; i < n0; i++) idx0[i] = ren - i - 1;
638:     for (i = 0; i < n1; i++) idx1[i] = rst + i;
639:     PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, n0, idx0, PETSC_OWN_POINTER, &Ais[0]));
640:     PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, n1, idx1, PETSC_OWN_POINTER, &Ais[1]));
641:     PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, n0, idx0, PETSC_COPY_VALUES, &Bis[0]));
642:     PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, n1, idx1, PETSC_COPY_VALUES, &Bis[1]));
643:     PetscCall(MatIncreaseOverlap(A, 2, Ais, 3));
644:     PetscCall(MatIncreaseOverlap(B, 2, Bis, 3));
645:     /* Non deterministic output! */
646:     PetscCall(ISSort(Ais[0]));
647:     PetscCall(ISSort(Ais[1]));
648:     PetscCall(ISSort(Bis[0]));
649:     PetscCall(ISSort(Bis[1]));
650:     PetscCall(ISView(Ais[0], NULL));
651:     PetscCall(ISView(Bis[0], NULL));
652:     PetscCall(ISView(Ais[1], NULL));
653:     PetscCall(ISView(Bis[1], NULL));
654:     PetscCall(MatCreateSubMatrices(A, 2, Ais, Ais, MAT_INITIAL_MATRIX, &Asub));
655:     PetscCall(MatCreateSubMatrices(B, 2, Bis, Ais, MAT_INITIAL_MATRIX, &Bsub));
656:     PetscCall(CheckMat(Asub[0], Bsub[0], PETSC_FALSE, "MatIncreaseOverlap[0]"));
657:     PetscCall(CheckMat(Asub[1], Bsub[1], PETSC_FALSE, "MatIncreaseOverlap[1]"));
658:     PetscCall(MatDestroySubMatrices(2, &Asub));
659:     PetscCall(MatDestroySubMatrices(2, &Bsub));
660:     PetscCall(ISDestroy(&Ais[0]));
661:     PetscCall(ISDestroy(&Ais[1]));
662:     PetscCall(ISDestroy(&Bis[0]));
663:     PetscCall(ISDestroy(&Bis[1]));
664:   }
665:   PetscCall(TestMatZeroRows(A, B, squaretest, is, 0.0, PETSC_FALSE));
666:   PetscCall(TestMatZeroRows(A, B, squaretest, lis, 0.0, PETSC_TRUE));
667:   PetscCall(ISDestroy(&is));
668:   PetscCall(ISDestroy(&lis));

670:   /* test MatTranspose */
671:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatTranspose\n"));
672:   PetscCall(MatTranspose(A, MAT_INITIAL_MATRIX, &A2));
673:   PetscCall(MatTranspose(B, MAT_INITIAL_MATRIX, &B2));
674:   PetscCall(CheckMat(A2, B2, PETSC_FALSE, "initial matrix MatTranspose"));

676:   PetscCall(MatTranspose(A, MAT_REUSE_MATRIX, &A2));
677:   PetscCall(CheckMat(A2, B2, PETSC_FALSE, "reuse matrix (not in place) MatTranspose"));
678:   PetscCall(MatDestroy(&A2));

680:   PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &A2));
681:   PetscCall(MatTranspose(A2, MAT_INPLACE_MATRIX, &A2));
682:   PetscCall(CheckMat(A2, B2, PETSC_FALSE, "reuse matrix (in place) MatTranspose"));
683:   PetscCall(MatDestroy(&A2));

685:   PetscCall(MatTranspose(A, MAT_INITIAL_MATRIX, &A2));
686:   PetscCall(CheckMat(A2, B2, PETSC_FALSE, "reuse matrix (different type) MatTranspose"));
687:   PetscCall(MatDestroy(&A2));
688:   PetscCall(MatDestroy(&B2));

690:   /* test MatISFixLocalEmpty */
691:   if (isaij) {
692:     PetscInt r[2];

694:     r[0] = 0;
695:     r[1] = PetscMin(m, n) - 1;
696:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatISFixLocalEmpty\n"));
697:     PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &A2));

699:     PetscCall(MatISFixLocalEmpty(A2, PETSC_TRUE));
700:     PetscCall(MatAssemblyBegin(A2, MAT_FINAL_ASSEMBLY));
701:     PetscCall(MatAssemblyEnd(A2, MAT_FINAL_ASSEMBLY));
702:     PetscCall(CheckMat(A2, B, PETSC_FALSE, "MatISFixLocalEmpty (null)"));

704:     PetscCall(MatZeroRows(A2, 2, r, 0.0, NULL, NULL));
705:     PetscCall(MatViewFromOptions(A2, NULL, "-fixempty_view"));
706:     PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &B2));
707:     PetscCall(MatZeroRows(B2, 2, r, 0.0, NULL, NULL));
708:     PetscCall(MatISFixLocalEmpty(A2, PETSC_TRUE));
709:     PetscCall(MatAssemblyBegin(A2, MAT_FINAL_ASSEMBLY));
710:     PetscCall(MatAssemblyEnd(A2, MAT_FINAL_ASSEMBLY));
711:     PetscCall(MatViewFromOptions(A2, NULL, "-fixempty_view"));
712:     PetscCall(CheckMat(A2, B2, PETSC_FALSE, "MatISFixLocalEmpty (rows)"));
713:     PetscCall(MatDestroy(&A2));

715:     PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &A2));
716:     PetscCall(MatZeroRows(A2, 2, r, 0.0, NULL, NULL));
717:     PetscCall(MatTranspose(A2, MAT_INPLACE_MATRIX, &A2));
718:     PetscCall(MatTranspose(B2, MAT_INPLACE_MATRIX, &B2));
719:     PetscCall(MatViewFromOptions(A2, NULL, "-fixempty_view"));
720:     PetscCall(MatISFixLocalEmpty(A2, PETSC_TRUE));
721:     PetscCall(MatAssemblyBegin(A2, MAT_FINAL_ASSEMBLY));
722:     PetscCall(MatAssemblyEnd(A2, MAT_FINAL_ASSEMBLY));
723:     PetscCall(MatViewFromOptions(A2, NULL, "-fixempty_view"));
724:     PetscCall(CheckMat(A2, B2, PETSC_FALSE, "MatISFixLocalEmpty (cols)"));

726:     PetscCall(MatDestroy(&A2));
727:     PetscCall(MatDestroy(&B2));

729:     if (squaretest) {
730:       PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &A2));
731:       PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &B2));
732:       PetscCall(MatZeroRowsColumns(A2, 2, r, 0.0, NULL, NULL));
733:       PetscCall(MatZeroRowsColumns(B2, 2, r, 0.0, NULL, NULL));
734:       PetscCall(MatViewFromOptions(A2, NULL, "-fixempty_view"));
735:       PetscCall(MatISFixLocalEmpty(A2, PETSC_TRUE));
736:       PetscCall(MatAssemblyBegin(A2, MAT_FINAL_ASSEMBLY));
737:       PetscCall(MatAssemblyEnd(A2, MAT_FINAL_ASSEMBLY));
738:       PetscCall(MatViewFromOptions(A2, NULL, "-fixempty_view"));
739:       PetscCall(CheckMat(A2, B2, PETSC_FALSE, "MatISFixLocalEmpty (rows+cols)"));
740:       PetscCall(MatDestroy(&A2));
741:       PetscCall(MatDestroy(&B2));
742:     }
743:   }

745:   /* test MatInvertBlockDiagonal
746:        special cases for block-diagonal matrices */
747:   if (m == n) {
748:     ISLocalToGlobalMapping map;
749:     Mat                    Abd, Bbd;
750:     IS                     is, bis;
751:     const PetscScalar     *isbd, *aijbd;
752:     const PetscInt        *sts, *idxs;
753:     PetscInt              *idxs2, diff, perm, nl, bs, st, en, in;
754:     PetscBool              ok;

756:     for (diff = 0; diff < 3; diff++) {
757:       for (perm = 0; perm < 3; perm++) {
758:         for (bs = 1; bs < 4; bs++) {
759:           PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatInvertBlockDiagonal blockdiag %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", n, diff, perm, bs));
760:           PetscCall(PetscMalloc1(bs * bs, &vals));
761:           PetscCall(MatGetOwnershipRanges(A, &sts));
762:           switch (diff) {
763:           case 1: /* inverted layout by processes */
764:             in = 1;
765:             st = sts[size - rank - 1];
766:             en = sts[size - rank];
767:             nl = en - st;
768:             break;
769:           case 2: /* round-robin layout */
770:             in = size;
771:             st = rank;
772:             nl = n / size;
773:             if (rank < n % size) nl++;
774:             break;
775:           default: /* same layout */
776:             in = 1;
777:             st = sts[rank];
778:             en = sts[rank + 1];
779:             nl = en - st;
780:             break;
781:           }
782:           PetscCall(ISCreateStride(PETSC_COMM_WORLD, nl, st, in, &is));
783:           PetscCall(ISGetLocalSize(is, &nl));
784:           PetscCall(ISGetIndices(is, &idxs));
785:           PetscCall(PetscMalloc1(nl, &idxs2));
786:           for (i = 0; i < nl; i++) {
787:             switch (perm) { /* invert some of the indices */
788:             case 2:
789:               idxs2[i] = rank % 2 ? idxs[i] : idxs[nl - i - 1];
790:               break;
791:             case 1:
792:               idxs2[i] = rank % 2 ? idxs[nl - i - 1] : idxs[i];
793:               break;
794:             default:
795:               idxs2[i] = idxs[i];
796:               break;
797:             }
798:           }
799:           PetscCall(ISRestoreIndices(is, &idxs));
800:           PetscCall(ISCreateBlock(PETSC_COMM_WORLD, bs, nl, idxs2, PETSC_OWN_POINTER, &bis));
801:           PetscCall(ISLocalToGlobalMappingCreateIS(bis, &map));
802:           PetscCall(MatCreateIS(PETSC_COMM_WORLD, bs, PETSC_DECIDE, PETSC_DECIDE, bs * n, bs * n, map, map, &Abd));
803:           PetscCall(ISLocalToGlobalMappingDestroy(&map));
804:           PetscCall(MatISSetPreallocation(Abd, bs, NULL, 0, NULL));
805:           for (i = 0; i < nl; i++) {
806:             for (PetscInt b1 = 0; b1 < bs; b1++)
807:               for (PetscInt b2 = 0; b2 < bs; b2++) vals[b1 * bs + b2] = i * bs * bs + b1 * bs + b2 + 1 + (b1 == b2 ? 1.0 : 0);
808:             PetscCall(MatSetValuesBlockedLocal(Abd, 1, &i, 1, &i, vals, INSERT_VALUES));
809:           }
810:           PetscCall(MatAssemblyBegin(Abd, MAT_FINAL_ASSEMBLY));
811:           PetscCall(MatAssemblyEnd(Abd, MAT_FINAL_ASSEMBLY));
812:           PetscCall(MatConvert(Abd, MATAIJ, MAT_INITIAL_MATRIX, &Bbd));
813:           PetscCall(MatInvertBlockDiagonal(Abd, &isbd));
814:           PetscCall(MatInvertBlockDiagonal(Bbd, &aijbd));
815:           PetscCall(MatGetLocalSize(Bbd, &nl, NULL));
816:           ok = PETSC_TRUE;
817:           for (i = 0; i < nl / bs; i++) {
818:             for (PetscInt b1 = 0; b1 < bs; b1++) {
819:               for (PetscInt b2 = 0; b2 < bs; b2++) {
820:                 if (PetscAbsScalar(isbd[i * bs * bs + b1 * bs + b2] - aijbd[i * bs * bs + b1 * bs + b2]) > PETSC_SMALL) ok = PETSC_FALSE;
821:                 if (!ok) {
822:                   PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] ERROR block %" PetscInt_FMT ", entry %" PetscInt_FMT " %" PetscInt_FMT ": %g %g\n", rank, i, b1, b2, (double)PetscAbsScalar(isbd[i * bs * bs + b1 * bs + b2]), (double)PetscAbsScalar(aijbd[i * bs * bs + b1 * bs + b2])));
823:                   break;
824:                 }
825:               }
826:               if (!ok) break;
827:             }
828:             if (!ok) break;
829:           }
830:           PetscCall(MatDestroy(&Abd));
831:           PetscCall(MatDestroy(&Bbd));
832:           PetscCall(PetscFree(vals));
833:           PetscCall(ISDestroy(&is));
834:           PetscCall(ISDestroy(&bis));
835:         }
836:       }
837:     }
838:   }

840:   /* test MatGetDiagonalBlock */
841:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatGetDiagonalBlock\n"));
842:   PetscCall(MatGetDiagonalBlock(A, &A2));
843:   PetscCall(MatGetDiagonalBlock(B, &B2));
844:   PetscCall(CheckMat(A2, B2, PETSC_FALSE, "MatGetDiagonalBlock"));
845:   PetscCall(MatScale(A, 2.0));
846:   PetscCall(MatScale(B, 2.0));
847:   PetscCall(MatGetDiagonalBlock(A, &A2));
848:   PetscCall(MatGetDiagonalBlock(B, &B2));
849:   PetscCall(CheckMat(A2, B2, PETSC_FALSE, "MatGetDiagonalBlock"));

851:   /* test MatISSetAllowRepeated on a MATIS */
852:   PetscCall(MatISSetAllowRepeated(A, allow_repeated));
853:   if (allow_repeated) { /* original MATIS maybe with repeated entries, test assembling of local matrices */
854:     Mat lA, lA2;

856:     for (PetscInt i = 0; i < 1; i++) { /* TODO: make MatScatter inherit from MATSHELL and support MatProducts */
857:       PetscBool usemult = PETSC_FALSE;

859:       PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &A2));
860:       if (i) {
861:         Mat tA;

863:         usemult = PETSC_TRUE;
864:         PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatISSetAllowRepeated(false) with possibly repeated entries and local shell matrices\n"));
865:         PetscCall(MatISGetLocalMat(A2, &lA2));
866:         PetscCall(MatConvert(lA2, MATSHELL, MAT_INITIAL_MATRIX, &tA));
867:         PetscCall(MatISRestoreLocalMat(A2, &lA2));
868:         PetscCall(MatISSetLocalMat(A2, tA));
869:         PetscCall(MatDestroy(&tA));
870:       } else {
871:         PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatISSetAllowRepeated(false) with possibly repeated entries\n"));
872:       }
873:       PetscCall(MatISSetAllowRepeated(A2, PETSC_FALSE));
874:       PetscCall(MatISGetLocalMat(A, &lA));
875:       PetscCall(MatISGetLocalMat(A2, &lA2));
876:       if (!repmap) PetscCall(CheckMat(lA, lA2, usemult, "MatISSetAllowRepeated(false) with non-repeated entries"));
877:       PetscCall(MatISRestoreLocalMat(A, &lA));
878:       PetscCall(MatISRestoreLocalMat(A2, &lA2));
879:       if (repmap) PetscCall(CheckMat(A2, B, usemult, "MatISSetAllowRepeated(false) with repeated entries"));
880:       else PetscCall(CheckMat(A2, B, usemult, "MatISSetAllowRepeated(false) with non-repeated entries"));
881:       PetscCall(MatDestroy(&A2));
882:     }
883:   } else { /* original matis with non-repeated entries, this should only recreate the local matrices */
884:     Mat       lA;
885:     PetscBool flg;

887:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatISSetAllowRepeated(true) with non repeated entries\n"));
888:     PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &A2));
889:     PetscCall(MatISSetAllowRepeated(A2, PETSC_TRUE));
890:     PetscCall(MatISGetLocalMat(A2, &lA));
891:     PetscCall(MatAssembled(lA, &flg));
892:     PetscCheck(!flg, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Local mat should be unassembled");
893:     PetscCall(MatISRestoreLocalMat(A2, &lA));
894:     PetscCall(MatDestroy(&A2));
895:   }

897:   /* Test MatZeroEntries */
898:   PetscCall(MatZeroEntries(A));
899:   PetscCall(MatZeroEntries(B));
900:   PetscCall(CheckMat(A, B, PETSC_FALSE, "MatZeroEntries"));

902:   /* Test MatSetValues and MatSetValuesBlocked */
903:   if (test_setvalues) {
904:     PetscCall(PetscMalloc1(lm * ln, &vals));
905:     for (i = 0; i < lm * ln; i++) vals[i] = i + 1.0;
906:     PetscCall(MatGetLocalSize(A, NULL, &ln));
907:     PetscCall(MatISSetPreallocation(A, ln, NULL, n - ln, NULL));
908:     PetscCall(MatSeqAIJSetPreallocation(B, ln, NULL));
909:     PetscCall(MatMPIAIJSetPreallocation(B, ln, NULL, n - ln, NULL));
910:     PetscCall(ISLocalToGlobalMappingGetSize(rmap, &lm));
911:     PetscCall(ISLocalToGlobalMappingGetSize(cmap, &ln));

913:     PetscCall(ISLocalToGlobalMappingGetIndices(rmap, &idxs1));
914:     PetscCall(ISLocalToGlobalMappingGetIndices(cmap, &idxs2));
915:     PetscCall(MatSetValues(A, lm, idxs1, ln, idxs2, vals, ADD_VALUES));
916:     PetscCall(MatSetValues(B, lm, idxs1, ln, idxs2, vals, ADD_VALUES));
917:     PetscCall(ISLocalToGlobalMappingRestoreIndices(rmap, &idxs1));
918:     PetscCall(ISLocalToGlobalMappingRestoreIndices(cmap, &idxs2));
919:     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
920:     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
921:     PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
922:     PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
923:     PetscCall(CheckMat(A, B, PETSC_FALSE, "MatSetValues"));

925:     PetscCall(ISLocalToGlobalMappingGetBlockIndices(rmap, &idxs1));
926:     PetscCall(ISLocalToGlobalMappingGetBlockIndices(cmap, &idxs2));
927:     PetscCall(MatSetValuesBlocked(A, lm / rbs, idxs1, ln / cbs, idxs2, vals, ADD_VALUES));
928:     PetscCall(MatSetValuesBlocked(B, lm / rbs, idxs1, ln / cbs, idxs2, vals, ADD_VALUES));
929:     PetscCall(ISLocalToGlobalMappingRestoreBlockIndices(rmap, &idxs1));
930:     PetscCall(ISLocalToGlobalMappingRestoreBlockIndices(cmap, &idxs2));
931:     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
932:     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
933:     PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
934:     PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
935:     PetscCall(CheckMat(A, B, PETSC_FALSE, "MatSetValuesBlocked"));
936:     PetscCall(PetscFree(vals));
937:   }

939:   /* free testing matrices */
940:   PetscCall(ISLocalToGlobalMappingDestroy(&cmap));
941:   PetscCall(ISLocalToGlobalMappingDestroy(&rmap));
942:   PetscCall(MatDestroy(&A));
943:   PetscCall(MatDestroy(&B));
944:   PetscCall(PetscFinalize());
945:   return 0;
946: }

948: PetscErrorCode CheckMat(Mat A, Mat B, PetscBool usemult, const char *func)
949: {
950:   Mat       Bcheck;
951:   PetscReal error;

953:   PetscFunctionBeginUser;
954:   if (!usemult && B) {
955:     PetscBool hasnorm;

957:     PetscCall(MatHasOperation(B, MATOP_NORM, &hasnorm));
958:     if (!hasnorm) usemult = PETSC_TRUE;
959:   }
960:   if (!usemult) {
961:     if (B) {
962:       MatType Btype;

964:       PetscCall(MatGetType(B, &Btype));
965:       PetscCall(MatConvert(A, Btype, MAT_INITIAL_MATRIX, &Bcheck));
966:     } else {
967:       PetscCall(MatConvert(A, MATAIJ, MAT_INITIAL_MATRIX, &Bcheck));
968:     }
969:     if (B) { /* if B is present, subtract it */
970:       PetscCall(MatAXPY(Bcheck, -1., B, DIFFERENT_NONZERO_PATTERN));
971:     }
972:     PetscCall(MatNorm(Bcheck, NORM_INFINITY, &error));
973:     if (error > PETSC_SQRT_MACHINE_EPSILON) {
974:       ISLocalToGlobalMapping rl2g, cl2g;

976:       PetscCall(PetscObjectSetName((PetscObject)Bcheck, "Bcheck"));
977:       PetscCall(MatView(Bcheck, NULL));
978:       if (B) {
979:         PetscCall(PetscObjectSetName((PetscObject)B, "B"));
980:         PetscCall(MatView(B, NULL));
981:         PetscCall(MatDestroy(&Bcheck));
982:         PetscCall(MatConvert(A, MATAIJ, MAT_INITIAL_MATRIX, &Bcheck));
983:         PetscCall(PetscObjectSetName((PetscObject)Bcheck, "Assembled A"));
984:         PetscCall(MatView(Bcheck, NULL));
985:       }
986:       PetscCall(MatDestroy(&Bcheck));
987:       PetscCall(PetscObjectSetName((PetscObject)A, "A"));
988:       PetscCall(MatView(A, NULL));
989:       PetscCall(MatGetLocalToGlobalMapping(A, &rl2g, &cl2g));
990:       if (rl2g) PetscCall(ISLocalToGlobalMappingView(rl2g, NULL));
991:       if (cl2g) PetscCall(ISLocalToGlobalMappingView(cl2g, NULL));
992:       SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "ERROR ON %s: %g", func, (double)error);
993:     }
994:     PetscCall(MatDestroy(&Bcheck));
995:   } else {
996:     PetscBool ok, okt;

998:     PetscCall(MatMultEqual(A, B, 3, &ok));
999:     PetscCall(MatMultTransposeEqual(A, B, 3, &okt));
1000:     PetscCheck(ok && okt, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "ERROR ON %s: mult ok ?  %d, multtranspose ok ? %d", func, ok, okt);
1001:   }
1002:   PetscFunctionReturn(PETSC_SUCCESS);
1003: }

1005: PetscErrorCode CheckVariableBlockSizes(Mat A)
1006: {
1007:   Mat             lA;
1008:   const PetscInt *bsizes;
1009:   PetscInt        m, nblocks, sum = 0;

1011:   PetscFunctionBeginUser;
1012:   PetscCall(MatISGetLocalMat(A, &lA));
1013:   PetscCall(MatGetLocalSize(lA, &m, NULL));
1014:   PetscCall(MatGetVariableBlockSizes(lA, &nblocks, &bsizes));
1015:   for (PetscInt i = 0; i < nblocks; i++) sum += bsizes[i];
1016:   PetscCheck(sum == m && (!m || nblocks), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid variable block sizes on loaded local matrix");
1017:   PetscCall(MatISRestoreLocalMat(A, &lA));
1018:   PetscFunctionReturn(PETSC_SUCCESS);
1019: }

1021: PetscErrorCode TestMatZeroRows(Mat A, Mat Afull, PetscBool squaretest, IS is, PetscScalar diag, PetscBool local)
1022: {
1023:   Mat                    B, Bcheck, B2 = NULL, lB;
1024:   Vec                    x = NULL, b = NULL, b2 = NULL;
1025:   ISLocalToGlobalMapping l2gr, l2gc;
1026:   PetscReal              error;
1027:   char                   diagstr[16];
1028:   const PetscInt        *idxs;
1029:   PetscInt               i, n, N;
1030:   PetscBool              haszerorows;
1031:   IS                     gis;

1033:   PetscFunctionBeginUser;
1034:   if (diag == 0.) {
1035:     PetscCall(PetscStrncpy(diagstr, "zero", sizeof(diagstr)));
1036:   } else {
1037:     PetscCall(PetscStrncpy(diagstr, "nonzero", sizeof(diagstr)));
1038:   }
1039:   PetscCall(ISView(is, NULL));
1040:   PetscCall(MatGetLocalToGlobalMapping(A, &l2gr, &l2gc));
1041:   /* tests MatDuplicate and MatCopy */
1042:   if (diag == 0.) PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &B));
1043:   else {
1044:     PetscCall(MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, &B));
1045:     PetscCall(MatCopy(A, B, SAME_NONZERO_PATTERN));
1046:   }
1047:   PetscCall(MatISGetLocalMat(B, &lB));
1048:   PetscCall(MatHasOperation(lB, MATOP_ZERO_ROWS, &haszerorows));
1049:   if (squaretest && haszerorows) {
1050:     PetscCall(MatCreateVecs(B, &x, &b));
1051:     PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &B2));
1052:     PetscCall(VecSetLocalToGlobalMapping(b, l2gr));
1053:     PetscCall(VecSetLocalToGlobalMapping(x, l2gc));
1054:     PetscCall(VecSetRandom(x, NULL));
1055:     PetscCall(VecSetRandom(b, NULL));
1056:     /* mimic b[is] = x[is] */
1057:     PetscCall(VecDuplicate(b, &b2));
1058:     PetscCall(VecSetLocalToGlobalMapping(b2, l2gr));
1059:     PetscCall(VecCopy(b, b2));
1060:     if (local) {
1061:       PetscCall(ISL2GMapNoNeg(l2gr, is, &gis));
1062:       PetscCall(ISGetLocalSize(gis, &n));
1063:       PetscCall(ISGetIndices(gis, &idxs));
1064:     } else {
1065:       PetscCall(ISGetLocalSize(is, &n));
1066:       PetscCall(ISGetIndices(is, &idxs));
1067:     }
1068:     PetscCall(VecGetSize(x, &N));
1069:     for (i = 0; i < n; i++) {
1070:       if (0 <= idxs[i] && idxs[i] < N) {
1071:         PetscCall(VecSetValue(b2, idxs[i], diag, INSERT_VALUES));
1072:         PetscCall(VecSetValue(x, idxs[i], 1., INSERT_VALUES));
1073:       }
1074:     }
1075:     if (local) {
1076:       PetscCall(ISRestoreIndices(gis, &idxs));
1077:       PetscCall(ISDestroy(&gis));
1078:     } else {
1079:       PetscCall(ISRestoreIndices(is, &idxs));
1080:     }
1081:     PetscCall(VecAssemblyBegin(b2));
1082:     PetscCall(VecAssemblyEnd(b2));
1083:     PetscCall(VecAssemblyBegin(x));
1084:     PetscCall(VecAssemblyEnd(x));
1085:     /*  test ZeroRows on MATIS */
1086:     if (local) {
1087:       PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatZeroRowsLocal (diag %s)\n", diagstr));
1088:       PetscCall(MatZeroRowsLocalIS(B, is, diag, x, b));
1089:       PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatZeroRowsColumnsLocal (diag %s)\n", diagstr));
1090:       PetscCall(MatZeroRowsColumnsLocalIS(B2, is, diag, NULL, NULL));
1091:     } else {
1092:       PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatZeroRows (diag %s)\n", diagstr));
1093:       PetscCall(MatZeroRowsIS(B, is, diag, x, b));
1094:       PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatZeroRowsColumns (diag %s)\n", diagstr));
1095:       PetscCall(MatZeroRowsColumnsIS(B2, is, diag, NULL, NULL));
1096:     }
1097:   } else if (haszerorows) {
1098:     /*  test ZeroRows on MATIS */
1099:     if (local) {
1100:       PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatZeroRowsLocal (diag %s)\n", diagstr));
1101:       PetscCall(MatZeroRowsLocalIS(B, is, diag, NULL, NULL));
1102:     } else {
1103:       PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test MatZeroRows (diag %s)\n", diagstr));
1104:       PetscCall(MatZeroRowsIS(B, is, diag, NULL, NULL));
1105:     }
1106:     b = b2 = x = NULL;
1107:   } else {
1108:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Skipping MatZeroRows (diag %s)\n", diagstr));
1109:     b = b2 = x = NULL;
1110:   }

1112:   if (squaretest && haszerorows) {
1113:     PetscCall(VecAXPY(b2, -1., b));
1114:     PetscCall(VecNorm(b2, NORM_INFINITY, &error));
1115:     PetscCheck(error <= PETSC_SQRT_MACHINE_EPSILON, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "ERROR IN ZEROROWS ON B %g (diag %s)", (double)error, diagstr);
1116:   }
1117:   PetscCall(VecDestroy(&x));
1118:   PetscCall(VecDestroy(&b));
1119:   PetscCall(VecDestroy(&b2));

1121:   /* check the result of ZeroRows with that from MPIAIJ routines
1122:      assuming that MatConvert_IS_XAIJ and MatZeroRows_MPIAIJ work fine */
1123:   if (haszerorows) {
1124:     PetscCall(MatDuplicate(Afull, MAT_COPY_VALUES, &Bcheck));
1125:     PetscCall(MatSetOption(Bcheck, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
1126:     if (local) {
1127:       PetscCall(ISL2GMapNoNeg(l2gr, is, &gis));
1128:       PetscCall(MatZeroRowsIS(Bcheck, gis, diag, NULL, NULL));
1129:       PetscCall(ISDestroy(&gis));
1130:     } else {
1131:       PetscCall(MatZeroRowsIS(Bcheck, is, diag, NULL, NULL));
1132:     }
1133:     PetscCall(CheckMat(B, Bcheck, PETSC_FALSE, "Zerorows"));
1134:     PetscCall(MatDestroy(&Bcheck));
1135:   }
1136:   PetscCall(MatDestroy(&B));

1138:   if (B2) { /* test MatZeroRowsColumns */
1139:     PetscCall(MatDuplicate(Afull, MAT_COPY_VALUES, &B));
1140:     PetscCall(MatSetOption(B, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
1141:     if (local) {
1142:       PetscCall(ISL2GMapNoNeg(l2gr, is, &gis));
1143:       PetscCall(MatZeroRowsColumnsIS(B, gis, diag, NULL, NULL));
1144:       PetscCall(ISDestroy(&gis));
1145:     } else {
1146:       PetscCall(MatZeroRowsColumnsIS(B, is, diag, NULL, NULL));
1147:     }
1148:     PetscCall(CheckMat(B2, B, PETSC_FALSE, "MatZeroRowsColumns"));
1149:     PetscCall(MatDestroy(&B));
1150:     PetscCall(MatDestroy(&B2));
1151:   }
1152:   PetscFunctionReturn(PETSC_SUCCESS);
1153: }

1155: PetscErrorCode ISL2GMapNoNeg(ISLocalToGlobalMapping mapping, IS is, IS *newis)
1156: {
1157:   PetscInt        n, *idxout, nn = 0;
1158:   const PetscInt *idxin;

1160:   PetscFunctionBegin;
1161:   PetscCall(ISGetLocalSize(is, &n));
1162:   PetscCall(ISGetIndices(is, &idxin));
1163:   PetscCall(PetscMalloc1(n, &idxout));
1164:   PetscCall(ISLocalToGlobalMappingApply(mapping, n, idxin, idxout));
1165:   PetscCall(ISRestoreIndices(is, &idxin));
1166:   for (PetscInt i = 0; i < n; i++)
1167:     if (idxout[i] > -1) idxout[nn++] = idxout[i];
1168:   PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)is), nn, idxout, PETSC_OWN_POINTER, newis));
1169:   PetscFunctionReturn(PETSC_SUCCESS);
1170: }

1172: /*TEST

1174:    test:
1175:       requires: !complex
1176:       args: -test_matlab -test_trans

1178:    test:
1179:       suffix: 2
1180:       nsize: 4
1181:       args: -mat_is_convert_local_nest -nr 3 -nc 4

1183:    test:
1184:       suffix: 3
1185:       nsize: 5
1186:       args: -m 11 -n 10 -mat_is_convert_local_nest -nr 2 -nc 1 -cbs 2

1188:    test:
1189:       suffix: 4
1190:       nsize: 6
1191:       args: -m 9 -n 12 -test_trans -nr 2 -nc 7

1193:    test:
1194:       suffix: 5
1195:       nsize: 6
1196:       args: -m 12 -n 12 -test_trans -nr 3 -nc 1 -rbs 2 -test_variableblocksizes -mat_is_view_variableblocksizes

1198:    test:
1199:       suffix: 6
1200:       args: -m 12 -n 12 -test_trans -nr 2 -nc 3 -diffmap -rbs 6 -cbs 3

1202:    test:
1203:       suffix: 7
1204:       args: -m 12 -n 12 -test_trans -nr 2 -nc 3 -diffmap -permmap

1206:    test:
1207:       suffix: 8
1208:       args: -m 12 -n 17 -test_trans -nr 2 -nc 3 -permmap

1210:    test:
1211:       suffix: 9
1212:       nsize: 5
1213:       args: -m 12 -n 12 -test_trans -nr 2 -nc 3 -diffmap

1215:    test:
1216:       suffix: 10
1217:       nsize: 5
1218:       args: -m 12 -n 12 -test_trans -nr 2 -nc 3 -diffmap -permmap

1220:    test:
1221:       suffix: vscat_default
1222:       nsize: 5
1223:       args: -m 12 -n 17 -test_trans -nr 2 -nc 3 -permmap
1224:       output_file: output/ex23_11.out

1226:    test:
1227:       suffix: 12
1228:       nsize: 3
1229:       args: -m 12 -n 12 -symmetric -mat_is_localmat_type sbaij -test_trans -nr 2 -nc 3 -test_setvalues 0

1231:    testset:
1232:       output_file: output/ex23_13.out
1233:       nsize: 3
1234:       args: -m 12 -n 17 -test_trans -nr 2 -nc 3 -diffmap -permmap
1235:       filter: grep -v "type:" | grep -v "not using I-node routines"
1236:       test:
1237:         suffix: baij
1238:         args: -mat_is_localmat_type baij
1239:       test:
1240:         requires: viennacl
1241:         suffix: viennacl
1242:         args: -mat_is_localmat_type aijviennacl
1243:       test:
1244:         requires: cuda
1245:         suffix: cusparse
1246:         args: -mat_is_localmat_type aijcusparse
1247:       test:
1248:         requires: kokkos_kernels
1249:         suffix: kokkos
1250:         args: -mat_is_localmat_type aijkokkos

1252:    test:
1253:       suffix: negrep
1254:       nsize: {{1 3}separate output}
1255:       args: -m {{5 7}separate output} -n {{5 7}separate output} -test_trans -nr 2 -nc 3 -negmap {{0 1}separate output} -repmap {{0 1}separate output} -permmap -diffmap {{0 1}separate output} -allow_repeated 0

1257:    test:
1258:       suffix: negrep_allowrep
1259:       nsize: {{1 3}separate output}
1260:       args: -m {{5 7}separate output} -n {{5 7}separate output} -test_trans -nr 2 -nc 3 -negmap {{0 1}separate output} -repmap {{0 1}separate output} -permmap -diffmap {{0 1}separate output} -allow_repeated

1262: TEST*/