Actual source code: ex32.c

petsc-3.4.5 2014-06-29
  2: static char help[] = "Reads in a matrix and vector in ASCII slap format. Writes\n\
  3: them using the PETSc sparse format. Input parameters are:\n\
  4:   -fin <filename> : input file\n\
  5:   -fout <filename> : output file\n\n";

  7: #include <petscmat.h>

 11: int main(int argc,char **args)
 12: {
 13:   Mat            A;
 14:   Vec            b;
 15:   char           filein[PETSC_MAX_PATH_LEN],fileout[PETSC_MAX_PATH_LEN];
 16:   PetscInt       i,j,m,n,nnz,start,end,*col,*row,*brow,length;
 18:   PetscMPIInt    size,rank;
 19:   PetscScalar    *val,*bval;
 20:   FILE           *file;
 21:   PetscViewer    view;
 22:   PetscBool      opt;

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

 26:   /* Read in matrix and RHS */
 27:   PetscOptionsGetString(NULL,"-fin",filein,PETSC_MAX_PATH_LEN,&opt);
 28:   if (!opt) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG, "No filename was specified for this test");
 29:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 30:   MPI_Comm_size(PETSC_COMM_WORLD,&size);

 32:   PetscFOpen(PETSC_COMM_SELF,filein,"r",&file);

 34:   fscanf(file,"  NUNKNS =%d  NCOEFF =%d\n",&n,&nnz);
 35:   fscanf(file,"  JA POINTER IN SLAPSV\n");

 37:   MatCreateSeqAIJ(PETSC_COMM_WORLD,n,n,20,0,&A);
 38:   VecCreate(PETSC_COMM_WORLD,&b);
 39:   VecSetSizes(b,PETSC_DECIDE,n);
 40:   VecSetFromOptions(b);

 42:   PetscMalloc((n+1)*sizeof(PetscInt),&col);
 43:   for (i=0; i<n+1; i++) fscanf(file,"     I=%d%d\n",&j,&col[i]);
 44:   fscanf(file,"  EOD JA\n");

 46:   PetscMalloc(nnz*sizeof(PetscScalar),&val);
 47:   PetscMalloc(nnz*sizeof(PetscInt),&row);
 48:   fscanf(file,"  COEFFICIENT MATRIX IN SLAPSV: I, IA, A\n");
 49:   for (i=0; i<nnz; i++) {
 50:     fscanf(file,"    %d%d%le\n",&j,&row[i],(double*)&val[i]);
 51:     row[i]--;
 52:   }
 53:   fscanf(file,"  EOD IA\n");

 55:   PetscMalloc(n*sizeof(PetscScalar),&bval);
 56:   PetscMalloc(n*sizeof(PetscInt),&brow);
 57:   fscanf(file,"  RESIDUAL IN SLAPSV ;IRHS=%d\n",&j);
 58:   for (i=0; i<n; i++) {
 59:     fscanf(file,"      %d%le%d\n",&j,(double*)(bval+i),&j);
 60:     brow[i] = i;
 61:   }
 62:   fscanf(file,"  EOD RESIDUAL");
 63:   fclose(file);

 65:   m     = n/size+1;
 66:   start = rank*m;
 67:   end   = (rank+1)*m; if (end > n) end = n;
 68:   for (j=start; j<end; j++) {
 69:     length = col[j+1]-col[j];
 70:     MatSetValues(A,length,&row[col[j]-1],1,&j,&val[col[j]-1],INSERT_VALUES);
 71:   }
 72:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 73:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 75:   VecGetOwnershipRange(b,&start,&end);
 76:   VecSetValues(b,end-start,brow+start,bval+start,INSERT_VALUES);
 77:   VecAssemblyBegin(b);
 78:   VecAssemblyEnd(b);

 80:   PetscFree(col);
 81:   PetscFree(val);
 82:   PetscFree(row);
 83:   PetscFree(bval);
 84:   PetscFree(brow);

 86:   PetscPrintf(PETSC_COMM_SELF,"Reading matrix completes.\n");
 87:   PetscOptionsGetString(NULL,"-fout",fileout,PETSC_MAX_PATH_LEN,NULL);
 88:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,fileout,FILE_MODE_WRITE,&view);
 89:   MatView(A,view);
 90:   VecView(b,view);
 91:   PetscViewerDestroy(&view);

 93:   VecDestroy(&b);
 94:   MatDestroy(&A);

 96:   PetscFinalize();
 97:   return 0;
 98: }