Actual source code: ex72.c
petsc-3.10.5 2019-03-28
2: #include <petscmat.h>
4: static char help[] = "Read in a Symmetric matrix in MatrixMarket format (only the lower triangle). \n\
5: Assemble it to a PETSc sparse SBAIJ (upper triangle) matrix. \n\
6: Write it in a AIJ matrix (entire matrix) to a file. \n\
7: Input parameters are: \n\
8: -fin <filename> : input file \n\
9: -fout <filename> : output file \n\n";
11: int main(int argc,char **args)
12: {
13: Mat A;
14: char filein[PETSC_MAX_PATH_LEN],fileout[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
15: PetscInt i,m,n,nnz;
17: PetscMPIInt size;
18: PetscScalar *val,zero=0.0;
19: FILE *file;
20: PetscViewer view;
21: int *row,*col,*rownz;
22: PetscBool flg;
24: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
25: MPI_Comm_size(PETSC_COMM_WORLD,&size);
26: if (size > 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Uniprocessor Example only\n");
28: /* Read in matrix and RHS */
29: PetscOptionsGetString(NULL,NULL,"-fin",filein,PETSC_MAX_PATH_LEN,&flg);
30: if (!flg) SETERRQ(PETSC_COMM_SELF,1,"Must indicate input file with -fin option");
31: PetscFOpen(PETSC_COMM_SELF,filein,"r",&file);
33: /* process header with comments */
34: do {
35: char *str = fgets(buf,PETSC_MAX_PATH_LEN-1,file);
36: if (!str) SETERRQ(PETSC_COMM_SELF,1,"Incorrect format in file");
37: }while (buf[0] == '%');
39: /* The first non-comment line has the matrix dimensions */
40: sscanf(buf,"%d %d %d\n",&m,&n,&nnz);
41: PetscPrintf (PETSC_COMM_SELF,"m = %d, n = %d, nnz = %d\n",m,n,nnz);
43: /* reseve memory for matrices */
44: PetscMalloc4(nnz,&row,nnz,&col,nnz,&val,m,&rownz);
45: for (i=0; i<m; i++) rownz[i] = 1; /* add 0.0 to diagonal entries */
47: for (i=0; i<nnz; i++) {
48: fscanf(file,"%d %d %le\n",&row[i],&col[i],(double*)&val[i]);
49: if (ierr == EOF) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"i=%d, reach EOF\n",i);
50: row[i]--; col[i]--; /* adjust from 1-based to 0-based */
51: rownz[col[i]]++;
52: }
53: fclose(file);
54: PetscPrintf(PETSC_COMM_SELF,"Read file completes.\n");
56: /* Creat and asseble SBAIJ matrix */
57: MatCreate(PETSC_COMM_SELF,&A);
58: MatSetType(A,MATSBAIJ);
59: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m,n);
60: MatSetFromOptions(A);
61: MatSeqSBAIJSetPreallocation(A,1,0,rownz);
63: /* Add zero to diagonals, in case the matrix missing diagonals */
64: for (i=0; i<m; i++){
65: MatSetValues(A,1,&i,1,&i,&zero,INSERT_VALUES);
66: }
67: for (i=0; i<nnz; i++) {
68: MatSetValues(A,1,&col[i],1,&row[i],&val[i],INSERT_VALUES);
69: }
70: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
71: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
72: PetscPrintf(PETSC_COMM_SELF,"Assemble SBAIJ matrix completes.\n");
74: /* Write the entire matrix in AIJ format to a file */
75: PetscOptionsGetString(NULL,NULL,"-fout",fileout,PETSC_MAX_PATH_LEN,&flg);
76: if (flg) {
77: PetscPrintf(PETSC_COMM_SELF,"Write the entire matrix in AIJ format to file %s\n",fileout);
78: PetscViewerBinaryOpen(PETSC_COMM_WORLD,fileout,FILE_MODE_WRITE,&view);
79: MatView(A,view);
80: PetscViewerDestroy(&view);
81: }
83: PetscFree4(row,col,val,rownz);
84: MatDestroy(&A);
85: PetscFinalize();
86: return ierr;
87: }
91: /*TEST
93: build:
94: requires: !complex !define(PETSC_USE_64BIT_INDICES)
96: test:
97: args: -fin ${wPETSC_DIR}/share/petsc/datafiles/matrices/amesos2_test_mat0.mtx -fout outputfile
98: requires: double
100: TEST*/