Actual source code: ex266.c

  1: static char help[] = "Test MatDuplicate() with new nonzeros on the duplicate\n\n";

  3: #include <petscmat.h>
  4: int main(int argc, char **args)
  5: {
  6:   Mat            A, B, C;
  7:   const PetscInt M = 18, N = 18;
  8:   PetscBool      equal;
  9:   PetscScalar   *vals;
 10:   PetscMPIInt    rank;

 12:   // clang-format off
 13:   // i0/j0[] has a dense diagonal
 14:   PetscInt i0[] = {7, 7, 8, 8, 9, 16, 17,  9, 10, 1, 1, -2, 2, 3, 3, 14, 4, 5, 10, 13,  9,  9, 10, 1, 0, 0, 5,  5,  6, 6, 13, 13, 14, -14, 4, 4, 5, 11, 11, 12, 15, 15, 16};
 15:   PetscInt j0[] = {7, 6, 8, 4, 9, 16, 17, 16, 10, 2, 1,  3, 2, 4, 3, 14, 4, 5, 15, 13, 10, 16, 11, 2, 0, 1, 5, -11, 0, 6, 15, 17, 11,  13, 4, 8, 2, 11, 17, 12,  3, 15,  9};

 17:   // i0/j0[] miss some diagonals
 18:   PetscInt i1[] = {8, 5, 15, 16, 6, 13, 4, 17, 8,  9, 9,  10, -6, 12, 7, 3, -4, 1, 1, 2, 5,  5, 6, 14, 17, 8,  9,  9, 10, 4,  5, 10, 11, 1, 2};
 19:   PetscInt j1[] = {2, 3, 16,  9, 5, 17, 1, 13, 4, 10, 16, 11, -5, 12, 1, 7, -1, 2, 7, 3, 6, 11, 0, 11, 13, 4, 10, 16, 11, 8, -2, 15, 12, 7, 3};

 21:   PetscInt i2[] = {3, 4, 1, 10, 0, 1, 1, 2, 1, 1, 2, 2, 3, 3, 4, 4, 1, 2, 5,  5, 6, 4, 17, 0, 1, 1, 8, 5,  5, 6, 4, 7, 8, 5};
 22:   PetscInt j2[] = {7, 1, 2, 11, 5, 2, 7, 3, 2, 7, 3, 8, 4, 9, 3, 5, 7, 3, 6, 11, 0, 1, 13, 5, 2, 7, 4, 6, 11, 0, 1, 3, 4, 2};
 23:   // clang-format on

 25:   typedef struct {
 26:     PetscInt *i, *j, n;
 27:   } coo_data;

 29:   coo_data coos[3] = {
 30:     {i0, j0, PETSC_STATIC_ARRAY_LENGTH(i0)},
 31:     {i1, j1, PETSC_STATIC_ARRAY_LENGTH(i1)},
 32:     {i2, j2, PETSC_STATIC_ARRAY_LENGTH(i2)}
 33:   };
 34:   coo_data mycoo;

 36:   PetscFunctionBeginUser;
 37:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 38:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));

 40:   mycoo = coos[rank / 3];

 42:   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
 43:   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, M, N));
 44:   PetscCall(MatSetFromOptions(A));

 46:   // Assemble matrix A with the full arrays
 47:   PetscCall(PetscMalloc1(mycoo.n, &vals));
 48:   for (PetscInt k = 0; k < mycoo.n; k++) {
 49:     vals[k] = mycoo.j[k];
 50:     PetscCall(MatSetValue(A, mycoo.i[k], mycoo.j[k], vals[k], ADD_VALUES));
 51:   }
 52:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 53:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));

 55:   // Assemble matrix B with the 1st half of the arrays
 56:   PetscCall(MatCreate(PETSC_COMM_WORLD, &B));
 57:   PetscCall(MatSetSizes(B, PETSC_DECIDE, PETSC_DECIDE, M, N));
 58:   PetscCall(MatSetFromOptions(B));
 59:   for (PetscInt k = 0; k < mycoo.n / 2; k++) PetscCall(MatSetValue(B, mycoo.i[k], mycoo.j[k], vals[k], ADD_VALUES));
 60:   PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
 61:   PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));

 63:   // Duplicate B to C and continue adding nozeros to C with the 2nd half
 64:   PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &C));
 65:   for (PetscInt k = mycoo.n / 2; k < mycoo.n; k++) PetscCall(MatSetValue(C, mycoo.i[k], mycoo.j[k], vals[k], ADD_VALUES));
 66:   PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
 67:   PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));

 69:   // Test if A == C
 70:   PetscCall(MatMultEqual(A, C, 10, &equal));
 71:   if (!equal) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "MatDuplicate() on regular matrices failed\n"));

 73:   PetscCall(PetscFree(vals));
 74:   PetscCall(MatDestroy(&A));
 75:   PetscCall(MatDestroy(&B));
 76:   PetscCall(MatDestroy(&C));
 77:   PetscCall(PetscFinalize());
 78:   return 0;
 79: }

 81: /*TEST

 83:   test:
 84:     nsize: 3
 85:     output_file: output/empty.out

 87: TEST*/