1 static char help[] = "Test conversion of ScaLAPACK matrices.\n\n"; 2 3 #include <petscmat.h> 4 5 int main(int argc, char **argv) { 6 Mat A, A_scalapack; 7 PetscInt i, j, M = 10, N = 5, nloc, mloc, nrows, ncols; 8 PetscMPIInt rank, size; 9 IS isrows, iscols; 10 const PetscInt *rows, *cols; 11 PetscScalar *v; 12 MatType type; 13 PetscBool isDense, isAIJ, flg; 14 15 PetscFunctionBeginUser; 16 PetscCall(PetscInitialize(&argc, &argv, (char *)0, help)); 17 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size)); 18 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 19 PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL)); 20 PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL)); 21 22 /* Create a matrix */ 23 PetscCall(MatCreate(PETSC_COMM_WORLD, &A)); 24 mloc = PETSC_DECIDE; 25 PetscCall(PetscSplitOwnershipEqual(PETSC_COMM_WORLD, &mloc, &M)); 26 nloc = PETSC_DECIDE; 27 PetscCall(PetscSplitOwnershipEqual(PETSC_COMM_WORLD, &nloc, &N)); 28 PetscCall(MatSetSizes(A, mloc, nloc, M, N)); 29 PetscCall(MatSetType(A, MATDENSE)); 30 PetscCall(MatSetFromOptions(A)); 31 PetscCall(MatSetUp(A)); 32 33 /* Set local matrix entries */ 34 PetscCall(MatGetOwnershipIS(A, &isrows, &iscols)); 35 PetscCall(ISGetLocalSize(isrows, &nrows)); 36 PetscCall(ISGetIndices(isrows, &rows)); 37 PetscCall(ISGetLocalSize(iscols, &ncols)); 38 PetscCall(ISGetIndices(iscols, &cols)); 39 PetscCall(PetscMalloc1(nrows * ncols, &v)); 40 41 for (i = 0; i < nrows; i++) { 42 for (j = 0; j < ncols; j++) { 43 if (size == 1) { 44 v[i * ncols + j] = (PetscScalar)(i + j); 45 } else { 46 v[i * ncols + j] = (PetscScalar)rank + j * 0.1; 47 } 48 } 49 } 50 PetscCall(MatSetValues(A, nrows, rows, ncols, cols, v, INSERT_VALUES)); 51 PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 52 PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 53 54 /* Test MatSetValues() by converting A to A_scalapack */ 55 PetscCall(MatGetType(A, &type)); 56 if (size == 1) { 57 PetscCall(PetscObjectTypeCompare((PetscObject)A, MATSEQDENSE, &isDense)); 58 PetscCall(PetscObjectTypeCompare((PetscObject)A, MATSEQAIJ, &isAIJ)); 59 } else { 60 PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMPIDENSE, &isDense)); 61 PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMPIAIJ, &isAIJ)); 62 } 63 64 if (isDense || isAIJ) { 65 Mat Aexplicit; 66 PetscCall(MatConvert(A, MATSCALAPACK, MAT_INITIAL_MATRIX, &A_scalapack)); 67 PetscCall(MatComputeOperator(A_scalapack, isAIJ ? MATAIJ : MATDENSE, &Aexplicit)); 68 PetscCall(MatMultEqual(Aexplicit, A_scalapack, 5, &flg)); 69 PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Aexplicit != A_scalapack."); 70 PetscCall(MatDestroy(&Aexplicit)); 71 72 /* Test MAT_REUSE_MATRIX which is only supported for inplace conversion */ 73 PetscCall(MatConvert(A, MATSCALAPACK, MAT_INPLACE_MATRIX, &A)); 74 PetscCall(MatMultEqual(A_scalapack, A, 5, &flg)); 75 PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "A_scalapack != A."); 76 PetscCall(MatDestroy(&A_scalapack)); 77 } 78 79 PetscCall(ISRestoreIndices(isrows, &rows)); 80 PetscCall(ISRestoreIndices(iscols, &cols)); 81 PetscCall(ISDestroy(&isrows)); 82 PetscCall(ISDestroy(&iscols)); 83 PetscCall(PetscFree(v)); 84 PetscCall(MatDestroy(&A)); 85 PetscCall(PetscFinalize()); 86 return 0; 87 } 88 89 /*TEST 90 91 build: 92 requires: scalapack 93 94 test: 95 nsize: 6 96 97 test: 98 suffix: 2 99 nsize: 6 100 args: -mat_type aij 101 output_file: output/ex243_1.out 102 103 test: 104 suffix: 3 105 nsize: 6 106 args: -mat_type scalapack 107 output_file: output/ex243_1.out 108 109 TEST*/ 110