Actual source code: ex72.c

  2: #if !defined(PETSC_USE_COMPLEX)

  4: static char help[] = "Reads in a Symmetric matrix in MatrixMarket format. Writes\n\
  5: it using the PETSc sparse format. It also adds a Vector set to random values to the\n\
  6: output file. Input parameters are:\n\
  7:   -fin <filename> : input file\n\
  8:   -fout <filename> : output file\n\n";

 10:  #include petscmat.h

 14: int main(int argc,char **args)
 15: {
 16:   Mat            A;
 17:   Vec            b;
 18:   char           filein[PETSC_MAX_PATH_LEN],fileout[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
 19:   PetscInt       i,m,n,nnz,col,row;
 21:   PetscMPIInt    size;
 22:   PetscScalar    val;
 23:   FILE*          file;
 24:   PetscViewer    view;
 25:   PetscRandom    r;

 27:   PetscInitialize(&argc,&args,(char *)0,help);

 29:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 30:   if (size > 1) SETERRQ(1,"Uniprocessor Example only\n");

 32:   /* Read in matrix and RHS */
 33:   PetscOptionsGetString(PETSC_NULL,"-fin",filein,PETSC_MAX_PATH_LEN-1,PETSC_NULL);
 34:   PetscFOpen(PETSC_COMM_SELF,filein,"r",&file);

 36:   /* Ignore the first line */
 37:   /* while (getc(file) != '\n'); */
 38:   fgets(buf,PETSC_MAX_PATH_LEN-1,file);
 39:   printf("%s",buf);
 40:   fscanf(file,"%d %d %d\n",&m,&n,&nnz);
 41:   printf ("m = %d, n = %d, nnz = %d\n",m,n,nnz);

 43:   MatCreateSeqAIJ(PETSC_COMM_WORLD,m,n,20,0,&A);
 44:   VecCreate(PETSC_COMM_WORLD,&b);
 45:   VecSetSizes(b,PETSC_DECIDE,n);
 46:   VecSetFromOptions(b);
 47:   PetscRandomCreate(PETSC_COMM_SELF,&r);
 48:   PetscRandomSetFromOptions(r);
 49:   VecSetRandom(b,r);

 51:   for (i=0; i<nnz; i++) {
 52:     fscanf(file,"%d %d %le\n",&row,&col,(double*)&val);
 53:     row = row-1; col = col-1 ;
 54:     MatSetValues(A,1,&row,1,&col,&val,INSERT_VALUES);
 55:     if (row != col) {
 56:       MatSetValues(A,1,&col,1,&row,&val,INSERT_VALUES);
 57:     }
 58:   }
 59:   fclose(file);

 61:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 62:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 64:   PetscPrintf(PETSC_COMM_SELF,"Reading matrix completes.\n");
 65:   PetscOptionsGetString(PETSC_NULL,"-fout",fileout,PETSC_MAX_PATH_LEN-1,PETSC_NULL);
 66:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,fileout,FILE_MODE_WRITE,&view);
 67:   MatView(A,view);
 68:   VecView(b,view);
 69:   PetscViewerDestroy(view);

 71:   VecDestroy(b);
 72:   MatDestroy(A);
 73:   PetscRandomDestroy(r);

 75:   PetscFinalize();
 76:   return 0;
 77: }
 78: #else
 79: #include <stdio.h>
 80: int main(int argc,char **args)
 81: {
 82:   fprintf(stdout,"This example does not work for complex numbers.\n");
 83:   return 0;
 84: }
 85: #endif