Actual source code: ex254.c

  1: static char help[] = "Test MatSetValuesCOO for MPIAIJ and its subclasses \n\n";

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

 14:   // clang-format off
 15:   /* Construct 18 x 18 matrices, which are big enough to have complex communication patterns but still small enough for debugging */
 16:   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};
 17:   PetscInt j0[] = {1, 6, 2, 4, 10, 15, 13, 16, 11, 2, 7,  3, 8, 4, 9, 13, 5, 2, 15, 14, 10, 16, 11, 2, 0, 1, 5, -11, 0, 7, 15, 17, 11,  13, 4, 8, 2, 12, 17, 13,  3, 16,  9};

 19:   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};
 20:   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};

 22:   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};
 23:   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};
 24:   // clang-format on

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

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

 37:   PetscFunctionBeginUser;
 38:   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
 39:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-ignore_remote", &flg, NULL));
 40:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-ncoos", &ncoos, NULL));

 42:   mycoo.n = 0;
 43:   if (ncoos > 1) {
 44:     PetscLayout map;

 46:     freecoo = PETSC_TRUE;
 47:     PetscCall(PetscLayoutCreate(PETSC_COMM_WORLD, &map));
 48:     PetscCall(PetscLayoutSetSize(map, ncoos));
 49:     PetscCall(PetscLayoutSetUp(map));
 50:     PetscCall(PetscLayoutGetLocalSize(map, &ncoos));
 51:     for (PetscInt i = 0; i < ncoos; i++) mycoo.n += coos[i % 3].n;
 52:     PetscCall(PetscMalloc2(mycoo.n, &mycoo.i, mycoo.n, &mycoo.j));
 53:     mycoo.n = 0;
 54:     for (PetscInt i = 0; i < ncoos; i++) {
 55:       PetscCall(PetscArraycpy(mycoo.i + mycoo.n, coos[i % 3].i, coos[i % 3].n));
 56:       PetscCall(PetscArraycpy(mycoo.j + mycoo.n, coos[i % 3].j, coos[i % 3].n));
 57:       mycoo.n += coos[i % 3].n;
 58:     }
 59:     PetscCall(PetscLayoutDestroy(&map));
 60:   } else if (ncoos == 1 && PetscGlobalRank < 3) mycoo = coos[PetscGlobalRank];

 62:   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
 63:   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, M, N));
 64:   PetscCall(MatSetType(A, MATAIJ));
 65:   // Do not preallocate A to also test MatHash with MAT_IGNORE_OFF_PROC_ENTRIES
 66:   // PetscCall(MatSeqAIJSetPreallocation(A, 2, NULL));
 67:   // PetscCall(MatMPIAIJSetPreallocation(A, 2, NULL, 2, NULL));
 68:   PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
 69:   PetscCall(MatSetOption(A, MAT_IGNORE_OFF_PROC_ENTRIES, flg));

 71:   PetscCall(PetscMalloc1(mycoo.n, &vals));
 72:   for (k = 0; k < mycoo.n; k++) {
 73:     vals[k] = mycoo.j[k];
 74:     PetscCall(MatSetValue(A, mycoo.i[k], mycoo.j[k], vals[k], ADD_VALUES));
 75:   }
 76:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 77:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
 78:   PetscCall(MatViewFromOptions(A, NULL, "-a_view"));

 80:   PetscCall(MatCreate(PETSC_COMM_WORLD, &B));
 81:   PetscCall(MatSetSizes(B, PETSC_DECIDE, PETSC_DECIDE, M, N));
 82:   PetscCall(MatSetFromOptions(B));
 83:   PetscCall(MatSetOption(B, MAT_IGNORE_OFF_PROC_ENTRIES, flg));
 84:   PetscCall(MatSetPreallocationCOO(B, mycoo.n, mycoo.i, mycoo.j));

 86:   /* Test with ADD_VALUES on a zeroed matrix */
 87:   PetscCall(MatSetValuesCOO(B, vals, ADD_VALUES));
 88:   PetscCall(MatMultEqual(A, B, 10, &equal));
 89:   if (!equal) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "MatSetValuesCOO() failed\n"));
 90:   PetscCall(MatViewFromOptions(B, NULL, "-b_view"));

 92:   /* Test with MatDuplicate on a zeroed matrix */
 93:   PetscCall(MatDuplicate(B, MAT_DO_NOT_COPY_VALUES, &C));
 94:   PetscCall(MatDestroy(&B));
 95:   PetscCall(MatSetValuesCOO(C, vals, ADD_VALUES));
 96:   PetscCall(MatMultEqual(A, C, 10, &equal));
 97:   if (!equal) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "MatSetValuesCOO() on duplicated matrix failed\n"));
 98:   PetscCall(MatViewFromOptions(C, NULL, "-c_view"));

100:   PetscCall(PetscFree(vals));
101:   if (freecoo) PetscCall(PetscFree2(mycoo.i, mycoo.j));
102:   PetscCall(MatDestroy(&A));
103:   PetscCall(MatDestroy(&C));

105:   PetscCall(PetscFinalize());
106:   return 0;
107: }

109: /*TEST

111:   testset:
112:     output_file: output/ex254_1.out
113:     nsize: {{1 2 3}}
114:     args: -ignore_remote {{0 1}}
115:     filter: grep -v type | grep -v "Mat Object"

117:     test:
118:       suffix: kokkos
119:       requires: kokkos_kernels
120:       args: -mat_type aijkokkos

122:     test:
123:       suffix: cuda
124:       requires: cuda
125:       args: -mat_type aijcusparse

127:     test:
128:       suffix: hip
129:       requires: hip
130:       args: -mat_type aijhipsparse

132:     test:
133:       suffix: aij
134:       args: -mat_type aij

136:     test:
137:       suffix: hypre
138:       requires: hypre
139:       args: -mat_type hypre

141:   testset:
142:     output_file: output/ex254_2.out
143:     nsize: 1
144:     args: -ncoos 3
145:     filter: grep -v type | grep -v "Mat Object"

147:     test:
148:       suffix: 2_kokkos
149:       requires: kokkos_kernels
150:       args: -mat_type aijkokkos

152:     test:
153:       suffix: 2_cuda
154:       requires: cuda
155:       args: -mat_type aijcusparse

157:     test:
158:       suffix: 2_hip
159:       requires: hip
160:       args: -mat_type aijhipsparse

162:     test:
163:       suffix: 2_aij
164:       args: -mat_type aij

166:     test:
167:       suffix: 2_hypre
168:       requires: hypre
169:       args: -mat_type hypre

171: TEST*/