xref: /petsc/src/mat/tests/ex100.c (revision 856bee69f0e0908e75ff837867b1777dfb1ced96)
1 
2 static char help[] = "Tests various routines in MatMAIJ format.\n";
3 
4 #include <petscmat.h>
5 #define IMAX 15
6 int main(int argc, char **args)
7 {
8   Mat         A, B, MA;
9   PetscViewer fd;
10   char        file[PETSC_MAX_PATH_LEN];
11   PetscInt    m, n, M, N, dof = 1;
12   PetscMPIInt rank, size;
13   PetscBool   flg;
14 
15   PetscFunctionBeginUser;
16   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
17   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
18   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
19 
20   /* Load aij matrix A */
21   PetscCall(PetscOptionsGetString(NULL, NULL, "-f", file, sizeof(file), &flg));
22   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must indicate binary file with the -f option");
23   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file, FILE_MODE_READ, &fd));
24   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
25   PetscCall(MatLoad(A, fd));
26   PetscCall(PetscViewerDestroy(&fd));
27 
28   /* Get dof, then create maij matrix MA */
29   PetscCall(PetscOptionsGetInt(NULL, NULL, "-dof", &dof, NULL));
30   PetscCall(MatCreateMAIJ(A, dof, &MA));
31   PetscCall(MatGetLocalSize(MA, &m, &n));
32   PetscCall(MatGetSize(MA, &M, &N));
33 
34   if (size == 1) {
35     PetscCall(MatConvert(MA, MATSEQAIJ, MAT_INITIAL_MATRIX, &B));
36   } else {
37     PetscCall(MatConvert(MA, MATMPIAIJ, MAT_INITIAL_MATRIX, &B));
38   }
39 
40   /* Test MatMult() */
41   PetscCall(MatMultEqual(MA, B, 10, &flg));
42   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_CONV_FAILED, "Error: MatMul() for MAIJ matrix");
43   /* Test MatMultAdd() */
44   PetscCall(MatMultAddEqual(MA, B, 10, &flg));
45   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_CONV_FAILED, "Error: MatMulAdd() for MAIJ matrix");
46 
47   /* Test MatMultTranspose() */
48   PetscCall(MatMultTransposeEqual(MA, B, 10, &flg));
49   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_CONV_FAILED, "Error: MatMulAdd() for MAIJ matrix");
50 
51   /* Test MatMultTransposeAdd() */
52   PetscCall(MatMultTransposeAddEqual(MA, B, 10, &flg));
53   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_CONV_FAILED, "Error: MatMulTransposeAdd() for MAIJ matrix");
54 
55   PetscCall(MatDestroy(&MA));
56   PetscCall(MatDestroy(&A));
57   PetscCall(MatDestroy(&B));
58   PetscCall(PetscFinalize());
59   return 0;
60 }
61 
62 /*TEST
63 
64    build:
65       requires: !complex
66 
67    test:
68       nsize: {{1 3}}
69       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
70       args: -f ${DATAFILESPATH}/matrices/arco1 -dof {{1 2 3 4 5 6 8 9 16}} -viewer_binary_skip_info
71 
72 TEST*/
73