1 static char help[] = "Saves a rectangular sparse matrix to disk.\n\n"; 2 3 #include <petscmat.h> 4 5 int main(int argc, char **args) 6 { 7 Mat A; 8 PetscInt m = 100, n = 11, js[11], i, j, cnt; 9 PetscScalar values[11]; 10 PetscViewer view; 11 12 PetscFunctionBeginUser; 13 PetscCall(PetscInitialize(&argc, &args, NULL, help)); 14 PetscCall(MatCreateSeqAIJ(PETSC_COMM_WORLD, m, n, 20, 0, &A)); 15 16 for (i = 0; i < n; i++) values[i] = (PetscReal)i; 17 18 for (i = 0; i < m; i++) { 19 cnt = 0; 20 if (i % 2) { 21 for (j = 0; j < n; j += 2) js[cnt++] = j; 22 } 23 PetscCall(MatSetValues(A, 1, &i, cnt, js, values, INSERT_VALUES)); 24 } 25 PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 26 PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 27 28 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "rect", FILE_MODE_WRITE, &view)); 29 PetscCall(MatView(A, view)); 30 PetscCall(PetscViewerDestroy(&view)); 31 32 PetscCall(MatDestroy(&A)); 33 34 PetscCall(PetscFinalize()); 35 return 0; 36 } 37 38 /*TEST 39 40 test: 41 output_file: output/empty.out 42 43 TEST*/ 44