xref: /petsc/src/vec/is/utils/pmap.c (revision d71ae5a4db6382e7f06317b8d368875286fe9008)
169ce434fSBarry Smith 
269ce434fSBarry Smith /*
369ce434fSBarry Smith    This file contains routines for basic map object implementation.
469ce434fSBarry Smith */
569ce434fSBarry Smith 
657e2745dSVaclav Hapla #include <petsc/private/isimpl.h> /*I "petscis.h" I*/
75c25fcd7SBarry Smith 
8c3002683SMatthew G. Knepley /*@
9a8643c1eSVaclav Hapla   PetscLayoutCreate - Allocates PetscLayout space and sets the PetscLayout contents to the default.
1069ce434fSBarry Smith 
11d083f849SBarry Smith   Collective
1269ce434fSBarry Smith 
1369ce434fSBarry Smith   Input Parameters:
14a8643c1eSVaclav Hapla . comm - the MPI communicator
15a8643c1eSVaclav Hapla 
16a8643c1eSVaclav Hapla   Output Parameters:
17a8643c1eSVaclav Hapla . map - the new PetscLayout
1869ce434fSBarry Smith 
19456fcb79SJed Brown   Level: advanced
2069ce434fSBarry Smith 
21456fcb79SJed Brown   Notes:
22456fcb79SJed Brown   Typical calling sequence
23456fcb79SJed Brown .vb
2469ce434fSBarry Smith        PetscLayoutCreate(MPI_Comm,PetscLayout *);
25a8643c1eSVaclav Hapla        PetscLayoutSetBlockSize(PetscLayout,bs);
26a8643c1eSVaclav Hapla        PetscLayoutSetSize(PetscLayout,N); // or PetscLayoutSetLocalSize(PetscLayout,n);
2769ce434fSBarry Smith        PetscLayoutSetUp(PetscLayout);
28456fcb79SJed Brown .ve
299621ec18SVaclav Hapla   Alternatively,
30147403d9SBarry Smith .vb
31147403d9SBarry Smith       PetscLayoutCreateFromSizes(comm,n,N,bs,&layout);
32147403d9SBarry Smith .ve
339621ec18SVaclav Hapla 
34147403d9SBarry Smith   Optionally use any of the following
35147403d9SBarry Smith .vb
36147403d9SBarry Smith   PetscLayoutGetSize(PetscLayout,PetscInt *);
37147403d9SBarry Smith   PetscLayoutGetLocalSize(PetscLayout,PetscInt *);
38147403d9SBarry Smith   PetscLayoutGetRange(PetscLayout,PetscInt *rstart,PetscInt *rend);
39147403d9SBarry Smith   PetscLayoutGetRanges(PetscLayout,const PetscInt *range[]);
40147403d9SBarry Smith   PetscLayoutDestroy(PetscLayout*);
41147403d9SBarry Smith .ve
4269ce434fSBarry Smith 
436aad120cSJose E. Roman   The PetscLayout object and methods are intended to be used in the PETSc Vec and Mat implementations; it is often not needed in
4469ce434fSBarry Smith   user codes unless you really gain something in their use.
4569ce434fSBarry Smith 
46db781477SPatrick Sanan .seealso: `PetscLayoutSetLocalSize()`, `PetscLayoutSetSize()`, `PetscLayoutGetSize()`, `PetscLayoutGetLocalSize()`, `PetscLayout`, `PetscLayoutDestroy()`,
47db781477SPatrick Sanan           `PetscLayoutGetRange()`, `PetscLayoutGetRanges()`, `PetscLayoutSetBlockSize()`, `PetscLayoutGetBlockSize()`, `PetscLayoutSetUp()`,
48db781477SPatrick Sanan           `PetscLayoutCreateFromSizes()`
4969ce434fSBarry Smith 
5069ce434fSBarry Smith @*/
51*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutCreate(MPI_Comm comm, PetscLayout *map)
52*d71ae5a4SJacob Faibussowitsch {
5369ce434fSBarry Smith   PetscFunctionBegin;
549566063dSJacob Faibussowitsch   PetscCall(PetscNew(map));
559566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &(*map)->size));
5669ce434fSBarry Smith   (*map)->comm        = comm;
5769ce434fSBarry Smith   (*map)->bs          = -1;
5869ce434fSBarry Smith   (*map)->n           = -1;
5969ce434fSBarry Smith   (*map)->N           = -1;
60f92d6284SStefano Zampini   (*map)->range       = NULL;
619621ec18SVaclav Hapla   (*map)->range_alloc = PETSC_TRUE;
6269ce434fSBarry Smith   (*map)->rstart      = 0;
6369ce434fSBarry Smith   (*map)->rend        = 0;
64ca5434daSLawrence Mitchell   (*map)->setupcalled = PETSC_FALSE;
65ca5434daSLawrence Mitchell   (*map)->oldn        = -1;
66ca5434daSLawrence Mitchell   (*map)->oldN        = -1;
67ca5434daSLawrence Mitchell   (*map)->oldbs       = -1;
6869ce434fSBarry Smith   PetscFunctionReturn(0);
6969ce434fSBarry Smith }
7069ce434fSBarry Smith 
71c3002683SMatthew G. Knepley /*@
729621ec18SVaclav Hapla   PetscLayoutCreateFromSizes - Allocates PetscLayout space, sets the layout sizes, and sets the layout up.
739621ec18SVaclav Hapla 
749621ec18SVaclav Hapla   Collective
759621ec18SVaclav Hapla 
769621ec18SVaclav Hapla   Input Parameters:
779621ec18SVaclav Hapla + comm  - the MPI communicator
789621ec18SVaclav Hapla . n     - the local size (or PETSC_DECIDE)
799621ec18SVaclav Hapla . N     - the global size (or PETSC_DECIDE)
80f0fc11ceSJed Brown - bs    - the block size (or PETSC_DECIDE)
819621ec18SVaclav Hapla 
829621ec18SVaclav Hapla   Output Parameters:
839621ec18SVaclav Hapla . map - the new PetscLayout
849621ec18SVaclav Hapla 
859621ec18SVaclav Hapla   Level: advanced
869621ec18SVaclav Hapla 
879621ec18SVaclav Hapla   Notes:
889621ec18SVaclav Hapla $ PetscLayoutCreateFromSizes(comm,n,N,bs,&layout);
899621ec18SVaclav Hapla   is a shorthand for
909621ec18SVaclav Hapla .vb
919621ec18SVaclav Hapla   PetscLayoutCreate(comm,&layout);
929621ec18SVaclav Hapla   PetscLayoutSetLocalSize(layout,n);
939621ec18SVaclav Hapla   PetscLayoutSetSize(layout,N);
949621ec18SVaclav Hapla   PetscLayoutSetBlockSize(layout,bs);
959621ec18SVaclav Hapla   PetscLayoutSetUp(layout);
969621ec18SVaclav Hapla .ve
979621ec18SVaclav Hapla 
98db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutSetLocalSize()`, `PetscLayoutSetSize()`, `PetscLayoutGetSize()`, `PetscLayoutGetLocalSize()`, `PetscLayout`, `PetscLayoutDestroy()`,
99db781477SPatrick Sanan           `PetscLayoutGetRange()`, `PetscLayoutGetRanges()`, `PetscLayoutSetBlockSize()`, `PetscLayoutGetBlockSize()`, `PetscLayoutSetUp()`, `PetscLayoutCreateFromRanges()`
1009621ec18SVaclav Hapla 
1019621ec18SVaclav Hapla @*/
102*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutCreateFromSizes(MPI_Comm comm, PetscInt n, PetscInt N, PetscInt bs, PetscLayout *map)
103*d71ae5a4SJacob Faibussowitsch {
1049621ec18SVaclav Hapla   PetscFunctionBegin;
1059566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, map));
1069566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(*map, n));
1079566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(*map, N));
1089566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(*map, bs));
1099566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(*map));
1109621ec18SVaclav Hapla   PetscFunctionReturn(0);
1119621ec18SVaclav Hapla }
1129621ec18SVaclav Hapla 
1139621ec18SVaclav Hapla /*@
11469ce434fSBarry Smith   PetscLayoutDestroy - Frees a map object and frees its range if that exists.
11569ce434fSBarry Smith 
116d083f849SBarry Smith   Collective
11769ce434fSBarry Smith 
11869ce434fSBarry Smith   Input Parameters:
11969ce434fSBarry Smith . map - the PetscLayout
12069ce434fSBarry Smith 
12169ce434fSBarry Smith   Level: developer
12269ce434fSBarry Smith 
123c3002683SMatthew G. Knepley   Note:
1246aad120cSJose E. Roman   The PetscLayout object and methods are intended to be used in the PETSc Vec and Mat implementations; it is
12569ce434fSBarry Smith   recommended they not be used in user codes unless you really gain something in their use.
12669ce434fSBarry Smith 
127db781477SPatrick Sanan .seealso: `PetscLayoutSetLocalSize()`, `PetscLayoutSetSize()`, `PetscLayoutGetSize()`, `PetscLayoutGetLocalSize()`, `PetscLayout`, `PetscLayoutCreate()`,
128db781477SPatrick Sanan           `PetscLayoutGetRange()`, `PetscLayoutGetRanges()`, `PetscLayoutSetBlockSize()`, `PetscLayoutGetBlockSize()`, `PetscLayoutSetUp()`
12969ce434fSBarry Smith 
13069ce434fSBarry Smith @*/
131*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutDestroy(PetscLayout *map)
132*d71ae5a4SJacob Faibussowitsch {
13369ce434fSBarry Smith   PetscFunctionBegin;
13469ce434fSBarry Smith   if (!*map) PetscFunctionReturn(0);
13569ce434fSBarry Smith   if (!(*map)->refcnt--) {
1369566063dSJacob Faibussowitsch     if ((*map)->range_alloc) PetscCall(PetscFree((*map)->range));
1379566063dSJacob Faibussowitsch     PetscCall(ISLocalToGlobalMappingDestroy(&(*map)->mapping));
1389566063dSJacob Faibussowitsch     PetscCall(PetscFree((*map)));
13969ce434fSBarry Smith   }
14069ce434fSBarry Smith   *map = NULL;
14169ce434fSBarry Smith   PetscFunctionReturn(0);
14269ce434fSBarry Smith }
14369ce434fSBarry Smith 
1449621ec18SVaclav Hapla /*@
1459621ec18SVaclav Hapla   PetscLayoutCreateFromRanges - Creates a new PetscLayout with the given ownership ranges and sets it up.
1469621ec18SVaclav Hapla 
1479621ec18SVaclav Hapla   Collective
1489621ec18SVaclav Hapla 
1499621ec18SVaclav Hapla   Input Parameters:
1509621ec18SVaclav Hapla + comm  - the MPI communicator
1519621ec18SVaclav Hapla . range - the array of ownership ranges for each rank with length commsize+1
1529621ec18SVaclav Hapla . mode  - the copy mode for range
1539621ec18SVaclav Hapla - bs    - the block size (or PETSC_DECIDE)
1549621ec18SVaclav Hapla 
1559621ec18SVaclav Hapla   Output Parameters:
1569621ec18SVaclav Hapla . newmap - the new PetscLayout
1579621ec18SVaclav Hapla 
1589621ec18SVaclav Hapla   Level: developer
1599621ec18SVaclav Hapla 
160db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutSetLocalSize()`, `PetscLayoutSetSize()`, `PetscLayoutGetSize()`, `PetscLayoutGetLocalSize()`, `PetscLayout`, `PetscLayoutDestroy()`,
161db781477SPatrick Sanan           `PetscLayoutGetRange()`, `PetscLayoutGetRanges()`, `PetscLayoutSetBlockSize()`, `PetscLayoutGetBlockSize()`, `PetscLayoutSetUp()`, `PetscLayoutCreateFromSizes()`
1629621ec18SVaclav Hapla 
1639621ec18SVaclav Hapla @*/
164*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutCreateFromRanges(MPI_Comm comm, const PetscInt range[], PetscCopyMode mode, PetscInt bs, PetscLayout *newmap)
165*d71ae5a4SJacob Faibussowitsch {
1669621ec18SVaclav Hapla   PetscLayout map;
16738a25198SStefano Zampini   PetscMPIInt rank;
1687b659617SVaclav Hapla 
1697b659617SVaclav Hapla   PetscFunctionBegin;
1709566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
1719566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &map));
1729566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(map, bs));
1739621ec18SVaclav Hapla   switch (mode) {
1749621ec18SVaclav Hapla   case PETSC_COPY_VALUES:
1759566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(map->size + 1, &map->range));
1769566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(map->range, range, map->size + 1));
1779621ec18SVaclav Hapla     break;
178*d71ae5a4SJacob Faibussowitsch   case PETSC_USE_POINTER:
179*d71ae5a4SJacob Faibussowitsch     map->range_alloc = PETSC_FALSE;
180*d71ae5a4SJacob Faibussowitsch     break;
181*d71ae5a4SJacob Faibussowitsch   default:
182*d71ae5a4SJacob Faibussowitsch     map->range = (PetscInt *)range;
183*d71ae5a4SJacob Faibussowitsch     break;
1849621ec18SVaclav Hapla   }
1857b659617SVaclav Hapla   map->rstart = map->range[rank];
1867b659617SVaclav Hapla   map->rend   = map->range[rank + 1];
1877b659617SVaclav Hapla   map->n      = map->rend - map->rstart;
18838a25198SStefano Zampini   map->N      = map->range[map->size];
18976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) { /* just check that n, N and bs are consistent */
1907b659617SVaclav Hapla     PetscInt tmp;
1911c2dc1cbSBarry Smith     PetscCall(MPIU_Allreduce(&map->n, &tmp, 1, MPIU_INT, MPI_SUM, map->comm));
19208401ef6SPierre Jolivet     PetscCheck(tmp == map->N, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Sum of local lengths %" PetscInt_FMT " does not equal global length %" PetscInt_FMT ", my local length %" PetscInt_FMT ".\nThe provided PetscLayout is wrong.", tmp, map->N, map->n);
193ad540459SPierre Jolivet     if (map->bs > 1) PetscCheck(map->n % map->bs == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Local size %" PetscInt_FMT " must be divisible by blocksize %" PetscInt_FMT, map->n, map->bs);
194ad540459SPierre Jolivet     if (map->bs > 1) PetscCheck(map->N % map->bs == 0, map->comm, PETSC_ERR_PLIB, "Global size %" PetscInt_FMT " must be divisible by blocksize %" PetscInt_FMT, map->N, map->bs);
19576bd3646SJed Brown   }
196ca5434daSLawrence Mitchell   /* lock the layout */
197ca5434daSLawrence Mitchell   map->setupcalled = PETSC_TRUE;
198ca5434daSLawrence Mitchell   map->oldn        = map->n;
199ca5434daSLawrence Mitchell   map->oldN        = map->N;
200ca5434daSLawrence Mitchell   map->oldbs       = map->bs;
2019621ec18SVaclav Hapla   *newmap          = map;
2027b659617SVaclav Hapla   PetscFunctionReturn(0);
2037b659617SVaclav Hapla }
2047b659617SVaclav Hapla 
205c3002683SMatthew G. Knepley /*@
20669ce434fSBarry Smith   PetscLayoutSetUp - given a map where you have set either the global or local
20769ce434fSBarry Smith                      size sets up the map so that it may be used.
20869ce434fSBarry Smith 
209d083f849SBarry Smith   Collective
21069ce434fSBarry Smith 
21169ce434fSBarry Smith   Input Parameters:
21269ce434fSBarry Smith . map - pointer to the map
21369ce434fSBarry Smith 
21469ce434fSBarry Smith   Level: developer
21569ce434fSBarry Smith 
21695452b02SPatrick Sanan   Notes:
21795452b02SPatrick Sanan     Typical calling sequence
218c3002683SMatthew G. Knepley $ PetscLayoutCreate(MPI_Comm,PetscLayout *);
219c3002683SMatthew G. Knepley $ PetscLayoutSetBlockSize(PetscLayout,1);
220c3002683SMatthew G. Knepley $ PetscLayoutSetSize(PetscLayout,n) or PetscLayoutSetLocalSize(PetscLayout,N); or both
221c3002683SMatthew G. Knepley $ PetscLayoutSetUp(PetscLayout);
222c3002683SMatthew G. Knepley $ PetscLayoutGetSize(PetscLayout,PetscInt *);
22369ce434fSBarry Smith 
2247b659617SVaclav Hapla   If range exists, and local size is not set, everything gets computed from the range.
22569ce434fSBarry Smith 
22669ce434fSBarry Smith   If the local size, global size are already set and range exists then this does nothing.
22769ce434fSBarry Smith 
228db781477SPatrick Sanan .seealso: `PetscLayoutSetLocalSize()`, `PetscLayoutSetSize()`, `PetscLayoutGetSize()`, `PetscLayoutGetLocalSize()`, `PetscLayout`, `PetscLayoutDestroy()`,
229f1f2ae84SBarry Smith           `PetscLayoutGetRange()`, `PetscLayoutGetRanges()`, `PetscLayoutSetBlockSize()`, `PetscLayoutGetBlockSize()`, `PetscLayoutCreate()`, `PetscSplitOwnership()`
23069ce434fSBarry Smith @*/
231*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutSetUp(PetscLayout map)
232*d71ae5a4SJacob Faibussowitsch {
23338a25198SStefano Zampini   PetscMPIInt rank;
23469ce434fSBarry Smith   PetscInt    p;
23569ce434fSBarry Smith 
23669ce434fSBarry Smith   PetscFunctionBegin;
2379371c9d4SSatish Balay   PetscCheck(!map->setupcalled || !(map->n != map->oldn || map->N != map->oldN), map->comm, PETSC_ERR_ARG_WRONGSTATE, "Layout is already setup with (local=%" PetscInt_FMT ",global=%" PetscInt_FMT "), cannot call setup again with (local=%" PetscInt_FMT ",global=%" PetscInt_FMT ")",
2389371c9d4SSatish Balay              map->oldn, map->oldN, map->n, map->N);
239ca5434daSLawrence Mitchell   if (map->setupcalled) PetscFunctionReturn(0);
24069ce434fSBarry Smith 
241ad540459SPierre Jolivet   if (map->n > 0 && map->bs > 1) PetscCheck(map->n % map->bs == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Local size %" PetscInt_FMT " must be divisible by blocksize %" PetscInt_FMT, map->n, map->bs);
242ad540459SPierre Jolivet   if (map->N > 0 && map->bs > 1) PetscCheck(map->N % map->bs == 0, map->comm, PETSC_ERR_PLIB, "Global size %" PetscInt_FMT " must be divisible by blocksize %" PetscInt_FMT, map->N, map->bs);
243b146b01cSBarry Smith 
2449566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(map->comm, &rank));
24533d57670SJed Brown   if (map->n > 0) map->n = map->n / PetscAbs(map->bs);
24633d57670SJed Brown   if (map->N > 0) map->N = map->N / PetscAbs(map->bs);
2479566063dSJacob Faibussowitsch   PetscCall(PetscSplitOwnership(map->comm, &map->n, &map->N));
24833d57670SJed Brown   map->n = map->n * PetscAbs(map->bs);
24933d57670SJed Brown   map->N = map->N * PetscAbs(map->bs);
25048a46eb9SPierre Jolivet   if (!map->range) PetscCall(PetscMalloc1(map->size + 1, &map->range));
2519566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allgather(&map->n, 1, MPIU_INT, map->range + 1, 1, MPIU_INT, map->comm));
25269ce434fSBarry Smith 
25369ce434fSBarry Smith   map->range[0] = 0;
25438a25198SStefano Zampini   for (p = 2; p <= map->size; p++) map->range[p] += map->range[p - 1];
25569ce434fSBarry Smith 
25669ce434fSBarry Smith   map->rstart = map->range[rank];
25769ce434fSBarry Smith   map->rend   = map->range[rank + 1];
258ca5434daSLawrence Mitchell 
259ca5434daSLawrence Mitchell   /* lock the layout */
260ca5434daSLawrence Mitchell   map->setupcalled = PETSC_TRUE;
261ca5434daSLawrence Mitchell   map->oldn        = map->n;
262ca5434daSLawrence Mitchell   map->oldN        = map->N;
263ca5434daSLawrence Mitchell   map->oldbs       = map->bs;
26469ce434fSBarry Smith   PetscFunctionReturn(0);
26569ce434fSBarry Smith }
26669ce434fSBarry Smith 
267c3002683SMatthew G. Knepley /*@
26869ce434fSBarry Smith   PetscLayoutDuplicate - creates a new PetscLayout with the same information as a given one. If the PetscLayout already exists it is destroyed first.
26969ce434fSBarry Smith 
27069ce434fSBarry Smith   Collective on PetscLayout
27169ce434fSBarry Smith 
27269ce434fSBarry Smith   Input Parameter:
27369ce434fSBarry Smith . in - input PetscLayout to be duplicated
27469ce434fSBarry Smith 
27569ce434fSBarry Smith   Output Parameter:
27669ce434fSBarry Smith . out - the copy
27769ce434fSBarry Smith 
27869ce434fSBarry Smith   Level: developer
27969ce434fSBarry Smith 
28095452b02SPatrick Sanan   Notes:
28195452b02SPatrick Sanan     PetscLayoutSetUp() does not need to be called on the resulting PetscLayout
28269ce434fSBarry Smith 
283db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutDestroy()`, `PetscLayoutSetUp()`, `PetscLayoutReference()`
28469ce434fSBarry Smith @*/
285*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutDuplicate(PetscLayout in, PetscLayout *out)
286*d71ae5a4SJacob Faibussowitsch {
28769ce434fSBarry Smith   MPI_Comm comm = in->comm;
28869ce434fSBarry Smith 
28969ce434fSBarry Smith   PetscFunctionBegin;
2909566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(out));
2919566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, out));
2929566063dSJacob Faibussowitsch   PetscCall(PetscMemcpy(*out, in, sizeof(struct _n_PetscLayout)));
293c168f6d8SVaclav Hapla   if (in->range) {
2949566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1((*out)->size + 1, &(*out)->range));
2959566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy((*out)->range, in->range, (*out)->size + 1));
296c168f6d8SVaclav Hapla   }
29769ce434fSBarry Smith   (*out)->refcnt = 0;
29869ce434fSBarry Smith   PetscFunctionReturn(0);
29969ce434fSBarry Smith }
30069ce434fSBarry Smith 
301c3002683SMatthew G. Knepley /*@
30269ce434fSBarry Smith   PetscLayoutReference - Causes a PETSc Vec or Mat to share a PetscLayout with one that already exists. Used by Vec/MatDuplicate_XXX()
30369ce434fSBarry Smith 
30469ce434fSBarry Smith   Collective on PetscLayout
30569ce434fSBarry Smith 
30669ce434fSBarry Smith   Input Parameter:
30769ce434fSBarry Smith . in - input PetscLayout to be copied
30869ce434fSBarry Smith 
30969ce434fSBarry Smith   Output Parameter:
31069ce434fSBarry Smith . out - the reference location
31169ce434fSBarry Smith 
31269ce434fSBarry Smith   Level: developer
31369ce434fSBarry Smith 
31495452b02SPatrick Sanan   Notes:
31595452b02SPatrick Sanan     PetscLayoutSetUp() does not need to be called on the resulting PetscLayout
31669ce434fSBarry Smith 
31769ce434fSBarry Smith   If the out location already contains a PetscLayout it is destroyed
31869ce434fSBarry Smith 
319db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutDestroy()`, `PetscLayoutSetUp()`, `PetscLayoutDuplicate()`
32069ce434fSBarry Smith @*/
321*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutReference(PetscLayout in, PetscLayout *out)
322*d71ae5a4SJacob Faibussowitsch {
32369ce434fSBarry Smith   PetscFunctionBegin;
32469ce434fSBarry Smith   in->refcnt++;
3259566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(out));
32669ce434fSBarry Smith   *out = in;
32769ce434fSBarry Smith   PetscFunctionReturn(0);
32869ce434fSBarry Smith }
32969ce434fSBarry Smith 
330c3002683SMatthew G. Knepley /*@
33169ce434fSBarry Smith   PetscLayoutSetISLocalToGlobalMapping - sets a ISLocalGlobalMapping into a PetscLayout
33269ce434fSBarry Smith 
33369ce434fSBarry Smith   Collective on PetscLayout
33469ce434fSBarry Smith 
335d8d19677SJose E. Roman   Input Parameters:
33669ce434fSBarry Smith + in - input PetscLayout
33769ce434fSBarry Smith - ltog - the local to global mapping
33869ce434fSBarry Smith 
33969ce434fSBarry Smith   Level: developer
34069ce434fSBarry Smith 
34195452b02SPatrick Sanan   Notes:
34295452b02SPatrick Sanan     PetscLayoutSetUp() does not need to be called on the resulting PetscLayout
34369ce434fSBarry Smith 
34469ce434fSBarry Smith   If the ltog location already contains a PetscLayout it is destroyed
34569ce434fSBarry Smith 
346db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutDestroy()`, `PetscLayoutSetUp()`, `PetscLayoutDuplicate()`
34769ce434fSBarry Smith @*/
348*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutSetISLocalToGlobalMapping(PetscLayout in, ISLocalToGlobalMapping ltog)
349*d71ae5a4SJacob Faibussowitsch {
35069ce434fSBarry Smith   PetscFunctionBegin;
351fc989267SStefano Zampini   if (ltog) {
352fc989267SStefano Zampini     PetscInt bs;
353fc989267SStefano Zampini 
3549566063dSJacob Faibussowitsch     PetscCall(ISLocalToGlobalMappingGetBlockSize(ltog, &bs));
355c9cc58a2SBarry Smith     PetscCheck(in->bs <= 0 || bs == 1 || in->bs == bs, in->comm, PETSC_ERR_PLIB, "Blocksize of layout %" PetscInt_FMT " must match that of mapping %" PetscInt_FMT " (or the latter must be 1)", in->bs, bs);
3569566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ltog));
357fc989267SStefano Zampini   }
3589566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&in->mapping));
35969ce434fSBarry Smith   in->mapping = ltog;
36069ce434fSBarry Smith   PetscFunctionReturn(0);
36169ce434fSBarry Smith }
36269ce434fSBarry Smith 
363c3002683SMatthew G. Knepley /*@
36469ce434fSBarry Smith   PetscLayoutSetLocalSize - Sets the local size for a PetscLayout object.
36569ce434fSBarry Smith 
36669ce434fSBarry Smith   Collective on PetscLayout
36769ce434fSBarry Smith 
36869ce434fSBarry Smith   Input Parameters:
36969ce434fSBarry Smith + map - pointer to the map
37069ce434fSBarry Smith - n - the local size
37169ce434fSBarry Smith 
37269ce434fSBarry Smith   Level: developer
37369ce434fSBarry Smith 
37469ce434fSBarry Smith   Notes:
37569ce434fSBarry Smith   Call this after the call to PetscLayoutCreate()
37669ce434fSBarry Smith 
377db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutSetSize()`, `PetscLayoutGetSize()`, `PetscLayoutGetLocalSize()`, `PetscLayoutSetUp()`
378db781477SPatrick Sanan           `PetscLayoutGetRange()`, `PetscLayoutGetRanges()`, `PetscLayoutSetBlockSize()`, `PetscLayoutGetBlockSize()`
37969ce434fSBarry Smith @*/
380*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutSetLocalSize(PetscLayout map, PetscInt n)
381*d71ae5a4SJacob Faibussowitsch {
38269ce434fSBarry Smith   PetscFunctionBegin;
383c9cc58a2SBarry Smith   PetscCheck(map->bs <= 1 || (n % map->bs) == 0, map->comm, PETSC_ERR_ARG_INCOMP, "Local size %" PetscInt_FMT " not compatible with block size %" PetscInt_FMT, n, map->bs);
38469ce434fSBarry Smith   map->n = n;
38569ce434fSBarry Smith   PetscFunctionReturn(0);
38669ce434fSBarry Smith }
38769ce434fSBarry Smith 
3884ffacfe2SAlexis Marboeuf /*@
38969ce434fSBarry Smith      PetscLayoutGetLocalSize - Gets the local size for a PetscLayout object.
39069ce434fSBarry Smith 
39169ce434fSBarry Smith     Not Collective
39269ce434fSBarry Smith 
39369ce434fSBarry Smith    Input Parameters:
39469ce434fSBarry Smith .    map - pointer to the map
39569ce434fSBarry Smith 
39669ce434fSBarry Smith    Output Parameters:
39769ce434fSBarry Smith .    n - the local size
39869ce434fSBarry Smith 
39969ce434fSBarry Smith    Level: developer
40069ce434fSBarry Smith 
40169ce434fSBarry Smith     Notes:
40269ce434fSBarry Smith        Call this after the call to PetscLayoutSetUp()
40369ce434fSBarry Smith 
40469ce434fSBarry Smith     Fortran Notes:
40569ce434fSBarry Smith       Not available from Fortran
40669ce434fSBarry Smith 
407db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutSetSize()`, `PetscLayoutGetSize()`, `PetscLayoutGetLocalSize()`, `PetscLayoutSetUp()`
408db781477SPatrick Sanan           `PetscLayoutGetRange()`, `PetscLayoutGetRanges()`, `PetscLayoutSetBlockSize()`, `PetscLayoutGetBlockSize()`
40969ce434fSBarry Smith 
41069ce434fSBarry Smith @*/
411*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutGetLocalSize(PetscLayout map, PetscInt *n)
412*d71ae5a4SJacob Faibussowitsch {
41369ce434fSBarry Smith   PetscFunctionBegin;
41469ce434fSBarry Smith   *n = map->n;
41569ce434fSBarry Smith   PetscFunctionReturn(0);
41669ce434fSBarry Smith }
41769ce434fSBarry Smith 
418c3002683SMatthew G. Knepley /*@
41969ce434fSBarry Smith   PetscLayoutSetSize - Sets the global size for a PetscLayout object.
42069ce434fSBarry Smith 
42169ce434fSBarry Smith   Logically Collective on PetscLayout
42269ce434fSBarry Smith 
42369ce434fSBarry Smith   Input Parameters:
42469ce434fSBarry Smith + map - pointer to the map
42569ce434fSBarry Smith - n - the global size
42669ce434fSBarry Smith 
42769ce434fSBarry Smith   Level: developer
42869ce434fSBarry Smith 
42969ce434fSBarry Smith   Notes:
43069ce434fSBarry Smith   Call this after the call to PetscLayoutCreate()
43169ce434fSBarry Smith 
432db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutSetLocalSize()`, `PetscLayoutGetLocalSize()`, `PetscLayoutGetSize()`, `PetscLayoutSetUp()`
433db781477SPatrick Sanan           `PetscLayoutGetRange()`, `PetscLayoutGetRanges()`, `PetscLayoutSetBlockSize()`, `PetscLayoutGetBlockSize()`
43469ce434fSBarry Smith @*/
435*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutSetSize(PetscLayout map, PetscInt n)
436*d71ae5a4SJacob Faibussowitsch {
43769ce434fSBarry Smith   PetscFunctionBegin;
43869ce434fSBarry Smith   map->N = n;
43969ce434fSBarry Smith   PetscFunctionReturn(0);
44069ce434fSBarry Smith }
44169ce434fSBarry Smith 
442c3002683SMatthew G. Knepley /*@
44369ce434fSBarry Smith   PetscLayoutGetSize - Gets the global size for a PetscLayout object.
44469ce434fSBarry Smith 
44569ce434fSBarry Smith   Not Collective
44669ce434fSBarry Smith 
44769ce434fSBarry Smith   Input Parameters:
44869ce434fSBarry Smith . map - pointer to the map
44969ce434fSBarry Smith 
45069ce434fSBarry Smith   Output Parameters:
45169ce434fSBarry Smith . n - the global size
45269ce434fSBarry Smith 
45369ce434fSBarry Smith   Level: developer
45469ce434fSBarry Smith 
45569ce434fSBarry Smith   Notes:
45669ce434fSBarry Smith   Call this after the call to PetscLayoutSetUp()
45769ce434fSBarry Smith 
458db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutSetLocalSize()`, `PetscLayoutGetLocalSize()`, `PetscLayoutSetSize()`, `PetscLayoutSetUp()`
459db781477SPatrick Sanan           `PetscLayoutGetRange()`, `PetscLayoutGetRanges()`, `PetscLayoutSetBlockSize()`, `PetscLayoutGetBlockSize()`
46069ce434fSBarry Smith @*/
461*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutGetSize(PetscLayout map, PetscInt *n)
462*d71ae5a4SJacob Faibussowitsch {
46369ce434fSBarry Smith   PetscFunctionBegin;
46469ce434fSBarry Smith   *n = map->N;
46569ce434fSBarry Smith   PetscFunctionReturn(0);
46669ce434fSBarry Smith }
46769ce434fSBarry Smith 
468c3002683SMatthew G. Knepley /*@
46969ce434fSBarry Smith   PetscLayoutSetBlockSize - Sets the block size for a PetscLayout object.
47069ce434fSBarry Smith 
47169ce434fSBarry Smith   Logically Collective on PetscLayout
47269ce434fSBarry Smith 
47369ce434fSBarry Smith   Input Parameters:
47469ce434fSBarry Smith + map - pointer to the map
47569ce434fSBarry Smith - bs - the size
47669ce434fSBarry Smith 
47769ce434fSBarry Smith   Level: developer
47869ce434fSBarry Smith 
47969ce434fSBarry Smith   Notes:
48069ce434fSBarry Smith   Call this after the call to PetscLayoutCreate()
48169ce434fSBarry Smith 
482db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutSetLocalSize()`, `PetscLayoutGetLocalSize()`, `PetscLayoutGetBlockSize()`,
483db781477SPatrick Sanan           `PetscLayoutGetRange()`, `PetscLayoutGetRanges()`, `PetscLayoutSetSize()`, `PetscLayoutGetSize()`, `PetscLayoutSetUp()`
48469ce434fSBarry Smith @*/
485*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutSetBlockSize(PetscLayout map, PetscInt bs)
486*d71ae5a4SJacob Faibussowitsch {
48769ce434fSBarry Smith   PetscFunctionBegin;
48869bbac97SJed Brown   if (bs < 0) PetscFunctionReturn(0);
489c9cc58a2SBarry Smith   PetscCheck(map->n <= 0 || (map->n % bs) == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local size %" PetscInt_FMT " not compatible with block size %" PetscInt_FMT, map->n, bs);
490565245c5SBarry Smith   if (map->mapping) {
491705e6b8bSstefano_zampini     PetscInt obs;
492565245c5SBarry Smith 
4939566063dSJacob Faibussowitsch     PetscCall(ISLocalToGlobalMappingGetBlockSize(map->mapping, &obs));
49448a46eb9SPierre Jolivet     if (obs > 1) PetscCall(ISLocalToGlobalMappingSetBlockSize(map->mapping, bs));
495705e6b8bSstefano_zampini   }
49669ce434fSBarry Smith   map->bs = bs;
49769ce434fSBarry Smith   PetscFunctionReturn(0);
49869ce434fSBarry Smith }
49969ce434fSBarry Smith 
500c3002683SMatthew G. Knepley /*@
50169ce434fSBarry Smith   PetscLayoutGetBlockSize - Gets the block size for a PetscLayout object.
50269ce434fSBarry Smith 
50369ce434fSBarry Smith   Not Collective
50469ce434fSBarry Smith 
50569ce434fSBarry Smith   Input Parameters:
50669ce434fSBarry Smith . map - pointer to the map
50769ce434fSBarry Smith 
50869ce434fSBarry Smith   Output Parameters:
50969ce434fSBarry Smith . bs - the size
51069ce434fSBarry Smith 
51169ce434fSBarry Smith   Level: developer
51269ce434fSBarry Smith 
51369ce434fSBarry Smith   Notes:
51469ce434fSBarry Smith   Call this after the call to PetscLayoutSetUp()
51569ce434fSBarry Smith 
516db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutSetLocalSize()`, `PetscLayoutGetLocalSize()`, `PetscLayoutSetSize()`, `PetscLayoutSetUp()`
517db781477SPatrick Sanan           `PetscLayoutGetRange()`, `PetscLayoutGetRanges()`, `PetscLayoutSetBlockSize()`, `PetscLayoutGetSize()`
51869ce434fSBarry Smith @*/
519*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutGetBlockSize(PetscLayout map, PetscInt *bs)
520*d71ae5a4SJacob Faibussowitsch {
52169ce434fSBarry Smith   PetscFunctionBegin;
52233d57670SJed Brown   *bs = PetscAbs(map->bs);
52369ce434fSBarry Smith   PetscFunctionReturn(0);
52469ce434fSBarry Smith }
52569ce434fSBarry Smith 
526c3002683SMatthew G. Knepley /*@
52769ce434fSBarry Smith   PetscLayoutGetRange - gets the range of values owned by this process
52869ce434fSBarry Smith 
52969ce434fSBarry Smith   Not Collective
53069ce434fSBarry Smith 
531f899ff85SJose E. Roman   Input Parameter:
53269ce434fSBarry Smith . map - pointer to the map
53369ce434fSBarry Smith 
53469ce434fSBarry Smith   Output Parameters:
53569ce434fSBarry Smith + rstart - first index owned by this process
53669ce434fSBarry Smith - rend   - one more than the last index owned by this process
53769ce434fSBarry Smith 
53869ce434fSBarry Smith   Level: developer
53969ce434fSBarry Smith 
54069ce434fSBarry Smith   Notes:
54169ce434fSBarry Smith   Call this after the call to PetscLayoutSetUp()
54269ce434fSBarry Smith 
543db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutSetLocalSize()`, `PetscLayoutGetLocalSize()`, `PetscLayoutSetSize()`,
544db781477SPatrick Sanan           `PetscLayoutGetSize()`, `PetscLayoutGetRanges()`, `PetscLayoutSetBlockSize()`, `PetscLayoutGetSize()`, `PetscLayoutSetUp()`
54569ce434fSBarry Smith @*/
546*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutGetRange(PetscLayout map, PetscInt *rstart, PetscInt *rend)
547*d71ae5a4SJacob Faibussowitsch {
54869ce434fSBarry Smith   PetscFunctionBegin;
54969ce434fSBarry Smith   if (rstart) *rstart = map->rstart;
55069ce434fSBarry Smith   if (rend) *rend = map->rend;
55169ce434fSBarry Smith   PetscFunctionReturn(0);
55269ce434fSBarry Smith }
55369ce434fSBarry Smith 
55469ce434fSBarry Smith /*@C
55569ce434fSBarry Smith      PetscLayoutGetRanges - gets the range of values owned by all processes
55669ce434fSBarry Smith 
55769ce434fSBarry Smith     Not Collective
55869ce434fSBarry Smith 
55969ce434fSBarry Smith    Input Parameters:
56069ce434fSBarry Smith .    map - pointer to the map
56169ce434fSBarry Smith 
56269ce434fSBarry Smith    Output Parameters:
563c3b5f7baSPierre Jolivet .    range - start of each processors range of indices (the final entry is one more than the
56469ce434fSBarry Smith              last index on the last process)
56569ce434fSBarry Smith 
56669ce434fSBarry Smith    Level: developer
56769ce434fSBarry Smith 
56869ce434fSBarry Smith     Notes:
56969ce434fSBarry Smith        Call this after the call to PetscLayoutSetUp()
57069ce434fSBarry Smith 
57169ce434fSBarry Smith     Fortran Notes:
57269ce434fSBarry Smith       Not available from Fortran
57369ce434fSBarry Smith 
574db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutSetLocalSize()`, `PetscLayoutGetLocalSize()`, `PetscLayoutSetSize()`,
575db781477SPatrick Sanan           `PetscLayoutGetSize()`, `PetscLayoutGetRange()`, `PetscLayoutSetBlockSize()`, `PetscLayoutGetSize()`, `PetscLayoutSetUp()`
57669ce434fSBarry Smith 
57769ce434fSBarry Smith @*/
578*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutGetRanges(PetscLayout map, const PetscInt *range[])
579*d71ae5a4SJacob Faibussowitsch {
58069ce434fSBarry Smith   PetscFunctionBegin;
58169ce434fSBarry Smith   *range = map->range;
58269ce434fSBarry Smith   PetscFunctionReturn(0);
58369ce434fSBarry Smith }
58469ce434fSBarry Smith 
585f92d6284SStefano Zampini /*@
586f92d6284SStefano Zampini   PetscLayoutCompare - Compares two layouts
587f92d6284SStefano Zampini 
588f92d6284SStefano Zampini   Not Collective
589f92d6284SStefano Zampini 
590f92d6284SStefano Zampini   Input Parameters:
591d11c674dSStefano Zampini + mapa - pointer to the first map
592f92d6284SStefano Zampini - mapb - pointer to the second map
593f92d6284SStefano Zampini 
594f92d6284SStefano Zampini   Output Parameters:
595f92d6284SStefano Zampini . congruent - PETSC_TRUE if the two layouts are congruent, PETSC_FALSE otherwise
596f92d6284SStefano Zampini 
597f92d6284SStefano Zampini   Level: beginner
598f92d6284SStefano Zampini 
599f92d6284SStefano Zampini   Notes:
600f92d6284SStefano Zampini 
601db781477SPatrick Sanan .seealso: `PetscLayoutCreate()`, `PetscLayoutSetLocalSize()`, `PetscLayoutGetLocalSize()`, `PetscLayoutGetBlockSize()`,
602db781477SPatrick Sanan           `PetscLayoutGetRange()`, `PetscLayoutGetRanges()`, `PetscLayoutSetSize()`, `PetscLayoutGetSize()`, `PetscLayoutSetUp()`
603f92d6284SStefano Zampini @*/
604*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLayoutCompare(PetscLayout mapa, PetscLayout mapb, PetscBool *congruent)
605*d71ae5a4SJacob Faibussowitsch {
606f92d6284SStefano Zampini   PetscFunctionBegin;
607f92d6284SStefano Zampini   *congruent = PETSC_FALSE;
60848a46eb9SPierre Jolivet   if (mapa->N == mapb->N && mapa->range && mapb->range && mapa->size == mapb->size) PetscCall(PetscArraycmp(mapa->range, mapb->range, mapa->size + 1, congruent));
609f92d6284SStefano Zampini   PetscFunctionReturn(0);
610f92d6284SStefano Zampini }
611