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: developer

 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:       The PetscMap object and methods are intended to be used in the PETSc Vec and Mat implementions; it is 
 29:       recommended they not be used in user codes unless you really gain something in their use.

 31:     Fortran Notes: 
 32:       Not available from Fortran

 34: .seealso: PetscMapSetLocalSize(), PetscMapSetSize(), PetscMapGetSize(), PetscMapGetLocalSize(), PetscMap,
 35:           PetscMapGetRange(), PetscMapGetRanges(), PetscMapSetBlockSize(), PetscMapGetBlockSize(), PetscMapSetUp()

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

 53: /*@C
 54:      PetscMapDestroy - Frees a map object and frees its range if that exists. 

 56:     Collective on MPI_Comm

 58:    Input Parameters:
 59: .    map - the PetscMap

 61:    Level: developer

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

 66:       The PetscMap object and methods are intended to be used in the PETSc Vec and Mat implementions; it is 
 67:       recommended they not be used in user codes unless you really gain something in their use.

 69:     Fortran Notes: 
 70:       Not available from Fortran

 72: .seealso: PetscMapSetLocalSize(), PetscMapSetSize(), PetscMapGetSize(), PetscMapGetLocalSize(), PetscMap, PetscMapInitialize(),
 73:           PetscMapGetRange(), PetscMapGetRanges(), PetscMapSetBlockSize(), PetscMapGetBlockSize(), PetscMapSetUp()

 75: @*/
 78: PetscErrorCode  PetscMapDestroy(PetscMap *map)
 79: {

 83:   if (!map->refcnt--) {
 84:     if (map->range) {PetscFree(map->range);}
 85:     PetscFree(map);
 86:   }
 87:   return(0);
 88: }

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

 94:     Collective on MPI_Comm

 96:    Input Parameters:
 97: .    map - pointer to the map

 99:    Level: developer

101:     Notes: Typical calling sequence
102:        PetscMapInitialize(MPI_Comm,PetscMap *);
103:        PetscMapSetBlockSize(PetscMap*,1);
104:        PetscMapSetSize(PetscMap*,n) or PetscMapSetLocalSize(PetscMap*,N); or both
105:        PetscMapSetUp(PetscMap*);
106:        PetscMapGetSize(PetscMap*,PetscInt *);

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

111:        If the local size, global size are already set and range exists then this does nothing.

113:     Fortran Notes: 
114:       Not available from Fortran

116: .seealso: PetscMapSetLocalSize(), PetscMapSetSize(), PetscMapGetSize(), PetscMapGetLocalSize(), PetscMap,
117:           PetscMapGetRange(), PetscMapGetRanges(), PetscMapSetBlockSize(), PetscMapGetBlockSize(), PetscMapInitialize()

119: @*/
122: PetscErrorCode  PetscMapSetUp(PetscMap *map)
123: {
124:   PetscMPIInt    rank,size;
125:   PetscInt       p;

129:   if (map->bs <=0) {SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"BlockSize not yet set");}
130:   if ((map->n >= 0) && (map->N >= 0) && (map->range)) return(0);

132:   MPI_Comm_size(map->comm, &size);
133:   MPI_Comm_rank(map->comm, &rank);
134:   if (map->n > 0) map->n = map->n/map->bs;
135:   if (map->N > 0) map->N = map->N/map->bs;
136:   PetscSplitOwnership(map->comm,&map->n,&map->N);
137:   map->n = map->n*map->bs;
138:   map->N = map->N*map->bs;
139:   if (!map->range) {
140:     PetscMalloc((size+1)*sizeof(PetscInt), &map->range);
141:   }
142:   MPI_Allgather(&map->n, 1, MPIU_INT, map->range+1, 1, MPIU_INT, map->comm);

144:   map->range[0] = 0;
145:   for(p = 2; p <= size; p++) {
146:     map->range[p] += map->range[p-1];
147:   }

149:   map->rstart = map->range[rank];
150:   map->rend   = map->range[rank+1];
151:   return(0);
152: }

156: PetscErrorCode  PetscMapCopy(MPI_Comm comm,PetscMap *in,PetscMap *out)
157: {
158:   PetscMPIInt    size;
160:   PetscInt       *range = out->range; /* keep copy of this since PetscMemcpy() below will cover it */

163:   MPI_Comm_size(comm,&size);
164:   PetscMemcpy(out,in,sizeof(PetscMap));
165:   if (!range) {
166:     PetscMalloc((size+1)*sizeof(PetscInt),&out->range);
167:   } else {
168:     out->range = range;
169:   }
170:   out->refcnt = 0;
171:   PetscMemcpy(out->range,in->range,(size+1)*sizeof(PetscInt));
172:   return(0);
173: }

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

178:     Collective on PetscMap

180:    Input Parameters:
181: +    map - pointer to the map
182: -    n - the local size

184:    Level: developer

186:     Notes:
187:        Call this after the call to PetscMapInitialize()

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

192:     Fortran Notes: 
193:       Not available from Fortran

195: .seealso: PetscMapInitialize(), PetscMapSetSize(), PetscMapGetSize(), PetscMapGetLocalSize(), PetscMapSetUp()
196:           PetscMapGetRange(), PetscMapGetRanges(), PetscMapSetBlockSize(), PetscMapGetBlockSize()

198: @*/
201: PetscErrorCode  PetscMapSetLocalSize(PetscMap *map,PetscInt n)
202: {
204:   map->n = n;
205:   return(0);
206: }

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

211:     Not Collective

213:    Input Parameters:
214: .    map - pointer to the map

216:    Output Parameters:
217: .    n - the local size

219:    Level: developer

221:     Notes:
222:        Call this after the call to PetscMapSetUp()

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

227:     Fortran Notes: 
228:       Not available from Fortran

230: .seealso: PetscMapInitialize(), PetscMapSetSize(), PetscMapGetSize(), PetscMapGetLocalSize(), PetscMapSetUp()
231:           PetscMapGetRange(), PetscMapGetRanges(), PetscMapSetBlockSize(), PetscMapGetBlockSize()

233: @*/
236: PetscErrorCode  PetscMapGetLocalSize(PetscMap *map,PetscInt *n)
237: {
239:   *n = map->n;
240:   return(0);
241: }

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

246:     Collective on PetscMap

248:    Input Parameters:
249: +    map - pointer to the map
250: -    n - the global size

252:    Level: developer

254:     Notes:
255:        Call this after the call to PetscMapInitialize()

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

260:     Fortran Notes: 
261:       Not available from Fortran

263: .seealso: PetscMapInitialize(), PetscMapSetLocalSize(), PetscMapGetLocalSize(), PetscMapGetSize(), PetscMapSetUp()
264:           PetscMapGetRange(), PetscMapGetRanges(), PetscMapSetBlockSize(), PetscMapGetBlockSize()

266: @*/
269: PetscErrorCode  PetscMapSetSize(PetscMap *map,PetscInt n)
270: {
272:   map->N = n;
273:   return(0);
274: }

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

279:     Not Collective

281:    Input Parameters:
282: .    map - pointer to the map

284:    Output Parameters:
285: .    n - the global size

287:    Level: developer

289:     Notes:
290:        Call this after the call to PetscMapSetUp()

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

295:     Fortran Notes: 
296:       Not available from Fortran

298: .seealso: PetscMapInitialize(), PetscMapSetLocalSize(), PetscMapGetLocalSize(), PetscMapSetSize(), PetscMapSetUp()
299:           PetscMapGetRange(), PetscMapGetRanges(), PetscMapSetBlockSize(), PetscMapGetBlockSize()

301: @*/
304: PetscErrorCode  PetscMapGetSize(PetscMap *map,PetscInt *n)
305: {
307:   *n = map->N;
308:   return(0);
309: }

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

314:     Collective on PetscMap

316:    Input Parameters:
317: +    map - pointer to the map
318: -    bs - the size

320:    Level: developer

322:     Notes:
323:        Call this after the call to PetscMapInitialize()

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

328:     Fortran Notes: 
329:       Not available from Fortran

331: .seealso: PetscMapInitialize(), PetscMapSetLocalSize(), PetscMapGetLocalSize(), PetscMapGetBlockSize(),
332:           PetscMapGetRange(), PetscMapGetRanges(), PetscMapSetSize(), PetscMapGetSize(), PetscMapSetUp()

334: @*/
337: PetscErrorCode  PetscMapSetBlockSize(PetscMap *map,PetscInt bs)
338: {
340:   map->bs = bs;
341:   return(0);
342: }

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

347:     Not Collective

349:    Input Parameters:
350: .    map - pointer to the map

352:    Output Parameters:
353: .    bs - the size

355:    Level: developer

357:     Notes:
358:        Call this after the call to PetscMapSetUp()

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

363:     Fortran Notes: 
364:       Not available from Fortran

366: .seealso: PetscMapInitialize(), PetscMapSetLocalSize(), PetscMapGetLocalSize(), PetscMapSetSize(), PetscMapSetUp()
367:           PetscMapGetRange(), PetscMapGetRanges(), PetscMapSetBlockSize(), PetscMapGetSize()

369: @*/
372: PetscErrorCode  PetscMapGetBlockSize(PetscMap *map,PetscInt *bs)
373: {
375:   *bs = map->bs;
376:   return(0);
377: }


380: /*@C
381:      PetscMapGetRange - gets the range of values owned by this process

383:     Not Collective

385:    Input Parameters:
386: .    map - pointer to the map

388:    Output Parameters:
389: +    rstart - first index owned by this process
390: -    rend - one more than the last index owned by this process

392:    Level: developer

394:     Notes:
395:        Call this after the call to PetscMapSetUp()

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

400:     Fortran Notes: 
401:       Not available from Fortran

403: .seealso: PetscMapInitialize(), PetscMapSetLocalSize(), PetscMapGetLocalSize(), PetscMapSetSize(),
404:           PetscMapGetSize(), PetscMapGetRanges(), PetscMapSetBlockSize(), PetscMapGetSize(), PetscMapSetUp()

406: @*/
409: PetscErrorCode  PetscMapGetRange(PetscMap *map,PetscInt *rstart,PetscInt *rend)
410: {
412:   if (rstart) *rstart = map->rstart;
413:   if (rend)   *rend   = map->rend;
414:   return(0);
415: }

417: /*@C
418:      PetscMapGetRanges - gets the range of values owned by all processes

420:     Not Collective

422:    Input Parameters:
423: .    map - pointer to the map

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

429:    Level: developer

431:     Notes:
432:        Call this after the call to PetscMapSetUp()

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

437:     Fortran Notes: 
438:       Not available from Fortran

440: .seealso: PetscMapInitialize(), PetscMapSetLocalSize(), PetscMapGetLocalSize(), PetscMapSetSize(),
441:           PetscMapGetSize(), PetscMapGetRange(), PetscMapSetBlockSize(), PetscMapGetSize(), PetscMapSetUp()

443: @*/
446: PetscErrorCode  PetscMapGetRanges(PetscMap *map,const PetscInt *range[])
447: {
449:   *range = map->range;
450:   return(0);
451: }