1 2 static char help[] = "Test MatAXPY()\n\n"; 3 4 #include <petscmat.h> 5 6 int main(int argc,char **args) 7 { 8 Mat C,C1,C2,CU; 9 PetscScalar v; 10 PetscInt Ii,J,Istart,Iend; 11 PetscInt i,j,m = 3,n; 12 PetscMPIInt size; 13 PetscBool mat_nonsymmetric = PETSC_FALSE,flg; 14 MatInfo info; 15 16 PetscFunctionBeginUser; 17 PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); 18 PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL)); 19 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 20 n = 2*size; 21 22 /* Set flag if we are doing a nonsymmetric problem; the default is symmetric. */ 23 PetscCall(PetscOptionsGetBool(NULL,NULL,"-mat_nonsym",&mat_nonsymmetric,NULL)); 24 25 PetscCall(MatCreate(PETSC_COMM_WORLD,&C)); 26 PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n)); 27 PetscCall(MatSetFromOptions(C)); 28 PetscCall(MatSeqAIJSetPreallocation(C,5,NULL)); 29 PetscCall(MatMPIAIJSetPreallocation(C,5,NULL,5,NULL)); 30 31 PetscCall(MatGetOwnershipRange(C,&Istart,&Iend)); 32 for (Ii=Istart; Ii<Iend; Ii++) { 33 v = -1.0; i = Ii/n; j = Ii - i*n; 34 if (i>0) {J = Ii - n; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));} 35 if (i<m-1) {J = Ii + n; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));} 36 if (j>0) {J = Ii - 1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));} 37 if (j<n-1) {J = Ii + 1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));} 38 v = 4.0; PetscCall(MatSetValues(C,1,&Ii,1,&Ii,&v,ADD_VALUES)); 39 } 40 41 /* Make the matrix nonsymmetric if desired */ 42 if (mat_nonsymmetric) { 43 for (Ii=Istart; Ii<Iend; Ii++) { 44 v = -1.5; i = Ii/n; 45 if (i>1) {J = Ii-n-1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));} 46 } 47 } else { 48 PetscCall(MatSetOption(C,MAT_SYMMETRIC,PETSC_TRUE)); 49 PetscCall(MatSetOption(C,MAT_SYMMETRY_ETERNAL,PETSC_TRUE)); 50 } 51 PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY)); 52 PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY)); 53 PetscCall(PetscObjectSetName((PetscObject)C,"C")); 54 PetscCall(MatViewFromOptions(C,NULL,"-view")); 55 56 /* C1 = 2.0*C1 + C, C1 is anti-diagonal and has different non-zeros than C */ 57 PetscCall(MatCreate(PETSC_COMM_WORLD,&C1)); 58 PetscCall(MatSetSizes(C1,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n)); 59 PetscCall(MatSetFromOptions(C1)); 60 PetscCall(MatSeqAIJSetPreallocation(C1,1,NULL)); 61 PetscCall(MatMPIAIJSetPreallocation(C1,1,NULL,1,NULL)); 62 for (Ii=Istart; Ii<Iend; Ii++) { 63 v = 1.0; 64 i = m*n - Ii -1; 65 j = Ii; 66 PetscCall(MatSetValues(C1,1,&i,1,&j,&v,ADD_VALUES)); 67 } 68 PetscCall(MatAssemblyBegin(C1,MAT_FINAL_ASSEMBLY)); 69 PetscCall(MatAssemblyEnd(C1,MAT_FINAL_ASSEMBLY)); 70 PetscCall(PetscObjectSetName((PetscObject)C1,"C1")); 71 PetscCall(MatViewFromOptions(C1,NULL,"-view")); 72 PetscCall(MatDuplicate(C1,MAT_COPY_VALUES,&CU)); 73 74 PetscCall(PetscPrintf(PETSC_COMM_WORLD," MatAXPY(C1,2.0,C,DIFFERENT_NONZERO_PATTERN)...\n")); 75 PetscCall(MatAXPY(C1,2.0,C,DIFFERENT_NONZERO_PATTERN)); 76 PetscCall(MatAXPY(CU,2.0,C,UNKNOWN_NONZERO_PATTERN)); 77 PetscCall(MatGetInfo(C1,MAT_GLOBAL_SUM,&info)); 78 PetscCall(PetscPrintf(PETSC_COMM_WORLD," C1: nz_allocated = %g; nz_used = %g; nz_unneeded = %g\n",info.nz_allocated,info.nz_used, info.nz_unneeded)); 79 PetscCall(MatViewFromOptions(C1,NULL,"-view")); 80 PetscCall(MatMultEqual(CU,C1,10,&flg)); 81 if (!flg) { 82 PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Error UNKNOWN_NONZERO_PATTERN (supposedly DIFFERENT_NONZERO_PATTERN)\n")); 83 PetscCall(MatViewFromOptions(CU,NULL,"-view")); 84 } 85 PetscCall(MatDestroy(&CU)); 86 87 /* Secondly, compute C1 = 2.0*C2 + C1, C2 has non-zero pattern of C */ 88 PetscCall(MatDuplicate(C,MAT_DO_NOT_COPY_VALUES,&C2)); 89 PetscCall(MatDuplicate(C1,MAT_COPY_VALUES,&CU)); 90 91 for (Ii=Istart; Ii<Iend; Ii++) { 92 v = 1.0; 93 PetscCall(MatSetValues(C2,1,&Ii,1,&Ii,&v,ADD_VALUES)); 94 } 95 PetscCall(MatAssemblyBegin(C2,MAT_FINAL_ASSEMBLY)); 96 PetscCall(MatAssemblyEnd(C2,MAT_FINAL_ASSEMBLY)); 97 PetscCall(PetscObjectSetName((PetscObject)C2,"C2")); 98 PetscCall(MatViewFromOptions(C2,NULL,"-view")); 99 PetscCall(PetscPrintf(PETSC_COMM_WORLD," MatAXPY(C1,2.0,C2,SUBSET_NONZERO_PATTERN)...\n")); 100 PetscCall(MatAXPY(C1,2.0,C2,SUBSET_NONZERO_PATTERN)); 101 PetscCall(MatAXPY(CU,2.0,C2,UNKNOWN_NONZERO_PATTERN)); 102 PetscCall(MatGetInfo(C1,MAT_GLOBAL_SUM,&info)); 103 PetscCall(PetscPrintf(PETSC_COMM_WORLD," C1: nz_allocated = %g; nz_used = %g; nz_unneeded = %g\n",info.nz_allocated,info.nz_used, info.nz_unneeded)); 104 PetscCall(MatViewFromOptions(C1,NULL,"-view")); 105 PetscCall(MatMultEqual(CU,C1,10,&flg)); 106 if (!flg) { 107 PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Error UNKNOWN_NONZERO_PATTERN (supposedly SUBSET_NONZERO_PATTERN)\n")); 108 PetscCall(MatViewFromOptions(CU,NULL,"-view")); 109 } 110 PetscCall(MatDestroy(&CU)); 111 112 /* Test SAME_NONZERO_PATTERN computing C2 = C2 + 2.0 * C */ 113 PetscCall(MatDuplicate(C2,MAT_COPY_VALUES,&CU)); 114 PetscCall(PetscPrintf(PETSC_COMM_WORLD," MatAXPY(C2,2.0,C,SAME_NONZERO_PATTERN)...\n")); 115 PetscCall(MatAXPY(C2,2.0,C,SAME_NONZERO_PATTERN)); 116 PetscCall(MatAXPY(CU,2.0,C,UNKNOWN_NONZERO_PATTERN)); 117 PetscCall(MatGetInfo(C2,MAT_GLOBAL_SUM,&info)); 118 PetscCall(PetscPrintf(PETSC_COMM_WORLD," C2: nz_allocated = %g; nz_used = %g; nz_unneeded = %g\n",info.nz_allocated,info.nz_used, info.nz_unneeded)); 119 PetscCall(MatViewFromOptions(C2,NULL,"-view")); 120 PetscCall(MatMultEqual(CU,C2,10,&flg)); 121 if (!flg) { 122 PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Error UNKNOWN_NONZERO_PATTERN (supposedly SUBSET_NONZERO_PATTERN)\n")); 123 PetscCall(MatViewFromOptions(CU,NULL,"-view")); 124 } 125 PetscCall(MatDestroy(&CU)); 126 127 PetscCall(MatDestroy(&C1)); 128 PetscCall(MatDestroy(&C2)); 129 PetscCall(MatDestroy(&C)); 130 131 PetscCall(PetscFinalize()); 132 return 0; 133 } 134 135 /*TEST 136 137 test: 138 suffix: 1 139 filter: grep -v " type:" | grep -v "Mat Object" 140 args: -view 141 diff_args: -j 142 143 test: 144 output_file: output/ex132_1.out 145 requires: cuda 146 suffix: 1_cuda 147 filter: grep -v " type:" | grep -v "Mat Object" 148 args: -view -mat_type aijcusparse 149 diff_args: -j 150 151 test: 152 output_file: output/ex132_1.out 153 requires: kokkos_kernels 154 suffix: 1_kokkos 155 filter: grep -v " type:" | grep -v "Mat Object" 156 args: -view -mat_type aijkokkos 157 diff_args: -j 158 159 test: 160 suffix: 2 161 filter: grep -v " type:" | grep -v "Mat Object" 162 args: -view -mat_nonsym 163 diff_args: -j 164 165 test: 166 output_file: output/ex132_2.out 167 requires: cuda 168 suffix: 2_cuda 169 filter: grep -v " type:" | grep -v "Mat Object" 170 args: -view -mat_type aijcusparse -mat_nonsym 171 diff_args: -j 172 173 test: 174 output_file: output/ex132_2.out 175 requires: kokkos_kernels 176 suffix: 2_kokkos 177 filter: grep -v " type:" | grep -v "Mat Object" 178 args: -view -mat_type aijkokkos -mat_nonsym 179 diff_args: -j 180 181 test: 182 nsize: 2 183 suffix: 1_par 184 filter: grep -v " type:" | grep -v "Mat Object" 185 args: -view 186 diff_args: -j 187 188 test: 189 nsize: 2 190 output_file: output/ex132_1_par.out 191 requires: cuda 192 suffix: 1_par_cuda 193 filter: grep -v " type:" | grep -v "Mat Object" 194 args: -view -mat_type aijcusparse 195 diff_args: -j 196 197 test: 198 nsize: 2 199 output_file: output/ex132_1_par.out 200 requires: !sycl kokkos_kernels 201 suffix: 1_par_kokkos 202 filter: grep -v " type:" | grep -v "Mat Object" 203 args: -view -mat_type aijkokkos 204 diff_args: -j 205 206 test: 207 nsize: 2 208 suffix: 2_par 209 filter: grep -v " type:" | grep -v "Mat Object" 210 args: -view -mat_nonsym 211 diff_args: -j 212 213 test: 214 nsize: 2 215 output_file: output/ex132_2_par.out 216 requires: cuda 217 suffix: 2_par_cuda 218 filter: grep -v " type:" | grep -v "Mat Object" 219 args: -view -mat_type aijcusparse -mat_nonsym 220 diff_args: -j 221 222 testset: 223 nsize: 2 224 output_file: output/ex132_2_par.out 225 requires: !sycl kokkos_kernels 226 filter: grep -v " type:" | grep -v "Mat Object" 227 args: -view -mat_type aijkokkos -mat_nonsym 228 diff_args: -j 229 test: 230 suffix: 2_par_kokkos_no_gpu_aware 231 args: -use_gpu_aware_mpi 0 232 test: 233 requires: defined(HAVE_MPI_GPU_AWARE) 234 suffix: 2_par_kokkos_gpu_aware 235 args: -use_gpu_aware_mpi 1 236 237 TEST*/ 238