1 static char help[] = "Test MatMatMult(), MatTranspose(), MatTransposeMatMult() for Dense and Elemental matrices.\n\n"; 2 /* 3 Example: 4 mpiexec -n <np> ./ex104 -mat_type elemental 5 */ 6 7 #include <petscmat.h> 8 9 int main(int argc, char **argv) 10 { 11 Mat A, B, C, D; 12 PetscInt i, M = 10, N = 5, j, nrows, ncols, am, an, rstart, rend; 13 PetscRandom r; 14 PetscBool equal, Aiselemental; 15 PetscBool columns_on_one_rank = PETSC_FALSE; 16 PetscReal fill = 1.0; 17 IS isrows, iscols; 18 const PetscInt *rows, *cols; 19 PetscScalar *v, rval; 20 PetscBool Test_MatMatMult = PETSC_TRUE; 21 PetscMPIInt size, rank; 22 23 PetscFunctionBeginUser; 24 PetscCall(PetscInitialize(&argc, &argv, NULL, help)); 25 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size)); 26 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 27 28 PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL)); 29 PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL)); 30 PetscCall(PetscOptionsGetBool(NULL, NULL, "-columns_on_one_rank", &columns_on_one_rank, NULL)); 31 PetscCall(MatCreate(PETSC_COMM_WORLD, &A)); 32 if (!columns_on_one_rank) { 33 PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, M, N)); 34 } else { 35 PetscCall(MatSetSizes(A, PETSC_DECIDE, rank == 0 ? N : 0, M, N)); 36 } 37 PetscCall(MatSetType(A, MATDENSE)); 38 PetscCall(MatSetFromOptions(A)); 39 PetscCall(MatSetUp(A)); 40 PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &r)); 41 PetscCall(PetscRandomSetFromOptions(r)); 42 43 /* Set local matrix entries */ 44 PetscCall(MatGetOwnershipIS(A, &isrows, &iscols)); 45 PetscCall(ISGetLocalSize(isrows, &nrows)); 46 PetscCall(ISGetIndices(isrows, &rows)); 47 PetscCall(ISGetLocalSize(iscols, &ncols)); 48 PetscCall(ISGetIndices(iscols, &cols)); 49 PetscCall(PetscMalloc1(nrows * ncols, &v)); 50 for (i = 0; i < nrows; i++) { 51 for (j = 0; j < ncols; j++) { 52 PetscCall(PetscRandomGetValue(r, &rval)); 53 v[i * ncols + j] = rval; 54 } 55 } 56 PetscCall(MatSetValues(A, nrows, rows, ncols, cols, v, INSERT_VALUES)); 57 PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 58 PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 59 PetscCall(ISRestoreIndices(isrows, &rows)); 60 PetscCall(ISRestoreIndices(iscols, &cols)); 61 PetscCall(ISDestroy(&isrows)); 62 PetscCall(ISDestroy(&iscols)); 63 PetscCall(PetscRandomDestroy(&r)); 64 65 PetscCall(PetscObjectTypeCompare((PetscObject)A, MATELEMENTAL, &Aiselemental)); 66 67 /* Test MatCreateTranspose() and MatTranspose() */ 68 PetscCall(MatCreateTranspose(A, &C)); 69 PetscCall(MatTranspose(A, MAT_INITIAL_MATRIX, &B)); /* B = A^T */ 70 PetscCall(MatMultEqual(C, B, 10, &equal)); 71 PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "A^T*x != (x^T*A)^T"); 72 PetscCall(MatDestroy(&B)); 73 74 PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &B)); 75 if (!Aiselemental) { 76 PetscCall(MatTranspose(B, MAT_INPLACE_MATRIX, &B)); 77 PetscCall(MatMultEqual(C, B, 10, &equal)); 78 PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "C*x != B*x"); 79 } 80 PetscCall(MatDestroy(&B)); 81 82 /* Test B = C*A for matrix type transpose and seqdense */ 83 if (size == 1 && !Aiselemental) { 84 PetscCall(MatMatMult(C, A, MAT_INITIAL_MATRIX, fill, &B)); 85 PetscCall(MatMatMultEqual(C, A, B, 10, &equal)); 86 PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "B != C*A for matrix type transpose and seqdense"); 87 PetscCall(MatDestroy(&B)); 88 } 89 PetscCall(MatDestroy(&C)); 90 91 /* Test MatMatMult() */ 92 if (Test_MatMatMult) { 93 PetscCall(MatTranspose(A, MAT_INITIAL_MATRIX, &B)); /* B = A^T */ 94 PetscCall(MatMatMult(B, A, MAT_INITIAL_MATRIX, fill, &C)); /* C = B*A = A^T*A */ 95 PetscCall(MatMatMult(B, A, MAT_REUSE_MATRIX, fill, &C)); 96 PetscCall(MatMatMultEqual(B, A, C, 10, &equal)); 97 PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "B*A*x != C*x"); 98 99 /* Test MatDuplicate for matrix product */ 100 PetscCall(MatDuplicate(C, MAT_COPY_VALUES, &D)); 101 102 PetscCall(MatDestroy(&D)); 103 PetscCall(MatDestroy(&C)); 104 PetscCall(MatDestroy(&B)); 105 } 106 107 /* Test MatTransposeMatMult() */ 108 if (!Aiselemental) { 109 Mat E; 110 111 PetscCall(MatTransposeMatMult(A, A, MAT_INITIAL_MATRIX, fill, &D)); /* D = A^T*A */ 112 PetscCall(MatTransposeMatMult(A, A, MAT_REUSE_MATRIX, fill, &D)); 113 PetscCall(MatTransposeMatMultEqual(A, A, D, 10, &equal)); 114 PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "D*x != A^T*A*x"); 115 116 /* Test MatDuplicate for matrix product */ 117 PetscCall(MatDuplicate(D, MAT_COPY_VALUES, &C)); 118 PetscCall(MatDestroy(&C)); 119 120 /* Test A*D for fast path when D is on one process */ 121 PetscCall(MatSetRandom(D, NULL)); 122 PetscCall(MatMatMult(A, D, MAT_INITIAL_MATRIX, fill, &E)); 123 PetscCall(MatMatMult(A, D, MAT_REUSE_MATRIX, fill, &E)); 124 PetscCall(MatMatMultEqual(A, D, E, 10, &equal)); 125 PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "E*x != A*D*x"); 126 PetscCall(MatDestroy(&E)); 127 128 PetscCall(MatDestroy(&D)); 129 130 /* Test D*x = A^T*C*A*x, where C is in AIJ format */ 131 PetscCall(MatGetLocalSize(A, &am, &an)); 132 PetscCall(MatCreate(PETSC_COMM_WORLD, &C)); 133 if (size == 1) { 134 PetscCall(MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, am, am)); 135 } else { 136 PetscCall(MatSetSizes(C, am, am, PETSC_DECIDE, PETSC_DECIDE)); 137 } 138 PetscCall(MatSetFromOptions(C)); 139 PetscCall(MatSetUp(C)); 140 PetscCall(MatGetOwnershipRange(C, &rstart, &rend)); 141 v[0] = 1.0; 142 for (i = rstart; i < rend; i++) PetscCall(MatSetValues(C, 1, &i, 1, &i, v, INSERT_VALUES)); 143 PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY)); 144 PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY)); 145 146 /* B = C*A, D = A^T*B */ 147 PetscCall(MatMatMult(C, A, MAT_INITIAL_MATRIX, 1.0, &B)); 148 PetscCall(MatTransposeMatMult(A, B, MAT_INITIAL_MATRIX, fill, &D)); 149 PetscCall(MatTransposeMatMultEqual(A, B, D, 10, &equal)); 150 PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "D*x != A^T*B*x"); 151 152 PetscCall(MatDestroy(&D)); 153 PetscCall(MatDestroy(&C)); 154 PetscCall(MatDestroy(&B)); 155 } 156 157 /* Test MatMatTransposeMult() */ 158 if (!Aiselemental) { 159 PetscReal diff, scale; 160 PetscInt am, an, aM, aN; 161 162 PetscCall(MatGetLocalSize(A, &am, &an)); 163 PetscCall(MatGetSize(A, &aM, &aN)); 164 PetscCall(MatCreateDense(PetscObjectComm((PetscObject)A), PETSC_DECIDE, an, aM + 10, aN, NULL, &B)); 165 PetscCall(MatSetRandom(B, NULL)); 166 PetscCall(MatMatTransposeMult(A, B, MAT_INITIAL_MATRIX, fill, &D)); /* D = A*A^T */ 167 168 /* Test MatDuplicate for matrix product */ 169 PetscCall(MatDuplicate(D, MAT_COPY_VALUES, &C)); 170 171 PetscCall(MatMatTransposeMult(A, B, MAT_REUSE_MATRIX, fill, &D)); 172 PetscCall(MatAXPY(C, -1., D, SAME_NONZERO_PATTERN)); 173 174 PetscCall(MatNorm(C, NORM_FROBENIUS, &diff)); 175 PetscCall(MatNorm(D, NORM_FROBENIUS, &scale)); 176 PetscCheck(diff <= PETSC_SMALL * scale, PetscObjectComm((PetscObject)D), PETSC_ERR_PLIB, "MatMatTransposeMult() differs between MAT_INITIAL_MATRIX and MAT_REUSE_MATRIX"); 177 PetscCall(MatDestroy(&C)); 178 179 PetscCall(MatMatTransposeMultEqual(A, B, D, 10, &equal)); 180 PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "D*x != A^T*A*x"); 181 PetscCall(MatDestroy(&D)); 182 PetscCall(MatDestroy(&B)); 183 } 184 185 PetscCall(MatDestroy(&A)); 186 PetscCall(PetscFree(v)); 187 PetscCall(PetscFinalize()); 188 return 0; 189 } 190 191 /*TEST 192 193 test: 194 output_file: output/empty.out 195 196 test: 197 suffix: 2 198 nsize: 2 199 output_file: output/empty.out 200 201 test: 202 suffix: 3 203 nsize: 4 204 output_file: output/empty.out 205 args: -M 23 -N 31 206 207 test: 208 suffix: 4 209 nsize: 4 210 output_file: output/empty.out 211 args: -M 23 -N 31 -matmattransmult_mpidense_mpidense_via cyclic 212 213 test: 214 suffix: 5 215 nsize: 4 216 output_file: output/empty.out 217 args: -M 23 -N 31 -matmattransmult_mpidense_mpidense_via allgatherv 218 219 test: 220 suffix: 6 221 args: -mat_type elemental 222 requires: elemental 223 output_file: output/empty.out 224 225 testset: 226 nsize: 2 227 output_file: output/empty.out 228 requires: elemental 229 test: 230 suffix: 7_dense 231 args: -mat_type dense -mat_product_algorithm elemental 232 test: 233 suffix: 7_elemental 234 args: -mat_type elemental 235 236 test: 237 suffix: 8 238 nsize: 4 239 args: -columns_on_one_rank 240 output_file: output/empty.out 241 242 test: 243 suffix: 9 244 nsize: 4 245 requires: cuda 246 args: -columns_on_one_rank -mat_type densecuda 247 output_file: output/empty.out 248 249 TEST*/ 250