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:      PetscLayoutCreate - Allocates PetscLayout space and 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:        PetscLayoutCreate(MPI_Comm,PetscLayout *);
 20:        PetscLayoutSetBlockSize(PetscLayout,1);
 21:        PetscLayoutSetSize(PetscLayout,n) or PetscLayoutSetLocalSize(PetscLayout,N);
 22:        PetscLayoutSetUp(PetscLayout);
 23:        PetscLayoutGetSize(PetscLayout,PetscInt *);
 24:        PetscLayoutDestroy(PetscLayout);

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

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

 32:     Fortran Notes: 
 33:       Not available from Fortran

 35: .seealso: PetscLayoutSetLocalSize(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayout, PetscLayoutDestroy(),
 36:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize(), PetscLayoutSetUp()

 38: @*/
 41: PetscErrorCode  PetscLayoutCreate(MPI_Comm comm,PetscLayout *map)
 42: {

 46:   PetscNew(struct _p_PetscLayout,map);
 47:   (*map)->comm   = comm;
 48:   (*map)->bs     = -1;
 49:   (*map)->n      = -1;
 50:   (*map)->N      = -1;
 51:   (*map)->range  = 0;
 52:   (*map)->rstart = 0;
 53:   (*map)->rend   = 0;
 54:   return(0);
 55: }

 57: /*@C
 58:      PetscLayoutDestroy - Frees a map object and frees its range if that exists. 

 60:     Collective on MPI_Comm

 62:    Input Parameters:
 63: .    map - the PetscLayout

 65:    Level: developer

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

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

 73:     Fortran Notes: 
 74:       Not available from Fortran

 76: .seealso: PetscLayoutSetLocalSize(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayout, PetscLayoutInitialize(),
 77:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize(), PetscLayoutSetUp()

 79: @*/
 82: PetscErrorCode  PetscLayoutDestroy(PetscLayout map)
 83: {

 87:   if (!map->refcnt--) {
 88:     if (map->range) {PetscFree(map->range);}
 89:     PetscFree(map);
 90:   }
 91:   return(0);
 92: }

 94: /*@C
 95:      PetscLayoutSetUp - given a map where you have set either the global or local
 96:            size sets up the map so that it may be used.

 98:     Collective on MPI_Comm

100:    Input Parameters:
101: .    map - pointer to the map

103:    Level: developer

105:     Notes: Typical calling sequence
106:        PetscLayoutInitialize(MPI_Comm,PetscLayout *);
107:        PetscLayoutSetBlockSize(PetscLayout,1);
108:        PetscLayoutSetSize(PetscLayout,n) or PetscLayoutSetLocalSize(PetscLayout,N); or both
109:        PetscLayoutSetUp(PetscLayout);
110:        PetscLayoutGetSize(PetscLayout,PetscInt *);

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

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

117:     Fortran Notes: 
118:       Not available from Fortran

120: .seealso: PetscLayoutSetLocalSize(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayout, PetscLayoutDestroy(),
121:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize(), PetscLayoutInitialize()

123: @*/
126: PetscErrorCode  PetscLayoutSetUp(PetscLayout map)
127: {
128:   PetscMPIInt    rank,size;
129:   PetscInt       p;

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

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

148:   map->range[0] = 0;
149:   for(p = 2; p <= size; p++) {
150:     map->range[p] += map->range[p-1];
151:   }

153:   map->rstart = map->range[rank];
154:   map->rend   = map->range[rank+1];
155:   return(0);
156: }

160: /*@C

162:     PetscLayoutCopy - creates a new PetscLayout with the same information as a given one. If the PetscLayout already exists it is destroyed first.

164:      Collective on PetscLayout

166:     Input Parameter:
167: .     in - input PetscLayout to be copied

169:     Output Parameter:
170: .     out - the copy

172:    Level: developer

174:     Notes: PetscLayoutSetUp() does not need to be called on the resulting PetscLayout

176: .seealso: PetscLayoutCreate(), PetscLayoutDestroy(), PetscLayoutSetUp()

178: @*/
179: PetscErrorCode  PetscLayoutCopy(PetscLayout in,PetscLayout *out)
180: {
181:   PetscMPIInt    size;
183:   MPI_Comm       comm = in->comm;

186:   if (*out) {PetscLayoutDestroy(*out);}
187:   PetscLayoutCreate(comm,out);
188:   MPI_Comm_size(comm,&size);
189:   PetscMemcpy(*out,in,sizeof(struct _p_PetscLayout));
190:   PetscMalloc((size+1)*sizeof(PetscInt),&(*out)->range);
191:   PetscMemcpy((*out)->range,in->range,(size+1)*sizeof(PetscInt));
192:   (*out)->refcnt = 0;
193:   return(0);
194: }

196: /*@C
197:      PetscLayoutSetLocalSize - Sets the local size for a PetscLayout object.

199:     Collective on PetscLayout

201:    Input Parameters:
202: +    map - pointer to the map
203: -    n - the local size

205:    Level: developer

207:     Notes:
208:        Call this after the call to PetscLayoutInitialize()

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

213:     Fortran Notes: 
214:       Not available from Fortran

216: .seealso: PetscLayoutInitialize(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayoutSetUp()
217:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()

219: @*/
222: PetscErrorCode  PetscLayoutSetLocalSize(PetscLayout map,PetscInt n)
223: {
225:   map->n = n;
226:   return(0);
227: }

229: /*@C
230:      PetscLayoutGetLocalSize - Gets the local size for a PetscLayout object.

232:     Not Collective

234:    Input Parameters:
235: .    map - pointer to the map

237:    Output Parameters:
238: .    n - the local size

240:    Level: developer

242:     Notes:
243:        Call this after the call to PetscLayoutSetUp()

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

248:     Fortran Notes: 
249:       Not available from Fortran

251: .seealso: PetscLayoutInitialize(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayoutSetUp()
252:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()

254: @*/
257: PetscErrorCode  PetscLayoutGetLocalSize(PetscLayout map,PetscInt *n)
258: {
260:   *n = map->n;
261:   return(0);
262: }

264: /*@C
265:      PetscLayoutSetSize - Sets the global size for a PetscLayout object.

267:     Collective on PetscLayout

269:    Input Parameters:
270: +    map - pointer to the map
271: -    n - the global size

273:    Level: developer

275:     Notes:
276:        Call this after the call to PetscLayoutInitialize()

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

281:     Fortran Notes: 
282:       Not available from Fortran

284: .seealso: PetscLayoutInitialize(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutGetSize(), PetscLayoutSetUp()
285:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()

287: @*/
290: PetscErrorCode  PetscLayoutSetSize(PetscLayout map,PetscInt n)
291: {
293:   map->N = n;
294:   return(0);
295: }

297: /*@C
298:      PetscLayoutGetSize - Gets the global size for a PetscLayout object.

300:     Not Collective

302:    Input Parameters:
303: .    map - pointer to the map

305:    Output Parameters:
306: .    n - the global size

308:    Level: developer

310:     Notes:
311:        Call this after the call to PetscLayoutSetUp()

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

316:     Fortran Notes: 
317:       Not available from Fortran

319: .seealso: PetscLayoutInitialize(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(), PetscLayoutSetUp()
320:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()

322: @*/
325: PetscErrorCode  PetscLayoutGetSize(PetscLayout map,PetscInt *n)
326: {
328:   *n = map->N;
329:   return(0);
330: }

332: /*@C
333:      PetscLayoutSetBlockSize - Sets the block size for a PetscLayout object.

335:     Collective on PetscLayout

337:    Input Parameters:
338: +    map - pointer to the map
339: -    bs - the size

341:    Level: developer

343:     Notes:
344:        Call this after the call to PetscLayoutInitialize()

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

349:     Fortran Notes: 
350:       Not available from Fortran

352: .seealso: PetscLayoutInitialize(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutGetBlockSize(),
353:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutSetUp()

355: @*/
358: PetscErrorCode  PetscLayoutSetBlockSize(PetscLayout map,PetscInt bs)
359: {
361:   map->bs = bs;
362:   return(0);
363: }

365: /*@C
366:      PetscLayoutGetBlockSize - Gets the block size for a PetscLayout object.

368:     Not Collective

370:    Input Parameters:
371: .    map - pointer to the map

373:    Output Parameters:
374: .    bs - the size

376:    Level: developer

378:     Notes:
379:        Call this after the call to PetscLayoutSetUp()

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

384:     Fortran Notes: 
385:       Not available from Fortran

387: .seealso: PetscLayoutInitialize(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(), PetscLayoutSetUp()
388:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetSize()

390: @*/
393: PetscErrorCode  PetscLayoutGetBlockSize(PetscLayout map,PetscInt *bs)
394: {
396:   *bs = map->bs;
397:   return(0);
398: }


401: /*@C
402:      PetscLayoutGetRange - gets the range of values owned by this process

404:     Not Collective

406:    Input Parameters:
407: .    map - pointer to the map

409:    Output Parameters:
410: +    rstart - first index owned by this process
411: -    rend - one more than the last index owned by this process

413:    Level: developer

415:     Notes:
416:        Call this after the call to PetscLayoutSetUp()

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

421:     Fortran Notes: 
422:       Not available from Fortran

424: .seealso: PetscLayoutInitialize(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(),
425:           PetscLayoutGetSize(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetSize(), PetscLayoutSetUp()

427: @*/
430: PetscErrorCode  PetscLayoutGetRange(PetscLayout map,PetscInt *rstart,PetscInt *rend)
431: {
433:   if (rstart) *rstart = map->rstart;
434:   if (rend)   *rend   = map->rend;
435:   return(0);
436: }

438: /*@C
439:      PetscLayoutGetRanges - gets the range of values owned by all processes

441:     Not Collective

443:    Input Parameters:
444: .    map - pointer to the map

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

450:    Level: developer

452:     Notes:
453:        Call this after the call to PetscLayoutSetUp()

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

458:     Fortran Notes: 
459:       Not available from Fortran

461: .seealso: PetscLayoutInitialize(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(),
462:           PetscLayoutGetSize(), PetscLayoutGetRange(), PetscLayoutSetBlockSize(), PetscLayoutGetSize(), PetscLayoutSetUp()

464: @*/
467: PetscErrorCode  PetscLayoutGetRanges(PetscLayout map,const PetscInt *range[])
468: {
470:   *range = map->range;
471:   return(0);
472: }