Actual source code: ex36.c
1: static char help[] = "Tests assembly of a matrix from another matrix's hash table.\n\n";
3: #include <petscmat.h>
5: PetscErrorCode SetValues(Mat A)
6: {
7: PetscInt m, n, i, j;
8: PetscScalar v;
10: PetscFunctionBeginUser;
11: PetscCall(MatGetSize(A, &m, &n));
12: for (i = 0; i < m; i++) {
13: for (j = 0; j < n; j++) {
14: v = 10.0 * i + j + 1;
15: PetscCall(MatSetValues(A, 1, &i, 1, &j, &v, ADD_VALUES));
16: }
17: }
18: PetscFunctionReturn(PETSC_SUCCESS);
19: }
21: PetscErrorCode CreateAndViewB(Mat A)
22: {
23: Mat B;
25: PetscFunctionBeginUser;
26: /* Create B */
27: PetscCall(MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, &B));
28: PetscCall(MatCopyHashToXAIJ(A, B));
29: PetscCall(MatView(B, PETSC_VIEWER_STDOUT_WORLD));
30: PetscCall(MatDestroy(&B));
31: PetscFunctionReturn(PETSC_SUCCESS);
32: }
34: PetscErrorCode AssembleAndViewA(Mat A)
35: {
36: PetscFunctionBeginUser;
37: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
38: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
39: PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));
40: PetscFunctionReturn(PETSC_SUCCESS);
41: }
43: int main(int argc, char **argv)
44: {
45: Mat A;
47: PetscFunctionBeginUser;
48: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
50: /* ------- Set values in A --------- */
51: PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
52: PetscCall(MatSetSizes(A, 1, 1, PETSC_DETERMINE, PETSC_DETERMINE));
53: PetscCall(MatSetFromOptions(A));
54: PetscCall(MatSetUp(A));
56: PetscCall(SetValues(A));
57: PetscCall(CreateAndViewB(A));
58: PetscCall(AssembleAndViewA(A));
60: PetscCall(MatResetHash(A));
62: PetscCall(SetValues(A));
63: PetscCall(CreateAndViewB(A));
64: PetscCall(AssembleAndViewA(A));
66: PetscCall(MatDestroy(&A));
67: PetscCall(PetscFinalize());
68: return 0;
69: }
71: /*TEST
73: test:
74: suffix: seq
75: args: -mat_type seqaij
76: filter: grep -v "Mat Object"
78: test:
79: suffix: mpi
80: args: -mat_type mpiaij
81: nsize: 4
82: filter: grep -v "Mat Object"
84: TEST*/