1 #define PETSCKSP_DLL 2 3 #include "petscmat.h" 4 5 /* 6 MatDumpSPAI - Dumps a PETSc matrix to a file in an ASCII format 7 suitable for the SPAI code of Stephen Barnard to solve. This routine 8 is simply here to allow testing of matrices directly with the SPAI 9 code, rather then through the PETSc interface. 10 11 */ 12 PetscErrorCode PETSCKSP_DLLEXPORT MatDumpSPAI(Mat A,FILE *file) 13 { 14 const PetscScalar *vals; 15 PetscErrorCode ierr; 16 int i,j,n,size,nz; 17 const int *cols; 18 MPI_Comm comm; 19 20 PetscObjectGetComm((PetscObject)A,&comm); 21 22 MPI_Comm_size(comm,&size); 23 if (size > 1) SETERRQ(PETSC_ERR_SUP,"Only single processor dumps"); 24 25 ierr = MatGetSize(A,&n,&n);CHKERRQ(ierr); 26 27 /* print the matrix */ 28 fprintf(file,"%d\n",n); 29 for (i=0; i<n; i++) { 30 ierr = MatGetRow(A,i,&nz,&cols,&vals);CHKERRQ(ierr); 31 for (j=0; j<nz; j++) { 32 fprintf(file,"%d %d %16.14e\n",i+1,cols[j]+1,vals[j]); 33 } 34 ierr = MatRestoreRow(A,i,&nz,&cols,&vals);CHKERRQ(ierr); 35 } 36 37 PetscFunctionReturn(0); 38 } 39 40 PetscErrorCode PETSCKSP_DLLEXPORT VecDumpSPAI(Vec b,FILE *file) 41 { 42 PetscErrorCode ierr; 43 int n,i; 44 PetscScalar *array; 45 46 ierr = VecGetSize(b,&n);CHKERRQ(ierr); 47 ierr = VecGetArray(b,&array);CHKERRQ(ierr); 48 49 fprintf(file,"%d\n",n); 50 for (i=0; i<n; i++) { 51 fprintf(file,"%d %16.14e\n",i+1,array[i]); 52 } 53 54 PetscFunctionReturn(0); 55 } 56