Actual source code: ex43.c


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

  4: #include <petscmat.h>

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

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

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

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

 41: /*TEST

 43:    test:

 45: TEST*/