Actual source code: ex44.c
1: #include <petscvec.h>
3: static char help[] = "Tests vecScatter Sequential to Sequential for (CUDA) vectors\n\
4: -m # : the size of the vectors\n \
5: -n # : the number of indices (with n<=m)\n \
6: -toFirst # : the starting index of the output vector for strided scatters\n \
7: -toStep # : the step size into the output vector for strided scatters\n \
8: -fromFirst # : the starting index of the input vector for strided scatters\n\
9: -fromStep # : the step size into the input vector for strided scatters\n\n";
11: int main(int argc, char *argv[])
12: {
13: Vec X, Y;
14: PetscInt m, n, i, n1, n2;
15: PetscInt toFirst, toStep, fromFirst, fromStep;
16: PetscInt *idx, *idy;
17: PetscBool flg;
18: IS toISStrided, fromISStrided, toISGeneral, fromISGeneral;
19: VecScatter vscatSStoSS, vscatSStoSG, vscatSGtoSS, vscatSGtoSG;
20: ScatterMode mode;
21: InsertMode addv;
23: PetscFunctionBeginUser;
24: PetscCall(PetscInitialize(&argc, &argv, 0, help));
25: PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, &flg));
26: if (!flg) m = 100;
28: PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, &flg));
29: if (!flg) n = 30;
31: PetscCall(PetscOptionsGetInt(NULL, NULL, "-toFirst", &toFirst, &flg));
32: if (!flg) toFirst = 3;
34: PetscCall(PetscOptionsGetInt(NULL, NULL, "-toStep", &toStep, &flg));
35: if (!flg) toStep = 3;
37: PetscCall(PetscOptionsGetInt(NULL, NULL, "-fromFirst", &fromFirst, &flg));
38: if (!flg) fromFirst = 2;
40: PetscCall(PetscOptionsGetInt(NULL, NULL, "-fromStep", &fromStep, &flg));
41: if (!flg) fromStep = 2;
43: if (n > m) {
44: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "The vector sizes are %" PetscInt_FMT ". The number of elements being scattered is %" PetscInt_FMT "\n", m, n));
45: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Adjust the parameters such that m>=n\n"));
46: } else if (toFirst + (n - 1) * toStep >= m) {
47: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "The vector sizes are %" PetscInt_FMT ". The number of elements being scattered is %" PetscInt_FMT "\n", m, n));
48: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "For the Strided Scatter, toFirst=%" PetscInt_FMT " and toStep=%" PetscInt_FMT ".\n", toFirst, toStep));
49: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "This produces an index (toFirst+(n-1)*toStep)>=m\n"));
50: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Adjust the parameterrs accordingly with -m, -n, -toFirst, or -toStep\n"));
51: } else if (fromFirst + (n - 1) * fromStep >= m) {
52: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "The vector sizes are %" PetscInt_FMT ". The number of elements being scattered is %" PetscInt_FMT "\n", m, n));
53: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "For the Strided Scatter, fromFirst=%" PetscInt_FMT " and fromStep=%" PetscInt_FMT ".\n", fromFirst, toStep));
54: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "This produces an index (fromFirst+(n-1)*fromStep)>=m\n"));
55: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Adjust the parameterrs accordingly with -m, -n, -fromFirst, or -fromStep\n"));
56: } else {
57: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "m=%" PetscInt_FMT "\tn=%" PetscInt_FMT "\tfromFirst=%" PetscInt_FMT "\tfromStep=%" PetscInt_FMT "\ttoFirst=%" PetscInt_FMT "\ttoStep=%" PetscInt_FMT "\n", m, n, fromFirst, fromStep, toFirst, toStep));
58: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "fromFirst+(n-1)*fromStep=%" PetscInt_FMT "\ttoFirst+(n-1)*toStep=%" PetscInt_FMT "\n", fromFirst + (n - 1) * fromStep, toFirst + (n - 1) * toStep));
60: /* Build the vectors */
61: PetscCall(VecCreate(PETSC_COMM_WORLD, &Y));
62: PetscCall(VecSetSizes(Y, m, PETSC_DECIDE));
63: PetscCall(VecCreate(PETSC_COMM_WORLD, &X));
64: PetscCall(VecSetSizes(X, m, PETSC_DECIDE));
66: PetscCall(VecSetFromOptions(Y));
67: PetscCall(VecSetFromOptions(X));
68: PetscCall(VecSet(X, 2.0));
69: PetscCall(VecSet(Y, 1.0));
71: /* Build the strided index sets */
72: PetscCall(ISCreate(PETSC_COMM_WORLD, &toISStrided));
73: PetscCall(ISCreate(PETSC_COMM_WORLD, &fromISStrided));
74: PetscCall(ISSetType(toISStrided, ISSTRIDE));
75: PetscCall(ISSetType(fromISStrided, ISSTRIDE));
76: PetscCall(ISStrideSetStride(fromISStrided, n, fromFirst, fromStep));
77: PetscCall(ISStrideSetStride(toISStrided, n, toFirst, toStep));
79: /* Build the general index sets */
80: PetscCall(PetscMalloc1(n, &idx));
81: PetscCall(PetscMalloc1(n, &idy));
82: for (i = 0; i < n; i++) {
83: idx[i] = i % m;
84: idy[i] = (i + m) % m;
85: }
86: n1 = n;
87: n2 = n;
88: PetscCall(PetscSortRemoveDupsInt(&n1, idx));
89: PetscCall(PetscSortRemoveDupsInt(&n2, idy));
91: PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, n1, idx, PETSC_COPY_VALUES, &toISGeneral));
92: PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, n2, idy, PETSC_COPY_VALUES, &fromISGeneral));
94: /* set the mode and the insert/add parameter */
95: mode = SCATTER_FORWARD;
96: addv = ADD_VALUES;
98: /* VecScatter : Seq Strided to Seq Strided */
99: PetscCall(VecScatterCreate(X, fromISStrided, Y, toISStrided, &vscatSStoSS));
100: PetscCall(VecScatterBegin(vscatSStoSS, X, Y, addv, mode));
101: PetscCall(VecScatterEnd(vscatSStoSS, X, Y, addv, mode));
102: PetscCall(VecScatterDestroy(&vscatSStoSS));
104: /* VecScatter : Seq General to Seq Strided */
105: PetscCall(VecScatterCreate(Y, fromISGeneral, X, toISStrided, &vscatSGtoSS));
106: PetscCall(VecScatterBegin(vscatSGtoSS, Y, X, addv, mode));
107: PetscCall(VecScatterEnd(vscatSGtoSS, Y, X, addv, mode));
108: PetscCall(VecScatterDestroy(&vscatSGtoSS));
110: /* VecScatter : Seq General to Seq General */
111: PetscCall(VecScatterCreate(X, fromISGeneral, Y, toISGeneral, &vscatSGtoSG));
112: PetscCall(VecScatterBegin(vscatSGtoSG, X, Y, addv, mode));
113: PetscCall(VecScatterEnd(vscatSGtoSG, X, Y, addv, mode));
114: PetscCall(VecScatterDestroy(&vscatSGtoSG));
116: /* VecScatter : Seq Strided to Seq General */
117: PetscCall(VecScatterCreate(Y, fromISStrided, X, toISGeneral, &vscatSStoSG));
118: PetscCall(VecScatterBegin(vscatSStoSG, Y, X, addv, mode));
119: PetscCall(VecScatterEnd(vscatSStoSG, Y, X, addv, mode));
120: PetscCall(VecScatterDestroy(&vscatSStoSG));
122: /* view the results */
123: PetscCall(VecView(Y, PETSC_VIEWER_STDOUT_WORLD));
125: /* Cleanup */
126: PetscCall(VecDestroy(&X));
127: PetscCall(VecDestroy(&Y));
128: PetscCall(ISDestroy(&toISStrided));
129: PetscCall(ISDestroy(&fromISStrided));
130: PetscCall(ISDestroy(&toISGeneral));
131: PetscCall(ISDestroy(&fromISGeneral));
132: PetscCall(PetscFree(idx));
133: PetscCall(PetscFree(idy));
134: }
135: PetscCall(PetscFinalize());
136: return 0;
137: }
139: /*TEST
141: test:
142: suffix: cuda
143: args: -vec_type cuda
144: requires: cuda
146: TEST*/