Actual source code: ex72.c
1: /*$Id: ex72.c,v 1.19 2001/09/11 16:32:50 bsmith Exp $*/
3: #if !defined(PETSC_USE_COMPLEX)
5: static char help[] = "Reads in a Symmetric matrix in MatrixMarket format. Writes\n\
6: it using the PETSc sparse format. It also adds a Vector set to random values to the\n\
7: output file. Input parameters are:\n\
8: -fin <filename> : input file\n\
9: -fout <filename> : output file\n\n";
11: #include petscmat.h
15: int main(int argc,char **args)
16: {
17: Mat A;
18: Vec b;
19: char filein[128],fileout[128],buf[128];
20: int i,m,n,nnz,ierr,size,col,row;
21: PetscScalar val;
22: FILE* file;
23: PetscViewer view;
24: PetscRandom r;
26: PetscInitialize(&argc,&args,(char *)0,help);
28: MPI_Comm_size(PETSC_COMM_WORLD,&size);
29: if (size > 1) SETERRQ(1,"Uniprocessor Example only\n");
31: /* Read in matrix and RHS */
32: PetscOptionsGetString(PETSC_NULL,"-fin",filein,127,PETSC_NULL);
33: PetscFOpen(PETSC_COMM_SELF,filein,"r",&file);
35: /* Ignore the first line */
36: /* while (getc(file) != '\n') ; */
37: fgets(buf,128,file);
38: printf("%s",buf);
39: fscanf(file,"%d %d %d\n",&m,&n,&nnz);
40: printf ("m = %d, n = %d, nnz = %d\n",m,n,nnz);
42: MatCreateSeqAIJ(PETSC_COMM_WORLD,m,n,20,0,&A);
43: VecCreate(PETSC_COMM_WORLD,&b);
44: VecSetSizes(b,PETSC_DECIDE,n);
45: VecSetFromOptions(b);
46: PetscRandomCreate(PETSC_COMM_SELF,RANDOM_DEFAULT,&r);
47: VecSetRandom(r,b);
49: for (i=0; i<nnz; i++) {
50: fscanf(file,"%d %d %le\n",&row,&col,&val);
51: row = row-1; col = col-1 ;
52: MatSetValues(A,1,&row,1,&col,&val,INSERT_VALUES);
53: if (row != col) {
54: MatSetValues(A,1,&col,1,&row,&val,INSERT_VALUES);
55: }
56: }
57: fclose(file);
59: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
60: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
62: PetscPrintf(PETSC_COMM_SELF,"Reading matrix completes.\n");
63: PetscOptionsGetString(PETSC_NULL,"-fout",fileout,127,PETSC_NULL);
64: PetscViewerBinaryOpen(PETSC_COMM_WORLD,fileout,PETSC_BINARY_CREATE,&view);
65: MatView(A,view);
66: VecView(b,view);
67: PetscViewerDestroy(view);
69: VecDestroy(b);
70: MatDestroy(A);
71: PetscRandomDestroy(r);
73: PetscFinalize();
74: return 0;
75: }
76: #else
77: #include <stdio.h>
78: int main(int argc,char **args)
79: {
80: fprintf(stdout,"This example does not work for complex numbers.\n");
81: return 0;
82: }
83: #endif