Actual source code: ex32.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

  2:  #include <petscmat.h>

  4: #if !defined(PETSC_USE_64BIT_INDICES)
  5: static char help[] = "Reads in a matrix and vector in ASCII slap format. Writes\n\
  6: them using the PETSc sparse format. Input parameters are:\n\
  7:   -fin <filename> : input file\n\
  8:   -fout <filename> : output file\n\n";
  9: #endif

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

 25:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 26:   /* Read in matrix and RHS */
 27:   PetscOptionsGetString(NULL,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:   if (fscanf(file,"  NUNKNS =%d  NCOEFF =%d\n",&n,&nnz) != 2) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG, "Incorrectly formatted file");
 35:   if (fscanf(file,"  JA POINTER IN SLAPSV\n")) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG, "Incorrectly formatted file");

 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:   PetscMalloc1(n+1,&col);
 43:   for (i=0; i<n+1; i++) {
 44:     if (fscanf(file,"     I=%d%d\n",&j,&col[i]) != 2)  SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG, "Incorrectly formatted file");
 45:   }
 46:   if (fscanf(file,"  EOD JA\n")) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG, "Incorrectly formatted file");

 48:   PetscMalloc1(nnz,&val);
 49:   PetscMalloc1(nnz,&row);
 50:   if (fscanf(file,"  COEFFICIENT MATRIX IN SLAPSV: I, IA, A\n")) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG, "Incorrectly formatted file");
 51:   for (i=0; i<nnz; i++) {
 52:     if (fscanf(file,"    %d%d%le\n",&j,&row[i],(double*)&val[i]) != 3) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG, "Incorrectly formatted file");
 53:     row[i]--;
 54:   }
 55:   if (fscanf(file,"  EOD IA\n")) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG, "Incorrectly formatted file");

 57:   PetscMalloc1(n,&bval);
 58:   PetscMalloc1(n,&brow);
 59:   if (fscanf(file,"  RESIDUAL IN SLAPSV ;IRHS=%d\n",&j) != 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG, "Incorrectly formatted file");
 60:   for (i=0; i<n; i++) {
 61:     if (fscanf(file,"      %d%le%d\n",&j,(double*)(bval+i),&j) != 3) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG, "Incorrectly formatted file");
 62:     brow[i] = i;
 63:   }
 64:   if (fscanf(file,"  EOD RESIDUAL")) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG, "Incorrectly formatted file");
 65:   fclose(file);

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

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

 82:   PetscFree(col);
 83:   PetscFree(val);
 84:   PetscFree(row);
 85:   PetscFree(bval);
 86:   PetscFree(brow);

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

 95:   VecDestroy(&b);
 96:   MatDestroy(&A);

 98:   PetscFinalize();
 99:   return ierr;
100: #else
101:   return 0;
102: #endif
103: }