xref: /petsc/src/mat/tests/ex191.c (revision 8fb5bd83c3955fefcf33a54e3bb66920a9fa884b) !
1 static char help[] = "Tests MatLoad() for dense matrix with uneven dimensions set in program\n\n";
2 
3 #include <petscmat.h>
4 
5 int main(int argc,char **args)
6 {
7   Mat            A;
8   PetscViewer    fd;
9   PetscMPIInt    rank;
10   PetscScalar    *Av;
11   PetscInt       i;
12 
13   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
14   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
15 
16   PetscCall(MatCreateDense(PETSC_COMM_WORLD,6,6,12,12,NULL,&A));
17   PetscCall(MatDenseGetArray(A,&Av));
18   for (i=0; i<6*12; i++) Av[i] = (PetscScalar) i;
19   PetscCall(MatDenseRestoreArray(A,&Av));
20 
21   /* Load matrices */
22   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"ex191matrix",FILE_MODE_WRITE,&fd));
23   PetscCall(PetscViewerPushFormat(fd,PETSC_VIEWER_NATIVE));
24   PetscCall(MatView(A,fd));
25   PetscCall(MatDestroy(&A));
26   PetscCall(PetscViewerPopFormat(fd));
27   PetscCall(PetscViewerDestroy(&fd));
28 
29   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
30   PetscCall(MatSetType(A,MATDENSE));
31   if (rank == 0) {
32     PetscCall(MatSetSizes(A, 4, PETSC_DETERMINE, PETSC_DETERMINE,PETSC_DETERMINE));
33   } else {
34     PetscCall(MatSetSizes(A, 8, PETSC_DETERMINE, PETSC_DETERMINE,PETSC_DETERMINE));
35   }
36   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"ex191matrix",FILE_MODE_READ,&fd));
37   PetscCall(MatLoad(A,fd));
38   PetscCall(PetscViewerDestroy(&fd));
39   PetscCall(MatView(A,PETSC_VIEWER_STDOUT_WORLD));
40   PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
41   PetscCall(MatView(A,PETSC_VIEWER_STDOUT_WORLD));
42   PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
43   PetscCall(MatDestroy(&A));
44   PetscCall(PetscFinalize());
45   return 0;
46 }
47 
48 /*TEST
49 
50    test:
51       nsize: 2
52       filter: grep -v alloced
53 
54 TEST*/
55