Actual source code: ex1.c


  2: static char help[] = "VecTagger interface routines.\n\n";

  4: #include <petscvec.h>

  6: static PetscErrorCode ISGetBlockGlobalIS(IS is, Vec vec, PetscInt bs, IS *isBlockGlobal)
  7: {
  8:   const PetscInt *idxin;
  9:   PetscInt       *idxout, i, n, rstart;
 10:   PetscLayout     map;


 13:   VecGetLayout(vec, &map);
 14:   rstart = map->rstart / bs;
 15:   ISGetLocalSize(is, &n);
 16:   PetscMalloc1(n, &idxout);
 17:   ISGetIndices(is, &idxin);
 18:   for (i = 0; i < n; i++) idxout[i] = rstart + idxin[i];
 19:   ISRestoreIndices(is, &idxin);
 20:   ISCreateBlock(PetscObjectComm((PetscObject)vec), bs, n, idxout, PETSC_OWN_POINTER, isBlockGlobal);
 21:   return 0;
 22: }

 24: int main(int argc, char **argv)
 25: {
 26:   Vec           vec, tagged, untagged;
 27:   VecScatter    taggedScatter, untaggedScatter;
 28:   PetscInt      bs;
 29:   PetscInt      n, nloc, nint, i, j, k, localStart, localEnd, ntagged, nuntagged;
 30:   MPI_Comm      comm;
 31:   VecTagger     tagger;
 32:   PetscScalar  *array;
 33:   PetscRandom   rand;
 34:   VecTaggerBox *defaultBox;
 35:   VecTaggerBox *boxes;
 36:   IS            is, isBlockGlobal, isComp;
 37:   PetscBool     listed;

 40:   PetscInitialize(&argc, &argv, NULL, help);
 41:   n    = 10.;
 42:   bs   = 1;
 43:   comm = PETSC_COMM_WORLD;
 44:   PetscOptionsBegin(comm, "", "VecTagger Test Options", "Vec");
 45:   PetscOptionsInt("-bs", "The block size of the vector", "ex1.c", bs, &bs, NULL);
 46:   PetscOptionsInt("-n", "The size of the vector (in blocks)", "ex1.c", n, &n, NULL);
 47:   PetscOptionsEnd();

 49:   PetscRandomCreate(comm, &rand);
 50:   PetscRandomSetFromOptions(rand);

 52:   VecCreate(comm, &vec);
 53:   PetscObjectSetName((PetscObject)vec, "Vec to Tag");
 54:   VecSetBlockSize(vec, bs);
 55:   VecSetSizes(vec, PETSC_DECIDE, n);
 56:   VecSetUp(vec);
 57:   VecGetLocalSize(vec, &nloc);
 58:   VecGetArray(vec, &array);
 59:   for (i = 0; i < nloc; i++) {
 60:     PetscScalar val;

 62:     PetscRandomGetValue(rand, &val);
 63:     array[i] = val;
 64:   }
 65:   VecRestoreArray(vec, &array);
 66:   PetscRandomDestroy(&rand);
 67:   VecViewFromOptions(vec, NULL, "-vec_view");

 69:   VecTaggerCreate(comm, &tagger);
 70:   VecTaggerSetBlockSize(tagger, bs);
 71:   VecTaggerSetType(tagger, VECTAGGERABSOLUTE);
 72:   PetscMalloc1(bs, &defaultBox);
 73:   for (i = 0; i < bs; i++) {
 74: #if !defined(PETSC_USE_COMPLEX)
 75:     defaultBox[i].min = 0.1;
 76:     defaultBox[i].max = 1.5;
 77: #else
 78:     defaultBox[i].min = PetscCMPLX(0.1, 0.1);
 79:     defaultBox[i].max = PetscCMPLX(1.5, 1.5);
 80: #endif
 81:   }
 82:   VecTaggerAbsoluteSetBox(tagger, defaultBox);
 83:   PetscFree(defaultBox);
 84:   VecTaggerSetFromOptions(tagger);
 85:   VecTaggerSetUp(tagger);
 86:   PetscObjectViewFromOptions((PetscObject)tagger, NULL, "-vec_tagger_view");
 87:   VecTaggerGetBlockSize(tagger, &bs);

 89:   VecTaggerComputeBoxes(tagger, vec, &nint, &boxes, &listed);
 90:   if (listed) {
 91:     PetscViewer viewer = NULL;

 93:     PetscOptionsGetViewer(comm, NULL, NULL, "-vec_tagger_boxes_view", &viewer, NULL, NULL);
 94:     if (viewer) {
 95:       PetscBool iascii;

 97:       PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii);
 98:       if (iascii) {
 99:         PetscViewerASCIIPrintf(viewer, "Num boxes: %" PetscInt_FMT "\n", nint);
100:         PetscViewerASCIIPushTab(viewer);
101:         for (i = 0, k = 0; i < nint; i++) {
102:           PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ": ", i);
103:           for (j = 0; j < bs; j++, k++) {
104:             if (j) PetscViewerASCIIPrintf(viewer, " x ");
105: #if !defined(PETSC_USE_COMPLEX)
106:             PetscViewerASCIIPrintf(viewer, "[%g,%g]", (double)boxes[k].min, (double)boxes[k].max);
107: #else
108:             PetscViewerASCIIPrintf(viewer, "[%g+%gi,%g+%gi]", (double)PetscRealPart(boxes[k].min), (double)PetscImaginaryPart(boxes[k].min), (double)PetscRealPart(boxes[k].max), (double)PetscImaginaryPart(boxes[k].max));
109: #endif
110:           }
111:           PetscViewerASCIIPrintf(viewer, "\n");
112:         }
113:         PetscViewerASCIIPopTab(viewer);
114:       }
115:     }
116:     PetscViewerDestroy(&viewer);
117:     PetscFree(boxes);
118:   }

120:   VecTaggerComputeIS(tagger, vec, &is, &listed);
121:   ISGetBlockGlobalIS(is, vec, bs, &isBlockGlobal);
122:   PetscObjectSetName((PetscObject)isBlockGlobal, "Tagged IS (block global)");
123:   ISViewFromOptions(isBlockGlobal, NULL, "-tagged_is_view");

125:   VecGetOwnershipRange(vec, &localStart, &localEnd);
126:   ISComplement(isBlockGlobal, localStart, localEnd, &isComp);
127:   PetscObjectSetName((PetscObject)isComp, "Untagged IS (global)");
128:   ISViewFromOptions(isComp, NULL, "-untagged_is_view");

130:   ISGetLocalSize(isBlockGlobal, &ntagged);
131:   ISGetLocalSize(isComp, &nuntagged);

133:   VecCreate(comm, &tagged);
134:   PetscObjectSetName((PetscObject)tagged, "Tagged selection");
135:   VecSetSizes(tagged, ntagged, PETSC_DETERMINE);
136:   VecSetUp(tagged);

138:   VecCreate(comm, &untagged);
139:   PetscObjectSetName((PetscObject)untagged, "Untagged selection");
140:   VecSetSizes(untagged, nuntagged, PETSC_DETERMINE);
141:   VecSetUp(untagged);

143:   VecScatterCreate(vec, isBlockGlobal, tagged, NULL, &taggedScatter);
144:   VecScatterCreate(vec, isComp, untagged, NULL, &untaggedScatter);

146:   VecScatterBegin(taggedScatter, vec, tagged, INSERT_VALUES, SCATTER_FORWARD);
147:   VecScatterEnd(taggedScatter, vec, tagged, INSERT_VALUES, SCATTER_FORWARD);
148:   VecScatterBegin(untaggedScatter, vec, untagged, INSERT_VALUES, SCATTER_FORWARD);
149:   VecScatterEnd(untaggedScatter, vec, untagged, INSERT_VALUES, SCATTER_FORWARD);

151:   VecViewFromOptions(tagged, NULL, "-tagged_vec_view");
152:   VecViewFromOptions(untagged, NULL, "-untagged_vec_view");

154:   VecScatterDestroy(&untaggedScatter);
155:   VecScatterDestroy(&taggedScatter);

157:   VecDestroy(&untagged);
158:   VecDestroy(&tagged);
159:   ISDestroy(&isComp);
160:   ISDestroy(&isBlockGlobal);
161:   ISDestroy(&is);

163:   VecTaggerDestroy(&tagger);
164:   VecDestroy(&vec);
165:   PetscFinalize();
166:   return 0;
167: }

169: /*TEST

171:   test:
172:     suffix: 0
173:     requires: !complex
174:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view

176:   test:
177:     suffix: 1
178:     requires: !complex
179:     nsize: 3
180:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view

182:   test:
183:     suffix: 2
184:     requires: !complex
185:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -bs 2

187:   test:
188:     suffix: 3
189:     requires: !complex
190:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_block_size 2 -vec_tagger_box 0.1,1.5,0.1,1.5

192:   test:
193:     suffix: 4
194:     requires: !complex
195:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_invert

197:   test:
198:     suffix: 5
199:     requires: !complex
200:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type relative -vec_tagger_box 0.25,0.75

202:   test:
203:     suffix: 6
204:     requires: !complex
205:     nsize: 3
206:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type cdf -vec_tagger_box 0.25,0.75

208:   test:
209:     suffix: 7
210:     requires: !complex
211:     nsize: 3
212:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type cdf -vec_tagger_box 0.25,0.75 -vec_tagger_cdf_method iterative -vec_tagger_cdf_max_it 10

214:   test:
215:     suffix: 8
216:     requires: !complex
217:     nsize: 3
218:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type or -vec_tagger_num_subs 2 -sub_0_vec_tagger_type absolute -sub_0_vec_tagger_box 0.0,0.25 -sub_1_vec_tagger_type relative -sub_1_vec_tagger_box 0.75,inf
219:     filter: sed -e s~Inf~inf~g

221:   test:
222:     suffix: 9
223:     requires: !complex
224:     nsize: 3
225:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type and -vec_tagger_num_subs 2 -sub_0_vec_tagger_type absolute -sub_0_vec_tagger_box -inf,0.5 -sub_1_vec_tagger_type relative -sub_1_vec_tagger_box 0.25,0.75
226:     filter: sed -e s~Inf~inf~g

228:   test:
229:     suffix: 10
230:     requires: complex
231:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view

233:   test:
234:     suffix: 11
235:     requires: complex
236:     nsize: 3
237:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view

239:   test:
240:     suffix: 12
241:     requires: complex
242:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -bs 2

244:   test:
245:     suffix: 13
246:     requires: complex
247:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_block_size 2 -vec_tagger_box 0.1+0.1i,1.5+1.5i,0.1+0.1i,1.5+1.5i

249:   test:
250:     suffix: 14
251:     requires: complex
252:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_invert

254:   test:
255:     suffix: 15
256:     requires: complex
257:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type relative -vec_tagger_box 0.25+0.25i,0.75+0.75i

259:   test:
260:     suffix: 16
261:     requires: complex
262:     nsize: 3
263:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type cdf -vec_tagger_box 0.25+0.25i,0.75+0.75i

265:   test:
266:     suffix: 17
267:     requires: complex
268:     nsize: 3
269:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type cdf -vec_tagger_box 0.25+0.25i,0.75+0.75i -vec_tagger_cdf_method iterative -vec_tagger_cdf_max_it 10

271:   test:
272:     suffix: 18
273:     requires: complex
274:     nsize: 3
275:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type or -vec_tagger_num_subs 2 -sub_0_vec_tagger_type absolute -sub_0_vec_tagger_box 0.0+0.0i,0.25+0.25i -sub_1_vec_tagger_type relative -sub_1_vec_tagger_box 0.75+0.75i,inf+infi
276:     filter: sed -e s~Inf~inf~g

278:   test:
279:     suffix: 19
280:     requires: complex
281:     nsize: 3
282:     args: -n 12 -vec_view -vec_tagger_view -vec_tagger_boxes_view -tagged_is_view -untagged_is_view -tagged_vec_view -untagged_vec_view -vec_tagger_type and -vec_tagger_num_subs 2 -sub_0_vec_tagger_type absolute -sub_0_vec_tagger_box -inf-infi,0.75+0.75i -sub_1_vec_tagger_type relative -sub_1_vec_tagger_box 0.25+0.25i,1.+1.i
283:     filter: sed -e s~Inf~inf~g

285: TEST*/