Actual source code: ex32.c

petsc-3.6.1 2015-08-06
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

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

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

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

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

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

 40:   MatCreateSeqAIJ(PETSC_COMM_WORLD,n,n,20,0,&A);
 41:   VecCreate(PETSC_COMM_WORLD,&b);
 42:   VecSetSizes(b,PETSC_DECIDE,n);
 43:   VecSetFromOptions(b);

 45:   PetscMalloc1(n+1,&col);
 46:   for (i=0; i<n+1; i++) fscanf(file,"     I=%d%d\n",&j,&col[i]);
 47:   fscanf(file,"  EOD JA\n");

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

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

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

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

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

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

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

 99:   PetscFinalize();
100: #endif
101:   return 0;
102: }