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: Notes: PetscLayoutSetUp() does not need to be called on the resulting PetscLayout
174: .seealso: PetscLayoutCreate(), PetscLayoutDestroy(), PetscLayoutSetUp()
176: @*/
177: PetscErrorCode PetscLayoutCopy(PetscLayout in,PetscLayout *out)
178: {
179: PetscMPIInt size;
181: MPI_Comm comm = in->comm;
184: if (*out) {PetscLayoutDestroy(*out);}
185: PetscLayoutCreate(comm,out);
186: MPI_Comm_size(comm,&size);
187: PetscMemcpy(*out,in,sizeof(struct _p_PetscLayout));
188: PetscMalloc((size+1)*sizeof(PetscInt),&(*out)->range);
189: PetscMemcpy((*out)->range,in->range,(size+1)*sizeof(PetscInt));
190: (*out)->refcnt = 0;
191: return(0);
192: }
194: /*@C
195: PetscLayoutSetLocalSize - Sets the local size for a PetscLayout object.
197: Collective on PetscLayout
199: Input Parameters:
200: + map - pointer to the map
201: - n - the local size
203: Level: developer
205: Notes:
206: Call this after the call to PetscLayoutInitialize()
208: Unlike regular PETSc objects you work with a pointer to the object instead of
209: the object directly.
211: Fortran Notes:
212: Not available from Fortran
214: .seealso: PetscLayoutInitialize(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayoutSetUp()
215: PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()
217: @*/
220: PetscErrorCode PetscLayoutSetLocalSize(PetscLayout map,PetscInt n)
221: {
223: map->n = n;
224: return(0);
225: }
227: /*@C
228: PetscLayoutGetLocalSize - Gets the local size for a PetscLayout object.
230: Not Collective
232: Input Parameters:
233: . map - pointer to the map
235: Output Parameters:
236: . n - the local size
238: Level: developer
240: Notes:
241: Call this after the call to PetscLayoutSetUp()
243: Unlike regular PETSc objects you work with a pointer to the object instead of
244: the object directly.
246: Fortran Notes:
247: Not available from Fortran
249: .seealso: PetscLayoutInitialize(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayoutSetUp()
250: PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()
252: @*/
255: PetscErrorCode PetscLayoutGetLocalSize(PetscLayout map,PetscInt *n)
256: {
258: *n = map->n;
259: return(0);
260: }
262: /*@C
263: PetscLayoutSetSize - Sets the global size for a PetscLayout object.
265: Collective on PetscLayout
267: Input Parameters:
268: + map - pointer to the map
269: - n - the global size
271: Level: developer
273: Notes:
274: Call this after the call to PetscLayoutInitialize()
276: Unlike regular PETSc objects you work with a pointer to the object instead of
277: the object directly.
279: Fortran Notes:
280: Not available from Fortran
282: .seealso: PetscLayoutInitialize(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutGetSize(), PetscLayoutSetUp()
283: PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()
285: @*/
288: PetscErrorCode PetscLayoutSetSize(PetscLayout map,PetscInt n)
289: {
291: map->N = n;
292: return(0);
293: }
295: /*@C
296: PetscLayoutGetSize - Gets the global size for a PetscLayout object.
298: Not Collective
300: Input Parameters:
301: . map - pointer to the map
303: Output Parameters:
304: . n - the global size
306: Level: developer
308: Notes:
309: Call this after the call to PetscLayoutSetUp()
311: Unlike regular PETSc objects you work with a pointer to the object instead of
312: the object directly.
314: Fortran Notes:
315: Not available from Fortran
317: .seealso: PetscLayoutInitialize(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(), PetscLayoutSetUp()
318: PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()
320: @*/
323: PetscErrorCode PetscLayoutGetSize(PetscLayout map,PetscInt *n)
324: {
326: *n = map->N;
327: return(0);
328: }
330: /*@C
331: PetscLayoutSetBlockSize - Sets the block size for a PetscLayout object.
333: Collective on PetscLayout
335: Input Parameters:
336: + map - pointer to the map
337: - bs - the size
339: Level: developer
341: Notes:
342: Call this after the call to PetscLayoutInitialize()
344: Unlike regular PETSc objects you work with a pointer to the object instead of
345: the object directly.
347: Fortran Notes:
348: Not available from Fortran
350: .seealso: PetscLayoutInitialize(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutGetBlockSize(),
351: PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutSetUp()
353: @*/
356: PetscErrorCode PetscLayoutSetBlockSize(PetscLayout map,PetscInt bs)
357: {
359: map->bs = bs;
360: return(0);
361: }
363: /*@C
364: PetscLayoutGetBlockSize - Gets the block size for a PetscLayout object.
366: Not Collective
368: Input Parameters:
369: . map - pointer to the map
371: Output Parameters:
372: . bs - the size
374: Level: developer
376: Notes:
377: Call this after the call to PetscLayoutSetUp()
379: Unlike regular PETSc objects you work with a pointer to the object instead of
380: the object directly.
382: Fortran Notes:
383: Not available from Fortran
385: .seealso: PetscLayoutInitialize(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(), PetscLayoutSetUp()
386: PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetSize()
388: @*/
391: PetscErrorCode PetscLayoutGetBlockSize(PetscLayout map,PetscInt *bs)
392: {
394: *bs = map->bs;
395: return(0);
396: }
399: /*@C
400: PetscLayoutGetRange - gets the range of values owned by this process
402: Not Collective
404: Input Parameters:
405: . map - pointer to the map
407: Output Parameters:
408: + rstart - first index owned by this process
409: - rend - one more than the last index owned by this process
411: Level: developer
413: Notes:
414: Call this after the call to PetscLayoutSetUp()
416: Unlike regular PETSc objects you work with a pointer to the object instead of
417: the object directly.
419: Fortran Notes:
420: Not available from Fortran
422: .seealso: PetscLayoutInitialize(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(),
423: PetscLayoutGetSize(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetSize(), PetscLayoutSetUp()
425: @*/
428: PetscErrorCode PetscLayoutGetRange(PetscLayout map,PetscInt *rstart,PetscInt *rend)
429: {
431: if (rstart) *rstart = map->rstart;
432: if (rend) *rend = map->rend;
433: return(0);
434: }
436: /*@C
437: PetscLayoutGetRanges - gets the range of values owned by all processes
439: Not Collective
441: Input Parameters:
442: . map - pointer to the map
444: Output Parameters:
445: . range - start of each processors range of indices (the final entry is one more then the
446: last index on the last process)
448: Level: developer
450: Notes:
451: Call this after the call to PetscLayoutSetUp()
453: Unlike regular PETSc objects you work with a pointer to the object instead of
454: the object directly.
456: Fortran Notes:
457: Not available from Fortran
459: .seealso: PetscLayoutInitialize(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(),
460: PetscLayoutGetSize(), PetscLayoutGetRange(), PetscLayoutSetBlockSize(), PetscLayoutGetSize(), PetscLayoutSetUp()
462: @*/
465: PetscErrorCode PetscLayoutGetRanges(PetscLayout map,const PetscInt *range[])
466: {
468: *range = map->range;
469: return(0);
470: }