Actual source code: dspai.c
2: #include <petscmat.h>
3: #include <petsc/private/petscimpl.h>
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.
11: */
12: PetscErrorCode MatDumpSPAI(Mat A, FILE *file)
13: {
14: const PetscScalar *vals;
15: int i, j, n, size, nz;
16: const int *cols;
17: MPI_Comm comm;
19: PetscObjectGetComm((PetscObject)A, &comm);
20: MPI_Comm_size(comm, &size);
22: MatGetSize(A, &n, &n);
23: /* print the matrix */
24: fprintf(file, "%d\n", n);
25: for (i = 0; i < n; i++) {
26: MatGetRow(A, i, &nz, &cols, &vals);
27: for (j = 0; j < nz; j++) fprintf(file, "%d %d %16.14e\n", i + 1, cols[j] + 1, vals[j]);
28: MatRestoreRow(A, i, &nz, &cols, &vals);
29: }
30: return 0;
31: }
33: PetscErrorCode VecDumpSPAI(Vec b, FILE *file)
34: {
35: int n, i;
36: PetscScalar *array;
38: VecGetSize(b, &n);
39: VecGetArray(b, &array);
40: fprintf(file, "%d\n", n);
41: for (i = 0; i < n; i++) fprintf(file, "%d %16.14e\n", i + 1, array[i]);
42: return 0;
43: }