Actual source code: ex274.c

  1: static char help[] = "Test MatGetOrdering() on MPIAIJ and its derived matrices. The returned permutations should be on the same MPI communicator as the matrix.\n\n";

  3: // Contributed by Steven Dargaville in issue #1897

  5: #include <petscmat.h>

  7: int main(int argc, char **argv)
  8: {
  9:   Mat      A, A_perm;
 10:   IS       rperm, cperm;
 11:   PetscInt i, n = 20, rstart, rend;

 13:   PetscFunctionBeginUser;
 14:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));

 16:   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
 17:   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, n, n));
 18:   PetscCall(MatSetFromOptions(A));
 19:   PetscCall(MatSetUp(A));
 20:   PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
 21:   for (i = rstart; i < rend; i++) {
 22:     PetscScalar two = 2.0, mone = -1.0;
 23:     PetscInt    ip1 = i + 1, im1 = i - 1;
 24:     PetscCall(MatSetValues(A, 1, &i, 1, &i, &two, INSERT_VALUES));

 26:     if (i > 0) PetscCall(MatSetValues(A, 1, &i, 1, &im1, &mone, INSERT_VALUES));
 27:     if (i < n - 1) PetscCall(MatSetValues(A, 1, &i, 1, &ip1, &mone, INSERT_VALUES));
 28:   }
 29:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 30:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));

 32:   PetscCall(MatGetOrdering(A, MATORDERINGNATURAL, &rperm, &cperm));
 33:   PetscCall(MatPermute(A, rperm, cperm, &A_perm));

 35:   PetscCall(MatDestroy(&A_perm));
 36:   PetscCall(ISDestroy(&rperm));
 37:   PetscCall(ISDestroy(&cperm));
 38:   PetscCall(MatDestroy(&A));
 39:   PetscCall(PetscFinalize());
 40:   return 0;
 41: }

 43: /*TEST
 44:   testset:
 45:     nsize: 2
 46:     output_file: output/empty.out

 48:     test:
 49:       suffix: host
 50:       args: -mat_type aij

 52:     test:
 53:       suffix: cuda
 54:       requires: cuda
 55:       args: -mat_type aijcusparse

 57:     test:
 58:       suffix: hip
 59:       requires: hip
 60:       args: -mat_type aijhipsparse

 62:     test:
 63:       suffix: kok
 64:       requires: kokkos_kernels
 65:       args: -mat_type aijkokkos

 67: TEST*/