xref: /petsc/src/dm/impls/composite/pack.c (revision 14354c39ee74d1dad3e3edac48acc7069f3a5e06)
147c6ae99SBarry Smith 
23c48a1e8SJed Brown #include "packimpl.h" /*I   "petscdmcomposite.h"   I*/
347c6ae99SBarry Smith 
447c6ae99SBarry Smith #undef __FUNCT__
547c6ae99SBarry Smith #define __FUNCT__ "DMCompositeSetCoupling"
647c6ae99SBarry Smith /*@C
747c6ae99SBarry Smith     DMCompositeSetCoupling - Sets user provided routines that compute the coupling between the
8aa219208SBarry Smith       seperate components (DMDA's and arrays) in a DMto build the correct matrix nonzero structure.
947c6ae99SBarry Smith 
1047c6ae99SBarry Smith 
1147c6ae99SBarry Smith     Logically Collective on MPI_Comm
1247c6ae99SBarry Smith 
1347c6ae99SBarry Smith     Input Parameter:
1447c6ae99SBarry Smith +   dm - the composite object
1547c6ae99SBarry Smith -   formcouplelocations - routine to set the nonzero locations in the matrix
1647c6ae99SBarry Smith 
1747c6ae99SBarry Smith     Level: advanced
1847c6ae99SBarry Smith 
191b2093e4SBarry Smith     Notes: See DMSetApplicationContext() and DMGetApplicationContext() for how to get user information into
2047c6ae99SBarry Smith         this routine
2147c6ae99SBarry Smith 
2247c6ae99SBarry Smith @*/
237087cfbeSBarry Smith PetscErrorCode  DMCompositeSetCoupling(DM dm,PetscErrorCode (*FormCoupleLocations)(DM,Mat,PetscInt*,PetscInt*,PetscInt,PetscInt,PetscInt,PetscInt))
2447c6ae99SBarry Smith {
2547c6ae99SBarry Smith   DM_Composite *com = (DM_Composite*)dm->data;
2647c6ae99SBarry Smith 
2747c6ae99SBarry Smith   PetscFunctionBegin;
2847c6ae99SBarry Smith   com->FormCoupleLocations = FormCoupleLocations;
2947c6ae99SBarry Smith   PetscFunctionReturn(0);
3047c6ae99SBarry Smith }
3147c6ae99SBarry Smith 
3247c6ae99SBarry Smith #undef __FUNCT__
330c010503SBarry Smith #define __FUNCT__ "DMDestroy_Composite"
346bf464f9SBarry Smith PetscErrorCode  DMDestroy_Composite(DM dm)
3547c6ae99SBarry Smith {
3647c6ae99SBarry Smith   PetscErrorCode         ierr;
3747c6ae99SBarry Smith   struct DMCompositeLink *next, *prev;
3847c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite *)dm->data;
3947c6ae99SBarry Smith 
4047c6ae99SBarry Smith   PetscFunctionBegin;
4147c6ae99SBarry Smith   next = com->next;
4247c6ae99SBarry Smith   while (next) {
4347c6ae99SBarry Smith     prev = next;
4447c6ae99SBarry Smith     next = next->next;
4547c6ae99SBarry Smith     if (prev->type == DMCOMPOSITE_DM) {
46fcfd50ebSBarry Smith       ierr = DMDestroy(&prev->dm);CHKERRQ(ierr);
4747c6ae99SBarry Smith     }
4847c6ae99SBarry Smith     ierr = PetscFree(prev->grstarts);CHKERRQ(ierr);
4947c6ae99SBarry Smith     ierr = PetscFree(prev);CHKERRQ(ierr);
5047c6ae99SBarry Smith   }
5147c6ae99SBarry Smith   PetscFunctionReturn(0);
5247c6ae99SBarry Smith }
5347c6ae99SBarry Smith 
5447c6ae99SBarry Smith #undef __FUNCT__
550c010503SBarry Smith #define __FUNCT__ "DMView_Composite"
567087cfbeSBarry Smith PetscErrorCode  DMView_Composite(DM dm,PetscViewer v)
5747c6ae99SBarry Smith {
5847c6ae99SBarry Smith   PetscErrorCode ierr;
5947c6ae99SBarry Smith   PetscBool      iascii;
6047c6ae99SBarry Smith   DM_Composite   *com = (DM_Composite *)dm->data;
6147c6ae99SBarry Smith 
6247c6ae99SBarry Smith   PetscFunctionBegin;
6347c6ae99SBarry Smith   ierr = PetscTypeCompare((PetscObject)v,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
6447c6ae99SBarry Smith   if (iascii) {
6547c6ae99SBarry Smith     struct DMCompositeLink *lnk = com->next;
6647c6ae99SBarry Smith     PetscInt               i;
6747c6ae99SBarry Smith 
6847c6ae99SBarry Smith     ierr = PetscViewerASCIIPrintf(v,"DM (%s)\n",((PetscObject)dm)->prefix?((PetscObject)dm)->prefix:"no prefix");CHKERRQ(ierr);
6947c6ae99SBarry Smith     ierr = PetscViewerASCIIPrintf(v,"  contains %d DMs and %d redundant arrays\n",com->nDM,com->nredundant);CHKERRQ(ierr);
7047c6ae99SBarry Smith     ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr);
7147c6ae99SBarry Smith     for (i=0; lnk; lnk=lnk->next,i++) {
7247c6ae99SBarry Smith       if (lnk->dm) {
7347c6ae99SBarry Smith         ierr = PetscViewerASCIIPrintf(v,"Link %d: DM of type %s\n",i,((PetscObject)lnk->dm)->type_name);CHKERRQ(ierr);
7447c6ae99SBarry Smith         ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr);
7547c6ae99SBarry Smith         ierr = DMView(lnk->dm,v);CHKERRQ(ierr);
7647c6ae99SBarry Smith         ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr);
7747c6ae99SBarry Smith       } else {
7806ebdd98SJed Brown         ierr = PetscViewerASCIIPrintf(v,"Link %d: Redundant array of size %d owned by rank %d\n",i,lnk->nlocal,lnk->rank);CHKERRQ(ierr);
7947c6ae99SBarry Smith       }
8047c6ae99SBarry Smith     }
8147c6ae99SBarry Smith     ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr);
8247c6ae99SBarry Smith   }
8347c6ae99SBarry Smith   PetscFunctionReturn(0);
8447c6ae99SBarry Smith }
8547c6ae99SBarry Smith 
8647c6ae99SBarry Smith /* --------------------------------------------------------------------------------------*/
8747c6ae99SBarry Smith #undef __FUNCT__
88d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp_Composite"
897087cfbeSBarry Smith PetscErrorCode  DMSetUp_Composite(DM dm)
9047c6ae99SBarry Smith {
9147c6ae99SBarry Smith   PetscErrorCode         ierr;
9247c6ae99SBarry Smith   PetscInt               nprev = 0;
9347c6ae99SBarry Smith   PetscMPIInt            rank,size;
9447c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
9547c6ae99SBarry Smith   struct DMCompositeLink *next = com->next;
9647c6ae99SBarry Smith   PetscLayout            map;
9747c6ae99SBarry Smith 
9847c6ae99SBarry Smith   PetscFunctionBegin;
9947c6ae99SBarry Smith   if (com->setup) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Packer has already been setup");
10047c6ae99SBarry Smith   ierr = PetscLayoutCreate(((PetscObject)dm)->comm,&map);CHKERRQ(ierr);
10147c6ae99SBarry Smith   ierr = PetscLayoutSetLocalSize(map,com->n);CHKERRQ(ierr);
10247c6ae99SBarry Smith   ierr = PetscLayoutSetSize(map,PETSC_DETERMINE);CHKERRQ(ierr);
10347c6ae99SBarry Smith   ierr = PetscLayoutSetBlockSize(map,1);CHKERRQ(ierr);
10447c6ae99SBarry Smith   ierr = PetscLayoutSetUp(map);CHKERRQ(ierr);
10547c6ae99SBarry Smith   ierr = PetscLayoutGetSize(map,&com->N);CHKERRQ(ierr);
10647c6ae99SBarry Smith   ierr = PetscLayoutGetRange(map,&com->rstart,PETSC_NULL);CHKERRQ(ierr);
107fcfd50ebSBarry Smith   ierr = PetscLayoutDestroy(&map);CHKERRQ(ierr);
10847c6ae99SBarry Smith 
10947c6ae99SBarry Smith   /* now set the rstart for each linked array/vector */
11047c6ae99SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)dm)->comm,&rank);CHKERRQ(ierr);
11147c6ae99SBarry Smith   ierr = MPI_Comm_size(((PetscObject)dm)->comm,&size);CHKERRQ(ierr);
11247c6ae99SBarry Smith   while (next) {
11347c6ae99SBarry Smith     next->rstart = nprev;
11406ebdd98SJed Brown     nprev += next->n;
11547c6ae99SBarry Smith     next->grstart = com->rstart + next->rstart;
11647c6ae99SBarry Smith     if (next->type == DMCOMPOSITE_ARRAY) {
11747c6ae99SBarry Smith       ierr = MPI_Bcast(&next->grstart,1,MPIU_INT,next->rank,((PetscObject)dm)->comm);CHKERRQ(ierr);
11847c6ae99SBarry Smith     } else {
11947c6ae99SBarry Smith       ierr = PetscMalloc(size*sizeof(PetscInt),&next->grstarts);CHKERRQ(ierr);
12047c6ae99SBarry Smith       ierr = MPI_Allgather(&next->grstart,1,MPIU_INT,next->grstarts,1,MPIU_INT,((PetscObject)dm)->comm);CHKERRQ(ierr);
12147c6ae99SBarry Smith     }
12247c6ae99SBarry Smith     next = next->next;
12347c6ae99SBarry Smith   }
12447c6ae99SBarry Smith   com->setup = PETSC_TRUE;
12547c6ae99SBarry Smith   PetscFunctionReturn(0);
12647c6ae99SBarry Smith }
12747c6ae99SBarry Smith 
12847c6ae99SBarry Smith 
12947c6ae99SBarry Smith #undef __FUNCT__
13047c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetAccess_Array"
13147c6ae99SBarry Smith PetscErrorCode DMCompositeGetAccess_Array(DM dm,struct DMCompositeLink *mine,Vec vec,PetscScalar **array)
13247c6ae99SBarry Smith {
13347c6ae99SBarry Smith   PetscErrorCode ierr;
13447c6ae99SBarry Smith   PetscScalar    *varray;
13547c6ae99SBarry Smith   PetscMPIInt    rank;
13647c6ae99SBarry Smith 
13747c6ae99SBarry Smith   PetscFunctionBegin;
13847c6ae99SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)dm)->comm,&rank);CHKERRQ(ierr);
13947c6ae99SBarry Smith   if (array) {
14047c6ae99SBarry Smith     if (rank == mine->rank) {
14147c6ae99SBarry Smith       ierr    = VecGetArray(vec,&varray);CHKERRQ(ierr);
14247c6ae99SBarry Smith       *array  = varray + mine->rstart;
14347c6ae99SBarry Smith       ierr    = VecRestoreArray(vec,&varray);CHKERRQ(ierr);
14447c6ae99SBarry Smith     } else {
14547c6ae99SBarry Smith       *array = 0;
14647c6ae99SBarry Smith     }
14747c6ae99SBarry Smith   }
14847c6ae99SBarry Smith   PetscFunctionReturn(0);
14947c6ae99SBarry Smith }
15047c6ae99SBarry Smith 
15147c6ae99SBarry Smith #undef __FUNCT__
15247c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetAccess_DM"
15347c6ae99SBarry Smith PetscErrorCode DMCompositeGetAccess_DM(DM dm,struct DMCompositeLink *mine,Vec vec,Vec *global)
15447c6ae99SBarry Smith {
15547c6ae99SBarry Smith   PetscErrorCode ierr;
15647c6ae99SBarry Smith   PetscScalar    *array;
15747c6ae99SBarry Smith 
15847c6ae99SBarry Smith   PetscFunctionBegin;
15947c6ae99SBarry Smith   if (global) {
16047c6ae99SBarry Smith     ierr    = DMGetGlobalVector(mine->dm,global);CHKERRQ(ierr);
16147c6ae99SBarry Smith     ierr    = VecGetArray(vec,&array);CHKERRQ(ierr);
16247c6ae99SBarry Smith     ierr    = VecPlaceArray(*global,array+mine->rstart);CHKERRQ(ierr);
16347c6ae99SBarry Smith     ierr    = VecRestoreArray(vec,&array);CHKERRQ(ierr);
16447c6ae99SBarry Smith   }
16547c6ae99SBarry Smith   PetscFunctionReturn(0);
16647c6ae99SBarry Smith }
16747c6ae99SBarry Smith 
16847c6ae99SBarry Smith #undef __FUNCT__
16947c6ae99SBarry Smith #define __FUNCT__ "DMCompositeRestoreAccess_Array"
17047c6ae99SBarry Smith PetscErrorCode DMCompositeRestoreAccess_Array(DM dm,struct DMCompositeLink *mine,Vec vec,PetscScalar **array)
17147c6ae99SBarry Smith {
17247c6ae99SBarry Smith   PetscFunctionBegin;
17347c6ae99SBarry Smith   PetscFunctionReturn(0);
17447c6ae99SBarry Smith }
17547c6ae99SBarry Smith 
17647c6ae99SBarry Smith #undef __FUNCT__
17747c6ae99SBarry Smith #define __FUNCT__ "DMCompositeRestoreAccess_DM"
17847c6ae99SBarry Smith PetscErrorCode DMCompositeRestoreAccess_DM(DM dm,struct DMCompositeLink *mine,Vec vec,Vec *global)
17947c6ae99SBarry Smith {
18047c6ae99SBarry Smith   PetscErrorCode ierr;
18147c6ae99SBarry Smith 
18247c6ae99SBarry Smith   PetscFunctionBegin;
18347c6ae99SBarry Smith   if (global) {
18447c6ae99SBarry Smith     ierr = VecResetArray(*global);CHKERRQ(ierr);
18547c6ae99SBarry Smith     ierr = DMRestoreGlobalVector(mine->dm,global);CHKERRQ(ierr);
18647c6ae99SBarry Smith   }
18747c6ae99SBarry Smith   PetscFunctionReturn(0);
18847c6ae99SBarry Smith }
18947c6ae99SBarry Smith 
19047c6ae99SBarry Smith #undef __FUNCT__
19147c6ae99SBarry Smith #define __FUNCT__ "DMCompositeScatter_Array"
19247c6ae99SBarry Smith PetscErrorCode DMCompositeScatter_Array(DM dm,struct DMCompositeLink *mine,Vec vec,PetscScalar *array)
19347c6ae99SBarry Smith {
19447c6ae99SBarry Smith   PetscErrorCode ierr;
19547c6ae99SBarry Smith   PetscScalar    *varray;
19647c6ae99SBarry Smith   PetscMPIInt    rank;
19747c6ae99SBarry Smith 
19847c6ae99SBarry Smith   PetscFunctionBegin;
19947c6ae99SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)dm)->comm,&rank);CHKERRQ(ierr);
20047c6ae99SBarry Smith   if (rank == mine->rank) {
20147c6ae99SBarry Smith     ierr    = VecGetArray(vec,&varray);CHKERRQ(ierr);
20247c6ae99SBarry Smith     ierr    = PetscMemcpy(array,varray+mine->rstart,mine->n*sizeof(PetscScalar));CHKERRQ(ierr);
20347c6ae99SBarry Smith     ierr    = VecRestoreArray(vec,&varray);CHKERRQ(ierr);
20447c6ae99SBarry Smith   }
20506ebdd98SJed Brown   ierr    = MPI_Bcast(array,mine->nlocal,MPIU_SCALAR,mine->rank,((PetscObject)dm)->comm);CHKERRQ(ierr);
20647c6ae99SBarry Smith   PetscFunctionReturn(0);
20747c6ae99SBarry Smith }
20847c6ae99SBarry Smith 
20947c6ae99SBarry Smith #undef __FUNCT__
21047c6ae99SBarry Smith #define __FUNCT__ "DMCompositeScatter_DM"
21147c6ae99SBarry Smith PetscErrorCode DMCompositeScatter_DM(DM dm,struct DMCompositeLink *mine,Vec vec,Vec local)
21247c6ae99SBarry Smith {
21347c6ae99SBarry Smith   PetscErrorCode ierr;
21447c6ae99SBarry Smith   PetscScalar    *array;
21547c6ae99SBarry Smith   Vec            global;
21647c6ae99SBarry Smith 
21747c6ae99SBarry Smith   PetscFunctionBegin;
21847c6ae99SBarry Smith   ierr = DMGetGlobalVector(mine->dm,&global);CHKERRQ(ierr);
21947c6ae99SBarry Smith   ierr = VecGetArray(vec,&array);CHKERRQ(ierr);
22047c6ae99SBarry Smith   ierr = VecPlaceArray(global,array+mine->rstart);CHKERRQ(ierr);
22147c6ae99SBarry Smith   ierr = DMGlobalToLocalBegin(mine->dm,global,INSERT_VALUES,local);CHKERRQ(ierr);
22247c6ae99SBarry Smith   ierr = DMGlobalToLocalEnd(mine->dm,global,INSERT_VALUES,local);CHKERRQ(ierr);
22347c6ae99SBarry Smith   ierr = VecRestoreArray(vec,&array);CHKERRQ(ierr);
22447c6ae99SBarry Smith   ierr = VecResetArray(global);CHKERRQ(ierr);
22547c6ae99SBarry Smith   ierr = DMRestoreGlobalVector(mine->dm,&global);CHKERRQ(ierr);
22647c6ae99SBarry Smith   PetscFunctionReturn(0);
22747c6ae99SBarry Smith }
22847c6ae99SBarry Smith 
22947c6ae99SBarry Smith #undef __FUNCT__
23047c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGather_Array"
231acc1e270SJed Brown PetscErrorCode DMCompositeGather_Array(DM dm,struct DMCompositeLink *mine,Vec vec,InsertMode imode,const PetscScalar *array)
23247c6ae99SBarry Smith {
23347c6ae99SBarry Smith   PetscErrorCode ierr;
23447c6ae99SBarry Smith   PetscScalar    *varray;
23547c6ae99SBarry Smith   PetscMPIInt    rank;
23647c6ae99SBarry Smith 
23747c6ae99SBarry Smith   PetscFunctionBegin;
23847c6ae99SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)dm)->comm,&rank);CHKERRQ(ierr);
23947c6ae99SBarry Smith   if (rank == mine->rank) {
24047c6ae99SBarry Smith     ierr = VecGetArray(vec,&varray);CHKERRQ(ierr);
24147c6ae99SBarry Smith     if (varray+mine->rstart == array) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"You need not DMCompositeGather() into objects obtained via DMCompositeGetAccess()");
242acc1e270SJed Brown   }
243df0c820aSJed Brown   switch (imode) {
244df0c820aSJed Brown   case INSERT_VALUES:
245acc1e270SJed Brown     if (rank == mine->rank) {
24647c6ae99SBarry Smith       ierr = PetscMemcpy(varray+mine->rstart,array,mine->n*sizeof(PetscScalar));CHKERRQ(ierr);
247acc1e270SJed Brown     }
248df0c820aSJed Brown     break;
249df0c820aSJed Brown   case ADD_VALUES: {
250df0c820aSJed Brown     PetscInt          i;
251760fd489SMatthew G Knepley     void             *source;
252acc1e270SJed Brown     PetscScalar       *buffer,*dest;
253acc1e270SJed Brown     if (rank == mine->rank) {
254acc1e270SJed Brown       dest = &varray[mine->rstart];
255acc1e270SJed Brown #if defined(PETSC_HAVE_MPI_IN_PLACE)
256acc1e270SJed Brown       buffer = dest;
257acc1e270SJed Brown       source = MPI_IN_PLACE;
258acc1e270SJed Brown #else
25906ebdd98SJed Brown       ierr = PetscMalloc(mine->nlocal*sizeof(PetscScalar),&buffer);CHKERRQ(ierr);
260760fd489SMatthew G Knepley       source = (void *) buffer;
261acc1e270SJed Brown #endif
26206ebdd98SJed Brown       for (i=0; i<mine->nlocal; i++) buffer[i] = varray[mine->rstart+i] + array[i];
263acc1e270SJed Brown     } else {
264760fd489SMatthew G Knepley       source = (void *) array;
265acc1e270SJed Brown       dest   = PETSC_NULL;
266acc1e270SJed Brown     }
26706ebdd98SJed Brown     ierr = MPI_Reduce(source,dest,mine->nlocal,MPIU_SCALAR,MPI_SUM,mine->rank,((PetscObject)dm)->comm);CHKERRQ(ierr);
268acc1e270SJed Brown #if !defined(PETSC_HAVE_MPI_IN_PLACE)
269acc1e270SJed Brown     if (rank == mine->rank) {ierr = PetscFree(source);CHKERRQ(ierr);}
270acc1e270SJed Brown #endif
271df0c820aSJed Brown   } break;
272df0c820aSJed Brown   default: SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"imode");
273df0c820aSJed Brown   }
274acc1e270SJed Brown   if (rank == mine->rank) {ierr = VecRestoreArray(vec,&varray);CHKERRQ(ierr);}
27547c6ae99SBarry Smith   PetscFunctionReturn(0);
27647c6ae99SBarry Smith }
27747c6ae99SBarry Smith 
27847c6ae99SBarry Smith #undef __FUNCT__
27947c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGather_DM"
280df0c820aSJed Brown PetscErrorCode DMCompositeGather_DM(DM dm,struct DMCompositeLink *mine,Vec vec,InsertMode imode,Vec local)
28147c6ae99SBarry Smith {
28247c6ae99SBarry Smith   PetscErrorCode ierr;
28347c6ae99SBarry Smith   PetscScalar    *array;
28447c6ae99SBarry Smith   Vec            global;
28547c6ae99SBarry Smith 
28647c6ae99SBarry Smith   PetscFunctionBegin;
28747c6ae99SBarry Smith   ierr = DMGetGlobalVector(mine->dm,&global);CHKERRQ(ierr);
28847c6ae99SBarry Smith   ierr = VecGetArray(vec,&array);CHKERRQ(ierr);
28947c6ae99SBarry Smith   ierr = VecPlaceArray(global,array+mine->rstart);CHKERRQ(ierr);
290df0c820aSJed Brown   ierr = DMLocalToGlobalBegin(mine->dm,local,imode,global);CHKERRQ(ierr);
291df0c820aSJed Brown   ierr = DMLocalToGlobalEnd(mine->dm,local,imode,global);CHKERRQ(ierr);
29247c6ae99SBarry Smith   ierr = VecRestoreArray(vec,&array);CHKERRQ(ierr);
29347c6ae99SBarry Smith   ierr = VecResetArray(global);CHKERRQ(ierr);
29447c6ae99SBarry Smith   ierr = DMRestoreGlobalVector(mine->dm,&global);CHKERRQ(ierr);
29547c6ae99SBarry Smith   PetscFunctionReturn(0);
29647c6ae99SBarry Smith }
29747c6ae99SBarry Smith 
29847c6ae99SBarry Smith /* ----------------------------------------------------------------------------------*/
29947c6ae99SBarry Smith 
30047c6ae99SBarry Smith #include <stdarg.h>
30147c6ae99SBarry Smith 
30247c6ae99SBarry Smith #undef __FUNCT__
30347c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetNumberDM"
30447c6ae99SBarry Smith /*@C
30547c6ae99SBarry Smith     DMCompositeGetNumberDM - Get's the number of DM objects in the DMComposite
30647c6ae99SBarry Smith        representation.
30747c6ae99SBarry Smith 
30847c6ae99SBarry Smith     Not Collective
30947c6ae99SBarry Smith 
31047c6ae99SBarry Smith     Input Parameter:
31147c6ae99SBarry Smith .    dm - the packer object
31247c6ae99SBarry Smith 
31347c6ae99SBarry Smith     Output Parameter:
31447c6ae99SBarry Smith .     nDM - the number of DMs
31547c6ae99SBarry Smith 
31647c6ae99SBarry Smith     Level: beginner
31747c6ae99SBarry Smith 
31847c6ae99SBarry Smith @*/
3197087cfbeSBarry Smith PetscErrorCode  DMCompositeGetNumberDM(DM dm,PetscInt *nDM)
32047c6ae99SBarry Smith {
32147c6ae99SBarry Smith   DM_Composite *com = (DM_Composite*)dm->data;
32247c6ae99SBarry Smith   PetscFunctionBegin;
32347c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32447c6ae99SBarry Smith   *nDM = com->nDM;
32547c6ae99SBarry Smith   PetscFunctionReturn(0);
32647c6ae99SBarry Smith }
32747c6ae99SBarry Smith 
32847c6ae99SBarry Smith 
32947c6ae99SBarry Smith #undef __FUNCT__
33047c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetAccess"
33147c6ae99SBarry Smith /*@C
33247c6ae99SBarry Smith     DMCompositeGetAccess - Allows one to access the individual packed vectors in their global
33347c6ae99SBarry Smith        representation.
33447c6ae99SBarry Smith 
33547c6ae99SBarry Smith     Collective on DMComposite
33647c6ae99SBarry Smith 
33747c6ae99SBarry Smith     Input Parameter:
33847c6ae99SBarry Smith +    dm - the packer object
33947c6ae99SBarry Smith .    gvec - the global vector
34047c6ae99SBarry Smith -    ... - the individual sequential or parallel objects (arrays or vectors)
34147c6ae99SBarry Smith 
34247c6ae99SBarry Smith     Notes: Use DMCompositeRestoreAccess() to return the vectors when you no longer need them
34347c6ae99SBarry Smith 
34447c6ae99SBarry Smith     Level: advanced
34547c6ae99SBarry Smith 
34647c6ae99SBarry Smith @*/
3477087cfbeSBarry Smith PetscErrorCode  DMCompositeGetAccess(DM dm,Vec gvec,...)
34847c6ae99SBarry Smith {
34947c6ae99SBarry Smith   va_list                Argp;
35047c6ae99SBarry Smith   PetscErrorCode         ierr;
35147c6ae99SBarry Smith   struct DMCompositeLink *next;
35247c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
35347c6ae99SBarry Smith 
35447c6ae99SBarry Smith   PetscFunctionBegin;
35547c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35647c6ae99SBarry Smith   PetscValidHeaderSpecific(gvec,VEC_CLASSID,2);
35747c6ae99SBarry Smith   next = com->next;
35847c6ae99SBarry Smith   if (!com->setup) {
359d7bf68aeSBarry Smith     ierr = DMSetUp(dm);CHKERRQ(ierr);
36047c6ae99SBarry Smith   }
36147c6ae99SBarry Smith 
36247c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
36347c6ae99SBarry Smith   va_start(Argp,gvec);
36447c6ae99SBarry Smith   while (next) {
36547c6ae99SBarry Smith     if (next->type == DMCOMPOSITE_ARRAY) {
36647c6ae99SBarry Smith       PetscScalar **array;
36747c6ae99SBarry Smith       array = va_arg(Argp, PetscScalar**);
36847c6ae99SBarry Smith       ierr  = DMCompositeGetAccess_Array(dm,next,gvec,array);CHKERRQ(ierr);
36947c6ae99SBarry Smith     } else if (next->type == DMCOMPOSITE_DM) {
37047c6ae99SBarry Smith       Vec *vec;
37147c6ae99SBarry Smith       vec  = va_arg(Argp, Vec*);
37247c6ae99SBarry Smith       ierr = DMCompositeGetAccess_DM(dm,next,gvec,vec);CHKERRQ(ierr);
37347c6ae99SBarry Smith     } else {
37447c6ae99SBarry Smith       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
37547c6ae99SBarry Smith     }
37647c6ae99SBarry Smith     next = next->next;
37747c6ae99SBarry Smith   }
37847c6ae99SBarry Smith   va_end(Argp);
37947c6ae99SBarry Smith   PetscFunctionReturn(0);
38047c6ae99SBarry Smith }
38147c6ae99SBarry Smith 
38247c6ae99SBarry Smith #undef __FUNCT__
38347c6ae99SBarry Smith #define __FUNCT__ "DMCompositeRestoreAccess"
38447c6ae99SBarry Smith /*@C
385aa219208SBarry Smith     DMCompositeRestoreAccess - Returns the vectors obtained with DMCompositeGetAccess()
38647c6ae99SBarry Smith        representation.
38747c6ae99SBarry Smith 
38847c6ae99SBarry Smith     Collective on DMComposite
38947c6ae99SBarry Smith 
39047c6ae99SBarry Smith     Input Parameter:
39147c6ae99SBarry Smith +    dm - the packer object
39247c6ae99SBarry Smith .    gvec - the global vector
39347c6ae99SBarry Smith -    ... - the individual sequential or parallel objects (arrays or vectors)
39447c6ae99SBarry Smith 
39547c6ae99SBarry Smith     Level: advanced
39647c6ae99SBarry Smith 
3970c010503SBarry Smith .seealso  DMCompositeAddArray(), DMCompositeAddDM(), DMCreateGlobalVector(),
3986eb61c8cSJed Brown          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeScatter(),
399aa219208SBarry Smith          DMCompositeRestoreAccess(), DMCompositeGetAccess()
40047c6ae99SBarry Smith 
40147c6ae99SBarry Smith @*/
4027087cfbeSBarry Smith PetscErrorCode  DMCompositeRestoreAccess(DM dm,Vec gvec,...)
40347c6ae99SBarry Smith {
40447c6ae99SBarry Smith   va_list                Argp;
40547c6ae99SBarry Smith   PetscErrorCode         ierr;
40647c6ae99SBarry Smith   struct DMCompositeLink *next;
40747c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
40847c6ae99SBarry Smith 
40947c6ae99SBarry Smith   PetscFunctionBegin;
41047c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41147c6ae99SBarry Smith   PetscValidHeaderSpecific(gvec,VEC_CLASSID,2);
41247c6ae99SBarry Smith   next = com->next;
41347c6ae99SBarry Smith   if (!com->setup) {
414d7bf68aeSBarry Smith     ierr = DMSetUp(dm);CHKERRQ(ierr);
41547c6ae99SBarry Smith   }
41647c6ae99SBarry Smith 
41747c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
41847c6ae99SBarry Smith   va_start(Argp,gvec);
41947c6ae99SBarry Smith   while (next) {
42047c6ae99SBarry Smith     if (next->type == DMCOMPOSITE_ARRAY) {
42147c6ae99SBarry Smith       PetscScalar **array;
42247c6ae99SBarry Smith       array = va_arg(Argp, PetscScalar**);
42347c6ae99SBarry Smith       ierr  = DMCompositeRestoreAccess_Array(dm,next,gvec,array);CHKERRQ(ierr);
42447c6ae99SBarry Smith     } else if (next->type == DMCOMPOSITE_DM) {
42547c6ae99SBarry Smith       Vec *vec;
42647c6ae99SBarry Smith       vec  = va_arg(Argp, Vec*);
42747c6ae99SBarry Smith       ierr = DMCompositeRestoreAccess_DM(dm,next,gvec,vec);CHKERRQ(ierr);
42847c6ae99SBarry Smith     } else {
42947c6ae99SBarry Smith       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
43047c6ae99SBarry Smith     }
43147c6ae99SBarry Smith     next = next->next;
43247c6ae99SBarry Smith   }
43347c6ae99SBarry Smith   va_end(Argp);
43447c6ae99SBarry Smith   PetscFunctionReturn(0);
43547c6ae99SBarry Smith }
43647c6ae99SBarry Smith 
43747c6ae99SBarry Smith #undef __FUNCT__
43847c6ae99SBarry Smith #define __FUNCT__ "DMCompositeScatter"
43947c6ae99SBarry Smith /*@C
44047c6ae99SBarry Smith     DMCompositeScatter - Scatters from a global packed vector into its individual local vectors
44147c6ae99SBarry Smith 
44247c6ae99SBarry Smith     Collective on DMComposite
44347c6ae99SBarry Smith 
44447c6ae99SBarry Smith     Input Parameter:
44547c6ae99SBarry Smith +    dm - the packer object
44647c6ae99SBarry Smith .    gvec - the global vector
4478fd8f222SJed Brown -    ... - the individual sequential objects (arrays or vectors), PETSC_NULL for those that are not needed
44847c6ae99SBarry Smith 
44947c6ae99SBarry Smith     Level: advanced
45047c6ae99SBarry Smith 
4510c010503SBarry Smith .seealso DMDestroy(), DMCompositeAddArray(), DMCompositeAddDM(), DMCreateGlobalVector(),
4526eb61c8cSJed Brown          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
45347c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries()
45447c6ae99SBarry Smith 
45547c6ae99SBarry Smith @*/
4567087cfbeSBarry Smith PetscErrorCode  DMCompositeScatter(DM dm,Vec gvec,...)
45747c6ae99SBarry Smith {
45847c6ae99SBarry Smith   va_list                Argp;
45947c6ae99SBarry Smith   PetscErrorCode         ierr;
46047c6ae99SBarry Smith   struct DMCompositeLink *next;
4618fd8f222SJed Brown   PetscInt               cnt;
46247c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
46347c6ae99SBarry Smith 
46447c6ae99SBarry Smith   PetscFunctionBegin;
46547c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
46647c6ae99SBarry Smith   PetscValidHeaderSpecific(gvec,VEC_CLASSID,2);
46747c6ae99SBarry Smith   if (!com->setup) {
468d7bf68aeSBarry Smith     ierr = DMSetUp(dm);CHKERRQ(ierr);
46947c6ae99SBarry Smith   }
47047c6ae99SBarry Smith 
47147c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
47247c6ae99SBarry Smith   va_start(Argp,gvec);
4738fd8f222SJed Brown   for (cnt=3,next=com->next; next; cnt++,next=next->next) {
47447c6ae99SBarry Smith     if (next->type == DMCOMPOSITE_ARRAY) {
47547c6ae99SBarry Smith       PetscScalar *array;
47647c6ae99SBarry Smith       array = va_arg(Argp, PetscScalar*);
477c72eef85SJed Brown       if (array) PetscValidScalarPointer(array,cnt);
478c72eef85SJed Brown       PetscValidLogicalCollectiveBool(dm,!!array,cnt);
4798fd8f222SJed Brown       if (!array) continue;
48047c6ae99SBarry Smith       ierr = DMCompositeScatter_Array(dm,next,gvec,array);CHKERRQ(ierr);
48147c6ae99SBarry Smith     } else if (next->type == DMCOMPOSITE_DM) {
48247c6ae99SBarry Smith       Vec vec;
48347c6ae99SBarry Smith       vec = va_arg(Argp, Vec);
4848fd8f222SJed Brown       if (!vec) continue;
48547c6ae99SBarry Smith       PetscValidHeaderSpecific(vec,VEC_CLASSID,cnt);
48647c6ae99SBarry Smith       ierr = DMCompositeScatter_DM(dm,next,gvec,vec);CHKERRQ(ierr);
48747c6ae99SBarry Smith     } else {
48847c6ae99SBarry Smith       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
48947c6ae99SBarry Smith     }
49047c6ae99SBarry Smith   }
49147c6ae99SBarry Smith   va_end(Argp);
49247c6ae99SBarry Smith   PetscFunctionReturn(0);
49347c6ae99SBarry Smith }
49447c6ae99SBarry Smith 
49547c6ae99SBarry Smith #undef __FUNCT__
49647c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGather"
49747c6ae99SBarry Smith /*@C
49847c6ae99SBarry Smith     DMCompositeGather - Gathers into a global packed vector from its individual local vectors
49947c6ae99SBarry Smith 
50047c6ae99SBarry Smith     Collective on DMComposite
50147c6ae99SBarry Smith 
50247c6ae99SBarry Smith     Input Parameter:
50347c6ae99SBarry Smith +    dm - the packer object
50447c6ae99SBarry Smith .    gvec - the global vector
5058fd8f222SJed Brown -    ... - the individual sequential objects (arrays or vectors), PETSC_NULL for any that are not needed
50647c6ae99SBarry Smith 
50747c6ae99SBarry Smith     Level: advanced
50847c6ae99SBarry Smith 
5090c010503SBarry Smith .seealso DMDestroy(), DMCompositeAddArray(), DMCompositeAddDM(), DMCreateGlobalVector(),
5106eb61c8cSJed Brown          DMCompositeScatter(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
51147c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries()
51247c6ae99SBarry Smith 
51347c6ae99SBarry Smith @*/
5147087cfbeSBarry Smith PetscErrorCode  DMCompositeGather(DM dm,Vec gvec,InsertMode imode,...)
51547c6ae99SBarry Smith {
51647c6ae99SBarry Smith   va_list                Argp;
51747c6ae99SBarry Smith   PetscErrorCode         ierr;
51847c6ae99SBarry Smith   struct DMCompositeLink *next;
51947c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
5208fd8f222SJed Brown   PetscInt               cnt;
52147c6ae99SBarry Smith 
52247c6ae99SBarry Smith   PetscFunctionBegin;
52347c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
52447c6ae99SBarry Smith   PetscValidHeaderSpecific(gvec,VEC_CLASSID,2);
52547c6ae99SBarry Smith   if (!com->setup) {
526d7bf68aeSBarry Smith     ierr = DMSetUp(dm);CHKERRQ(ierr);
52747c6ae99SBarry Smith   }
52847c6ae99SBarry Smith 
52947c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
530df0c820aSJed Brown   va_start(Argp,imode);
5318fd8f222SJed Brown   for (cnt=3,next=com->next; next; cnt++,next=next->next) {
53247c6ae99SBarry Smith     if (next->type == DMCOMPOSITE_ARRAY) {
53347c6ae99SBarry Smith       PetscScalar *array;
53447c6ae99SBarry Smith       array = va_arg(Argp, PetscScalar*);
5358fd8f222SJed Brown       if (!array) continue;
5368fd8f222SJed Brown       PetscValidScalarPointer(array,cnt);
537df0c820aSJed Brown       ierr  = DMCompositeGather_Array(dm,next,gvec,imode,array);CHKERRQ(ierr);
53847c6ae99SBarry Smith     } else if (next->type == DMCOMPOSITE_DM) {
53947c6ae99SBarry Smith       Vec vec;
54047c6ae99SBarry Smith       vec = va_arg(Argp, Vec);
5418fd8f222SJed Brown       if (!vec) continue;
5428fd8f222SJed Brown       PetscValidHeaderSpecific(vec,VEC_CLASSID,cnt);
543df0c820aSJed Brown       ierr = DMCompositeGather_DM(dm,next,gvec,imode,vec);CHKERRQ(ierr);
54447c6ae99SBarry Smith     } else {
54547c6ae99SBarry Smith       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
54647c6ae99SBarry Smith     }
54747c6ae99SBarry Smith   }
54847c6ae99SBarry Smith   va_end(Argp);
54947c6ae99SBarry Smith   PetscFunctionReturn(0);
55047c6ae99SBarry Smith }
55147c6ae99SBarry Smith 
55247c6ae99SBarry Smith #undef __FUNCT__
55347c6ae99SBarry Smith #define __FUNCT__ "DMCompositeAddArray"
55447c6ae99SBarry Smith /*@C
55547c6ae99SBarry Smith     DMCompositeAddArray - adds an "redundant" array to a DMComposite. The array values will
55647c6ae99SBarry Smith        be stored in part of the array on process orank.
55747c6ae99SBarry Smith 
55847c6ae99SBarry Smith     Collective on DMComposite
55947c6ae99SBarry Smith 
56047c6ae99SBarry Smith     Input Parameter:
56147c6ae99SBarry Smith +    dm - the packer object
56247c6ae99SBarry Smith .    orank - the process on which the array entries officially live, this number must be
56347c6ae99SBarry Smith              the same on all processes.
56447c6ae99SBarry Smith -    n - the length of the array
56547c6ae99SBarry Smith 
56647c6ae99SBarry Smith     Level: advanced
56747c6ae99SBarry Smith 
5680c010503SBarry Smith .seealso DMDestroy(), DMCompositeGather(), DMCompositeAddDM(), DMCreateGlobalVector(),
5696eb61c8cSJed Brown          DMCompositeScatter(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
57047c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries()
57147c6ae99SBarry Smith 
57247c6ae99SBarry Smith @*/
5737087cfbeSBarry Smith PetscErrorCode  DMCompositeAddArray(DM dm,PetscMPIInt orank,PetscInt n)
57447c6ae99SBarry Smith {
57547c6ae99SBarry Smith   struct DMCompositeLink *mine,*next;
57647c6ae99SBarry Smith   PetscErrorCode         ierr;
57747c6ae99SBarry Smith   PetscMPIInt            rank;
57847c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
57947c6ae99SBarry Smith 
58047c6ae99SBarry Smith   PetscFunctionBegin;
58147c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58247c6ae99SBarry Smith   next = com->next;
58347c6ae99SBarry Smith   if (com->setup) {
58447c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Cannot add an array once you have used the DMComposite");
58547c6ae99SBarry Smith   }
58647c6ae99SBarry Smith #if defined(PETSC_USE_DEBUG)
58747c6ae99SBarry Smith   {
58847c6ae99SBarry Smith     PetscMPIInt orankmax;
58947c6ae99SBarry Smith     ierr = MPI_Allreduce(&orank,&orankmax,1,MPI_INT,MPI_MAX,((PetscObject)dm)->comm);CHKERRQ(ierr);
59047c6ae99SBarry Smith     if (orank != orankmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"orank %d must be equal on all processes, another process has value %d",orank,orankmax);
59147c6ae99SBarry Smith   }
59247c6ae99SBarry Smith #endif
59347c6ae99SBarry Smith 
59447c6ae99SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)dm)->comm,&rank);CHKERRQ(ierr);
59547c6ae99SBarry Smith   /* create new link */
59647c6ae99SBarry Smith   ierr                = PetscNew(struct DMCompositeLink,&mine);CHKERRQ(ierr);
59706ebdd98SJed Brown   mine->nlocal        = n;
59806ebdd98SJed Brown   mine->n             = (rank == orank) ? n : 0;
59947c6ae99SBarry Smith   mine->rank          = orank;
60047c6ae99SBarry Smith   mine->dm            = PETSC_NULL;
60147c6ae99SBarry Smith   mine->type          = DMCOMPOSITE_ARRAY;
60247c6ae99SBarry Smith   mine->next          = PETSC_NULL;
60347c6ae99SBarry Smith   if (rank == mine->rank) {com->n += n;com->nmine++;}
60447c6ae99SBarry Smith 
60547c6ae99SBarry Smith   /* add to end of list */
60647c6ae99SBarry Smith   if (!next) {
60747c6ae99SBarry Smith     com->next = mine;
60847c6ae99SBarry Smith   } else {
60947c6ae99SBarry Smith     while (next->next) next = next->next;
61047c6ae99SBarry Smith     next->next = mine;
61147c6ae99SBarry Smith   }
61247c6ae99SBarry Smith   com->nredundant++;
61347c6ae99SBarry Smith   PetscFunctionReturn(0);
61447c6ae99SBarry Smith }
61547c6ae99SBarry Smith 
61647c6ae99SBarry Smith #undef __FUNCT__
61747c6ae99SBarry Smith #define __FUNCT__ "DMCompositeAddDM"
61847c6ae99SBarry Smith /*@C
619aa219208SBarry Smith     DMCompositeAddDM - adds a DM  vector to a DMComposite
62047c6ae99SBarry Smith 
62147c6ae99SBarry Smith     Collective on DMComposite
62247c6ae99SBarry Smith 
62347c6ae99SBarry Smith     Input Parameter:
62447c6ae99SBarry Smith +    dm - the packer object
62547c6ae99SBarry Smith -    dm - the DM object, if the DM is a da you will need to caste it with a (DM)
62647c6ae99SBarry Smith 
62747c6ae99SBarry Smith     Level: advanced
62847c6ae99SBarry Smith 
6290c010503SBarry Smith .seealso DMDestroy(), DMCompositeGather(), DMCompositeAddDM(), DMCreateGlobalVector(),
6306eb61c8cSJed Brown          DMCompositeScatter(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
63147c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries()
63247c6ae99SBarry Smith 
63347c6ae99SBarry Smith @*/
6347087cfbeSBarry Smith PetscErrorCode  DMCompositeAddDM(DM dmc,DM dm)
63547c6ae99SBarry Smith {
63647c6ae99SBarry Smith   PetscErrorCode         ierr;
63706ebdd98SJed Brown   PetscInt               n,nlocal;
63847c6ae99SBarry Smith   struct DMCompositeLink *mine,*next;
63906ebdd98SJed Brown   Vec                    global,local;
64047c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dmc->data;
64147c6ae99SBarry Smith 
64247c6ae99SBarry Smith   PetscFunctionBegin;
64347c6ae99SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
64447c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
64547c6ae99SBarry Smith   next = com->next;
646aa219208SBarry Smith   if (com->setup) SETERRQ(((PetscObject)dmc)->comm,PETSC_ERR_ARG_WRONGSTATE,"Cannot add a DM once you have used the DMComposite");
64747c6ae99SBarry Smith 
64847c6ae99SBarry Smith   /* create new link */
64947c6ae99SBarry Smith   ierr = PetscNew(struct DMCompositeLink,&mine);CHKERRQ(ierr);
65047c6ae99SBarry Smith   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
65147c6ae99SBarry Smith   ierr = DMGetGlobalVector(dm,&global);CHKERRQ(ierr);
65247c6ae99SBarry Smith   ierr = VecGetLocalSize(global,&n);CHKERRQ(ierr);
65347c6ae99SBarry Smith   ierr = DMRestoreGlobalVector(dm,&global);CHKERRQ(ierr);
65406ebdd98SJed Brown   ierr = DMGetLocalVector(dm,&local);CHKERRQ(ierr);
65506ebdd98SJed Brown   ierr = VecGetSize(local,&nlocal);CHKERRQ(ierr);
65606ebdd98SJed Brown   ierr = DMRestoreLocalVector(dm,&local);CHKERRQ(ierr);
65747c6ae99SBarry Smith   mine->n      = n;
65806ebdd98SJed Brown   mine->nlocal = nlocal;
65947c6ae99SBarry Smith   mine->dm     = dm;
66047c6ae99SBarry Smith   mine->type   = DMCOMPOSITE_DM;
66147c6ae99SBarry Smith   mine->next   = PETSC_NULL;
66247c6ae99SBarry Smith   com->n       += n;
66347c6ae99SBarry Smith 
66447c6ae99SBarry Smith   /* add to end of list */
66547c6ae99SBarry Smith   if (!next) {
66647c6ae99SBarry Smith     com->next = mine;
66747c6ae99SBarry Smith   } else {
66847c6ae99SBarry Smith     while (next->next) next = next->next;
66947c6ae99SBarry Smith     next->next = mine;
67047c6ae99SBarry Smith   }
67147c6ae99SBarry Smith   com->nDM++;
67247c6ae99SBarry Smith   com->nmine++;
67347c6ae99SBarry Smith   PetscFunctionReturn(0);
67447c6ae99SBarry Smith }
67547c6ae99SBarry Smith 
6767087cfbeSBarry Smith extern PetscErrorCode  VecView_MPI(Vec,PetscViewer);
67747c6ae99SBarry Smith EXTERN_C_BEGIN
67847c6ae99SBarry Smith #undef __FUNCT__
67947c6ae99SBarry Smith #define __FUNCT__ "VecView_DMComposite"
6807087cfbeSBarry Smith PetscErrorCode  VecView_DMComposite(Vec gvec,PetscViewer viewer)
68147c6ae99SBarry Smith {
68247c6ae99SBarry Smith   DM                     dm;
68347c6ae99SBarry Smith   PetscErrorCode         ierr;
68447c6ae99SBarry Smith   struct DMCompositeLink *next;
68547c6ae99SBarry Smith   PetscBool              isdraw;
686cef07954SSatish Balay   DM_Composite           *com;
68747c6ae99SBarry Smith 
68847c6ae99SBarry Smith   PetscFunctionBegin;
6893c0c59f3SBarry Smith   ierr = PetscObjectQuery((PetscObject)gvec,"DM",(PetscObject*)&dm);CHKERRQ(ierr);
69047c6ae99SBarry Smith   if (!dm) SETERRQ(((PetscObject)gvec)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMComposite");
69147c6ae99SBarry Smith   com = (DM_Composite*)dm->data;
69247c6ae99SBarry Smith   next = com->next;
69347c6ae99SBarry Smith 
69447c6ae99SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
69547c6ae99SBarry Smith   if (!isdraw) {
69647c6ae99SBarry Smith     /* do I really want to call this? */
69747c6ae99SBarry Smith     ierr = VecView_MPI(gvec,viewer);CHKERRQ(ierr);
69847c6ae99SBarry Smith   } else {
69947c6ae99SBarry Smith     PetscInt cnt = 0;
70047c6ae99SBarry Smith 
70147c6ae99SBarry Smith     /* loop over packed objects, handling one at at time */
70247c6ae99SBarry Smith     while (next) {
70347c6ae99SBarry Smith       if (next->type == DMCOMPOSITE_ARRAY) {
70447c6ae99SBarry Smith 	PetscScalar *array;
70547c6ae99SBarry Smith 	ierr  = DMCompositeGetAccess_Array(dm,next,gvec,&array);CHKERRQ(ierr);
70647c6ae99SBarry Smith 
70747c6ae99SBarry Smith 	/*skip it for now */
70847c6ae99SBarry Smith       } else if (next->type == DMCOMPOSITE_DM) {
70947c6ae99SBarry Smith 	Vec      vec;
71047c6ae99SBarry Smith         PetscInt bs;
71147c6ae99SBarry Smith 
71247c6ae99SBarry Smith 	ierr = DMCompositeGetAccess_DM(dm,next,gvec,&vec);CHKERRQ(ierr);
71347c6ae99SBarry Smith 	ierr = VecView(vec,viewer);CHKERRQ(ierr);
71447c6ae99SBarry Smith         ierr = VecGetBlockSize(vec,&bs);CHKERRQ(ierr);
71547c6ae99SBarry Smith 	ierr = DMCompositeRestoreAccess_DM(dm,next,gvec,&vec);CHKERRQ(ierr);
71647c6ae99SBarry Smith         ierr = PetscViewerDrawBaseAdd(viewer,bs);CHKERRQ(ierr);
71747c6ae99SBarry Smith         cnt += bs;
71847c6ae99SBarry Smith       } else {
71947c6ae99SBarry Smith 	SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
72047c6ae99SBarry Smith       }
72147c6ae99SBarry Smith       next = next->next;
72247c6ae99SBarry Smith     }
72347c6ae99SBarry Smith     ierr = PetscViewerDrawBaseAdd(viewer,-cnt);CHKERRQ(ierr);
72447c6ae99SBarry Smith   }
72547c6ae99SBarry Smith   PetscFunctionReturn(0);
72647c6ae99SBarry Smith }
72747c6ae99SBarry Smith EXTERN_C_END
72847c6ae99SBarry Smith 
72947c6ae99SBarry Smith 
73047c6ae99SBarry Smith #undef __FUNCT__
7310c010503SBarry Smith #define __FUNCT__ "DMCreateGlobalVector_Composite"
7327087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector_Composite(DM dm,Vec *gvec)
73347c6ae99SBarry Smith {
73447c6ae99SBarry Smith   PetscErrorCode         ierr;
73547c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
73647c6ae99SBarry Smith 
73747c6ae99SBarry Smith   PetscFunctionBegin;
73847c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
73947c6ae99SBarry Smith   if (!com->setup) {
740d7bf68aeSBarry Smith     ierr = DMSetUp(dm);CHKERRQ(ierr);
74147c6ae99SBarry Smith   }
74247c6ae99SBarry Smith   ierr = VecCreateMPI(((PetscObject)dm)->comm,com->n,com->N,gvec);CHKERRQ(ierr);
7433c0c59f3SBarry Smith   ierr = PetscObjectCompose((PetscObject)*gvec,"DM",(PetscObject)dm);CHKERRQ(ierr);
74447c6ae99SBarry Smith   ierr = VecSetOperation(*gvec,VECOP_VIEW,(void(*)(void))VecView_DMComposite);CHKERRQ(ierr);
74547c6ae99SBarry Smith   PetscFunctionReturn(0);
74647c6ae99SBarry Smith }
74747c6ae99SBarry Smith 
74847c6ae99SBarry Smith #undef __FUNCT__
7490c010503SBarry Smith #define __FUNCT__ "DMCreateLocalVector_Composite"
7507087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector_Composite(DM dm,Vec *lvec)
75147c6ae99SBarry Smith {
75247c6ae99SBarry Smith   PetscErrorCode         ierr;
75347c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
75447c6ae99SBarry Smith 
75547c6ae99SBarry Smith   PetscFunctionBegin;
75647c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
75747c6ae99SBarry Smith   if (!com->setup) {
758d7bf68aeSBarry Smith     ierr = DMSetUp(dm);CHKERRQ(ierr);
75947c6ae99SBarry Smith   }
76047c6ae99SBarry Smith   ierr = VecCreateSeq(((PetscObject)dm)->comm,com->nghost,lvec);CHKERRQ(ierr);
7613c0c59f3SBarry Smith   ierr = PetscObjectCompose((PetscObject)*lvec,"DM",(PetscObject)dm);CHKERRQ(ierr);
76247c6ae99SBarry Smith   PetscFunctionReturn(0);
76347c6ae99SBarry Smith }
76447c6ae99SBarry Smith 
76547c6ae99SBarry Smith #undef __FUNCT__
7666eb61c8cSJed Brown #define __FUNCT__ "DMCompositeGetISLocalToGlobalMappings"
76747c6ae99SBarry Smith /*@C
7686eb61c8cSJed Brown     DMCompositeGetISLocalToGlobalMappings - gets an ISLocalToGlobalMapping for each DM/array in the DMComposite, maps to the composite global space
76947c6ae99SBarry Smith 
77006ebdd98SJed Brown     Collective on DM
77147c6ae99SBarry Smith 
77247c6ae99SBarry Smith     Input Parameter:
77347c6ae99SBarry Smith .    dm - the packer object
77447c6ae99SBarry Smith 
77547c6ae99SBarry Smith     Output Parameters:
7766eb61c8cSJed Brown .    ltogs - the individual mappings for each packed vector/array. Note that this includes
777aa219208SBarry Smith            all the ghost points that individual ghosted DMDA's may have. Also each process has an
7786eb61c8cSJed Brown            mapping for EACH redundant array (not just the local redundant arrays).
77947c6ae99SBarry Smith 
78047c6ae99SBarry Smith     Level: advanced
78147c6ae99SBarry Smith 
78247c6ae99SBarry Smith     Notes:
7836eb61c8cSJed Brown        Each entry of ltogs should be destroyed with ISLocalToGlobalMappingDestroy(), the ltogs array should be freed with PetscFree().
78447c6ae99SBarry Smith 
7850c010503SBarry Smith .seealso DMDestroy(), DMCompositeAddArray(), DMCompositeAddDM(), DMCreateGlobalVector(),
78647c6ae99SBarry Smith          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetAccess(), DMCompositeScatter(),
78747c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(),DMCompositeGetEntries()
78847c6ae99SBarry Smith 
78947c6ae99SBarry Smith @*/
7907087cfbeSBarry Smith PetscErrorCode  DMCompositeGetISLocalToGlobalMappings(DM dm,ISLocalToGlobalMapping **ltogs)
79147c6ae99SBarry Smith {
79247c6ae99SBarry Smith   PetscErrorCode         ierr;
79347c6ae99SBarry Smith   PetscInt               i,*idx,n,cnt;
79447c6ae99SBarry Smith   struct DMCompositeLink *next;
79547c6ae99SBarry Smith   PetscMPIInt            rank;
79647c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
79747c6ae99SBarry Smith 
79847c6ae99SBarry Smith   PetscFunctionBegin;
79947c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
800acc1e270SJed Brown   ierr = PetscMalloc((com->nDM+com->nredundant)*sizeof(ISLocalToGlobalMapping),ltogs);CHKERRQ(ierr);
80147c6ae99SBarry Smith   next = com->next;
80247c6ae99SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)dm)->comm,&rank);CHKERRQ(ierr);
80347c6ae99SBarry Smith 
80447c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
80547c6ae99SBarry Smith   cnt = 0;
80647c6ae99SBarry Smith   while (next) {
80747c6ae99SBarry Smith     if (next->type == DMCOMPOSITE_ARRAY) {
80806ebdd98SJed Brown       ierr = PetscMalloc(next->nlocal*sizeof(PetscInt),&idx);CHKERRQ(ierr);
80947c6ae99SBarry Smith       if (rank == next->rank) {
81006ebdd98SJed Brown         for (i=0; i<next->nlocal; i++) idx[i] = next->grstart + i;
81147c6ae99SBarry Smith       }
81206ebdd98SJed Brown       ierr = MPI_Bcast(idx,next->nlocal,MPIU_INT,next->rank,((PetscObject)dm)->comm);CHKERRQ(ierr);
81306ebdd98SJed Brown       ierr = ISLocalToGlobalMappingCreate(((PetscObject)dm)->comm,next->nlocal,idx,PETSC_OWN_POINTER,&(*ltogs)[cnt]);CHKERRQ(ierr);
81447c6ae99SBarry Smith     } else if (next->type == DMCOMPOSITE_DM) {
8156eb61c8cSJed Brown       ISLocalToGlobalMapping ltog;
8166eb61c8cSJed Brown       PetscMPIInt            size;
81786994e45SJed Brown       const PetscInt         *suboff,*indices;
8186eb61c8cSJed Brown       Vec                    global;
81947c6ae99SBarry Smith 
8206eb61c8cSJed Brown       /* Get sub-DM global indices for each local dof */
8211411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(next->dm,&ltog);CHKERRQ(ierr);
8226eb61c8cSJed Brown       ierr = ISLocalToGlobalMappingGetSize(ltog,&n);CHKERRQ(ierr);
82386994e45SJed Brown       ierr = ISLocalToGlobalMappingGetIndices(ltog,&indices);CHKERRQ(ierr);
82447c6ae99SBarry Smith       ierr = PetscMalloc(n*sizeof(PetscInt),&idx);CHKERRQ(ierr);
82547c6ae99SBarry Smith 
8266eb61c8cSJed Brown       /* Get the offsets for the sub-DM global vector */
8276eb61c8cSJed Brown       ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr);
8286eb61c8cSJed Brown       ierr = VecGetOwnershipRanges(global,&suboff);CHKERRQ(ierr);
8296eb61c8cSJed Brown       ierr = MPI_Comm_size(((PetscObject)global)->comm,&size);CHKERRQ(ierr);
8306eb61c8cSJed Brown 
8316eb61c8cSJed Brown       /* Shift the sub-DM definition of the global space to the composite global space */
8326eb61c8cSJed Brown       for (i=0; i<n; i++) {
83386994e45SJed Brown         PetscInt subi = indices[i],lo = 0,hi = size,t;
8346eb61c8cSJed Brown         /* Binary search to find which rank owns subi */
8356eb61c8cSJed Brown         while (hi-lo > 1) {
8366eb61c8cSJed Brown           t = lo + (hi-lo)/2;
8376eb61c8cSJed Brown           if (suboff[t] > subi) hi = t;
8386eb61c8cSJed Brown           else                  lo = t;
8396eb61c8cSJed Brown         }
8406eb61c8cSJed Brown         idx[i] = subi - suboff[lo] + next->grstarts[lo];
8416eb61c8cSJed Brown       }
84286994e45SJed Brown       ierr = ISLocalToGlobalMappingRestoreIndices(ltog,&indices);CHKERRQ(ierr);
8436eb61c8cSJed Brown       ierr = ISLocalToGlobalMappingCreate(((PetscObject)dm)->comm,n,idx,PETSC_OWN_POINTER,&(*ltogs)[cnt]);CHKERRQ(ierr);
8446eb61c8cSJed Brown       ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr);
8456eb61c8cSJed Brown     } else SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
84647c6ae99SBarry Smith     next = next->next;
84747c6ae99SBarry Smith     cnt++;
84847c6ae99SBarry Smith   }
84947c6ae99SBarry Smith   PetscFunctionReturn(0);
85047c6ae99SBarry Smith }
85147c6ae99SBarry Smith 
85247c6ae99SBarry Smith #undef __FUNCT__
85387c85e80SJed Brown #define __FUNCT__ "DMCompositeGetLocalISs"
85487c85e80SJed Brown /*@C
85587c85e80SJed Brown    DMCompositeGetLocalISs - Gets index sets for each DM/array component of a composite local vector
85687c85e80SJed Brown 
85787c85e80SJed Brown    Not Collective
85887c85e80SJed Brown 
85987c85e80SJed Brown    Input Arguments:
86087c85e80SJed Brown . dm - composite DM
86187c85e80SJed Brown 
86287c85e80SJed Brown    Output Arguments:
86387c85e80SJed Brown . is - array of serial index sets for each each component of the DMComposite
86487c85e80SJed Brown 
86587c85e80SJed Brown    Level: intermediate
86687c85e80SJed Brown 
86787c85e80SJed Brown    Notes:
86887c85e80SJed Brown    At present, a composite local vector does not normally exist.  This function is used to provide index sets for
86987c85e80SJed Brown    MatGetLocalSubMatrix().  In the future, the scatters for each entry in the DMComposite may be be merged into a single
87087c85e80SJed Brown    scatter to a composite local vector.
87187c85e80SJed Brown 
87287c85e80SJed Brown    To get the composite global indices at all local points (including ghosts), use DMCompositeGetISLocalToGlobalMappings().
87387c85e80SJed Brown 
87487c85e80SJed Brown    To get index sets for pieces of the composite global vector, use DMCompositeGetGlobalISs().
87587c85e80SJed Brown 
87687c85e80SJed Brown    Each returned IS should be destroyed with ISDestroy(), the array should be freed with PetscFree().
87787c85e80SJed Brown 
87887c85e80SJed Brown .seealso: DMCompositeGetGlobalISs(), DMCompositeGetISLocalToGlobalMappings(), MatGetLocalSubMatrix(), MatCreateLocalRef()
87987c85e80SJed Brown @*/
8807087cfbeSBarry Smith PetscErrorCode  DMCompositeGetLocalISs(DM dm,IS **is)
88187c85e80SJed Brown {
88287c85e80SJed Brown   PetscErrorCode         ierr;
88387c85e80SJed Brown   DM_Composite           *com = (DM_Composite*)dm->data;
88487c85e80SJed Brown   struct DMCompositeLink *link;
88587c85e80SJed Brown   PetscInt cnt,start;
88687c85e80SJed Brown 
88787c85e80SJed Brown   PetscFunctionBegin;
88887c85e80SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
88987c85e80SJed Brown   PetscValidPointer(is,2);
89087c85e80SJed Brown   ierr = PetscMalloc(com->nmine*sizeof(IS),is);CHKERRQ(ierr);
89106ebdd98SJed Brown   for (cnt=0,start=0,link=com->next; link; start+=link->nlocal,cnt++,link=link->next) {
89206ebdd98SJed Brown     ierr = ISCreateStride(PETSC_COMM_SELF,link->nlocal,start,1,&(*is)[cnt]);CHKERRQ(ierr);
893520db06cSJed Brown     if (link->type == DMCOMPOSITE_DM) {
894520db06cSJed Brown       PetscInt bs;
8951411c6eeSJed Brown       ierr = DMGetBlockSize(link->dm,&bs);CHKERRQ(ierr);
896520db06cSJed Brown       ierr = ISSetBlockSize((*is)[cnt],bs);CHKERRQ(ierr);
897520db06cSJed Brown     }
89887c85e80SJed Brown   }
89987c85e80SJed Brown   PetscFunctionReturn(0);
90087c85e80SJed Brown }
90187c85e80SJed Brown 
90287c85e80SJed Brown #undef __FUNCT__
90347c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetGlobalISs"
90447c6ae99SBarry Smith /*@C
90547c6ae99SBarry Smith     DMCompositeGetGlobalISs - Gets the index sets for each composed object
90647c6ae99SBarry Smith 
90747c6ae99SBarry Smith     Collective on DMComposite
90847c6ae99SBarry Smith 
90947c6ae99SBarry Smith     Input Parameter:
91047c6ae99SBarry Smith .    dm - the packer object
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith     Output Parameters:
91347c6ae99SBarry Smith .    is - the array of index sets
91447c6ae99SBarry Smith 
91547c6ae99SBarry Smith     Level: advanced
91647c6ae99SBarry Smith 
91747c6ae99SBarry Smith     Notes:
91847c6ae99SBarry Smith        The is entries should be destroyed with ISDestroy(), the is array should be freed with PetscFree()
91947c6ae99SBarry Smith 
92047c6ae99SBarry Smith        These could be used to extract a subset of vector entries for a "multi-physics" preconditioner
92147c6ae99SBarry Smith 
9226eb61c8cSJed Brown        Use DMCompositeGetLocalISs() for index sets in the packed local numbering, and
9236eb61c8cSJed Brown        DMCompositeGetISLocalToGlobalMappings() for to map local sub-DM (including ghost) indices to packed global
9246eb61c8cSJed Brown        indices.
92547c6ae99SBarry Smith 
9260c010503SBarry Smith .seealso DMDestroy(), DMCompositeAddArray(), DMCompositeAddDM(), DMCreateGlobalVector(),
92747c6ae99SBarry Smith          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetAccess(), DMCompositeScatter(),
92847c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(),DMCompositeGetEntries()
92947c6ae99SBarry Smith 
93047c6ae99SBarry Smith @*/
9316eb61c8cSJed Brown 
9327087cfbeSBarry Smith PetscErrorCode  DMCompositeGetGlobalISs(DM dm,IS *is[])
93347c6ae99SBarry Smith {
93447c6ae99SBarry Smith   PetscErrorCode         ierr;
93547c6ae99SBarry Smith   PetscInt               cnt = 0,*idx,i;
93647c6ae99SBarry Smith   struct DMCompositeLink *next;
93747c6ae99SBarry Smith   PetscMPIInt            rank;
93847c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
93947c6ae99SBarry Smith 
94047c6ae99SBarry Smith   PetscFunctionBegin;
94147c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
94206ebdd98SJed Brown   ierr = PetscMalloc((com->nDM+com->nredundant)*sizeof(IS),is);CHKERRQ(ierr);
94347c6ae99SBarry Smith   next = com->next;
94447c6ae99SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)dm)->comm,&rank);CHKERRQ(ierr);
94547c6ae99SBarry Smith 
94647c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
94747c6ae99SBarry Smith   while (next) {
94847c6ae99SBarry Smith     ierr = PetscMalloc(next->n*sizeof(PetscInt),&idx);CHKERRQ(ierr);
94947c6ae99SBarry Smith     for (i=0; i<next->n; i++) idx[i] = next->grstart + i;
95047c6ae99SBarry Smith     ierr = ISCreateGeneral(((PetscObject)dm)->comm,next->n,idx,PETSC_OWN_POINTER,&(*is)[cnt]);CHKERRQ(ierr);
95147c6ae99SBarry Smith     cnt++;
95247c6ae99SBarry Smith     next = next->next;
95347c6ae99SBarry Smith   }
95447c6ae99SBarry Smith   PetscFunctionReturn(0);
95547c6ae99SBarry Smith }
95647c6ae99SBarry Smith 
95747c6ae99SBarry Smith /* -------------------------------------------------------------------------------------*/
95847c6ae99SBarry Smith #undef __FUNCT__
95947c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetLocalVectors_Array"
96047c6ae99SBarry Smith PetscErrorCode DMCompositeGetLocalVectors_Array(DM dm,struct DMCompositeLink *mine,PetscScalar **array)
96147c6ae99SBarry Smith {
96247c6ae99SBarry Smith   PetscErrorCode ierr;
96347c6ae99SBarry Smith   PetscFunctionBegin;
96447c6ae99SBarry Smith   if (array) {
965c72eef85SJed Brown     ierr = PetscMalloc(mine->nlocal*sizeof(PetscScalar),array);CHKERRQ(ierr);
96647c6ae99SBarry Smith   }
96747c6ae99SBarry Smith   PetscFunctionReturn(0);
96847c6ae99SBarry Smith }
96947c6ae99SBarry Smith 
97047c6ae99SBarry Smith #undef __FUNCT__
97147c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetLocalVectors_DM"
97247c6ae99SBarry Smith PetscErrorCode DMCompositeGetLocalVectors_DM(DM dm,struct DMCompositeLink *mine,Vec *local)
97347c6ae99SBarry Smith {
97447c6ae99SBarry Smith   PetscErrorCode ierr;
97547c6ae99SBarry Smith   PetscFunctionBegin;
97647c6ae99SBarry Smith   if (local) {
97747c6ae99SBarry Smith     ierr = DMGetLocalVector(mine->dm,local);CHKERRQ(ierr);
97847c6ae99SBarry Smith   }
97947c6ae99SBarry Smith   PetscFunctionReturn(0);
98047c6ae99SBarry Smith }
98147c6ae99SBarry Smith 
98247c6ae99SBarry Smith #undef __FUNCT__
98347c6ae99SBarry Smith #define __FUNCT__ "DMCompositeRestoreLocalVectors_Array"
98447c6ae99SBarry Smith PetscErrorCode DMCompositeRestoreLocalVectors_Array(DM dm,struct DMCompositeLink *mine,PetscScalar **array)
98547c6ae99SBarry Smith {
98647c6ae99SBarry Smith   PetscErrorCode ierr;
98747c6ae99SBarry Smith   PetscFunctionBegin;
98847c6ae99SBarry Smith   if (array) {
98947c6ae99SBarry Smith     ierr = PetscFree(*array);CHKERRQ(ierr);
99047c6ae99SBarry Smith   }
99147c6ae99SBarry Smith   PetscFunctionReturn(0);
99247c6ae99SBarry Smith }
99347c6ae99SBarry Smith 
99447c6ae99SBarry Smith #undef __FUNCT__
99547c6ae99SBarry Smith #define __FUNCT__ "DMCompositeRestoreLocalVectors_DM"
99647c6ae99SBarry Smith PetscErrorCode DMCompositeRestoreLocalVectors_DM(DM dm,struct DMCompositeLink *mine,Vec *local)
99747c6ae99SBarry Smith {
99847c6ae99SBarry Smith   PetscErrorCode ierr;
99947c6ae99SBarry Smith   PetscFunctionBegin;
100047c6ae99SBarry Smith   if (local) {
100147c6ae99SBarry Smith     ierr = DMRestoreLocalVector(mine->dm,local);CHKERRQ(ierr);
100247c6ae99SBarry Smith   }
100347c6ae99SBarry Smith   PetscFunctionReturn(0);
100447c6ae99SBarry Smith }
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith #undef __FUNCT__
100747c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetLocalVectors"
100847c6ae99SBarry Smith /*@C
100947c6ae99SBarry Smith     DMCompositeGetLocalVectors - Gets local vectors and arrays for each part of a DMComposite.
101047c6ae99SBarry Smith        Use DMCompositeRestoreLocalVectors() to return them.
101147c6ae99SBarry Smith 
101247c6ae99SBarry Smith     Not Collective
101347c6ae99SBarry Smith 
101447c6ae99SBarry Smith     Input Parameter:
101547c6ae99SBarry Smith .    dm - the packer object
101647c6ae99SBarry Smith 
101747c6ae99SBarry Smith     Output Parameter:
101847c6ae99SBarry Smith .    ... - the individual sequential objects (arrays or vectors)
101947c6ae99SBarry Smith 
102047c6ae99SBarry Smith     Level: advanced
102147c6ae99SBarry Smith 
10220c010503SBarry Smith .seealso DMDestroy(), DMCompositeAddArray(), DMCompositeAddDM(), DMCreateGlobalVector(),
10236eb61c8cSJed Brown          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
102447c6ae99SBarry Smith          DMCompositeRestoreLocalVectors(), DMCompositeScatter(), DMCompositeGetEntries()
102547c6ae99SBarry Smith 
102647c6ae99SBarry Smith @*/
10277087cfbeSBarry Smith PetscErrorCode  DMCompositeGetLocalVectors(DM dm,...)
102847c6ae99SBarry Smith {
102947c6ae99SBarry Smith   va_list                Argp;
103047c6ae99SBarry Smith   PetscErrorCode         ierr;
103147c6ae99SBarry Smith   struct DMCompositeLink *next;
103247c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
103347c6ae99SBarry Smith 
103447c6ae99SBarry Smith   PetscFunctionBegin;
103547c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
103647c6ae99SBarry Smith   next = com->next;
103747c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
103847c6ae99SBarry Smith   va_start(Argp,dm);
103947c6ae99SBarry Smith   while (next) {
104047c6ae99SBarry Smith     if (next->type == DMCOMPOSITE_ARRAY) {
104147c6ae99SBarry Smith       PetscScalar **array;
104247c6ae99SBarry Smith       array = va_arg(Argp, PetscScalar**);
104347c6ae99SBarry Smith       ierr = DMCompositeGetLocalVectors_Array(dm,next,array);CHKERRQ(ierr);
104447c6ae99SBarry Smith     } else if (next->type == DMCOMPOSITE_DM) {
104547c6ae99SBarry Smith       Vec *vec;
104647c6ae99SBarry Smith       vec = va_arg(Argp, Vec*);
104747c6ae99SBarry Smith       ierr = DMCompositeGetLocalVectors_DM(dm,next,vec);CHKERRQ(ierr);
104847c6ae99SBarry Smith     } else {
104947c6ae99SBarry Smith       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
105047c6ae99SBarry Smith     }
105147c6ae99SBarry Smith     next = next->next;
105247c6ae99SBarry Smith   }
105347c6ae99SBarry Smith   va_end(Argp);
105447c6ae99SBarry Smith   PetscFunctionReturn(0);
105547c6ae99SBarry Smith }
105647c6ae99SBarry Smith 
105747c6ae99SBarry Smith #undef __FUNCT__
105847c6ae99SBarry Smith #define __FUNCT__ "DMCompositeRestoreLocalVectors"
105947c6ae99SBarry Smith /*@C
106047c6ae99SBarry Smith     DMCompositeRestoreLocalVectors - Restores local vectors and arrays for each part of a DMComposite.
106147c6ae99SBarry Smith 
106247c6ae99SBarry Smith     Not Collective
106347c6ae99SBarry Smith 
106447c6ae99SBarry Smith     Input Parameter:
106547c6ae99SBarry Smith .    dm - the packer object
106647c6ae99SBarry Smith 
106747c6ae99SBarry Smith     Output Parameter:
106847c6ae99SBarry Smith .    ... - the individual sequential objects (arrays or vectors)
106947c6ae99SBarry Smith 
107047c6ae99SBarry Smith     Level: advanced
107147c6ae99SBarry Smith 
10720c010503SBarry Smith .seealso DMDestroy(), DMCompositeAddArray(), DMCompositeAddDM(), DMCreateGlobalVector(),
10736eb61c8cSJed Brown          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
107447c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeScatter(), DMCompositeGetEntries()
107547c6ae99SBarry Smith 
107647c6ae99SBarry Smith @*/
10777087cfbeSBarry Smith PetscErrorCode  DMCompositeRestoreLocalVectors(DM dm,...)
107847c6ae99SBarry Smith {
107947c6ae99SBarry Smith   va_list                Argp;
108047c6ae99SBarry Smith   PetscErrorCode         ierr;
108147c6ae99SBarry Smith   struct DMCompositeLink *next;
108247c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
108347c6ae99SBarry Smith 
108447c6ae99SBarry Smith   PetscFunctionBegin;
108547c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
108647c6ae99SBarry Smith   next = com->next;
108747c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
108847c6ae99SBarry Smith   va_start(Argp,dm);
108947c6ae99SBarry Smith   while (next) {
109047c6ae99SBarry Smith     if (next->type == DMCOMPOSITE_ARRAY) {
109147c6ae99SBarry Smith       PetscScalar **array;
109247c6ae99SBarry Smith       array = va_arg(Argp, PetscScalar**);
109347c6ae99SBarry Smith       ierr = DMCompositeRestoreLocalVectors_Array(dm,next,array);CHKERRQ(ierr);
109447c6ae99SBarry Smith     } else if (next->type == DMCOMPOSITE_DM) {
109547c6ae99SBarry Smith       Vec *vec;
109647c6ae99SBarry Smith       vec = va_arg(Argp, Vec*);
109747c6ae99SBarry Smith       ierr = DMCompositeRestoreLocalVectors_DM(dm,next,vec);CHKERRQ(ierr);
109847c6ae99SBarry Smith     } else {
109947c6ae99SBarry Smith       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
110047c6ae99SBarry Smith     }
110147c6ae99SBarry Smith     next = next->next;
110247c6ae99SBarry Smith   }
110347c6ae99SBarry Smith   va_end(Argp);
110447c6ae99SBarry Smith   PetscFunctionReturn(0);
110547c6ae99SBarry Smith }
110647c6ae99SBarry Smith 
110747c6ae99SBarry Smith /* -------------------------------------------------------------------------------------*/
110847c6ae99SBarry Smith #undef __FUNCT__
110947c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetEntries_Array"
111047c6ae99SBarry Smith PetscErrorCode DMCompositeGetEntries_Array(DM dm,struct DMCompositeLink *mine,PetscInt *n)
111147c6ae99SBarry Smith {
111247c6ae99SBarry Smith   PetscFunctionBegin;
111306ebdd98SJed Brown   if (n) *n = mine->nlocal;
111447c6ae99SBarry Smith   PetscFunctionReturn(0);
111547c6ae99SBarry Smith }
111647c6ae99SBarry Smith 
111747c6ae99SBarry Smith #undef __FUNCT__
111847c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetEntries_DM"
111947c6ae99SBarry Smith PetscErrorCode DMCompositeGetEntries_DM(DM dmi,struct DMCompositeLink *mine,DM *dm)
112047c6ae99SBarry Smith {
112147c6ae99SBarry Smith   PetscFunctionBegin;
112247c6ae99SBarry Smith   if (dm) *dm = mine->dm;
112347c6ae99SBarry Smith   PetscFunctionReturn(0);
112447c6ae99SBarry Smith }
112547c6ae99SBarry Smith 
112647c6ae99SBarry Smith #undef __FUNCT__
112747c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetEntries"
112847c6ae99SBarry Smith /*@C
1129aa219208SBarry Smith     DMCompositeGetEntries - Gets the DM, redundant size, etc for each entry in a DMComposite.
113047c6ae99SBarry Smith 
113147c6ae99SBarry Smith     Not Collective
113247c6ae99SBarry Smith 
113347c6ae99SBarry Smith     Input Parameter:
113447c6ae99SBarry Smith .    dm - the packer object
113547c6ae99SBarry Smith 
113647c6ae99SBarry Smith     Output Parameter:
1137aa219208SBarry Smith .    ... - the individual entries, DMs or integer sizes)
113847c6ae99SBarry Smith 
113947c6ae99SBarry Smith     Level: advanced
114047c6ae99SBarry Smith 
11410c010503SBarry Smith .seealso DMDestroy(), DMCompositeAddArray(), DMCompositeAddDM(), DMCreateGlobalVector(),
11426eb61c8cSJed Brown          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
114347c6ae99SBarry Smith          DMCompositeRestoreLocalVectors(), DMCompositeGetLocalVectors(),  DMCompositeScatter(),
114447c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors()
114547c6ae99SBarry Smith 
114647c6ae99SBarry Smith @*/
11477087cfbeSBarry Smith PetscErrorCode  DMCompositeGetEntries(DM dm,...)
114847c6ae99SBarry Smith {
114947c6ae99SBarry Smith   va_list                Argp;
115047c6ae99SBarry Smith   PetscErrorCode         ierr;
115147c6ae99SBarry Smith   struct DMCompositeLink *next;
115247c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
115347c6ae99SBarry Smith 
115447c6ae99SBarry Smith   PetscFunctionBegin;
115547c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
115647c6ae99SBarry Smith   next = com->next;
115747c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
115847c6ae99SBarry Smith   va_start(Argp,dm);
115947c6ae99SBarry Smith   while (next) {
116047c6ae99SBarry Smith     if (next->type == DMCOMPOSITE_ARRAY) {
116147c6ae99SBarry Smith       PetscInt *n;
116247c6ae99SBarry Smith       n = va_arg(Argp, PetscInt*);
116347c6ae99SBarry Smith       ierr = DMCompositeGetEntries_Array(dm,next,n);CHKERRQ(ierr);
116447c6ae99SBarry Smith     } else if (next->type == DMCOMPOSITE_DM) {
116547c6ae99SBarry Smith       DM *dmn;
116647c6ae99SBarry Smith       dmn = va_arg(Argp, DM*);
116747c6ae99SBarry Smith       ierr = DMCompositeGetEntries_DM(dm,next,dmn);CHKERRQ(ierr);
116847c6ae99SBarry Smith     } else {
116947c6ae99SBarry Smith       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
117047c6ae99SBarry Smith     }
117147c6ae99SBarry Smith     next = next->next;
117247c6ae99SBarry Smith   }
117347c6ae99SBarry Smith   va_end(Argp);
117447c6ae99SBarry Smith   PetscFunctionReturn(0);
117547c6ae99SBarry Smith }
117647c6ae99SBarry Smith 
117747c6ae99SBarry Smith #undef __FUNCT__
11780c010503SBarry Smith #define __FUNCT__ "DMRefine_Composite"
11797087cfbeSBarry Smith PetscErrorCode  DMRefine_Composite(DM dmi,MPI_Comm comm,DM *fine)
118047c6ae99SBarry Smith {
118147c6ae99SBarry Smith   PetscErrorCode         ierr;
118247c6ae99SBarry Smith   struct DMCompositeLink *next;
118347c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dmi->data;
118447c6ae99SBarry Smith   DM                     dm;
118547c6ae99SBarry Smith 
118647c6ae99SBarry Smith   PetscFunctionBegin;
118747c6ae99SBarry Smith   PetscValidHeaderSpecific(dmi,DM_CLASSID,1);
118847c6ae99SBarry Smith   next = com->next;
118947c6ae99SBarry Smith   ierr = DMCompositeCreate(comm,fine);CHKERRQ(ierr);
119047c6ae99SBarry Smith 
119147c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
119247c6ae99SBarry Smith   while (next) {
119347c6ae99SBarry Smith     if (next->type == DMCOMPOSITE_ARRAY) {
119406ebdd98SJed Brown       ierr = DMCompositeAddArray(*fine,next->rank,next->nlocal);CHKERRQ(ierr);
119547c6ae99SBarry Smith     } else if (next->type == DMCOMPOSITE_DM) {
119647c6ae99SBarry Smith       ierr = DMRefine(next->dm,comm,&dm);CHKERRQ(ierr);
119747c6ae99SBarry Smith       ierr = DMCompositeAddDM(*fine,dm);CHKERRQ(ierr);
119847c6ae99SBarry Smith       ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
119947c6ae99SBarry Smith     } else {
120047c6ae99SBarry Smith       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
120147c6ae99SBarry Smith     }
120247c6ae99SBarry Smith     next = next->next;
120347c6ae99SBarry Smith   }
120447c6ae99SBarry Smith   PetscFunctionReturn(0);
120547c6ae99SBarry Smith }
120647c6ae99SBarry Smith 
1207*14354c39SJed Brown #undef __FUNCT__
1208*14354c39SJed Brown #define __FUNCT__ "DMCoarsen_Composite"
1209*14354c39SJed Brown PetscErrorCode  DMCoarsen_Composite(DM dmi,MPI_Comm comm,DM *fine)
1210*14354c39SJed Brown {
1211*14354c39SJed Brown   PetscErrorCode         ierr;
1212*14354c39SJed Brown   struct DMCompositeLink *next;
1213*14354c39SJed Brown   DM_Composite           *com = (DM_Composite*)dmi->data;
1214*14354c39SJed Brown   DM                     dm;
1215*14354c39SJed Brown 
1216*14354c39SJed Brown   PetscFunctionBegin;
1217*14354c39SJed Brown   PetscValidHeaderSpecific(dmi,DM_CLASSID,1);
1218*14354c39SJed Brown   next = com->next;
1219*14354c39SJed Brown   ierr = DMCompositeCreate(comm,fine);CHKERRQ(ierr);
1220*14354c39SJed Brown 
1221*14354c39SJed Brown   /* loop over packed objects, handling one at at time */
1222*14354c39SJed Brown   while (next) {
1223*14354c39SJed Brown     if (next->type == DMCOMPOSITE_ARRAY) {
1224*14354c39SJed Brown       ierr = DMCompositeAddArray(*fine,next->rank,next->nlocal);CHKERRQ(ierr);
1225*14354c39SJed Brown     } else if (next->type == DMCOMPOSITE_DM) {
1226*14354c39SJed Brown       ierr = DMCoarsen(next->dm,comm,&dm);CHKERRQ(ierr);
1227*14354c39SJed Brown       ierr = DMCompositeAddDM(*fine,dm);CHKERRQ(ierr);
1228*14354c39SJed Brown       ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
1229*14354c39SJed Brown     } else {
1230*14354c39SJed Brown       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
1231*14354c39SJed Brown     }
1232*14354c39SJed Brown     next = next->next;
1233*14354c39SJed Brown   }
1234*14354c39SJed Brown   PetscFunctionReturn(0);
1235*14354c39SJed Brown }
123647c6ae99SBarry Smith 
123747c6ae99SBarry Smith struct MatPackLink {
123847c6ae99SBarry Smith   Mat                A;
123947c6ae99SBarry Smith   struct MatPackLink *next;
124047c6ae99SBarry Smith };
124147c6ae99SBarry Smith 
124247c6ae99SBarry Smith struct MatPack {
124347c6ae99SBarry Smith   DM                 right,left;
124447c6ae99SBarry Smith   struct MatPackLink *next;
124547c6ae99SBarry Smith };
124647c6ae99SBarry Smith 
124747c6ae99SBarry Smith #undef __FUNCT__
124847c6ae99SBarry Smith #define __FUNCT__ "MatMultBoth_Shell_Pack"
124947c6ae99SBarry Smith PetscErrorCode MatMultBoth_Shell_Pack(Mat A,Vec x,Vec y,PetscBool  add)
125047c6ae99SBarry Smith {
125147c6ae99SBarry Smith   struct MatPack         *mpack;
125247c6ae99SBarry Smith   struct DMCompositeLink *xnext,*ynext;
125347c6ae99SBarry Smith   struct MatPackLink     *anext;
125447c6ae99SBarry Smith   PetscScalar            *xarray,*yarray;
125547c6ae99SBarry Smith   PetscErrorCode         ierr;
125647c6ae99SBarry Smith   PetscInt               i;
125747c6ae99SBarry Smith   Vec                    xglobal,yglobal;
125847c6ae99SBarry Smith   PetscMPIInt            rank;
125947c6ae99SBarry Smith   DM_Composite           *comright;
126047c6ae99SBarry Smith   DM_Composite           *comleft;
126147c6ae99SBarry Smith 
126247c6ae99SBarry Smith   PetscFunctionBegin;
126347c6ae99SBarry Smith   ierr = MatShellGetContext(A,(void**)&mpack);CHKERRQ(ierr);
126447c6ae99SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)mpack->right)->comm,&rank);CHKERRQ(ierr);
126547c6ae99SBarry Smith   comright = (DM_Composite*)mpack->right->data;
126647c6ae99SBarry Smith   comleft = (DM_Composite*)mpack->left->data;
126747c6ae99SBarry Smith   xnext = comright->next;
126847c6ae99SBarry Smith   ynext = comleft->next;
126947c6ae99SBarry Smith   anext = mpack->next;
127047c6ae99SBarry Smith 
127147c6ae99SBarry Smith   while (xnext) {
127247c6ae99SBarry Smith     if (xnext->type == DMCOMPOSITE_ARRAY) {
127347c6ae99SBarry Smith       if (rank == xnext->rank) {
127447c6ae99SBarry Smith         ierr    = VecGetArray(x,&xarray);CHKERRQ(ierr);
127547c6ae99SBarry Smith         ierr    = VecGetArray(y,&yarray);CHKERRQ(ierr);
127647c6ae99SBarry Smith         if (add) {
127747c6ae99SBarry Smith           for (i=0; i<xnext->n; i++) {
127847c6ae99SBarry Smith             yarray[ynext->rstart+i] += xarray[xnext->rstart+i];
127947c6ae99SBarry Smith           }
128047c6ae99SBarry Smith         } else {
128147c6ae99SBarry Smith           ierr    = PetscMemcpy(yarray+ynext->rstart,xarray+xnext->rstart,xnext->n*sizeof(PetscScalar));CHKERRQ(ierr);
128247c6ae99SBarry Smith         }
128347c6ae99SBarry Smith         ierr    = VecRestoreArray(x,&xarray);CHKERRQ(ierr);
128447c6ae99SBarry Smith         ierr    = VecRestoreArray(y,&yarray);CHKERRQ(ierr);
128547c6ae99SBarry Smith       }
128647c6ae99SBarry Smith     } else if (xnext->type == DMCOMPOSITE_DM) {
128747c6ae99SBarry Smith       ierr  = VecGetArray(x,&xarray);CHKERRQ(ierr);
128847c6ae99SBarry Smith       ierr  = VecGetArray(y,&yarray);CHKERRQ(ierr);
128947c6ae99SBarry Smith       ierr  = DMGetGlobalVector(xnext->dm,&xglobal);CHKERRQ(ierr);
129047c6ae99SBarry Smith       ierr  = DMGetGlobalVector(ynext->dm,&yglobal);CHKERRQ(ierr);
129147c6ae99SBarry Smith       ierr  = VecPlaceArray(xglobal,xarray+xnext->rstart);CHKERRQ(ierr);
129247c6ae99SBarry Smith       ierr  = VecPlaceArray(yglobal,yarray+ynext->rstart);CHKERRQ(ierr);
129347c6ae99SBarry Smith       if (add) {
129447c6ae99SBarry Smith         ierr  = MatMultAdd(anext->A,xglobal,yglobal,yglobal);CHKERRQ(ierr);
129547c6ae99SBarry Smith       } else {
129647c6ae99SBarry Smith         ierr  = MatMult(anext->A,xglobal,yglobal);CHKERRQ(ierr);
129747c6ae99SBarry Smith       }
129847c6ae99SBarry Smith       ierr  = VecRestoreArray(x,&xarray);CHKERRQ(ierr);
129947c6ae99SBarry Smith       ierr  = VecRestoreArray(y,&yarray);CHKERRQ(ierr);
130047c6ae99SBarry Smith       ierr  = VecResetArray(xglobal);CHKERRQ(ierr);
130147c6ae99SBarry Smith       ierr  = VecResetArray(yglobal);CHKERRQ(ierr);
130247c6ae99SBarry Smith       ierr  = DMRestoreGlobalVector(xnext->dm,&xglobal);CHKERRQ(ierr);
130347c6ae99SBarry Smith       ierr  = DMRestoreGlobalVector(ynext->dm,&yglobal);CHKERRQ(ierr);
130447c6ae99SBarry Smith       anext = anext->next;
130547c6ae99SBarry Smith     } else {
130647c6ae99SBarry Smith       SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
130747c6ae99SBarry Smith     }
130847c6ae99SBarry Smith     xnext = xnext->next;
130947c6ae99SBarry Smith     ynext = ynext->next;
131047c6ae99SBarry Smith   }
131147c6ae99SBarry Smith   PetscFunctionReturn(0);
131247c6ae99SBarry Smith }
131347c6ae99SBarry Smith 
131447c6ae99SBarry Smith #undef __FUNCT__
131547c6ae99SBarry Smith #define __FUNCT__ "MatMultAdd_Shell_Pack"
131647c6ae99SBarry Smith PetscErrorCode MatMultAdd_Shell_Pack(Mat A,Vec x,Vec y,Vec z)
131747c6ae99SBarry Smith {
131847c6ae99SBarry Smith   PetscErrorCode ierr;
131947c6ae99SBarry Smith   PetscFunctionBegin;
132047c6ae99SBarry Smith   if (z != y) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Handles y == z only");
132147c6ae99SBarry Smith   ierr = MatMultBoth_Shell_Pack(A,x,y,PETSC_TRUE);CHKERRQ(ierr);
132247c6ae99SBarry Smith   PetscFunctionReturn(0);
132347c6ae99SBarry Smith }
132447c6ae99SBarry Smith 
132547c6ae99SBarry Smith #undef __FUNCT__
132647c6ae99SBarry Smith #define __FUNCT__ "MatMult_Shell_Pack"
132747c6ae99SBarry Smith PetscErrorCode MatMult_Shell_Pack(Mat A,Vec x,Vec y)
132847c6ae99SBarry Smith {
132947c6ae99SBarry Smith   PetscErrorCode ierr;
133047c6ae99SBarry Smith   PetscFunctionBegin;
133147c6ae99SBarry Smith   ierr = MatMultBoth_Shell_Pack(A,x,y,PETSC_FALSE);CHKERRQ(ierr);
133247c6ae99SBarry Smith   PetscFunctionReturn(0);
133347c6ae99SBarry Smith }
133447c6ae99SBarry Smith 
133547c6ae99SBarry Smith #undef __FUNCT__
133647c6ae99SBarry Smith #define __FUNCT__ "MatMultTranspose_Shell_Pack"
133747c6ae99SBarry Smith PetscErrorCode MatMultTranspose_Shell_Pack(Mat A,Vec x,Vec y)
133847c6ae99SBarry Smith {
133947c6ae99SBarry Smith   struct MatPack         *mpack;
134047c6ae99SBarry Smith   struct DMCompositeLink *xnext,*ynext;
134147c6ae99SBarry Smith   struct MatPackLink     *anext;
134247c6ae99SBarry Smith   PetscScalar            *xarray,*yarray;
134347c6ae99SBarry Smith   PetscErrorCode         ierr;
134447c6ae99SBarry Smith   Vec                    xglobal,yglobal;
134547c6ae99SBarry Smith   PetscMPIInt            rank;
134647c6ae99SBarry Smith   DM_Composite           *comright;
134747c6ae99SBarry Smith   DM_Composite           *comleft;
134847c6ae99SBarry Smith 
134947c6ae99SBarry Smith   PetscFunctionBegin;
135047c6ae99SBarry Smith   ierr  = MatShellGetContext(A,(void**)&mpack);CHKERRQ(ierr);
135147c6ae99SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)mpack->right)->comm,&rank);CHKERRQ(ierr);
135247c6ae99SBarry Smith   comright = (DM_Composite*)mpack->right->data;
135347c6ae99SBarry Smith   comleft = (DM_Composite*)mpack->left->data;
135447c6ae99SBarry Smith   ynext = comright->next;
135547c6ae99SBarry Smith   xnext = comleft->next;
135647c6ae99SBarry Smith   anext = mpack->next;
135747c6ae99SBarry Smith 
135847c6ae99SBarry Smith   while (xnext) {
135947c6ae99SBarry Smith     if (xnext->type == DMCOMPOSITE_ARRAY) {
136047c6ae99SBarry Smith       if (rank == ynext->rank) {
136147c6ae99SBarry Smith         ierr    = VecGetArray(x,&xarray);CHKERRQ(ierr);
136247c6ae99SBarry Smith         ierr    = VecGetArray(y,&yarray);CHKERRQ(ierr);
136347c6ae99SBarry Smith         ierr    = PetscMemcpy(yarray+ynext->rstart,xarray+xnext->rstart,xnext->n*sizeof(PetscScalar));CHKERRQ(ierr);
136447c6ae99SBarry Smith         ierr    = VecRestoreArray(x,&xarray);CHKERRQ(ierr);
136547c6ae99SBarry Smith         ierr    = VecRestoreArray(y,&yarray);CHKERRQ(ierr);
136647c6ae99SBarry Smith       }
136747c6ae99SBarry Smith     } else if (xnext->type == DMCOMPOSITE_DM) {
136847c6ae99SBarry Smith       ierr  = VecGetArray(x,&xarray);CHKERRQ(ierr);
136947c6ae99SBarry Smith       ierr  = VecGetArray(y,&yarray);CHKERRQ(ierr);
137047c6ae99SBarry Smith       ierr  = DMGetGlobalVector(xnext->dm,&xglobal);CHKERRQ(ierr);
137147c6ae99SBarry Smith       ierr  = DMGetGlobalVector(ynext->dm,&yglobal);CHKERRQ(ierr);
137247c6ae99SBarry Smith       ierr  = VecPlaceArray(xglobal,xarray+xnext->rstart);CHKERRQ(ierr);
137347c6ae99SBarry Smith       ierr  = VecPlaceArray(yglobal,yarray+ynext->rstart);CHKERRQ(ierr);
137447c6ae99SBarry Smith       ierr  = MatMultTranspose(anext->A,xglobal,yglobal);CHKERRQ(ierr);
137547c6ae99SBarry Smith       ierr  = VecRestoreArray(x,&xarray);CHKERRQ(ierr);
137647c6ae99SBarry Smith       ierr  = VecRestoreArray(y,&yarray);CHKERRQ(ierr);
137747c6ae99SBarry Smith       ierr  = VecResetArray(xglobal);CHKERRQ(ierr);
137847c6ae99SBarry Smith       ierr  = VecResetArray(yglobal);CHKERRQ(ierr);
137947c6ae99SBarry Smith       ierr  = DMRestoreGlobalVector(xnext->dm,&xglobal);CHKERRQ(ierr);
138047c6ae99SBarry Smith       ierr  = DMRestoreGlobalVector(ynext->dm,&yglobal);CHKERRQ(ierr);
138147c6ae99SBarry Smith       anext = anext->next;
138247c6ae99SBarry Smith     } else {
138347c6ae99SBarry Smith       SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
138447c6ae99SBarry Smith     }
138547c6ae99SBarry Smith     xnext = xnext->next;
138647c6ae99SBarry Smith     ynext = ynext->next;
138747c6ae99SBarry Smith   }
138847c6ae99SBarry Smith   PetscFunctionReturn(0);
138947c6ae99SBarry Smith }
139047c6ae99SBarry Smith 
139147c6ae99SBarry Smith #undef __FUNCT__
139247c6ae99SBarry Smith #define __FUNCT__ "MatDestroy_Shell_Pack"
139347c6ae99SBarry Smith PetscErrorCode MatDestroy_Shell_Pack(Mat A)
139447c6ae99SBarry Smith {
139547c6ae99SBarry Smith   struct MatPack     *mpack;
139647c6ae99SBarry Smith   struct MatPackLink *anext,*oldanext;
139747c6ae99SBarry Smith   PetscErrorCode     ierr;
139847c6ae99SBarry Smith 
139947c6ae99SBarry Smith   PetscFunctionBegin;
140047c6ae99SBarry Smith   ierr  = MatShellGetContext(A,(void**)&mpack);CHKERRQ(ierr);
140147c6ae99SBarry Smith   anext = mpack->next;
140247c6ae99SBarry Smith 
140347c6ae99SBarry Smith   while (anext) {
1404fcfd50ebSBarry Smith     ierr     = MatDestroy(&anext->A);CHKERRQ(ierr);
140547c6ae99SBarry Smith     oldanext = anext;
140647c6ae99SBarry Smith     anext    = anext->next;
140747c6ae99SBarry Smith     ierr     = PetscFree(oldanext);CHKERRQ(ierr);
140847c6ae99SBarry Smith   }
140947c6ae99SBarry Smith   ierr = PetscFree(mpack);CHKERRQ(ierr);
141047c6ae99SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
141147c6ae99SBarry Smith   PetscFunctionReturn(0);
141247c6ae99SBarry Smith }
141347c6ae99SBarry Smith 
141447c6ae99SBarry Smith #undef __FUNCT__
14150c010503SBarry Smith #define __FUNCT__ "DMGetInterpolation_Composite"
14167087cfbeSBarry Smith PetscErrorCode  DMGetInterpolation_Composite(DM coarse,DM fine,Mat *A,Vec *v)
141747c6ae99SBarry Smith {
141847c6ae99SBarry Smith   PetscErrorCode         ierr;
141947c6ae99SBarry Smith   PetscInt               m,n,M,N;
142047c6ae99SBarry Smith   struct DMCompositeLink *nextc;
142147c6ae99SBarry Smith   struct DMCompositeLink *nextf;
142247c6ae99SBarry Smith   struct MatPackLink     *nextmat,*pnextmat = 0;
142347c6ae99SBarry Smith   struct MatPack         *mpack;
142447c6ae99SBarry Smith   Vec                    gcoarse,gfine;
142547c6ae99SBarry Smith   DM_Composite           *comcoarse = (DM_Composite*)coarse->data;
142647c6ae99SBarry Smith   DM_Composite           *comfine = (DM_Composite*)fine->data;
142747c6ae99SBarry Smith 
142847c6ae99SBarry Smith   PetscFunctionBegin;
142947c6ae99SBarry Smith   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
143047c6ae99SBarry Smith   PetscValidHeaderSpecific(fine,DM_CLASSID,2);
143147c6ae99SBarry Smith   nextc = comcoarse->next;
143247c6ae99SBarry Smith   nextf = comfine->next;
143347c6ae99SBarry Smith   /* use global vectors only for determining matrix layout */
14340c010503SBarry Smith   ierr = DMCreateGlobalVector(coarse,&gcoarse);CHKERRQ(ierr);
14350c010503SBarry Smith   ierr = DMCreateGlobalVector(fine,&gfine);CHKERRQ(ierr);
143647c6ae99SBarry Smith   ierr = VecGetLocalSize(gcoarse,&n);CHKERRQ(ierr);
143747c6ae99SBarry Smith   ierr = VecGetLocalSize(gfine,&m);CHKERRQ(ierr);
143847c6ae99SBarry Smith   ierr = VecGetSize(gcoarse,&N);CHKERRQ(ierr);
143947c6ae99SBarry Smith   ierr = VecGetSize(gfine,&M);CHKERRQ(ierr);
1440fcfd50ebSBarry Smith   ierr = VecDestroy(&gcoarse);CHKERRQ(ierr);
1441fcfd50ebSBarry Smith   ierr = VecDestroy(&gfine);CHKERRQ(ierr);
144247c6ae99SBarry Smith 
144347c6ae99SBarry Smith   ierr         = PetscNew(struct MatPack,&mpack);CHKERRQ(ierr);
144447c6ae99SBarry Smith   mpack->right = coarse;
144547c6ae99SBarry Smith   mpack->left  = fine;
144647c6ae99SBarry Smith   ierr  = MatCreate(((PetscObject)fine)->comm,A);CHKERRQ(ierr);
144747c6ae99SBarry Smith   ierr  = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr);
144847c6ae99SBarry Smith   ierr  = MatSetType(*A,MATSHELL);CHKERRQ(ierr);
144947c6ae99SBarry Smith   ierr  = MatShellSetContext(*A,mpack);CHKERRQ(ierr);
145047c6ae99SBarry Smith   ierr  = MatShellSetOperation(*A,MATOP_MULT,(void(*)(void))MatMult_Shell_Pack);CHKERRQ(ierr);
145147c6ae99SBarry Smith   ierr  = MatShellSetOperation(*A,MATOP_MULT_TRANSPOSE,(void(*)(void))MatMultTranspose_Shell_Pack);CHKERRQ(ierr);
145247c6ae99SBarry Smith   ierr  = MatShellSetOperation(*A,MATOP_MULT_ADD,(void(*)(void))MatMultAdd_Shell_Pack);CHKERRQ(ierr);
145347c6ae99SBarry Smith   ierr  = MatShellSetOperation(*A,MATOP_DESTROY,(void(*)(void))MatDestroy_Shell_Pack);CHKERRQ(ierr);
145447c6ae99SBarry Smith 
145547c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
145647c6ae99SBarry Smith   while (nextc) {
145747c6ae99SBarry Smith     if (nextc->type != nextf->type) SETERRQ(((PetscObject)fine)->comm,PETSC_ERR_ARG_INCOMP,"Two DM have different layout");
145847c6ae99SBarry Smith 
145947c6ae99SBarry Smith     if (nextc->type == DMCOMPOSITE_ARRAY) {
146047c6ae99SBarry Smith       ;
146147c6ae99SBarry Smith     } else if (nextc->type == DMCOMPOSITE_DM) {
146247c6ae99SBarry Smith       ierr          = PetscNew(struct MatPackLink,&nextmat);CHKERRQ(ierr);
146347c6ae99SBarry Smith       nextmat->next = 0;
146447c6ae99SBarry Smith       if (pnextmat) {
146547c6ae99SBarry Smith         pnextmat->next = nextmat;
146647c6ae99SBarry Smith         pnextmat       = nextmat;
146747c6ae99SBarry Smith       } else {
146847c6ae99SBarry Smith         pnextmat    = nextmat;
146947c6ae99SBarry Smith         mpack->next = nextmat;
147047c6ae99SBarry Smith       }
147147c6ae99SBarry Smith       ierr = DMGetInterpolation(nextc->dm,nextf->dm,&nextmat->A,PETSC_NULL);CHKERRQ(ierr);
147247c6ae99SBarry Smith     } else {
147347c6ae99SBarry Smith       SETERRQ(((PetscObject)fine)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
147447c6ae99SBarry Smith     }
147547c6ae99SBarry Smith     nextc = nextc->next;
147647c6ae99SBarry Smith     nextf = nextf->next;
147747c6ae99SBarry Smith   }
147847c6ae99SBarry Smith   PetscFunctionReturn(0);
147947c6ae99SBarry Smith }
148047c6ae99SBarry Smith 
148147c6ae99SBarry Smith #undef __FUNCT__
14821411c6eeSJed Brown #define __FUNCT__ "DMCreateLocalToGlobalMapping_Composite"
14831411c6eeSJed Brown static PetscErrorCode DMCreateLocalToGlobalMapping_Composite(DM dm)
14841411c6eeSJed Brown {
14851411c6eeSJed Brown   DM_Composite           *com = (DM_Composite*)dm->data;
14861411c6eeSJed Brown   ISLocalToGlobalMapping *ltogs;
1487f7efa3c7SJed Brown   PetscInt               i;
14881411c6eeSJed Brown   PetscErrorCode         ierr;
14891411c6eeSJed Brown 
14901411c6eeSJed Brown   PetscFunctionBegin;
14911411c6eeSJed Brown   /* Set the ISLocalToGlobalMapping on the new matrix */
14921411c6eeSJed Brown   ierr = DMCompositeGetISLocalToGlobalMappings(dm,&ltogs);CHKERRQ(ierr);
1493f7efa3c7SJed Brown   ierr = ISLocalToGlobalMappingConcatenate(((PetscObject)dm)->comm,com->nDM+com->nredundant,ltogs,&dm->ltogmap);CHKERRQ(ierr);
1494fcfd50ebSBarry Smith   for (i=0; i<com->nDM+com->nredundant; i++) {ierr = ISLocalToGlobalMappingDestroy(&ltogs[i]);CHKERRQ(ierr);}
14951411c6eeSJed Brown   ierr = PetscFree(ltogs);CHKERRQ(ierr);
14961411c6eeSJed Brown   PetscFunctionReturn(0);
14971411c6eeSJed Brown }
14981411c6eeSJed Brown 
14991411c6eeSJed Brown 
15001411c6eeSJed Brown #undef __FUNCT__
15010c010503SBarry Smith #define __FUNCT__ "DMGetColoring_Composite"
15027087cfbeSBarry Smith PetscErrorCode  DMGetColoring_Composite(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
150347c6ae99SBarry Smith {
150447c6ae99SBarry Smith   PetscErrorCode         ierr;
150547c6ae99SBarry Smith   PetscInt               n,i,cnt;
150647c6ae99SBarry Smith   ISColoringValue        *colors;
150747c6ae99SBarry Smith   PetscBool              dense = PETSC_FALSE;
150847c6ae99SBarry Smith   ISColoringValue        maxcol = 0;
150947c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
151047c6ae99SBarry Smith 
151147c6ae99SBarry Smith   PetscFunctionBegin;
151247c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
151347c6ae99SBarry Smith   if (ctype == IS_COLORING_GHOSTED) {
151447c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Currently you must use -dmmg_iscoloring_type global" );
151547c6ae99SBarry Smith   } else if (ctype == IS_COLORING_GLOBAL) {
151647c6ae99SBarry Smith     n = com->n;
151747c6ae99SBarry Smith   } else SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown ISColoringType");
151847c6ae99SBarry Smith   ierr = PetscMalloc(n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); /* freed in ISColoringDestroy() */
151947c6ae99SBarry Smith 
1520671f6225SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-dmcomposite_dense_jacobian",&dense,PETSC_NULL);CHKERRQ(ierr);
152147c6ae99SBarry Smith   if (dense) {
152247c6ae99SBarry Smith     for (i=0; i<n; i++) {
152347c6ae99SBarry Smith       colors[i] = (ISColoringValue)(com->rstart + i);
152447c6ae99SBarry Smith     }
152547c6ae99SBarry Smith     maxcol = com->N;
152647c6ae99SBarry Smith   } else {
152747c6ae99SBarry Smith     struct DMCompositeLink *next = com->next;
152847c6ae99SBarry Smith     PetscMPIInt            rank;
152947c6ae99SBarry Smith 
153047c6ae99SBarry Smith     ierr = MPI_Comm_rank(((PetscObject)dm)->comm,&rank);CHKERRQ(ierr);
153147c6ae99SBarry Smith     cnt  = 0;
153247c6ae99SBarry Smith     while (next) {
153347c6ae99SBarry Smith       if (next->type == DMCOMPOSITE_ARRAY) {
153447c6ae99SBarry Smith         if (rank == next->rank) {  /* each column gets is own color */
153547c6ae99SBarry Smith           for (i=com->rstart+next->rstart; i<com->rstart+next->rstart+next->n; i++) {
153647c6ae99SBarry Smith             colors[cnt++] = maxcol++;
153747c6ae99SBarry Smith           }
153847c6ae99SBarry Smith         }
153947c6ae99SBarry Smith         ierr = MPI_Bcast(&maxcol,1,MPIU_COLORING_VALUE,next->rank,((PetscObject)dm)->comm);CHKERRQ(ierr);
154047c6ae99SBarry Smith       } else if (next->type == DMCOMPOSITE_DM) {
154147c6ae99SBarry Smith         ISColoring     lcoloring;
154247c6ae99SBarry Smith 
154347c6ae99SBarry Smith         ierr = DMGetColoring(next->dm,IS_COLORING_GLOBAL,mtype,&lcoloring);CHKERRQ(ierr);
154447c6ae99SBarry Smith         for (i=0; i<lcoloring->N; i++) {
154547c6ae99SBarry Smith           colors[cnt++] = maxcol + lcoloring->colors[i];
154647c6ae99SBarry Smith         }
154747c6ae99SBarry Smith         maxcol += lcoloring->n;
1548fcfd50ebSBarry Smith         ierr = ISColoringDestroy(&lcoloring);CHKERRQ(ierr);
154996e147daSBarry Smith       } else SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
155047c6ae99SBarry Smith       next = next->next;
155147c6ae99SBarry Smith     }
155247c6ae99SBarry Smith   }
155347c6ae99SBarry Smith   ierr = ISColoringCreate(((PetscObject)dm)->comm,maxcol,n,colors,coloring);CHKERRQ(ierr);
155447c6ae99SBarry Smith   PetscFunctionReturn(0);
155547c6ae99SBarry Smith }
155647c6ae99SBarry Smith 
155747c6ae99SBarry Smith #undef __FUNCT__
15580c010503SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin_Composite"
15597087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin_Composite(DM dm,Vec gvec,InsertMode mode,Vec lvec)
156047c6ae99SBarry Smith {
156147c6ae99SBarry Smith   PetscErrorCode         ierr;
156247c6ae99SBarry Smith   struct DMCompositeLink *next;
156347c6ae99SBarry Smith   PetscInt               cnt = 3;
156447c6ae99SBarry Smith   PetscMPIInt            rank;
156547c6ae99SBarry Smith   PetscScalar            *garray,*larray;
156647c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
156747c6ae99SBarry Smith 
156847c6ae99SBarry Smith   PetscFunctionBegin;
156947c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
157047c6ae99SBarry Smith   PetscValidHeaderSpecific(gvec,VEC_CLASSID,2);
157147c6ae99SBarry Smith   next = com->next;
157247c6ae99SBarry Smith   if (!com->setup) {
1573d7bf68aeSBarry Smith     ierr = DMSetUp(dm);CHKERRQ(ierr);
157447c6ae99SBarry Smith   }
157547c6ae99SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)dm)->comm,&rank);CHKERRQ(ierr);
157647c6ae99SBarry Smith   ierr = VecGetArray(gvec,&garray);CHKERRQ(ierr);
157747c6ae99SBarry Smith   ierr = VecGetArray(lvec,&larray);CHKERRQ(ierr);
157847c6ae99SBarry Smith 
157947c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
158047c6ae99SBarry Smith   while (next) {
158147c6ae99SBarry Smith     if (next->type == DMCOMPOSITE_ARRAY) {
158247c6ae99SBarry Smith       if (rank == next->rank) {
158347c6ae99SBarry Smith         ierr    = PetscMemcpy(larray,garray,next->n*sizeof(PetscScalar));CHKERRQ(ierr);
158447c6ae99SBarry Smith         garray += next->n;
158547c6ae99SBarry Smith       }
158647c6ae99SBarry Smith       /* does not handle ADD_VALUES */
158706ebdd98SJed Brown       ierr = MPI_Bcast(larray,next->nlocal,MPIU_SCALAR,next->rank,((PetscObject)dm)->comm);CHKERRQ(ierr);
158847c6ae99SBarry Smith     } else if (next->type == DMCOMPOSITE_DM) {
158947c6ae99SBarry Smith       Vec      local,global;
159047c6ae99SBarry Smith       PetscInt N;
159147c6ae99SBarry Smith 
159247c6ae99SBarry Smith       ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr);
159347c6ae99SBarry Smith       ierr = VecGetLocalSize(global,&N);CHKERRQ(ierr);
159447c6ae99SBarry Smith       ierr = VecPlaceArray(global,garray);CHKERRQ(ierr);
159547c6ae99SBarry Smith       ierr = DMGetLocalVector(next->dm,&local);CHKERRQ(ierr);
159647c6ae99SBarry Smith       ierr = VecPlaceArray(local,larray);CHKERRQ(ierr);
159747c6ae99SBarry Smith       ierr = DMGlobalToLocalBegin(next->dm,global,mode,local);CHKERRQ(ierr);
159847c6ae99SBarry Smith       ierr = DMGlobalToLocalEnd(next->dm,global,mode,local);CHKERRQ(ierr);
159947c6ae99SBarry Smith       ierr = VecResetArray(global);CHKERRQ(ierr);
160047c6ae99SBarry Smith       ierr = VecResetArray(local);CHKERRQ(ierr);
160147c6ae99SBarry Smith       ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr);
160247c6ae99SBarry Smith       ierr = DMRestoreGlobalVector(next->dm,&local);CHKERRQ(ierr);
160347c6ae99SBarry Smith     } else {
160447c6ae99SBarry Smith       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"Cannot handle that object type yet");
160547c6ae99SBarry Smith     }
160647c6ae99SBarry Smith     cnt++;
160706ebdd98SJed Brown     larray += next->nlocal;
160847c6ae99SBarry Smith     next    = next->next;
160947c6ae99SBarry Smith   }
161047c6ae99SBarry Smith 
161147c6ae99SBarry Smith   ierr = VecRestoreArray(gvec,PETSC_NULL);CHKERRQ(ierr);
161247c6ae99SBarry Smith   ierr = VecRestoreArray(lvec,PETSC_NULL);CHKERRQ(ierr);
161347c6ae99SBarry Smith   PetscFunctionReturn(0);
161447c6ae99SBarry Smith }
161547c6ae99SBarry Smith 
161647c6ae99SBarry Smith #undef __FUNCT__
16170c010503SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd_Composite"
16187087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd_Composite(DM dm,Vec gvec,InsertMode mode,Vec lvec)
16190c010503SBarry Smith {
16200c010503SBarry Smith   PetscFunctionBegin;
16210c010503SBarry Smith   PetscFunctionReturn(0);
16220c010503SBarry Smith }
162347c6ae99SBarry Smith 
1624a4121054SBarry Smith EXTERN_C_BEGIN
1625a4121054SBarry Smith #undef __FUNCT__
1626a4121054SBarry Smith #define __FUNCT__ "DMCreate_Composite"
16277087cfbeSBarry Smith PetscErrorCode  DMCreate_Composite(DM p)
1628a4121054SBarry Smith {
1629a4121054SBarry Smith   PetscErrorCode ierr;
1630a4121054SBarry Smith   DM_Composite   *com;
1631a4121054SBarry Smith 
1632a4121054SBarry Smith   PetscFunctionBegin;
1633a4121054SBarry Smith   ierr = PetscNewLog(p,DM_Composite,&com);CHKERRQ(ierr);
1634a4121054SBarry Smith   p->data = com;
1635a4121054SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)p,"DMComposite");CHKERRQ(ierr);
1636a4121054SBarry Smith   com->n            = 0;
1637a4121054SBarry Smith   com->next         = PETSC_NULL;
1638a4121054SBarry Smith   com->nredundant   = 0;
1639a4121054SBarry Smith   com->nDM          = 0;
1640a4121054SBarry Smith 
1641a4121054SBarry Smith   p->ops->createglobalvector              = DMCreateGlobalVector_Composite;
1642a4121054SBarry Smith   p->ops->createlocalvector               = DMCreateLocalVector_Composite;
16431411c6eeSJed Brown   p->ops->createlocaltoglobalmapping      = DMCreateLocalToGlobalMapping_Composite;
16441411c6eeSJed Brown   p->ops->createlocaltoglobalmappingblock = 0;
1645a4121054SBarry Smith   p->ops->refine                          = DMRefine_Composite;
1646*14354c39SJed Brown   p->ops->coarsen                         = DMCoarsen_Composite;
1647a4121054SBarry Smith   p->ops->getinterpolation                = DMGetInterpolation_Composite;
1648a4121054SBarry Smith   p->ops->getmatrix                       = DMGetMatrix_Composite;
1649a4121054SBarry Smith   p->ops->getcoloring                     = DMGetColoring_Composite;
1650a4121054SBarry Smith   p->ops->globaltolocalbegin              = DMGlobalToLocalBegin_Composite;
1651a4121054SBarry Smith   p->ops->globaltolocalend                = DMGlobalToLocalEnd_Composite;
1652a4121054SBarry Smith   p->ops->destroy                         = DMDestroy_Composite;
1653a4121054SBarry Smith   p->ops->view                            = DMView_Composite;
1654a4121054SBarry Smith   p->ops->setup                           = DMSetUp_Composite;
1655a4121054SBarry Smith   PetscFunctionReturn(0);
1656a4121054SBarry Smith }
1657a4121054SBarry Smith EXTERN_C_END
1658a4121054SBarry Smith 
16590c010503SBarry Smith #undef __FUNCT__
16600c010503SBarry Smith #define __FUNCT__ "DMCompositeCreate"
16610c010503SBarry Smith /*@C
16620c010503SBarry Smith     DMCompositeCreate - Creates a vector packer, used to generate "composite"
16630c010503SBarry Smith       vectors made up of several subvectors.
16640c010503SBarry Smith 
16650c010503SBarry Smith     Collective on MPI_Comm
166647c6ae99SBarry Smith 
166747c6ae99SBarry Smith     Input Parameter:
16680c010503SBarry Smith .   comm - the processors that will share the global vector
16690c010503SBarry Smith 
16700c010503SBarry Smith     Output Parameters:
16710c010503SBarry Smith .   packer - the packer object
167247c6ae99SBarry Smith 
167347c6ae99SBarry Smith     Level: advanced
167447c6ae99SBarry Smith 
16750c010503SBarry Smith .seealso DMDestroy(), DMCompositeAddArray(), DMCompositeAddDM(), DMCompositeScatter(),
16766eb61c8cSJed Brown          DMCompositeGather(), DMCreateGlobalVector(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess()
167747c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries()
167847c6ae99SBarry Smith 
167947c6ae99SBarry Smith @*/
16807087cfbeSBarry Smith PetscErrorCode  DMCompositeCreate(MPI_Comm comm,DM *packer)
168147c6ae99SBarry Smith {
16820c010503SBarry Smith   PetscErrorCode ierr;
16830c010503SBarry Smith 
168447c6ae99SBarry Smith   PetscFunctionBegin;
16850c010503SBarry Smith   PetscValidPointer(packer,2);
1686a4121054SBarry Smith   ierr = DMCreate(comm,packer);CHKERRQ(ierr);
1687a4121054SBarry Smith   ierr = DMSetType(*packer,DMCOMPOSITE);CHKERRQ(ierr);
168847c6ae99SBarry Smith   PetscFunctionReturn(0);
168947c6ae99SBarry Smith }
1690