xref: /petsc/src/mat/tests/ex258.c (revision 609caa7c8c030312b00807b4f015fd827bb80932)
11cdffd5eSHong Zhang static char help[] = "Test MatProductReplaceMats() \n\
21cdffd5eSHong Zhang Modified from the code contributed by Pierre Jolivet \n\n";
31cdffd5eSHong Zhang 
41cdffd5eSHong Zhang #include <petscmat.h>
51cdffd5eSHong Zhang 
main(int argc,char ** args)6d71ae5a4SJacob Faibussowitsch int main(int argc, char **args)
7d71ae5a4SJacob Faibussowitsch {
81cdffd5eSHong Zhang   PetscInt  n = 2, convert;
91cdffd5eSHong Zhang   Mat       A, B, Bdense, Conjugate;
101cdffd5eSHong Zhang   PetscBool conjugate = PETSC_FALSE, equal, flg;
111cdffd5eSHong Zhang 
121cdffd5eSHong Zhang   PetscFunctionBeginUser;
131cdffd5eSHong Zhang   PetscCall(PetscInitialize(&argc, &args, NULL, help));
141cdffd5eSHong Zhang 
151cdffd5eSHong Zhang   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
161cdffd5eSHong Zhang   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, n, n));
171cdffd5eSHong Zhang   PetscCall(MatSetType(A, MATDENSE));
181cdffd5eSHong Zhang   PetscCall(MatSetFromOptions(A));
191cdffd5eSHong Zhang   PetscCall(MatSeqDenseSetPreallocation(A, NULL));
201cdffd5eSHong Zhang   PetscCall(MatMPIDenseSetPreallocation(A, NULL));
211cdffd5eSHong Zhang   PetscCall(MatSetRandom(A, NULL));
221cdffd5eSHong Zhang   PetscCall(MatViewFromOptions(A, NULL, "-A_view"));
231cdffd5eSHong Zhang   PetscCall(PetscOptionsGetBool(NULL, NULL, "-conjugate", &conjugate, NULL));
241cdffd5eSHong Zhang 
251cdffd5eSHong Zhang   for (convert = 0; convert < 2; convert++) {
261cdffd5eSHong Zhang     /* convert dense matrix A to aij format */
271cdffd5eSHong Zhang     if (convert) PetscCall(MatConvert(A, MATAIJ, MAT_INPLACE_MATRIX, &A));
281cdffd5eSHong Zhang 
291cdffd5eSHong Zhang     /* compute B = A^T * A or  B = A^H * A */
301cdffd5eSHong Zhang     PetscCall(MatProductCreate(A, A, NULL, &B));
311cdffd5eSHong Zhang 
321cdffd5eSHong Zhang     flg = PETSC_FALSE;
331cdffd5eSHong Zhang     PetscCall(PetscOptionsGetBool(NULL, NULL, "-atb", &flg, NULL));
341cdffd5eSHong Zhang     if (flg) {
351cdffd5eSHong Zhang       PetscCall(MatProductSetType(B, MATPRODUCT_AtB));
361cdffd5eSHong Zhang     } else {
371cdffd5eSHong Zhang       PetscCall(PetscOptionsGetBool(NULL, NULL, "-ptap", &flg, NULL));
381cdffd5eSHong Zhang       if (flg) {
391cdffd5eSHong Zhang         PetscCall(MatProductSetType(B, MATPRODUCT_PtAP));
401cdffd5eSHong Zhang       } else {
411cdffd5eSHong Zhang         PetscCall(PetscOptionsGetBool(NULL, NULL, "-abt", &flg, NULL));
421cdffd5eSHong Zhang         if (flg) {
431cdffd5eSHong Zhang           PetscCall(MatProductSetType(B, MATPRODUCT_ABt));
441cdffd5eSHong Zhang         } else {
451cdffd5eSHong Zhang           PetscCall(MatProductSetType(B, MATPRODUCT_AB));
461cdffd5eSHong Zhang         }
471cdffd5eSHong Zhang       }
481cdffd5eSHong Zhang     }
491cdffd5eSHong Zhang     PetscCall(MatProductSetFromOptions(B));
501cdffd5eSHong Zhang     PetscCall(MatProductSymbolic(B));
511cdffd5eSHong Zhang 
521cdffd5eSHong Zhang     PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &Conjugate));
531cdffd5eSHong Zhang     if (conjugate) PetscCall(MatConjugate(Conjugate));
541cdffd5eSHong Zhang 
551cdffd5eSHong Zhang     /* replace input A by Conjugate */
561cdffd5eSHong Zhang     PetscCall(MatProductReplaceMats(Conjugate, NULL, NULL, B));
571cdffd5eSHong Zhang 
581cdffd5eSHong Zhang     PetscCall(MatProductNumeric(B));
591cdffd5eSHong Zhang     PetscCall(MatViewFromOptions(B, NULL, "-product_view"));
601cdffd5eSHong Zhang 
611cdffd5eSHong Zhang     PetscCall(MatDestroy(&Conjugate));
621cdffd5eSHong Zhang     if (!convert) {
639371c9d4SSatish Balay       Bdense = B;
649371c9d4SSatish Balay       B      = NULL;
651cdffd5eSHong Zhang     }
661cdffd5eSHong Zhang   }
671cdffd5eSHong Zhang 
681cdffd5eSHong Zhang   /* Compare Bdense and B */
691cdffd5eSHong Zhang   PetscCall(MatMultEqual(Bdense, B, 10, &equal));
701cdffd5eSHong Zhang   PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Bdense != B");
711cdffd5eSHong Zhang 
721cdffd5eSHong Zhang   PetscCall(MatDestroy(&Bdense));
731cdffd5eSHong Zhang   PetscCall(MatDestroy(&B));
741cdffd5eSHong Zhang   PetscCall(MatDestroy(&A));
751cdffd5eSHong Zhang   PetscCall(PetscFinalize());
761cdffd5eSHong Zhang   return 0;
771cdffd5eSHong Zhang }
781cdffd5eSHong Zhang 
791cdffd5eSHong Zhang /*TEST
801cdffd5eSHong Zhang 
811cdffd5eSHong Zhang    test:
821cdffd5eSHong Zhang       suffix: 1
831cdffd5eSHong Zhang       args: -conjugate false -atb
84*3886731fSPierre Jolivet       output_file: output/empty.out
851cdffd5eSHong Zhang 
861cdffd5eSHong Zhang    test:
871cdffd5eSHong Zhang       suffix: 2
881cdffd5eSHong Zhang       args: -conjugate true -atb
89*3886731fSPierre Jolivet       output_file: output/empty.out
901cdffd5eSHong Zhang 
911cdffd5eSHong Zhang    test:
921cdffd5eSHong Zhang       suffix: 3
931cdffd5eSHong Zhang       args: -conjugate false
94*3886731fSPierre Jolivet       output_file: output/empty.out
951cdffd5eSHong Zhang 
961cdffd5eSHong Zhang    test:
971cdffd5eSHong Zhang       suffix: 4
981cdffd5eSHong Zhang       args: -ptap
99*3886731fSPierre Jolivet       output_file: output/empty.out
1001cdffd5eSHong Zhang 
1011cdffd5eSHong Zhang    test:
1021cdffd5eSHong Zhang       suffix: 5
1031cdffd5eSHong Zhang       args: -abt
104*3886731fSPierre Jolivet       output_file: output/empty.out
1051cdffd5eSHong Zhang 
1061cdffd5eSHong Zhang    test:
1071cdffd5eSHong Zhang       suffix: 6
1081cdffd5eSHong Zhang       nsize: 2
1091cdffd5eSHong Zhang       args: -conjugate false -atb
110*3886731fSPierre Jolivet       output_file: output/empty.out
1111cdffd5eSHong Zhang 
1121cdffd5eSHong Zhang    test:
1131cdffd5eSHong Zhang       suffix: 7
1141cdffd5eSHong Zhang       nsize: 2
1151cdffd5eSHong Zhang       args: -conjugate true -atb
116*3886731fSPierre Jolivet       output_file: output/empty.out
1171cdffd5eSHong Zhang 
1181cdffd5eSHong Zhang    test:
1191cdffd5eSHong Zhang       suffix: 8
1201cdffd5eSHong Zhang       nsize: 2
1211cdffd5eSHong Zhang       args: -conjugate false
122*3886731fSPierre Jolivet       output_file: output/empty.out
1231cdffd5eSHong Zhang 
1241cdffd5eSHong Zhang    test:
1251cdffd5eSHong Zhang       suffix: 9
1261cdffd5eSHong Zhang       nsize: 2
1271cdffd5eSHong Zhang       args: -ptap
128*3886731fSPierre Jolivet       output_file: output/empty.out
1291cdffd5eSHong Zhang 
1301cdffd5eSHong Zhang    test:
1311cdffd5eSHong Zhang       suffix: 10
1321cdffd5eSHong Zhang       nsize: 2
1331cdffd5eSHong Zhang       args: -abt
134*3886731fSPierre Jolivet       output_file: output/empty.out
1351cdffd5eSHong Zhang 
1361cdffd5eSHong Zhang    test:
1371cdffd5eSHong Zhang       suffix: 11
1381cdffd5eSHong Zhang       nsize: 2
1391cdffd5eSHong Zhang       args: -conjugate true -atb -mat_product_algorithm backend
1401cdffd5eSHong Zhang       TODO: bug: MatProductReplaceMats() does not change the product for this test
1411cdffd5eSHong Zhang 
1421cdffd5eSHong Zhang TEST*/
143