Actual source code: ex43.c
2: static char help[] = "Saves a dense matrix in a dense format (binary).\n\n";
4: #include petscmat.h
8: int main(int argc,char **args)
9: {
10: Mat C;
11: PetscScalar v;
12: PetscInt i,j,m = 4,n = 4;
14: PetscMPIInt rank,size;
15: PetscViewer viewer;
17: PetscInitialize(&argc,&args,(char *)0,help);
18: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
19: MPI_Comm_size(PETSC_COMM_WORLD,&size);
20: PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
21: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
23: /* PART 1: Generate matrix, then write it in binary format */
25: /* Generate matrix */
26: MatCreateSeqDense(PETSC_COMM_WORLD,m,n,PETSC_NULL,&C);
27: for (i=0; i<m; i++) {
28: for (j=0; j<n; j++) {
29: v = i*m+j;
30: MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES);
31: }
32: }
33: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
34: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
35: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",FILE_MODE_WRITE,&viewer);
36: PetscViewerSetFormat(viewer,PETSC_VIEWER_BINARY_NATIVE);
37: MatView(C,viewer);
38: PetscViewerDestroy(viewer);
39: MatDestroy(C);
40: PetscFinalize();
41: return 0;
42: }