xref: /petsc/src/vec/is/utils/pmap.c (revision cf79137f598479ad8a3dc6699812db188a3543a4)
169ce434fSBarry Smith 
269ce434fSBarry Smith /*
369ce434fSBarry Smith    This file contains routines for basic map object implementation.
469ce434fSBarry Smith */
569ce434fSBarry Smith 
6c3002683SMatthew G. Knepley #include <petscis.h> /*I "petscis.h" I*/
70c312b8eSJed Brown #include <petscsf.h>
85c25fcd7SBarry Smith 
9c3002683SMatthew G. Knepley /*@
1069ce434fSBarry Smith   PetscLayoutCreate - Allocates PetscLayout space and sets the map contents to the default.
1169ce434fSBarry Smith 
1269ce434fSBarry Smith   Collective on MPI_Comm
1369ce434fSBarry Smith 
1469ce434fSBarry Smith   Input Parameters:
1569ce434fSBarry Smith + comm - the MPI communicator
1669ce434fSBarry Smith - map - pointer to the map
1769ce434fSBarry Smith 
18456fcb79SJed Brown   Level: advanced
1969ce434fSBarry Smith 
20456fcb79SJed Brown   Notes:
21456fcb79SJed Brown   Typical calling sequence
22456fcb79SJed Brown .vb
2369ce434fSBarry Smith        PetscLayoutCreate(MPI_Comm,PetscLayout *);
2469ce434fSBarry Smith        PetscLayoutSetBlockSize(PetscLayout,1);
25456fcb79SJed Brown        PetscLayoutSetSize(PetscLayout,N) // or PetscLayoutSetLocalSize(PetscLayout,n);
2669ce434fSBarry Smith        PetscLayoutSetUp(PetscLayout);
27456fcb79SJed Brown .ve
2869ce434fSBarry Smith   Optionally use any of the following:
29456fcb79SJed Brown 
30456fcb79SJed Brown + PetscLayoutGetSize(PetscLayout,PetscInt *);
31456fcb79SJed Brown . PetscLayoutGetLocalSize(PetscLayout,PetscInt *);
32456fcb79SJed Brown . PetscLayoutGetRange(PetscLayout,PetscInt *rstart,PetscInt *rend);
33456fcb79SJed Brown . PetscLayoutGetRanges(PetscLayout,const PetscInt *range[]);
34456fcb79SJed Brown - PetscLayoutDestroy(PetscLayout*);
3569ce434fSBarry Smith 
3669ce434fSBarry Smith   The PetscLayout object and methods are intended to be used in the PETSc Vec and Mat implementions; it is often not needed in
3769ce434fSBarry Smith   user codes unless you really gain something in their use.
3869ce434fSBarry Smith 
3969ce434fSBarry Smith .seealso: PetscLayoutSetLocalSize(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayout, PetscLayoutDestroy(),
4069ce434fSBarry Smith           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize(), PetscLayoutSetUp()
4169ce434fSBarry Smith 
4269ce434fSBarry Smith @*/
4369ce434fSBarry Smith PetscErrorCode PetscLayoutCreate(MPI_Comm comm,PetscLayout *map)
4469ce434fSBarry Smith {
4569ce434fSBarry Smith   PetscErrorCode ierr;
4669ce434fSBarry Smith 
4769ce434fSBarry Smith   PetscFunctionBegin;
48b00a9115SJed Brown   ierr = PetscNew(map);CHKERRQ(ierr);
4969ce434fSBarry Smith 
5069ce434fSBarry Smith   (*map)->comm   = comm;
5169ce434fSBarry Smith   (*map)->bs     = -1;
5269ce434fSBarry Smith   (*map)->n      = -1;
5369ce434fSBarry Smith   (*map)->N      = -1;
54f92d6284SStefano Zampini   (*map)->range  = NULL;
5569ce434fSBarry Smith   (*map)->rstart = 0;
5669ce434fSBarry Smith   (*map)->rend   = 0;
5769ce434fSBarry Smith   PetscFunctionReturn(0);
5869ce434fSBarry Smith }
5969ce434fSBarry Smith 
60c3002683SMatthew G. Knepley /*@
6169ce434fSBarry Smith   PetscLayoutDestroy - Frees a map object and frees its range if that exists.
6269ce434fSBarry Smith 
6369ce434fSBarry Smith   Collective on MPI_Comm
6469ce434fSBarry Smith 
6569ce434fSBarry Smith   Input Parameters:
6669ce434fSBarry Smith . map - the PetscLayout
6769ce434fSBarry Smith 
6869ce434fSBarry Smith   Level: developer
6969ce434fSBarry Smith 
70c3002683SMatthew G. Knepley   Note:
7169ce434fSBarry Smith   The PetscLayout object and methods are intended to be used in the PETSc Vec and Mat implementions; it is
7269ce434fSBarry Smith   recommended they not be used in user codes unless you really gain something in their use.
7369ce434fSBarry Smith 
7469ce434fSBarry Smith .seealso: PetscLayoutSetLocalSize(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayout, PetscLayoutCreate(),
7569ce434fSBarry Smith           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize(), PetscLayoutSetUp()
7669ce434fSBarry Smith 
7769ce434fSBarry Smith @*/
7869ce434fSBarry Smith PetscErrorCode PetscLayoutDestroy(PetscLayout *map)
7969ce434fSBarry Smith {
8069ce434fSBarry Smith   PetscErrorCode ierr;
8169ce434fSBarry Smith 
8269ce434fSBarry Smith   PetscFunctionBegin;
8369ce434fSBarry Smith   if (!*map) PetscFunctionReturn(0);
8469ce434fSBarry Smith   if (!(*map)->refcnt--) {
8569ce434fSBarry Smith     ierr = PetscFree((*map)->range);CHKERRQ(ierr);
8669ce434fSBarry Smith     ierr = ISLocalToGlobalMappingDestroy(&(*map)->mapping);CHKERRQ(ierr);
8769ce434fSBarry Smith     ierr = PetscFree((*map));CHKERRQ(ierr);
8869ce434fSBarry Smith   }
8969ce434fSBarry Smith   *map = NULL;
9069ce434fSBarry Smith   PetscFunctionReturn(0);
9169ce434fSBarry Smith }
9269ce434fSBarry Smith 
93c3002683SMatthew G. Knepley /*@
9469ce434fSBarry Smith   PetscLayoutSetUp - given a map where you have set either the global or local
9569ce434fSBarry Smith                      size sets up the map so that it may be used.
9669ce434fSBarry Smith 
9769ce434fSBarry Smith   Collective on MPI_Comm
9869ce434fSBarry Smith 
9969ce434fSBarry Smith   Input Parameters:
10069ce434fSBarry Smith . map - pointer to the map
10169ce434fSBarry Smith 
10269ce434fSBarry Smith   Level: developer
10369ce434fSBarry Smith 
10469ce434fSBarry Smith   Notes: Typical calling sequence
105c3002683SMatthew G. Knepley $ PetscLayoutCreate(MPI_Comm,PetscLayout *);
106c3002683SMatthew G. Knepley $ PetscLayoutSetBlockSize(PetscLayout,1);
107c3002683SMatthew G. Knepley $ PetscLayoutSetSize(PetscLayout,n) or PetscLayoutSetLocalSize(PetscLayout,N); or both
108c3002683SMatthew G. Knepley $ PetscLayoutSetUp(PetscLayout);
109c3002683SMatthew G. Knepley $ PetscLayoutGetSize(PetscLayout,PetscInt *);
11069ce434fSBarry Smith 
11169ce434fSBarry Smith 
11269ce434fSBarry Smith   If the local size, global size are already set and range exists then this does nothing.
11369ce434fSBarry Smith 
11469ce434fSBarry Smith .seealso: PetscLayoutSetLocalSize(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayout, PetscLayoutDestroy(),
11569ce434fSBarry Smith           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize(), PetscLayoutCreate()
11669ce434fSBarry Smith @*/
11769ce434fSBarry Smith PetscErrorCode PetscLayoutSetUp(PetscLayout map)
11869ce434fSBarry Smith {
11969ce434fSBarry Smith   PetscMPIInt    rank,size;
12069ce434fSBarry Smith   PetscInt       p;
12169ce434fSBarry Smith   PetscErrorCode ierr;
12269ce434fSBarry Smith 
12369ce434fSBarry Smith   PetscFunctionBegin;
12469ce434fSBarry Smith   if ((map->n >= 0) && (map->N >= 0) && (map->range)) PetscFunctionReturn(0);
12569ce434fSBarry Smith 
126b146b01cSBarry Smith   if (map->n > 0 && map->bs > 1) {
1276c4ed002SBarry Smith     if (map->n % map->bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Local matrix size %D must be divisible by blocksize %D",map->n,map->bs);
128b146b01cSBarry Smith   }
129b146b01cSBarry Smith   if (map->N > 0 && map->bs > 1) {
1306c4ed002SBarry Smith     if (map->N % map->bs) SETERRQ2(map->comm,PETSC_ERR_PLIB,"Global matrix size %D must be divisible by blocksize %D",map->N,map->bs);
131b146b01cSBarry Smith   }
132b146b01cSBarry Smith 
13369ce434fSBarry Smith   ierr = MPI_Comm_size(map->comm, &size);CHKERRQ(ierr);
13469ce434fSBarry Smith   ierr = MPI_Comm_rank(map->comm, &rank);CHKERRQ(ierr);
13533d57670SJed Brown   if (map->n > 0) map->n = map->n/PetscAbs(map->bs);
13633d57670SJed Brown   if (map->N > 0) map->N = map->N/PetscAbs(map->bs);
13769ce434fSBarry Smith   ierr = PetscSplitOwnership(map->comm,&map->n,&map->N);CHKERRQ(ierr);
13833d57670SJed Brown   map->n = map->n*PetscAbs(map->bs);
13933d57670SJed Brown   map->N = map->N*PetscAbs(map->bs);
14069ce434fSBarry Smith   if (!map->range) {
141854ce69bSBarry Smith     ierr = PetscMalloc1(size+1, &map->range);CHKERRQ(ierr);
14269ce434fSBarry Smith   }
14369ce434fSBarry Smith   ierr = MPI_Allgather(&map->n, 1, MPIU_INT, map->range+1, 1, MPIU_INT, map->comm);CHKERRQ(ierr);
14469ce434fSBarry Smith 
14569ce434fSBarry Smith   map->range[0] = 0;
14669ce434fSBarry Smith   for (p = 2; p <= size; p++) map->range[p] += map->range[p-1];
14769ce434fSBarry Smith 
14869ce434fSBarry Smith   map->rstart = map->range[rank];
14969ce434fSBarry Smith   map->rend   = map->range[rank+1];
15069ce434fSBarry Smith   PetscFunctionReturn(0);
15169ce434fSBarry Smith }
15269ce434fSBarry Smith 
153c3002683SMatthew G. Knepley /*@
15469ce434fSBarry Smith   PetscLayoutDuplicate - creates a new PetscLayout with the same information as a given one. If the PetscLayout already exists it is destroyed first.
15569ce434fSBarry Smith 
15669ce434fSBarry Smith   Collective on PetscLayout
15769ce434fSBarry Smith 
15869ce434fSBarry Smith   Input Parameter:
15969ce434fSBarry Smith . in - input PetscLayout to be duplicated
16069ce434fSBarry Smith 
16169ce434fSBarry Smith   Output Parameter:
16269ce434fSBarry Smith . out - the copy
16369ce434fSBarry Smith 
16469ce434fSBarry Smith   Level: developer
16569ce434fSBarry Smith 
16669ce434fSBarry Smith   Notes: PetscLayoutSetUp() does not need to be called on the resulting PetscLayout
16769ce434fSBarry Smith 
16869ce434fSBarry Smith .seealso: PetscLayoutCreate(), PetscLayoutDestroy(), PetscLayoutSetUp(), PetscLayoutReference()
16969ce434fSBarry Smith @*/
17069ce434fSBarry Smith PetscErrorCode PetscLayoutDuplicate(PetscLayout in,PetscLayout *out)
17169ce434fSBarry Smith {
17269ce434fSBarry Smith   PetscMPIInt    size;
17369ce434fSBarry Smith   PetscErrorCode ierr;
17469ce434fSBarry Smith   MPI_Comm       comm = in->comm;
17569ce434fSBarry Smith 
17669ce434fSBarry Smith   PetscFunctionBegin;
17769ce434fSBarry Smith   ierr = PetscLayoutDestroy(out);CHKERRQ(ierr);
17869ce434fSBarry Smith   ierr = PetscLayoutCreate(comm,out);CHKERRQ(ierr);
17969ce434fSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
18069ce434fSBarry Smith   ierr = PetscMemcpy(*out,in,sizeof(struct _n_PetscLayout));CHKERRQ(ierr);
181854ce69bSBarry Smith   ierr = PetscMalloc1(size+1,&(*out)->range);CHKERRQ(ierr);
18269ce434fSBarry Smith   ierr = PetscMemcpy((*out)->range,in->range,(size+1)*sizeof(PetscInt));CHKERRQ(ierr);
18369ce434fSBarry Smith 
18469ce434fSBarry Smith   (*out)->refcnt = 0;
18569ce434fSBarry Smith   PetscFunctionReturn(0);
18669ce434fSBarry Smith }
18769ce434fSBarry Smith 
188c3002683SMatthew G. Knepley /*@
18969ce434fSBarry Smith   PetscLayoutReference - Causes a PETSc Vec or Mat to share a PetscLayout with one that already exists. Used by Vec/MatDuplicate_XXX()
19069ce434fSBarry Smith 
19169ce434fSBarry Smith   Collective on PetscLayout
19269ce434fSBarry Smith 
19369ce434fSBarry Smith   Input Parameter:
19469ce434fSBarry Smith . in - input PetscLayout to be copied
19569ce434fSBarry Smith 
19669ce434fSBarry Smith   Output Parameter:
19769ce434fSBarry Smith . out - the reference location
19869ce434fSBarry Smith 
19969ce434fSBarry Smith   Level: developer
20069ce434fSBarry Smith 
20169ce434fSBarry Smith   Notes: PetscLayoutSetUp() does not need to be called on the resulting PetscLayout
20269ce434fSBarry Smith 
20369ce434fSBarry Smith   If the out location already contains a PetscLayout it is destroyed
20469ce434fSBarry Smith 
20569ce434fSBarry Smith .seealso: PetscLayoutCreate(), PetscLayoutDestroy(), PetscLayoutSetUp(), PetscLayoutDuplicate()
20669ce434fSBarry Smith @*/
20769ce434fSBarry Smith PetscErrorCode PetscLayoutReference(PetscLayout in,PetscLayout *out)
20869ce434fSBarry Smith {
20969ce434fSBarry Smith   PetscErrorCode ierr;
21069ce434fSBarry Smith 
21169ce434fSBarry Smith   PetscFunctionBegin;
21269ce434fSBarry Smith   in->refcnt++;
21369ce434fSBarry Smith   ierr = PetscLayoutDestroy(out);CHKERRQ(ierr);
21469ce434fSBarry Smith   *out = in;
21569ce434fSBarry Smith   PetscFunctionReturn(0);
21669ce434fSBarry Smith }
21769ce434fSBarry Smith 
218c3002683SMatthew G. Knepley /*@
21969ce434fSBarry Smith   PetscLayoutSetISLocalToGlobalMapping - sets a ISLocalGlobalMapping into a PetscLayout
22069ce434fSBarry Smith 
22169ce434fSBarry Smith   Collective on PetscLayout
22269ce434fSBarry Smith 
22369ce434fSBarry Smith   Input Parameter:
22469ce434fSBarry Smith + in - input PetscLayout
22569ce434fSBarry Smith - ltog - the local to global mapping
22669ce434fSBarry Smith 
22769ce434fSBarry Smith 
22869ce434fSBarry Smith   Level: developer
22969ce434fSBarry Smith 
23069ce434fSBarry Smith   Notes: PetscLayoutSetUp() does not need to be called on the resulting PetscLayout
23169ce434fSBarry Smith 
23269ce434fSBarry Smith   If the ltog location already contains a PetscLayout it is destroyed
23369ce434fSBarry Smith 
234a188d78dSBarry Smith .seealso: PetscLayoutCreate(), PetscLayoutDestroy(), PetscLayoutSetUp(), PetscLayoutDuplicate()
23569ce434fSBarry Smith @*/
23669ce434fSBarry Smith PetscErrorCode PetscLayoutSetISLocalToGlobalMapping(PetscLayout in,ISLocalToGlobalMapping ltog)
23769ce434fSBarry Smith {
23869ce434fSBarry Smith   PetscErrorCode ierr;
23945b6f7e9SBarry Smith   PetscInt       bs;
24069ce434fSBarry Smith 
24169ce434fSBarry Smith   PetscFunctionBegin;
24245b6f7e9SBarry Smith   ierr = ISLocalToGlobalMappingGetBlockSize(ltog,&bs);CHKERRQ(ierr);
24345b6f7e9SBarry Smith   if (in->bs > 0 && in->bs != bs) SETERRQ2(in->comm,PETSC_ERR_PLIB,"Blocksize of layout %D must match that of mapping %D",in->bs,bs);
24469ce434fSBarry Smith   ierr = PetscObjectReference((PetscObject)ltog);CHKERRQ(ierr);
24569ce434fSBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&in->mapping);CHKERRQ(ierr);
24669ce434fSBarry Smith   in->mapping = ltog;
24769ce434fSBarry Smith   PetscFunctionReturn(0);
24869ce434fSBarry Smith }
24969ce434fSBarry Smith 
250c3002683SMatthew G. Knepley /*@
25169ce434fSBarry Smith   PetscLayoutSetLocalSize - Sets the local size for a PetscLayout object.
25269ce434fSBarry Smith 
25369ce434fSBarry Smith   Collective on PetscLayout
25469ce434fSBarry Smith 
25569ce434fSBarry Smith   Input Parameters:
25669ce434fSBarry Smith + map - pointer to the map
25769ce434fSBarry Smith - n - the local size
25869ce434fSBarry Smith 
25969ce434fSBarry Smith   Level: developer
26069ce434fSBarry Smith 
26169ce434fSBarry Smith   Notes:
26269ce434fSBarry Smith   Call this after the call to PetscLayoutCreate()
26369ce434fSBarry Smith 
26469ce434fSBarry Smith .seealso: PetscLayoutCreate(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayoutSetUp()
26569ce434fSBarry Smith           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()
26669ce434fSBarry Smith @*/
26769ce434fSBarry Smith PetscErrorCode PetscLayoutSetLocalSize(PetscLayout map,PetscInt n)
26869ce434fSBarry Smith {
26969ce434fSBarry Smith   PetscFunctionBegin;
27069ce434fSBarry Smith   if (map->bs > 1 && n % map->bs) SETERRQ2(map->comm,PETSC_ERR_ARG_INCOMP,"Local size %D not compatible with block size %D",n,map->bs);
27169ce434fSBarry Smith   map->n = n;
27269ce434fSBarry Smith   PetscFunctionReturn(0);
27369ce434fSBarry Smith }
27469ce434fSBarry Smith 
27569ce434fSBarry Smith /*@C
27669ce434fSBarry Smith      PetscLayoutGetLocalSize - Gets the local size for a PetscLayout object.
27769ce434fSBarry Smith 
27869ce434fSBarry Smith     Not Collective
27969ce434fSBarry Smith 
28069ce434fSBarry Smith    Input Parameters:
28169ce434fSBarry Smith .    map - pointer to the map
28269ce434fSBarry Smith 
28369ce434fSBarry Smith    Output Parameters:
28469ce434fSBarry Smith .    n - the local size
28569ce434fSBarry Smith 
28669ce434fSBarry Smith    Level: developer
28769ce434fSBarry Smith 
28869ce434fSBarry Smith     Notes:
28969ce434fSBarry Smith        Call this after the call to PetscLayoutSetUp()
29069ce434fSBarry Smith 
29169ce434fSBarry Smith     Fortran Notes:
29269ce434fSBarry Smith       Not available from Fortran
29369ce434fSBarry Smith 
29469ce434fSBarry Smith .seealso: PetscLayoutCreate(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutGetLocalSize(), PetscLayoutSetUp()
29569ce434fSBarry Smith           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()
29669ce434fSBarry Smith 
29769ce434fSBarry Smith @*/
29869ce434fSBarry Smith PetscErrorCode  PetscLayoutGetLocalSize(PetscLayout map,PetscInt *n)
29969ce434fSBarry Smith {
30069ce434fSBarry Smith   PetscFunctionBegin;
30169ce434fSBarry Smith   *n = map->n;
30269ce434fSBarry Smith   PetscFunctionReturn(0);
30369ce434fSBarry Smith }
30469ce434fSBarry Smith 
305c3002683SMatthew G. Knepley /*@
30669ce434fSBarry Smith   PetscLayoutSetSize - Sets the global size for a PetscLayout object.
30769ce434fSBarry Smith 
30869ce434fSBarry Smith   Logically Collective on PetscLayout
30969ce434fSBarry Smith 
31069ce434fSBarry Smith   Input Parameters:
31169ce434fSBarry Smith + map - pointer to the map
31269ce434fSBarry Smith - n - the global size
31369ce434fSBarry Smith 
31469ce434fSBarry Smith   Level: developer
31569ce434fSBarry Smith 
31669ce434fSBarry Smith   Notes:
31769ce434fSBarry Smith   Call this after the call to PetscLayoutCreate()
31869ce434fSBarry Smith 
31969ce434fSBarry Smith .seealso: PetscLayoutCreate(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutGetSize(), PetscLayoutSetUp()
32069ce434fSBarry Smith           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()
32169ce434fSBarry Smith @*/
32269ce434fSBarry Smith PetscErrorCode PetscLayoutSetSize(PetscLayout map,PetscInt n)
32369ce434fSBarry Smith {
32469ce434fSBarry Smith   PetscFunctionBegin;
32569ce434fSBarry Smith   map->N = n;
32669ce434fSBarry Smith   PetscFunctionReturn(0);
32769ce434fSBarry Smith }
32869ce434fSBarry Smith 
329c3002683SMatthew G. Knepley /*@
33069ce434fSBarry Smith   PetscLayoutGetSize - Gets the global size for a PetscLayout object.
33169ce434fSBarry Smith 
33269ce434fSBarry Smith   Not Collective
33369ce434fSBarry Smith 
33469ce434fSBarry Smith   Input Parameters:
33569ce434fSBarry Smith . map - pointer to the map
33669ce434fSBarry Smith 
33769ce434fSBarry Smith   Output Parameters:
33869ce434fSBarry Smith . n - the global size
33969ce434fSBarry Smith 
34069ce434fSBarry Smith   Level: developer
34169ce434fSBarry Smith 
34269ce434fSBarry Smith   Notes:
34369ce434fSBarry Smith   Call this after the call to PetscLayoutSetUp()
34469ce434fSBarry Smith 
34569ce434fSBarry Smith .seealso: PetscLayoutCreate(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(), PetscLayoutSetUp()
34669ce434fSBarry Smith           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetBlockSize()
34769ce434fSBarry Smith @*/
34869ce434fSBarry Smith PetscErrorCode PetscLayoutGetSize(PetscLayout map,PetscInt *n)
34969ce434fSBarry Smith {
35069ce434fSBarry Smith   PetscFunctionBegin;
35169ce434fSBarry Smith   *n = map->N;
35269ce434fSBarry Smith   PetscFunctionReturn(0);
35369ce434fSBarry Smith }
35469ce434fSBarry Smith 
355c3002683SMatthew G. Knepley /*@
35669ce434fSBarry Smith   PetscLayoutSetBlockSize - Sets the block size for a PetscLayout object.
35769ce434fSBarry Smith 
35869ce434fSBarry Smith   Logically Collective on PetscLayout
35969ce434fSBarry Smith 
36069ce434fSBarry Smith   Input Parameters:
36169ce434fSBarry Smith + map - pointer to the map
36269ce434fSBarry Smith - bs - the size
36369ce434fSBarry Smith 
36469ce434fSBarry Smith   Level: developer
36569ce434fSBarry Smith 
36669ce434fSBarry Smith   Notes:
36769ce434fSBarry Smith   Call this after the call to PetscLayoutCreate()
36869ce434fSBarry Smith 
36969ce434fSBarry Smith .seealso: PetscLayoutCreate(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutGetBlockSize(),
37069ce434fSBarry Smith           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutSetUp()
37169ce434fSBarry Smith @*/
37269ce434fSBarry Smith PetscErrorCode PetscLayoutSetBlockSize(PetscLayout map,PetscInt bs)
37369ce434fSBarry Smith {
37469ce434fSBarry Smith   PetscFunctionBegin;
37569bbac97SJed Brown   if (bs < 0) PetscFunctionReturn(0);
37669ce434fSBarry Smith   if (map->n > 0 && map->n % bs) SETERRQ2(map->comm,PETSC_ERR_ARG_INCOMP,"Local size %D not compatible with block size %D",map->n,bs);
377565245c5SBarry Smith   if (map->mapping) {
378705e6b8bSstefano_zampini     PetscInt       obs;
379565245c5SBarry Smith     PetscErrorCode ierr;
380565245c5SBarry Smith 
381705e6b8bSstefano_zampini     ierr = ISLocalToGlobalMappingGetBlockSize(map->mapping,&obs);CHKERRQ(ierr);
382705e6b8bSstefano_zampini     if (obs > 1) {
38363fa5c83Sstefano_zampini       ierr = ISLocalToGlobalMappingSetBlockSize(map->mapping,bs);CHKERRQ(ierr);
384565245c5SBarry Smith     }
385705e6b8bSstefano_zampini   }
38669ce434fSBarry Smith   map->bs = bs;
38769ce434fSBarry Smith   PetscFunctionReturn(0);
38869ce434fSBarry Smith }
38969ce434fSBarry Smith 
390c3002683SMatthew G. Knepley /*@
39169ce434fSBarry Smith   PetscLayoutGetBlockSize - Gets the block size for a PetscLayout object.
39269ce434fSBarry Smith 
39369ce434fSBarry Smith   Not Collective
39469ce434fSBarry Smith 
39569ce434fSBarry Smith   Input Parameters:
39669ce434fSBarry Smith . map - pointer to the map
39769ce434fSBarry Smith 
39869ce434fSBarry Smith   Output Parameters:
39969ce434fSBarry Smith . bs - the size
40069ce434fSBarry Smith 
40169ce434fSBarry Smith   Level: developer
40269ce434fSBarry Smith 
40369ce434fSBarry Smith   Notes:
40469ce434fSBarry Smith   Call this after the call to PetscLayoutSetUp()
40569ce434fSBarry Smith 
40669ce434fSBarry Smith .seealso: PetscLayoutCreate(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(), PetscLayoutSetUp()
40769ce434fSBarry Smith           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetSize()
40869ce434fSBarry Smith @*/
40969ce434fSBarry Smith PetscErrorCode PetscLayoutGetBlockSize(PetscLayout map,PetscInt *bs)
41069ce434fSBarry Smith {
41169ce434fSBarry Smith   PetscFunctionBegin;
41233d57670SJed Brown   *bs = PetscAbs(map->bs);
41369ce434fSBarry Smith   PetscFunctionReturn(0);
41469ce434fSBarry Smith }
41569ce434fSBarry Smith 
416c3002683SMatthew G. Knepley /*@
41769ce434fSBarry Smith   PetscLayoutGetRange - gets the range of values owned by this process
41869ce434fSBarry Smith 
41969ce434fSBarry Smith   Not Collective
42069ce434fSBarry Smith 
42169ce434fSBarry Smith   Input Parameters:
42269ce434fSBarry Smith . map - pointer to the map
42369ce434fSBarry Smith 
42469ce434fSBarry Smith   Output Parameters:
42569ce434fSBarry Smith + rstart - first index owned by this process
42669ce434fSBarry Smith - rend   - one more than the last index owned by this process
42769ce434fSBarry Smith 
42869ce434fSBarry Smith   Level: developer
42969ce434fSBarry Smith 
43069ce434fSBarry Smith   Notes:
43169ce434fSBarry Smith   Call this after the call to PetscLayoutSetUp()
43269ce434fSBarry Smith 
43369ce434fSBarry Smith .seealso: PetscLayoutCreate(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(),
43469ce434fSBarry Smith           PetscLayoutGetSize(), PetscLayoutGetRanges(), PetscLayoutSetBlockSize(), PetscLayoutGetSize(), PetscLayoutSetUp()
43569ce434fSBarry Smith @*/
43669ce434fSBarry Smith PetscErrorCode PetscLayoutGetRange(PetscLayout map,PetscInt *rstart,PetscInt *rend)
43769ce434fSBarry Smith {
43869ce434fSBarry Smith   PetscFunctionBegin;
43969ce434fSBarry Smith   if (rstart) *rstart = map->rstart;
44069ce434fSBarry Smith   if (rend)   *rend   = map->rend;
44169ce434fSBarry Smith   PetscFunctionReturn(0);
44269ce434fSBarry Smith }
44369ce434fSBarry Smith 
44469ce434fSBarry Smith /*@C
44569ce434fSBarry Smith      PetscLayoutGetRanges - gets the range of values owned by all processes
44669ce434fSBarry Smith 
44769ce434fSBarry Smith     Not Collective
44869ce434fSBarry Smith 
44969ce434fSBarry Smith    Input Parameters:
45069ce434fSBarry Smith .    map - pointer to the map
45169ce434fSBarry Smith 
45269ce434fSBarry Smith    Output Parameters:
45369ce434fSBarry Smith .    range - start of each processors range of indices (the final entry is one more then the
45469ce434fSBarry Smith              last index on the last process)
45569ce434fSBarry Smith 
45669ce434fSBarry Smith    Level: developer
45769ce434fSBarry Smith 
45869ce434fSBarry Smith     Notes:
45969ce434fSBarry Smith        Call this after the call to PetscLayoutSetUp()
46069ce434fSBarry Smith 
46169ce434fSBarry Smith     Fortran Notes:
46269ce434fSBarry Smith       Not available from Fortran
46369ce434fSBarry Smith 
46469ce434fSBarry Smith .seealso: PetscLayoutCreate(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutSetSize(),
46569ce434fSBarry Smith           PetscLayoutGetSize(), PetscLayoutGetRange(), PetscLayoutSetBlockSize(), PetscLayoutGetSize(), PetscLayoutSetUp()
46669ce434fSBarry Smith 
46769ce434fSBarry Smith @*/
46869ce434fSBarry Smith PetscErrorCode  PetscLayoutGetRanges(PetscLayout map,const PetscInt *range[])
46969ce434fSBarry Smith {
47069ce434fSBarry Smith   PetscFunctionBegin;
47169ce434fSBarry Smith   *range = map->range;
47269ce434fSBarry Smith   PetscFunctionReturn(0);
47369ce434fSBarry Smith }
47469ce434fSBarry Smith 
47569ce434fSBarry Smith /*@C
47669ce434fSBarry Smith    PetscSFSetGraphLayout - Set a parallel star forest via global indices and a PetscLayout
47769ce434fSBarry Smith 
47869ce434fSBarry Smith    Collective
47969ce434fSBarry Smith 
48069ce434fSBarry Smith    Input Arguments:
48169ce434fSBarry Smith +  sf - star forest
48269ce434fSBarry Smith .  layout - PetscLayout defining the global space
48369ce434fSBarry Smith .  nleaves - number of leaf vertices on the current process, each of these references a root on any process
48469ce434fSBarry Smith .  ilocal - locations of leaves in leafdata buffers, pass NULL for contiguous storage
485*cf79137fSStefano Zampini .  localmode - copy mode for ilocal
48669ce434fSBarry Smith -  iremote - remote locations of root vertices for each leaf on the current process
48769ce434fSBarry Smith 
48869ce434fSBarry Smith    Level: intermediate
48969ce434fSBarry Smith 
490*cf79137fSStefano Zampini    Developers Note: Local indices which are the identity permutation in the range [0,nleaves) are discarded as they
491*cf79137fSStefano Zampini    encode contiguous storage. In such case, if localmode is PETSC_OWN_POINTER, the memory is deallocated as it is not
492*cf79137fSStefano Zampini    needed
493*cf79137fSStefano Zampini 
49469ce434fSBarry Smith .seealso: PetscSFCreate(), PetscSFView(), PetscSFSetGraph(), PetscSFGetGraph()
49569ce434fSBarry Smith @*/
49669ce434fSBarry Smith PetscErrorCode PetscSFSetGraphLayout(PetscSF sf,PetscLayout layout,PetscInt nleaves,const PetscInt *ilocal,PetscCopyMode localmode,const PetscInt *iremote)
49769ce434fSBarry Smith {
49869ce434fSBarry Smith   PetscErrorCode ierr;
49969ce434fSBarry Smith   PetscInt       i,nroots;
50069ce434fSBarry Smith   PetscSFNode    *remote;
50169ce434fSBarry Smith 
50269ce434fSBarry Smith   PetscFunctionBegin;
50369ce434fSBarry Smith   ierr = PetscLayoutGetLocalSize(layout,&nroots);CHKERRQ(ierr);
504785e854fSJed Brown   ierr = PetscMalloc1(nleaves,&remote);CHKERRQ(ierr);
50569ce434fSBarry Smith   for (i=0; i<nleaves; i++) {
50669ce434fSBarry Smith     PetscInt owner = -1;
50769ce434fSBarry Smith     ierr = PetscLayoutFindOwner(layout,iremote[i],&owner);CHKERRQ(ierr);
50869ce434fSBarry Smith     remote[i].rank  = owner;
50969ce434fSBarry Smith     remote[i].index = iremote[i] - layout->range[owner];
51069ce434fSBarry Smith   }
51169ce434fSBarry Smith   ierr = PetscSFSetGraph(sf,nroots,nleaves,ilocal,localmode,remote,PETSC_OWN_POINTER);CHKERRQ(ierr);
51269ce434fSBarry Smith   PetscFunctionReturn(0);
51369ce434fSBarry Smith }
51469ce434fSBarry Smith 
515f92d6284SStefano Zampini /*@
516f92d6284SStefano Zampini   PetscLayoutCompare - Compares two layouts
517f92d6284SStefano Zampini 
518f92d6284SStefano Zampini   Not Collective
519f92d6284SStefano Zampini 
520f92d6284SStefano Zampini   Input Parameters:
521d11c674dSStefano Zampini + mapa - pointer to the first map
522f92d6284SStefano Zampini - mapb - pointer to the second map
523f92d6284SStefano Zampini 
524f92d6284SStefano Zampini   Output Parameters:
525f92d6284SStefano Zampini . congruent - PETSC_TRUE if the two layouts are congruent, PETSC_FALSE otherwise
526f92d6284SStefano Zampini 
527f92d6284SStefano Zampini   Level: beginner
528f92d6284SStefano Zampini 
529f92d6284SStefano Zampini   Notes:
530f92d6284SStefano Zampini 
531f92d6284SStefano Zampini .seealso: PetscLayoutCreate(), PetscLayoutSetLocalSize(), PetscLayoutGetLocalSize(), PetscLayoutGetBlockSize(),
532f92d6284SStefano Zampini           PetscLayoutGetRange(), PetscLayoutGetRanges(), PetscLayoutSetSize(), PetscLayoutGetSize(), PetscLayoutSetUp()
533f92d6284SStefano Zampini @*/
534f92d6284SStefano Zampini PetscErrorCode PetscLayoutCompare(PetscLayout mapa,PetscLayout mapb,PetscBool *congruent)
535f92d6284SStefano Zampini {
536f92d6284SStefano Zampini   PetscErrorCode ierr;
537f92d6284SStefano Zampini   PetscMPIInt    sizea,sizeb;
538f92d6284SStefano Zampini 
539f92d6284SStefano Zampini   PetscFunctionBegin;
540f92d6284SStefano Zampini   *congruent = PETSC_FALSE;
541f92d6284SStefano Zampini   ierr = MPI_Comm_size(mapa->comm,&sizea);CHKERRQ(ierr);
542f92d6284SStefano Zampini   ierr = MPI_Comm_size(mapb->comm,&sizeb);CHKERRQ(ierr);
543f92d6284SStefano Zampini   if (mapa->N == mapb->N && mapa->range && mapb->range && sizea == sizeb) {
544f92d6284SStefano Zampini     ierr = PetscMemcmp(mapa->range,mapb->range,(sizea+1)*sizeof(PetscInt),congruent);CHKERRQ(ierr);
545f92d6284SStefano Zampini   }
546f92d6284SStefano Zampini   PetscFunctionReturn(0);
547f92d6284SStefano Zampini }
548