Actual source code: ex305.c

  1: #include <petscmat.h>

  3: int main(int argc, char **args)
  4: {
  5:   PetscViewer     viewer_read, viewer_write, viewer_read2;
  6:   Mat             adj_mat, adj_mat2, adj_aij_mat, transpose;
  7:   MatPartitioning part;
  8:   PetscBool       mats_equal, flg;
  9:   char            adj_mat_file[PETSC_MAX_PATH_LEN];
 10:   PetscMPIInt     size;
 11:   IS              is;

 13:   PetscFunctionBeginUser;
 14:   PetscCall(PetscInitialize(&argc, &args, NULL, NULL));
 15:   PetscCall(PetscOptionsGetString(NULL, NULL, "-f", adj_mat_file, sizeof(adj_mat_file), &flg));
 16:   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must indicate binary file with the -f option");
 17:   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, adj_mat_file, FILE_MODE_READ, &viewer_read));
 18:   PetscCall(MatCreate(PETSC_COMM_WORLD, &adj_aij_mat));
 19:   // Binary file contains an AIJ matrix
 20:   PetscCall(MatLoad(adj_aij_mat, viewer_read));
 21:   PetscCall(MatConvert(adj_aij_mat, MATMPIADJ, MAT_INITIAL_MATRIX, &adj_mat));
 22:   // Now write out again as AIJ
 23:   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "adj_mat2", FILE_MODE_WRITE, &viewer_write));
 24:   PetscCall(MatView(adj_mat, viewer_write));
 25:   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "adj_mat2", FILE_MODE_READ, &viewer_read2));
 26:   PetscCall(MatLoad(adj_aij_mat, viewer_read2));
 27:   PetscCall(MatConvert(adj_aij_mat, MATMPIADJ, MAT_INITIAL_MATRIX, &adj_mat2));
 28:   PetscCall(MatEqual(adj_mat, adj_mat2, &mats_equal));
 29:   PetscCheck(mats_equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Adjacency matrix not reproduced");
 30:   PetscCall(MatConvert(adj_mat2, MATAIJ, MAT_REUSE_MATRIX, &adj_aij_mat));
 31:   PetscCall(MatTranspose(adj_aij_mat, MAT_INITIAL_MATRIX, &transpose));
 32:   PetscCall(MatEqual(adj_aij_mat, transpose, &mats_equal));
 33:   PetscCheck(mats_equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Adjacency matrix not symmetric");

 35:   // Now let's make sure we can construct a partition from this
 36:   PetscCall(MatPartitioningCreate(PETSC_COMM_WORLD, &part));
 37:   PetscCall(MatPartitioningSetUseEdgeWeights(part, PETSC_TRUE));
 38:   PetscCall(MatPartitioningSetAdjacency(part, adj_mat2));
 39:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 40:   PetscCall(MatPartitioningSetNParts(part, size));
 41:   PetscCall(MatPartitioningSetFromOptions(part));
 42:   PetscCall(MatPartitioningApply(part, &is));
 43:   PetscCall(MatDestroy(&adj_mat));
 44:   PetscCall(MatDestroy(&adj_mat2));
 45:   PetscCall(MatDestroy(&adj_aij_mat));
 46:   PetscCall(MatDestroy(&transpose));
 47:   PetscCall(PetscViewerDestroy(&viewer_read));
 48:   PetscCall(PetscViewerDestroy(&viewer_read2));
 49:   PetscCall(PetscViewerDestroy(&viewer_write));
 50:   PetscCall(ISDestroy(&is));
 51: }

 53: /*TEST

 55:    testset:
 56:       requires: defined(PETSC_USE_64BIT_INDICES) !complex datafilespath
 57:       nsize: 2
 58:       output_file: output/empty.out
 59:       args: -f ${DATAFILESPATH}/matrices/adj_mat
 60:       test:
 61:          requires: parmetis
 62:          suffix: view_adj_parmetis
 63:          args: -mat_partitioning_type parmetis
 64:       test:
 65:          suffix: view_adj_ptscotch
 66:          requires: ptscotch
 67:          args: -mat_partitioning_type ptscotch
 68:       test:
 69:          suffix: view_adj_hierarch
 70:          requires: hierarch
 71:          args: -mat_partitioning_type hierarch

 73: TEST*/