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
  9: /*@C
 10:      PetscLayoutCreate - Allocates PetscLayout space and sets the map contents to the default.

 12:     Collective on MPI_Comm

 14:    Input Parameters:
 15: +    comm - the MPI communicator
 16: -    map - pointer to the map

 18:    Level: developer

 20:     Notes: Typical calling sequence
 21:        PetscLayoutCreate(MPI_Comm,PetscLayout *);
 22:        PetscLayoutSetBlockSize(PetscLayout,1);
 23:        PetscLayoutSetSize(PetscLayout,n) or PetscLayoutSetLocalSize(PetscLayout,N);
 24:        PetscLayoutSetUp(PetscLayout);
 25:        PetscLayoutGetSize(PetscLayout,PetscInt *);
 26:        PetscLayoutDestroy(PetscLayout);

 28:       The PetscLayout 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: PetscLayoutSetLocalSize(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayout, PetscLayoutDestroy(),
 35:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize(), PetscLayoutSetUp()

 37: @*/
 38: PetscErrorCode  PetscLayoutCreate(MPI_Comm comm,PetscLayout *map)
 39: {

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

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

 57:     Collective on MPI_Comm

 59:    Input Parameters:
 60: .    map - the PetscLayout

 62:    Level: developer

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

 67:     Fortran Notes: 
 68:       Not available from Fortran

 70: .seealso: PetscLayoutSetLocalSize(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayout, PetscLayoutCreate(),
 71:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize(), PetscLayoutSetUp()

 73: @*/
 76: PetscErrorCode  PetscLayoutDestroy(PetscLayout map)
 77: {

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

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

 92:     Collective on MPI_Comm

 94:    Input Parameters:
 95: .    map - pointer to the map

 97:    Level: developer

 99:     Notes: Typical calling sequence
100:        PetscLayoutCreate(MPI_Comm,PetscLayout *);
101:        PetscLayoutSetBlockSize(PetscLayout,1);
102:        PetscLayoutSetSize(PetscLayout,n) or PetscLayoutSetLocalSize(PetscLayout,N); or both
103:        PetscLayoutSetUp(PetscLayout);
104:        PetscLayoutGetSize(PetscLayout,PetscInt *);


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

109:     Fortran Notes: 
110:       Not available from Fortran

112: .seealso: PetscLayoutSetLocalSize(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayout, PetscLayoutDestroy(),
113:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize(), PetscLayoutCreate()

115: @*/
118: PetscErrorCode  PetscLayoutSetUp(PetscLayout map)
119: {
120:   PetscMPIInt    rank,size;
121:   PetscInt       p;

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

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

140:   map->range[0] = 0;
141:   for(p = 2; p <= size; p++) {
142:     map->range[p] += map->range[p-1];
143:   }

145:   map->rstart = map->range[rank];
146:   map->rend   = map->range[rank+1];
147:   return(0);
148: }

152: /*@C

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

156:      Collective on PetscLayout

158:     Input Parameter:
159: .     in - input PetscLayout to be copied

161:     Output Parameter:
162: .     out - the copy

164:    Level: developer

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

168:     Developer Note: Unlike all other copy routines this destroys any input object and makes a new one. This routine should be fixed to have a PetscLayoutDuplicate() 
169:       that ONLY creates a new one and a PetscLayoutCopy() that truely copies the data and does not delete the old object.

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

173: @*/
174: PetscErrorCode  PetscLayoutCopy(PetscLayout in,PetscLayout *out)
175: {
176:   PetscMPIInt    size;
178:   MPI_Comm       comm = in->comm;

181:   if (*out) {PetscLayoutDestroy(*out);}
182:   PetscLayoutCreate(comm,out);
183:   MPI_Comm_size(comm,&size);
184:   PetscMemcpy(*out,in,sizeof(struct _p_PetscLayout));
185:   PetscMalloc((size+1)*sizeof(PetscInt),&(*out)->range);
186:   PetscMemcpy((*out)->range,in->range,(size+1)*sizeof(PetscInt));
187:   (*out)->refcnt = 0;
188:   return(0);
189: }

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

194:     Collective on PetscLayout

196:    Input Parameters:
197: +    map - pointer to the map
198: -    n - the local size

200:    Level: developer

202:     Notes:
203:        Call this after the call to PetscLayoutCreate()

205:     Fortran Notes: 
206:       Not available from Fortran

208: .seealso: PetscLayoutCreate(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayoutSetUp()
209:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()

211: @*/
214: PetscErrorCode  PetscLayoutSetLocalSize(PetscLayout map,PetscInt n)
215: {
217:   map->n = n;
218:   return(0);
219: }

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

224:     Not Collective

226:    Input Parameters:
227: .    map - pointer to the map

229:    Output Parameters:
230: .    n - the local size

232:    Level: developer

234:     Notes:
235:        Call this after the call to PetscLayoutSetUp()

237:     Fortran Notes: 
238:       Not available from Fortran

240: .seealso: PetscLayoutCreate(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayoutSetUp()
241:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()

243: @*/
246: PetscErrorCode  PetscLayoutGetLocalSize(PetscLayout map,PetscInt *n)
247: {
249:   *n = map->n;
250:   return(0);
251: }

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

256:     Collective on PetscLayout

258:    Input Parameters:
259: +    map - pointer to the map
260: -    n - the global size

262:    Level: developer

264:     Notes:
265:        Call this after the call to PetscLayoutCreate()

267:     Fortran Notes: 
268:       Not available from Fortran

270: .seealso: PetscLayoutCreate(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutGetSize(), PetscLayoutSetUp()
271:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()

273: @*/
276: PetscErrorCode  PetscLayoutSetSize(PetscLayout map,PetscInt n)
277: {
279:   map->N = n;
280:   return(0);
281: }

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

286:     Not Collective

288:    Input Parameters:
289: .    map - pointer to the map

291:    Output Parameters:
292: .    n - the global size

294:    Level: developer

296:     Notes:
297:        Call this after the call to PetscLayoutSetUp()

299:     Fortran Notes: 
300:       Not available from Fortran

302: .seealso: PetscLayoutCreate(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(), PetscLayoutSetUp()
303:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()

305: @*/
308: PetscErrorCode  PetscLayoutGetSize(PetscLayout map,PetscInt *n)
309: {
311:   *n = map->N;
312:   return(0);
313: }

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

318:     Collective on PetscLayout

320:    Input Parameters:
321: +    map - pointer to the map
322: -    bs - the size

324:    Level: developer

326:     Notes:
327:        Call this after the call to PetscLayoutCreate()

329:     Fortran Notes: 
330:       Not available from Fortran

332: .seealso: PetscLayoutCreate(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutGetBlockSize(),
333:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutSetUp()

335: @*/
338: PetscErrorCode  PetscLayoutSetBlockSize(PetscLayout map,PetscInt bs)
339: {
341:   map->bs = bs;
342:   return(0);
343: }

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

348:     Not Collective

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

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

356:    Level: developer

358:     Notes:
359:        Call this after the call to PetscLayoutSetUp()

361:     Fortran Notes: 
362:       Not available from Fortran

364: .seealso: PetscLayoutCreate(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(), PetscLayoutSetUp()
365:           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetSize()

367: @*/
370: PetscErrorCode  PetscLayoutGetBlockSize(PetscLayout map,PetscInt *bs)
371: {
373:   *bs = map->bs;
374:   return(0);
375: }


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

381:     Not Collective

383:    Input Parameters:
384: .    map - pointer to the map

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

390:    Level: developer

392:     Notes:
393:        Call this after the call to PetscLayoutSetUp()

395:     Fortran Notes: 
396:       Not available from Fortran

398: .seealso: PetscLayoutCreate(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(),
399:           PetscLayoutGetSize(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetSize(), PetscLayoutSetUp()

401: @*/
404: PetscErrorCode  PetscLayoutGetRange(PetscLayout map,PetscInt *rstart,PetscInt *rend)
405: {
407:   if (rstart) *rstart = map->rstart;
408:   if (rend)   *rend   = map->rend;
409:   return(0);
410: }

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

415:     Not Collective

417:    Input Parameters:
418: .    map - pointer to the map

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

424:    Level: developer

426:     Notes:
427:        Call this after the call to PetscLayoutSetUp()

429:     Fortran Notes: 
430:       Not available from Fortran

432: .seealso: PetscLayoutCreate(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(),
433:           PetscLayoutGetSize(), PetscLayoutGetRange(), PetscLayoutSetBlockSize(), PetscLayoutGetSize(), PetscLayoutSetUp()

435: @*/
438: PetscErrorCode  PetscLayoutGetRanges(PetscLayout map,const PetscInt *range[])
439: {
441:   *range = map->range;
442:   return(0);
443: }