Actual source code: ex135.c
1: static const char help[] = "Test parallel assembly of SBAIJ matrices\n\n";
3: #include <petscmat.h>
5: PetscErrorCode Assemble(MPI_Comm comm, PetscInt n, MatType mtype)
6: {
7: Mat A;
8: PetscInt first, last, i;
9: PetscMPIInt rank, size;
11: MatCreate(PETSC_COMM_WORLD, &A);
12: MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, n, n);
13: MatSetType(A, MATMPISBAIJ);
14: MatSetFromOptions(A);
15: MPI_Comm_size(comm, &size);
16: MPI_Comm_rank(comm, &rank);
17: if (rank < size - 1) {
18: MatMPISBAIJSetPreallocation(A, 1, 1, NULL, 1, NULL);
19: } else {
20: MatMPISBAIJSetPreallocation(A, 1, 2, NULL, 0, NULL);
21: }
22: MatGetOwnershipRange(A, &first, &last);
23: MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
24: last--;
25: for (i = first; i <= last; i++) {
26: MatSetValue(A, i, i, 2., INSERT_VALUES);
27: if (i != n - 1) MatSetValue(A, i, n - 1, -1., INSERT_VALUES);
28: }
29: MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
30: MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
31: MatDestroy(&A);
32: return 0;
33: }
35: int main(int argc, char *argv[])
36: {
37: MPI_Comm comm;
38: PetscInt n = 6;
41: PetscInitialize(&argc, &argv, NULL, help);
42: comm = PETSC_COMM_WORLD;
43: PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL);
44: Assemble(comm, n, MATMPISBAIJ);
45: PetscFinalize();
46: return 0;
47: }
49: /*TEST
51: test:
52: nsize: 4
53: args: -n 1000 -mat_view ascii::ascii_info_detail
54: requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
56: TEST*/