1 static char help[] = "Scatters between parallel vectors. \n\ 2 uses block index sets\n\n"; 3 4 #include <petscvec.h> 5 6 int main(int argc, char **argv) 7 { 8 PetscInt bs = 1, n = 5, N, i, low; 9 PetscInt ix0[3] = {5, 7, 9}, iy0[3] = {1, 2, 4}, ix1[3] = {2, 3, 1}, iy1[3] = {0, 3, 9}; 10 PetscMPIInt size, rank; 11 PetscScalar *array; 12 Vec x, x1, y; 13 IS isx, isy; 14 VecScatter ctx; 15 VecScatterType type; 16 PetscBool flg; 17 18 PetscFunctionBeginUser; 19 PetscCall(PetscInitialize(&argc, &argv, (char *)0, help)); 20 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size)); 21 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 22 23 PetscCheck(size >= 2, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Must run more than one processor"); 24 25 PetscCall(PetscOptionsGetInt(NULL, NULL, "-bs", &bs, NULL)); 26 n = bs * n; 27 28 /* Create vector x over shared memory */ 29 PetscCall(VecCreate(PETSC_COMM_WORLD, &x)); 30 PetscCall(VecSetSizes(x, n, PETSC_DECIDE)); 31 PetscCall(VecSetFromOptions(x)); 32 33 PetscCall(VecGetOwnershipRange(x, &low, NULL)); 34 PetscCall(VecGetArray(x, &array)); 35 for (i = 0; i < n; i++) array[i] = (PetscScalar)(i + low); 36 PetscCall(VecRestoreArray(x, &array)); 37 /* PetscCall(VecView(x,PETSC_VIEWER_STDOUT_WORLD)); */ 38 39 /* Test some vector functions */ 40 PetscCall(VecAssemblyBegin(x)); 41 PetscCall(VecAssemblyEnd(x)); 42 43 PetscCall(VecGetSize(x, &N)); 44 PetscCall(VecGetLocalSize(x, &n)); 45 46 PetscCall(VecDuplicate(x, &x1)); 47 PetscCall(VecCopy(x, x1)); 48 PetscCall(VecEqual(x, x1, &flg)); 49 PetscCheck(flg, PetscObjectComm((PetscObject)x), PETSC_ERR_ARG_WRONG, "x1 != x"); 50 51 PetscCall(VecScale(x1, 2.0)); 52 PetscCall(VecSet(x1, 10.0)); 53 /* PetscCall(VecView(x1,PETSC_VIEWER_STDOUT_WORLD)); */ 54 55 /* Create vector y over shared memory */ 56 PetscCall(VecCreate(PETSC_COMM_WORLD, &y)); 57 PetscCall(VecSetSizes(y, n, PETSC_DECIDE)); 58 PetscCall(VecSetFromOptions(y)); 59 PetscCall(VecGetArray(y, &array)); 60 for (i = 0; i < n; i++) array[i] = -(PetscScalar)(i + 100 * rank); 61 PetscCall(VecRestoreArray(y, &array)); 62 PetscCall(VecAssemblyBegin(y)); 63 PetscCall(VecAssemblyEnd(y)); 64 /* PetscCall(VecView(y,PETSC_VIEWER_STDOUT_WORLD)); */ 65 66 /* Create two index sets */ 67 if (rank == 0) { 68 PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, 3, ix0, PETSC_COPY_VALUES, &isx)); 69 PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, 3, iy0, PETSC_COPY_VALUES, &isy)); 70 } else { 71 PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, 3, ix1, PETSC_COPY_VALUES, &isx)); 72 PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, 3, iy1, PETSC_COPY_VALUES, &isy)); 73 } 74 75 if (rank == 10) { 76 PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n[%d] isx:\n", rank)); 77 PetscCall(ISView(isx, PETSC_VIEWER_STDOUT_SELF)); 78 PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n[%d] isy:\n", rank)); 79 PetscCall(ISView(isy, PETSC_VIEWER_STDOUT_SELF)); 80 } 81 82 /* Create Vector scatter */ 83 PetscCall(VecScatterCreate(x, isx, y, isy, &ctx)); 84 PetscCall(VecScatterSetFromOptions(ctx)); 85 PetscCall(VecScatterGetType(ctx, &type)); 86 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "scatter type %s\n", type)); 87 88 /* Test forward vecscatter */ 89 PetscCall(VecSet(y, 0.0)); 90 PetscCall(VecScatterBegin(ctx, x, y, ADD_VALUES, SCATTER_FORWARD)); 91 PetscCall(VecScatterEnd(ctx, x, y, ADD_VALUES, SCATTER_FORWARD)); 92 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\nSCATTER_FORWARD y:\n")); 93 PetscCall(VecView(y, PETSC_VIEWER_STDOUT_WORLD)); 94 95 /* Test reverse vecscatter */ 96 PetscCall(VecSet(x, 0.0)); 97 PetscCall(VecSet(y, 0.0)); 98 PetscCall(VecGetOwnershipRange(y, &low, NULL)); 99 PetscCall(VecGetArray(y, &array)); 100 for (i = 0; i < n; i++) array[i] = (PetscScalar)(i + low); 101 PetscCall(VecRestoreArray(y, &array)); 102 PetscCall(VecScatterBegin(ctx, y, x, ADD_VALUES, SCATTER_REVERSE)); 103 PetscCall(VecScatterEnd(ctx, y, x, ADD_VALUES, SCATTER_REVERSE)); 104 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\nSCATTER_REVERSE x:\n")); 105 PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD)); 106 107 /* Free objects */ 108 PetscCall(VecScatterDestroy(&ctx)); 109 PetscCall(ISDestroy(&isx)); 110 PetscCall(ISDestroy(&isy)); 111 PetscCall(VecDestroy(&x)); 112 PetscCall(VecDestroy(&x1)); 113 PetscCall(VecDestroy(&y)); 114 PetscCall(PetscFinalize()); 115 return 0; 116 } 117 118 /*TEST 119 120 test: 121 nsize: 2 122 123 TEST*/ 124