xref: /petsc/src/mat/tests/ex254.c (revision ea9ee2c1d69c9e3cf6d2b3c8a205b9880d3dba39)
1 static char help[] = "Test MatSetValuesCOO for MPIAIJ and its subclasses \n\n";
2 
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   PetscMPIInt    rank, size;
10   PetscBool      equal;
11   PetscScalar   *vals;
12   PetscBool      flg = PETSC_FALSE;
13 
14   /* Construct 18 x 18 matrices, which are big enough to have complex communication patterns but still small enough for debugging */
15   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};
16   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};
17 
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};
20 
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 
24   struct {
25     PetscInt *i, *j, n;
26   } coo[3] = {
27     {i0, j0, sizeof(i0) / sizeof(PetscInt)},
28     {i1, j1, sizeof(i1) / sizeof(PetscInt)},
29     {i2, j2, sizeof(i2) / sizeof(PetscInt)}
30   };
31 
32   PetscFunctionBeginUser;
33   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
34   PetscCall(PetscOptionsGetBool(NULL, NULL, "-ignore_remote", &flg, NULL));
35   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
36   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
37 
38   PetscCheck(size <= 3, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This test requires at most 3 processes");
39 
40   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
41   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, M, N));
42   PetscCall(MatSetType(A, MATAIJ));
43   PetscCall(MatSeqAIJSetPreallocation(A, 2, NULL));
44   PetscCall(MatMPIAIJSetPreallocation(A, 2, NULL, 2, NULL));
45   PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
46   PetscCall(MatSetOption(A, MAT_IGNORE_OFF_PROC_ENTRIES, flg));
47 
48   PetscCall(PetscMalloc1(coo[rank].n, &vals));
49   for (k = 0; k < coo[rank].n; k++) {
50     vals[k] = coo[rank].j[k];
51     PetscCall(MatSetValue(A, coo[rank].i[k], coo[rank].j[k], vals[k], ADD_VALUES));
52   }
53   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
54   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
55 
56   PetscCall(MatCreate(PETSC_COMM_WORLD, &B));
57   PetscCall(MatSetSizes(B, PETSC_DECIDE, PETSC_DECIDE, M, N));
58   PetscCall(MatSetFromOptions(B));
59   PetscCall(MatSetOption(B, MAT_IGNORE_OFF_PROC_ENTRIES, flg));
60   PetscCall(MatSetPreallocationCOO(B, coo[rank].n, coo[rank].i, coo[rank].j));
61   PetscCall(MatDuplicate(B, MAT_DO_NOT_COPY_VALUES, &C));
62   PetscCall(MatDestroy(&B));
63   PetscCall(MatSetValuesCOO(C, vals, ADD_VALUES));
64   PetscCall(MatEqual(A, C, &equal));
65 
66   if (!equal) {
67     PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));
68     PetscCall(MatView(C, PETSC_VIEWER_STDOUT_WORLD));
69     SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "MatSetValuesCOO() failed");
70   }
71 
72   PetscCall(PetscFree(vals));
73   PetscCall(MatDestroy(&A));
74   PetscCall(MatDestroy(&C));
75 
76   PetscCall(PetscFinalize());
77   return 0;
78 }
79 
80 /*TEST
81 
82   testset:
83     output_file: output/ex254_1.out
84     nsize: {{1 2 3}}
85     args: -ignore_remote {{0 1}}
86 
87     test:
88       suffix: kokkos
89       requires: kokkos_kernels
90       args: -mat_type aijkokkos
91 
92     test:
93       suffix: cuda
94       requires: cuda
95       args: -mat_type aijcusparse
96 
97     test:
98       suffix: hip
99       requires: hip
100       args: -mat_type aijhipsparse
101 
102     test:
103       suffix: aij
104       args: -mat_type aij
105 
106   testset:
107     # MATHYPRE does not support MAT_IGNORE_OFF_PROC_ENTRIES yet
108     output_file: output/ex254_1.out
109     nsize: {{1 2 3}}
110     suffix: hypre
111     requires: hypre kokkos_kernels
112     args: -ignore_remote 0 -mat_type hypre
113 
114 TEST*/
115