Actual source code: ex43.c

  1: static char help[] = "Saves a dense matrix in a dense format (binary).\n\n";

  3: #include <petscmat.h>

  5: int main(int argc, char **args)
  6: {
  7:   Mat         C;
  8:   PetscScalar v;
  9:   PetscInt    i, j, m = 4, n = 4;
 10:   PetscMPIInt rank, size;
 11:   PetscViewer viewer;

 13:   PetscFunctionBeginUser;
 14:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 15:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 16:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 17:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
 18:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));

 20:   /* PART 1:  Generate matrix, then write it in binary format */

 22:   /* Generate matrix */
 23:   PetscCall(MatCreateSeqDense(PETSC_COMM_WORLD, m, n, NULL, &C));
 24:   for (i = 0; i < m; i++) {
 25:     for (j = 0; j < n; j++) {
 26:       v = i * m + j;
 27:       PetscCall(MatSetValues(C, 1, &i, 1, &j, &v, INSERT_VALUES));
 28:     }
 29:   }
 30:   PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
 31:   PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
 32:   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "matrix.dat", FILE_MODE_WRITE, &viewer));
 33:   PetscCall(MatView(C, viewer));
 34:   PetscCall(PetscViewerDestroy(&viewer));
 35:   PetscCall(MatDestroy(&C));
 36:   PetscCall(PetscFinalize());
 37:   return 0;
 38: }

 40: /*TEST

 42:    test:

 44: TEST*/