Actual source code: pmap.c

  1: #define PETSCVEC_DLL
  2: /*
  3:    This file contains routines for basic map object implementation.
  4: */

 6:  #include private/vecimpl.h
  7: /*@C
  8:      PetscMapInitialize - Sets the map contents to the default.

 10:     Collective on MPI_Comm

 12:    Input Parameters:
 13: +    comm - the MPI communicator
 14: -    map - pointer to the map

 16:    Level: intermediate

 18:     Notes: Typical calling sequence
 19:        PetscMapInitialize(MPI_Comm,PetscMap *);
 20:        PetscMapSetBlockSize(PetscMap*,1);
 21:        PetscMapSetSize(PetscMap*,n) or PetscMapSetLocalSize(PetscMap*,N);
 22:        PetscMapSetUp(PetscMap*);
 23:        PetscMapGetSize(PetscMap*,PetscInt *);

 25:        Unlike regular PETSc objects you work with a pointer to the object instead of 
 26:      the object directly.

 28:     Fortran Notes: 
 29:       Not available from Fortran

 31: .seealso: PetscMapSetLocalSize(), PetscMapSetSize(), PetscMapGetSize(), PetscMapGetLocalSize(), PetscMap,
 32:           PetscMapGetLocalRange(), PetscMapGetGlobalRange(), PetscMapSetBlockSize(), PetscMapGetBlockSize(), PetscMapSetUp()

 34: @*/
 37: PetscErrorCode  PetscMapInitialize(MPI_Comm comm,PetscMap *map)
 38: {
 40:   map->comm   = comm;
 41:   map->bs     = -1;
 42:   map->n      = -1;
 43:   map->N      = -1;
 44:   map->range  = 0;
 45:   map->rstart = 0;
 46:   map->rend   = 0;
 47:   return(0);
 48: }

 50: /*@C
 51:      PetscMapSetUp - given a map where you have set either the global or local
 52:            size sets up the map so that it may be used.

 54:     Collective on MPI_Comm

 56:    Input Parameters:
 57: .    map - pointer to the map

 59:    Level: intermediate

 61:     Notes: Typical calling sequence
 62:        PetscMapInitialize(MPI_Comm,PetscMap *);
 63:        PetscMapSetBlockSize(PetscMap*,1);
 64:        PetscMapSetSize(PetscMap*,n) or PetscMapSetLocalSize(PetscMap*,N);
 65:        PetscMapSetUp(PetscMap*);
 66:        PetscMapGetSize(PetscMap*,PetscInt *);

 68:        Unlike regular PETSc objects you work with a pointer to the object instead of 
 69:      the object directly.

 71:     Fortran Notes: 
 72:       Not available from Fortran

 74: .seealso: PetscMapSetLocalSize(), PetscMapSetSize(), PetscMapGetSize(), PetscMapGetLocalSize(), PetscMap,
 75:           PetscMapGetLocalRange(), PetscMapGetGlobalRange(), PetscMapSetBlockSize(), PetscMapGetBlockSize(), PetscMapInitialize()

 77: @*/
 80: PetscErrorCode  PetscMapSetUp(PetscMap *map)
 81: {
 82:   PetscMPIInt    rank,size;
 83:   PetscInt       p;

 87:   MPI_Comm_size(map->comm, &size);
 88:   MPI_Comm_rank(map->comm, &rank);
 89:   if (map->bs <=0) {SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"BlockSize not yet set");}
 90:   if (map->n > 0) map->n = map->n/map->bs;
 91:   if (map->N > 0) map->N = map->N/map->bs;
 92:   PetscSplitOwnership(map->comm,&map->n,&map->N);
 93:   map->n = map->n*map->bs;
 94:   map->N = map->N*map->bs;
 95:   if (!map->range) {
 96:     PetscMalloc((size+1)*sizeof(PetscInt), &map->range);
 97:   }
 98:   MPI_Allgather(&map->n, 1, MPIU_INT, map->range+1, 1, MPIU_INT, map->comm);

100:   map->range[0] = 0;
101:   for(p = 2; p <= size; p++) {
102:     map->range[p] += map->range[p-1];
103:   }

105:   map->rstart = map->range[rank];
106:   map->rend   = map->range[rank+1];
107:   return(0);
108: }

112: PetscErrorCode  PetscMapCopy(MPI_Comm comm,PetscMap *in,PetscMap *out)
113: {
114:   PetscMPIInt    size;
116:   PetscInt       *range = out->range;

119:   MPI_Comm_size(comm,&size);
120:   PetscMemcpy(out,in,sizeof(PetscMap));
121:   if (!range) {
122:     PetscMalloc((size+1)*sizeof(PetscInt),&out->range);
123:   } else {
124:     out->range = range;
125:   }
126:   PetscMemcpy(out->range,in->range,(size+1)*sizeof(PetscInt));
127:   return(0);
128: }

130: /*@C
131:      PetscMapSetLocalSize - Sets the local size for a PetscMap object.

133:     Collective on PetscMap

135:    Input Parameters:
136: +    map - pointer to the map
137: -    n - the local size

139:    Level: intermediate

141:     Notes:
142:        Call this after the call to PetscMapInitialize()

144:        Unlike regular PETSc objects you work with a pointer to the object instead of 
145:      the object directly.

147:     Fortran Notes: 
148:       Not available from Fortran

150: .seealso: PetscMapInitialize(), PetscMapSetSize(), PetscMapGetSize(), PetscMapGetLocalSize(), PetscMapSetUp()
151:           PetscMapGetLocalRange(), PetscMapGetGlobalRange(), PetscMapSetBlockSize(), PetscMapGetBlockSize()

153: @*/
156: PetscErrorCode  PetscMapSetLocalSize(PetscMap *map,PetscInt n)
157: {
159:   map->n = n;
160:   return(0);
161: }

163: /*@C
164:      PetscMapGetLocalSize - Gets the local size for a PetscMap object.

166:     Not Collective

168:    Input Parameters:
169: .    map - pointer to the map

171:    Output Parameters:
172: .    n - the local size

174:    Level: intermediate

176:     Notes:
177:        Call this after the call to PetscMapSetUp()

179:        Unlike regular PETSc objects you work with a pointer to the object instead of 
180:      the object directly.

182:     Fortran Notes: 
183:       Not available from Fortran

185: .seealso: PetscMapInitialize(), PetscMapSetSize(), PetscMapGetSize(), PetscMapGetLocalSize(), PetscMapSetUp()
186:           PetscMapGetLocalRange(), PetscMapGetGlobalRange(), PetscMapSetBlockSize(), PetscMapGetBlockSize()

188: @*/
191: PetscErrorCode  PetscMapGetLocalSize(PetscMap *map,PetscInt *n)
192: {
194:   *n = map->n;
195:   return(0);
196: }

198: /*@C
199:      PetscMapSetSize - Sets the global size for a PetscMap object.

201:     Collective on PetscMap

203:    Input Parameters:
204: +    map - pointer to the map
205: -    n - the global size

207:    Level: intermediate

209:     Notes:
210:        Call this after the call to PetscMapInitialize()

212:        Unlike regular PETSc objects you work with a pointer to the object instead of 
213:      the object directly.

215:     Fortran Notes: 
216:       Not available from Fortran

218: .seealso: PetscMapInitialize(), PetscMapSetLocalSize(), PetscMapGetLocalSize(), PetscMapGetSize(), PetscMapSetUp()
219:           PetscMapGetLocalRange(), PetscMapGetGlobalRange(), PetscMapSetBlockSize(), PetscMapGetBlockSize()

221: @*/
224: PetscErrorCode  PetscMapSetSize(PetscMap *map,PetscInt n)
225: {
227:   map->N = n;
228:   return(0);
229: }

231: /*@C
232:      PetscMapGetSize - Gets the global size for a PetscMap object.

234:     Not Collective

236:    Input Parameters:
237: .    map - pointer to the map

239:    Output Parameters:
240: .    n - the global size

242:    Level: intermediate

244:     Notes:
245:        Call this after the call to PetscMapSetUp()

247:        Unlike regular PETSc objects you work with a pointer to the object instead of 
248:      the object directly.

250:     Fortran Notes: 
251:       Not available from Fortran

253: .seealso: PetscMapInitialize(), PetscMapSetLocalSize(), PetscMapGetLocalSize(), PetscMapSetSize(), PetscMapSetUp()
254:           PetscMapGetLocalRange(), PetscMapGetGlobalRange(), PetscMapSetBlockSize(), PetscMapGetBlockSize()

256: @*/
259: PetscErrorCode  PetscMapGetSize(PetscMap *map,PetscInt *n)
260: {
262:   *n = map->N;
263:   return(0);
264: }

266: /*@C
267:      PetscMapSetBlockSize - Sets the block size for a PetscMap object.

269:     Collective on PetscMap

271:    Input Parameters:
272: +    map - pointer to the map
273: -    bs - the size

275:    Level: intermediate

277:     Notes:
278:        Call this after the call to PetscMapInitialize()

280:        Unlike regular PETSc objects you work with a pointer to the object instead of 
281:      the object directly.

283:     Fortran Notes: 
284:       Not available from Fortran

286: .seealso: PetscMapInitialize(), PetscMapSetLocalSize(), PetscMapGetLocalSize(), PetscMapGetBlockSize(),
287:           PetscMapGetLocalRange(), PetscMapGetGlobalRange(), PetscMapSetSize(), PetscMapGetSize(), PetscMapSetUp()

289: @*/
292: PetscErrorCode  PetscMapSetBlockSize(PetscMap *map,PetscInt bs)
293: {
295:   map->bs = bs;
296:   return(0);
297: }

299: /*@C
300:      PetscMapGetBlockSize - Gets the block size for a PetscMap object.

302:     Not Collective

304:    Input Parameters:
305: .    map - pointer to the map

307:    Output Parameters:
308: .    bs - the size

310:    Level: intermediate

312:     Notes:
313:        Call this after the call to PetscMapSetUp()

315:        Unlike regular PETSc objects you work with a pointer to the object instead of 
316:      the object directly.

318:     Fortran Notes: 
319:       Not available from Fortran

321: .seealso: PetscMapInitialize(), PetscMapSetLocalSize(), PetscMapGetLocalSize(), PetscMapSetSize(), PetscMapSetUp()
322:           PetscMapGetLocalRange(), PetscMapGetGlobalRange(), PetscMapSetBlockSize(), PetscMapGetSize()

324: @*/
327: PetscErrorCode  PetscMapGetBlockSize(PetscMap *map,PetscInt *bs)
328: {
330:   *bs = map->bs;
331:   return(0);
332: }


335: /*@C
336:      PetscMapGetLocalRange - gets the range of values owned by this process

338:     Not Collective

340:    Input Parameters:
341: .    map - pointer to the map

343:    Output Parameters:
344: +    rstart - first index owned by this process
345: -    rend - one more than the last index owned by this process

347:    Level: intermediate

349:     Notes:
350:        Call this after the call to PetscMapSetUp()

352:        Unlike regular PETSc objects you work with a pointer to the object instead of 
353:      the object directly.

355:     Fortran Notes: 
356:       Not available from Fortran

358: .seealso: PetscMapInitialize(), PetscMapSetLocalSize(), PetscMapGetLocalSize(), PetscMapSetSize(),
359:           PetscMapGetSize(), PetscMapGetGlobalRange(), PetscMapSetBlockSize(), PetscMapGetSize(), PetscMapSetUp()

361: @*/
364: PetscErrorCode  PetscMapGetLocalRange(PetscMap *map,PetscInt *rstart,PetscInt *rend)
365: {
367:   if (rstart) *rstart = map->rstart;
368:   if (rend)   *rend   = map->rend;
369:   return(0);
370: }

372: /*@C
373:      PetscMapGetGlobalRange - gets the range of values owned by all processes

375:     Not Collective

377:    Input Parameters:
378: .    map - pointer to the map

380:    Output Parameters:
381: .    range - start of each processors range of indices (the final entry is one more then the
382:              last index on the last process)

384:    Level: intermediate

386:     Notes:
387:        Call this after the call to PetscMapSetUp()

389:        Unlike regular PETSc objects you work with a pointer to the object instead of 
390:      the object directly.

392:     Fortran Notes: 
393:       Not available from Fortran

395: .seealso: PetscMapInitialize(), PetscMapSetLocalSize(), PetscMapGetLocalSize(), PetscMapSetSize(),
396:           PetscMapGetSize(), PetscMapGetLocalRange(), PetscMapSetBlockSize(), PetscMapGetSize(), PetscMapSetUp()

398: @*/
401: PetscErrorCode  PetscMapGetGlobalRange(PetscMap *map,const PetscInt *range[])
402: {
404:   *range = map->range;
405:   return(0);
406: }