Actual source code: ex107.c
1: static char help[] = "Test MatCreate() with MAT_STRUCTURE_ONLY.\n\n";
3: #include <petscmat.h>
5: int main(int argc, char **argv)
6: {
7: Mat mat;
8: PetscInt m = 7, n, nlocal, i, j, rstart, rend, bs;
9: PetscMPIInt size;
10: PetscScalar v;
11: PetscBool struct_only = PETSC_TRUE, explicit_preallocation = PETSC_FALSE, ismpiaij, ismpibaij, ismpisbaij;
13: PetscFunctionBeginUser;
14: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
15: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
17: PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_COMMON));
18: PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
19: PetscCall(PetscOptionsGetBool(NULL, NULL, "-struct_only", &struct_only, NULL));
20: PetscCall(PetscOptionsGetBool(NULL, NULL, "-explicit_preallocation", &explicit_preallocation, NULL));
21: n = m;
22: nlocal = PETSC_DECIDE;
23: PetscCall(PetscSplitOwnership(PETSC_COMM_WORLD, &nlocal, &n));
25: /* ------- Assemble matrix, test MatValid() --------- */
26: PetscCall(MatCreate(PETSC_COMM_WORLD, &mat));
27: PetscCall(MatSetSizes(mat, PETSC_DECIDE, PETSC_DECIDE, m, n));
28: PetscCall(MatSetFromOptions(mat));
29: PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATMPIAIJ, &ismpiaij));
30: PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATMPIBAIJ, &ismpibaij));
31: PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATMPISBAIJ, &ismpisbaij));
32: if (struct_only) PetscCall(MatSetOption(mat, MAT_STRUCTURE_ONLY, PETSC_TRUE));
33: if (explicit_preallocation) {
34: PetscCall(MatGetBlockSize(mat, &bs));
35: if (ismpiaij) PetscCall(MatMPIAIJSetPreallocation(mat, nlocal, NULL, n - nlocal, NULL));
36: else if (ismpibaij) PetscCall(MatMPIBAIJSetPreallocation(mat, bs, nlocal / bs, NULL, (n - nlocal) / bs, NULL));
37: else if (ismpisbaij) PetscCall(MatMPISBAIJSetPreallocation(mat, bs, nlocal / bs, NULL, (n - nlocal) / bs, NULL));
38: else {
39: PetscCall(MatSeqAIJSetPreallocation(mat, n, NULL));
40: PetscCall(MatSeqBAIJSetPreallocation(mat, bs, n / bs, NULL));
41: PetscCall(MatSeqSBAIJSetPreallocation(mat, bs, n / bs, NULL));
42: }
43: } else PetscCall(MatSetUp(mat));
44: PetscCall(MatGetOwnershipRange(mat, &rstart, &rend));
45: for (i = rstart; i < rend; i++) {
46: for (j = 0; j < n; j++) {
47: v = 10.0 * i + j;
48: PetscCall(MatSetValues(mat, 1, &i, 1, &j, &v, INSERT_VALUES));
49: }
50: }
51: PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
52: PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
53: if (size == 1) PetscCall(MatView(mat, PETSC_VIEWER_STDOUT_WORLD));
55: /* Free data structures */
56: PetscCall(MatDestroy(&mat));
57: PetscCall(PetscFinalize());
58: return 0;
59: }
61: /*TEST
63: test:
64: output_file: output/ex107.out
66: test:
67: suffix: 2
68: args: -mat_type {{baij sbaij}separate output} -mat_block_size 2 -m 10
70: testset:
71: nsize: 2
72: output_file: output/empty.out
73: test:
74: suffix: aij
75: args: -explicit_preallocation {{true false}shared output}
76: test:
77: suffix: block
78: args: -mat_type {{baij sbaij}shared output} -mat_block_size 2 -m 8 -explicit_preallocation {{true false}shared output}
80: TEST*/