147c6ae99SBarry Smith 2ccd284c7SBarry Smith #include <../src/dm/impls/composite/packimpl.h> /*I "petscdmcomposite.h" I*/ 3af0996ceSBarry Smith #include <petsc/private/isimpl.h> 4e10fd815SStefano Zampini #include <petsc/private/glvisviewerimpl.h> 52764a2aaSMatthew G. Knepley #include <petscds.h> 647c6ae99SBarry Smith 747c6ae99SBarry Smith /*@C 847c6ae99SBarry Smith DMCompositeSetCoupling - Sets user provided routines that compute the coupling between the 9bebe2cf6SSatish Balay separate components (DMs) in a DMto build the correct matrix nonzero structure. 1047c6ae99SBarry Smith 11d083f849SBarry Smith Logically Collective 1247c6ae99SBarry Smith 13d8d19677SJose E. Roman Input Parameters: 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 19f5f57ec0SBarry Smith Not available from Fortran 20f5f57ec0SBarry Smith 2195452b02SPatrick Sanan Notes: 2295452b02SPatrick Sanan See DMSetApplicationContext() and DMGetApplicationContext() for how to get user information into 2347c6ae99SBarry Smith this routine 2447c6ae99SBarry Smith 2547c6ae99SBarry Smith @*/ 267087cfbeSBarry Smith PetscErrorCode DMCompositeSetCoupling(DM dm,PetscErrorCode (*FormCoupleLocations)(DM,Mat,PetscInt*,PetscInt*,PetscInt,PetscInt,PetscInt,PetscInt)) 2747c6ae99SBarry Smith { 2847c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 2971b14b3eSStefano Zampini PetscBool flg; 3071b14b3eSStefano Zampini PetscErrorCode ierr; 3147c6ae99SBarry Smith 3247c6ae99SBarry Smith PetscFunctionBegin; 3371b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 34*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 3547c6ae99SBarry Smith com->FormCoupleLocations = FormCoupleLocations; 3647c6ae99SBarry Smith PetscFunctionReturn(0); 3747c6ae99SBarry Smith } 3847c6ae99SBarry Smith 396bf464f9SBarry Smith PetscErrorCode DMDestroy_Composite(DM dm) 4047c6ae99SBarry Smith { 4147c6ae99SBarry Smith PetscErrorCode ierr; 4247c6ae99SBarry Smith struct DMCompositeLink *next, *prev; 4347c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 4447c6ae99SBarry Smith 4547c6ae99SBarry Smith PetscFunctionBegin; 4647c6ae99SBarry Smith next = com->next; 4747c6ae99SBarry Smith while (next) { 4847c6ae99SBarry Smith prev = next; 4947c6ae99SBarry Smith next = next->next; 50fcfd50ebSBarry Smith ierr = DMDestroy(&prev->dm);CHKERRQ(ierr); 5147c6ae99SBarry Smith ierr = PetscFree(prev->grstarts);CHKERRQ(ierr); 5247c6ae99SBarry Smith ierr = PetscFree(prev);CHKERRQ(ierr); 5347c6ae99SBarry Smith } 54e10fd815SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr); 55435a35e8SMatthew G Knepley /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */ 56435a35e8SMatthew G Knepley ierr = PetscFree(com);CHKERRQ(ierr); 5747c6ae99SBarry Smith PetscFunctionReturn(0); 5847c6ae99SBarry Smith } 5947c6ae99SBarry Smith 607087cfbeSBarry Smith PetscErrorCode DMView_Composite(DM dm,PetscViewer v) 6147c6ae99SBarry Smith { 6247c6ae99SBarry Smith PetscErrorCode ierr; 6347c6ae99SBarry Smith PetscBool iascii; 6447c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 6547c6ae99SBarry Smith 6647c6ae99SBarry Smith PetscFunctionBegin; 67251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 6847c6ae99SBarry Smith if (iascii) { 6947c6ae99SBarry Smith struct DMCompositeLink *lnk = com->next; 7047c6ae99SBarry Smith PetscInt i; 7147c6ae99SBarry Smith 7247c6ae99SBarry Smith ierr = PetscViewerASCIIPrintf(v,"DM (%s)\n",((PetscObject)dm)->prefix ? ((PetscObject)dm)->prefix : "no prefix");CHKERRQ(ierr); 739ae5db72SJed Brown ierr = PetscViewerASCIIPrintf(v," contains %D DMs\n",com->nDM);CHKERRQ(ierr); 7447c6ae99SBarry Smith ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr); 7547c6ae99SBarry Smith for (i=0; lnk; lnk=lnk->next,i++) { 769ae5db72SJed Brown ierr = PetscViewerASCIIPrintf(v,"Link %D: DM of type %s\n",i,((PetscObject)lnk->dm)->type_name);CHKERRQ(ierr); 7747c6ae99SBarry Smith ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr); 7847c6ae99SBarry Smith ierr = DMView(lnk->dm,v);CHKERRQ(ierr); 7947c6ae99SBarry Smith ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr); 8047c6ae99SBarry Smith } 8147c6ae99SBarry Smith ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr); 8247c6ae99SBarry Smith } 8347c6ae99SBarry Smith PetscFunctionReturn(0); 8447c6ae99SBarry Smith } 8547c6ae99SBarry Smith 8647c6ae99SBarry Smith /* --------------------------------------------------------------------------------------*/ 877087cfbeSBarry Smith PetscErrorCode DMSetUp_Composite(DM dm) 8847c6ae99SBarry Smith { 8947c6ae99SBarry Smith PetscErrorCode ierr; 9047c6ae99SBarry Smith PetscInt nprev = 0; 9147c6ae99SBarry Smith PetscMPIInt rank,size; 9247c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 9347c6ae99SBarry Smith struct DMCompositeLink *next = com->next; 9447c6ae99SBarry Smith PetscLayout map; 9547c6ae99SBarry Smith 9647c6ae99SBarry Smith PetscFunctionBegin; 972c71b3e2SJacob Faibussowitsch PetscCheckFalse(com->setup,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Packer has already been setup"); 98ce94432eSBarry Smith ierr = PetscLayoutCreate(PetscObjectComm((PetscObject)dm),&map);CHKERRQ(ierr); 9947c6ae99SBarry Smith ierr = PetscLayoutSetLocalSize(map,com->n);CHKERRQ(ierr); 10047c6ae99SBarry Smith ierr = PetscLayoutSetSize(map,PETSC_DETERMINE);CHKERRQ(ierr); 10147c6ae99SBarry Smith ierr = PetscLayoutSetBlockSize(map,1);CHKERRQ(ierr); 10247c6ae99SBarry Smith ierr = PetscLayoutSetUp(map);CHKERRQ(ierr); 10347c6ae99SBarry Smith ierr = PetscLayoutGetSize(map,&com->N);CHKERRQ(ierr); 1040298fd71SBarry Smith ierr = PetscLayoutGetRange(map,&com->rstart,NULL);CHKERRQ(ierr); 105fcfd50ebSBarry Smith ierr = PetscLayoutDestroy(&map);CHKERRQ(ierr); 10647c6ae99SBarry Smith 1079ae5db72SJed Brown /* now set the rstart for each linked vector */ 108ffc4695bSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm),&rank);CHKERRMPI(ierr); 109ffc4695bSBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRMPI(ierr); 11047c6ae99SBarry Smith while (next) { 11147c6ae99SBarry Smith next->rstart = nprev; 11206ebdd98SJed Brown nprev += next->n; 11347c6ae99SBarry Smith next->grstart = com->rstart + next->rstart; 114785e854fSJed Brown ierr = PetscMalloc1(size,&next->grstarts);CHKERRQ(ierr); 11555b25c41SPierre Jolivet ierr = MPI_Allgather(&next->grstart,1,MPIU_INT,next->grstarts,1,MPIU_INT,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr); 11647c6ae99SBarry Smith next = next->next; 11747c6ae99SBarry Smith } 11847c6ae99SBarry Smith com->setup = PETSC_TRUE; 11947c6ae99SBarry Smith PetscFunctionReturn(0); 12047c6ae99SBarry Smith } 12147c6ae99SBarry Smith 12247c6ae99SBarry Smith /* ----------------------------------------------------------------------------------*/ 12347c6ae99SBarry Smith 12473e31fe2SJed Brown /*@ 12547c6ae99SBarry Smith DMCompositeGetNumberDM - Get's the number of DM objects in the DMComposite 12647c6ae99SBarry Smith representation. 12747c6ae99SBarry Smith 12847c6ae99SBarry Smith Not Collective 12947c6ae99SBarry Smith 13047c6ae99SBarry Smith Input Parameter: 13147c6ae99SBarry Smith . dm - the packer object 13247c6ae99SBarry Smith 13347c6ae99SBarry Smith Output Parameter: 13447c6ae99SBarry Smith . nDM - the number of DMs 13547c6ae99SBarry Smith 13647c6ae99SBarry Smith Level: beginner 13747c6ae99SBarry Smith 13847c6ae99SBarry Smith @*/ 1397087cfbeSBarry Smith PetscErrorCode DMCompositeGetNumberDM(DM dm,PetscInt *nDM) 14047c6ae99SBarry Smith { 14147c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 14271b14b3eSStefano Zampini PetscBool flg; 14371b14b3eSStefano Zampini PetscErrorCode ierr; 1445fd66863SKarl Rupp 14547c6ae99SBarry Smith PetscFunctionBegin; 14647c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 14771b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 148*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 14947c6ae99SBarry Smith *nDM = com->nDM; 15047c6ae99SBarry Smith PetscFunctionReturn(0); 15147c6ae99SBarry Smith } 15247c6ae99SBarry Smith 15347c6ae99SBarry Smith /*@C 15447c6ae99SBarry Smith DMCompositeGetAccess - Allows one to access the individual packed vectors in their global 15547c6ae99SBarry Smith representation. 15647c6ae99SBarry Smith 157d083f849SBarry Smith Collective on dm 15847c6ae99SBarry Smith 1599ae5db72SJed Brown Input Parameters: 16047c6ae99SBarry Smith + dm - the packer object 1619ae5db72SJed Brown - gvec - the global vector 1629ae5db72SJed Brown 1639ae5db72SJed Brown Output Parameters: 1640298fd71SBarry Smith . Vec* ... - the packed parallel vectors, NULL for those that are not needed 16547c6ae99SBarry Smith 16695452b02SPatrick Sanan Notes: 16795452b02SPatrick Sanan Use DMCompositeRestoreAccess() to return the vectors when you no longer need them 16847c6ae99SBarry Smith 169f73e5cebSJed Brown Fortran Notes: 170f73e5cebSJed Brown 171f73e5cebSJed Brown Fortran callers must use numbered versions of this routine, e.g., DMCompositeGetAccess4(dm,gvec,vec1,vec2,vec3,vec4) 172f73e5cebSJed Brown or use the alternative interface DMCompositeGetAccessArray(). 173f73e5cebSJed Brown 17447c6ae99SBarry Smith Level: advanced 17547c6ae99SBarry Smith 176f73e5cebSJed Brown .seealso: DMCompositeGetEntries(), DMCompositeScatter() 17747c6ae99SBarry Smith @*/ 1787087cfbeSBarry Smith PetscErrorCode DMCompositeGetAccess(DM dm,Vec gvec,...) 17947c6ae99SBarry Smith { 18047c6ae99SBarry Smith va_list Argp; 18147c6ae99SBarry Smith PetscErrorCode ierr; 18247c6ae99SBarry Smith struct DMCompositeLink *next; 18347c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 1845edff71fSBarry Smith PetscInt readonly; 18571b14b3eSStefano Zampini PetscBool flg; 18647c6ae99SBarry Smith 18747c6ae99SBarry Smith PetscFunctionBegin; 18847c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 18947c6ae99SBarry Smith PetscValidHeaderSpecific(gvec,VEC_CLASSID,2); 19071b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 191*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 19247c6ae99SBarry Smith next = com->next; 19347c6ae99SBarry Smith if (!com->setup) { 194d7bf68aeSBarry Smith ierr = DMSetUp(dm);CHKERRQ(ierr); 19547c6ae99SBarry Smith } 19647c6ae99SBarry Smith 1975edff71fSBarry Smith ierr = VecLockGet(gvec,&readonly);CHKERRQ(ierr); 19847c6ae99SBarry Smith /* loop over packed objects, handling one at at time */ 19947c6ae99SBarry Smith va_start(Argp,gvec); 20047c6ae99SBarry Smith while (next) { 20147c6ae99SBarry Smith Vec *vec; 20247c6ae99SBarry Smith vec = va_arg(Argp, Vec*); 2039ae5db72SJed Brown if (vec) { 2049ae5db72SJed Brown ierr = DMGetGlobalVector(next->dm,vec);CHKERRQ(ierr); 2055edff71fSBarry Smith if (readonly) { 2065edff71fSBarry Smith const PetscScalar *array; 2075edff71fSBarry Smith ierr = VecGetArrayRead(gvec,&array);CHKERRQ(ierr); 2085edff71fSBarry Smith ierr = VecPlaceArray(*vec,array+next->rstart);CHKERRQ(ierr); 2098860a134SJunchao Zhang ierr = VecLockReadPush(*vec);CHKERRQ(ierr); 2105edff71fSBarry Smith ierr = VecRestoreArrayRead(gvec,&array);CHKERRQ(ierr); 2115edff71fSBarry Smith } else { 2125edff71fSBarry Smith PetscScalar *array; 2139ae5db72SJed Brown ierr = VecGetArray(gvec,&array);CHKERRQ(ierr); 2149ae5db72SJed Brown ierr = VecPlaceArray(*vec,array+next->rstart);CHKERRQ(ierr); 2159ae5db72SJed Brown ierr = VecRestoreArray(gvec,&array);CHKERRQ(ierr); 21647c6ae99SBarry Smith } 2175edff71fSBarry Smith } 21847c6ae99SBarry Smith next = next->next; 21947c6ae99SBarry Smith } 22047c6ae99SBarry Smith va_end(Argp); 22147c6ae99SBarry Smith PetscFunctionReturn(0); 22247c6ae99SBarry Smith } 22347c6ae99SBarry Smith 224f73e5cebSJed Brown /*@C 225f73e5cebSJed Brown DMCompositeGetAccessArray - Allows one to access the individual packed vectors in their global 226f73e5cebSJed Brown representation. 227f73e5cebSJed Brown 228d083f849SBarry Smith Collective on dm 229f73e5cebSJed Brown 230f73e5cebSJed Brown Input Parameters: 231f73e5cebSJed Brown + dm - the packer object 232f73e5cebSJed Brown . pvec - packed vector 233f73e5cebSJed Brown . nwanted - number of vectors wanted 2340298fd71SBarry Smith - wanted - sorted array of vectors wanted, or NULL to get all vectors 235f73e5cebSJed Brown 236f73e5cebSJed Brown Output Parameters: 237f73e5cebSJed Brown . vecs - array of requested global vectors (must be allocated) 238f73e5cebSJed Brown 23995452b02SPatrick Sanan Notes: 24095452b02SPatrick Sanan Use DMCompositeRestoreAccessArray() to return the vectors when you no longer need them 241f73e5cebSJed Brown 242f73e5cebSJed Brown Level: advanced 243f73e5cebSJed Brown 244f73e5cebSJed Brown .seealso: DMCompositeGetAccess(), DMCompositeGetEntries(), DMCompositeScatter(), DMCompositeGather() 245f73e5cebSJed Brown @*/ 246f73e5cebSJed Brown PetscErrorCode DMCompositeGetAccessArray(DM dm,Vec pvec,PetscInt nwanted,const PetscInt *wanted,Vec *vecs) 247f73e5cebSJed Brown { 248f73e5cebSJed Brown PetscErrorCode ierr; 249f73e5cebSJed Brown struct DMCompositeLink *link; 250f73e5cebSJed Brown PetscInt i,wnum; 251f73e5cebSJed Brown DM_Composite *com = (DM_Composite*)dm->data; 252bee642f7SBarry Smith PetscInt readonly; 25371b14b3eSStefano Zampini PetscBool flg; 254f73e5cebSJed Brown 255f73e5cebSJed Brown PetscFunctionBegin; 256f73e5cebSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 257f73e5cebSJed Brown PetscValidHeaderSpecific(pvec,VEC_CLASSID,2); 25871b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 259*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 260f73e5cebSJed Brown if (!com->setup) { 261f73e5cebSJed Brown ierr = DMSetUp(dm);CHKERRQ(ierr); 262f73e5cebSJed Brown } 263f73e5cebSJed Brown 264bee642f7SBarry Smith ierr = VecLockGet(pvec,&readonly);CHKERRQ(ierr); 265f73e5cebSJed Brown for (i=0,wnum=0,link=com->next; link && wnum<nwanted; i++,link=link->next) { 266f73e5cebSJed Brown if (!wanted || i == wanted[wnum]) { 267f73e5cebSJed Brown Vec v; 268f73e5cebSJed Brown ierr = DMGetGlobalVector(link->dm,&v);CHKERRQ(ierr); 269bee642f7SBarry Smith if (readonly) { 270bee642f7SBarry Smith const PetscScalar *array; 271bee642f7SBarry Smith ierr = VecGetArrayRead(pvec,&array);CHKERRQ(ierr); 272bee642f7SBarry Smith ierr = VecPlaceArray(v,array+link->rstart);CHKERRQ(ierr); 2738860a134SJunchao Zhang ierr = VecLockReadPush(v);CHKERRQ(ierr); 274bee642f7SBarry Smith ierr = VecRestoreArrayRead(pvec,&array);CHKERRQ(ierr); 275bee642f7SBarry Smith } else { 276bee642f7SBarry Smith PetscScalar *array; 277f73e5cebSJed Brown ierr = VecGetArray(pvec,&array);CHKERRQ(ierr); 278f73e5cebSJed Brown ierr = VecPlaceArray(v,array+link->rstart);CHKERRQ(ierr); 279f73e5cebSJed Brown ierr = VecRestoreArray(pvec,&array);CHKERRQ(ierr); 280bee642f7SBarry Smith } 281f73e5cebSJed Brown vecs[wnum++] = v; 282f73e5cebSJed Brown } 283f73e5cebSJed Brown } 284f73e5cebSJed Brown PetscFunctionReturn(0); 285f73e5cebSJed Brown } 286f73e5cebSJed Brown 2877ac2b803SAlex Fikl /*@C 2887ac2b803SAlex Fikl DMCompositeGetLocalAccessArray - Allows one to access the individual 2897ac2b803SAlex Fikl packed vectors in their local representation. 2907ac2b803SAlex Fikl 291d083f849SBarry Smith Collective on dm. 2927ac2b803SAlex Fikl 2937ac2b803SAlex Fikl Input Parameters: 2947ac2b803SAlex Fikl + dm - the packer object 2957ac2b803SAlex Fikl . pvec - packed vector 2967ac2b803SAlex Fikl . nwanted - number of vectors wanted 2977ac2b803SAlex Fikl - wanted - sorted array of vectors wanted, or NULL to get all vectors 2987ac2b803SAlex Fikl 2997ac2b803SAlex Fikl Output Parameters: 3007ac2b803SAlex Fikl . vecs - array of requested local vectors (must be allocated) 3017ac2b803SAlex Fikl 30295452b02SPatrick Sanan Notes: 30395452b02SPatrick Sanan Use DMCompositeRestoreLocalAccessArray() to return the vectors 3047ac2b803SAlex Fikl when you no longer need them. 3057ac2b803SAlex Fikl 3067ac2b803SAlex Fikl Level: advanced 3077ac2b803SAlex Fikl 3087ac2b803SAlex Fikl .seealso: DMCompositeRestoreLocalAccessArray(), DMCompositeGetAccess(), 3097ac2b803SAlex Fikl DMCompositeGetEntries(), DMCompositeScatter(), DMCompositeGather() 3107ac2b803SAlex Fikl @*/ 3117ac2b803SAlex Fikl PetscErrorCode DMCompositeGetLocalAccessArray(DM dm,Vec pvec,PetscInt nwanted,const PetscInt *wanted,Vec *vecs) 3127ac2b803SAlex Fikl { 3137ac2b803SAlex Fikl PetscErrorCode ierr; 3147ac2b803SAlex Fikl struct DMCompositeLink *link; 3157ac2b803SAlex Fikl PetscInt i,wnum; 3167ac2b803SAlex Fikl DM_Composite *com = (DM_Composite*)dm->data; 3177ac2b803SAlex Fikl PetscInt readonly; 3187ac2b803SAlex Fikl PetscInt nlocal = 0; 31971b14b3eSStefano Zampini PetscBool flg; 3207ac2b803SAlex Fikl 3217ac2b803SAlex Fikl PetscFunctionBegin; 3227ac2b803SAlex Fikl PetscValidHeaderSpecific(dm,DM_CLASSID,1); 3237ac2b803SAlex Fikl PetscValidHeaderSpecific(pvec,VEC_CLASSID,2); 32471b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 325*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 3267ac2b803SAlex Fikl if (!com->setup) { 3277ac2b803SAlex Fikl ierr = DMSetUp(dm);CHKERRQ(ierr); 3287ac2b803SAlex Fikl } 3297ac2b803SAlex Fikl 3307ac2b803SAlex Fikl ierr = VecLockGet(pvec,&readonly);CHKERRQ(ierr); 3317ac2b803SAlex Fikl for (i=0,wnum=0,link=com->next; link && wnum<nwanted; i++,link=link->next) { 3327ac2b803SAlex Fikl if (!wanted || i == wanted[wnum]) { 3337ac2b803SAlex Fikl Vec v; 3347ac2b803SAlex Fikl ierr = DMGetLocalVector(link->dm,&v);CHKERRQ(ierr); 3357ac2b803SAlex Fikl if (readonly) { 3367ac2b803SAlex Fikl const PetscScalar *array; 3377ac2b803SAlex Fikl ierr = VecGetArrayRead(pvec,&array);CHKERRQ(ierr); 3387ac2b803SAlex Fikl ierr = VecPlaceArray(v,array+nlocal);CHKERRQ(ierr); 339b1c3483dSMark Adams // this method does not make sense. The local vectors are not updated with a global-to-local and the user can not do it because it is locked 3408860a134SJunchao Zhang ierr = VecLockReadPush(v);CHKERRQ(ierr); 3417ac2b803SAlex Fikl ierr = VecRestoreArrayRead(pvec,&array);CHKERRQ(ierr); 3427ac2b803SAlex Fikl } else { 3437ac2b803SAlex Fikl PetscScalar *array; 3447ac2b803SAlex Fikl ierr = VecGetArray(pvec,&array);CHKERRQ(ierr); 3457ac2b803SAlex Fikl ierr = VecPlaceArray(v,array+nlocal);CHKERRQ(ierr); 3467ac2b803SAlex Fikl ierr = VecRestoreArray(pvec,&array);CHKERRQ(ierr); 3477ac2b803SAlex Fikl } 3487ac2b803SAlex Fikl vecs[wnum++] = v; 3497ac2b803SAlex Fikl } 3507ac2b803SAlex Fikl 3517ac2b803SAlex Fikl nlocal += link->nlocal; 3527ac2b803SAlex Fikl } 3537ac2b803SAlex Fikl 3547ac2b803SAlex Fikl PetscFunctionReturn(0); 3557ac2b803SAlex Fikl } 3567ac2b803SAlex Fikl 35747c6ae99SBarry Smith /*@C 358aa219208SBarry Smith DMCompositeRestoreAccess - Returns the vectors obtained with DMCompositeGetAccess() 35947c6ae99SBarry Smith representation. 36047c6ae99SBarry Smith 361d083f849SBarry Smith Collective on dm 36247c6ae99SBarry Smith 3639ae5db72SJed Brown Input Parameters: 36447c6ae99SBarry Smith + dm - the packer object 36547c6ae99SBarry Smith . gvec - the global vector 3660298fd71SBarry Smith - Vec* ... - the individual parallel vectors, NULL for those that are not needed 36747c6ae99SBarry Smith 36847c6ae99SBarry Smith Level: advanced 36947c6ae99SBarry Smith 3709ae5db72SJed Brown .seealso DMCompositeAddDM(), DMCreateGlobalVector(), 3716eb61c8cSJed Brown DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeScatter(), 372aa219208SBarry Smith DMCompositeRestoreAccess(), DMCompositeGetAccess() 37347c6ae99SBarry Smith 37447c6ae99SBarry Smith @*/ 3757087cfbeSBarry Smith PetscErrorCode DMCompositeRestoreAccess(DM dm,Vec gvec,...) 37647c6ae99SBarry Smith { 37747c6ae99SBarry Smith va_list Argp; 37847c6ae99SBarry Smith PetscErrorCode ierr; 37947c6ae99SBarry Smith struct DMCompositeLink *next; 38047c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 3815edff71fSBarry Smith PetscInt readonly; 38271b14b3eSStefano Zampini PetscBool flg; 38347c6ae99SBarry Smith 38447c6ae99SBarry Smith PetscFunctionBegin; 38547c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 38647c6ae99SBarry Smith PetscValidHeaderSpecific(gvec,VEC_CLASSID,2); 38771b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 388*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 38947c6ae99SBarry Smith next = com->next; 39047c6ae99SBarry Smith if (!com->setup) { 391d7bf68aeSBarry Smith ierr = DMSetUp(dm);CHKERRQ(ierr); 39247c6ae99SBarry Smith } 39347c6ae99SBarry Smith 3945edff71fSBarry Smith ierr = VecLockGet(gvec,&readonly);CHKERRQ(ierr); 39547c6ae99SBarry Smith /* loop over packed objects, handling one at at time */ 39647c6ae99SBarry Smith va_start(Argp,gvec); 39747c6ae99SBarry Smith while (next) { 39847c6ae99SBarry Smith Vec *vec; 39947c6ae99SBarry Smith vec = va_arg(Argp, Vec*); 4009ae5db72SJed Brown if (vec) { 4019ae5db72SJed Brown ierr = VecResetArray(*vec);CHKERRQ(ierr); 4025edff71fSBarry Smith if (readonly) { 4038860a134SJunchao Zhang ierr = VecLockReadPop(*vec);CHKERRQ(ierr); 4045edff71fSBarry Smith } 405bee642f7SBarry Smith ierr = DMRestoreGlobalVector(next->dm,vec);CHKERRQ(ierr); 40647c6ae99SBarry Smith } 40747c6ae99SBarry Smith next = next->next; 40847c6ae99SBarry Smith } 40947c6ae99SBarry Smith va_end(Argp); 41047c6ae99SBarry Smith PetscFunctionReturn(0); 41147c6ae99SBarry Smith } 41247c6ae99SBarry Smith 413f73e5cebSJed Brown /*@C 414f73e5cebSJed Brown DMCompositeRestoreAccessArray - Returns the vectors obtained with DMCompositeGetAccessArray() 415f73e5cebSJed Brown 416d083f849SBarry Smith Collective on dm 417f73e5cebSJed Brown 418f73e5cebSJed Brown Input Parameters: 419f73e5cebSJed Brown + dm - the packer object 420f73e5cebSJed Brown . pvec - packed vector 421f73e5cebSJed Brown . nwanted - number of vectors wanted 4220298fd71SBarry Smith . wanted - sorted array of vectors wanted, or NULL to get all vectors 423f73e5cebSJed Brown - vecs - array of global vectors to return 424f73e5cebSJed Brown 425f73e5cebSJed Brown Level: advanced 426f73e5cebSJed Brown 427f73e5cebSJed Brown .seealso: DMCompositeRestoreAccess(), DMCompositeRestoreEntries(), DMCompositeScatter(), DMCompositeGather() 428f73e5cebSJed Brown @*/ 429f73e5cebSJed Brown PetscErrorCode DMCompositeRestoreAccessArray(DM dm,Vec pvec,PetscInt nwanted,const PetscInt *wanted,Vec *vecs) 430f73e5cebSJed Brown { 431f73e5cebSJed Brown PetscErrorCode ierr; 432f73e5cebSJed Brown struct DMCompositeLink *link; 433f73e5cebSJed Brown PetscInt i,wnum; 434f73e5cebSJed Brown DM_Composite *com = (DM_Composite*)dm->data; 435bee642f7SBarry Smith PetscInt readonly; 43671b14b3eSStefano Zampini PetscBool flg; 437f73e5cebSJed Brown 438f73e5cebSJed Brown PetscFunctionBegin; 439f73e5cebSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 440f73e5cebSJed Brown PetscValidHeaderSpecific(pvec,VEC_CLASSID,2); 44171b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 442*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 443f73e5cebSJed Brown if (!com->setup) { 444f73e5cebSJed Brown ierr = DMSetUp(dm);CHKERRQ(ierr); 445f73e5cebSJed Brown } 446f73e5cebSJed Brown 447bee642f7SBarry Smith ierr = VecLockGet(pvec,&readonly);CHKERRQ(ierr); 448f73e5cebSJed Brown for (i=0,wnum=0,link=com->next; link && wnum<nwanted; i++,link=link->next) { 449f73e5cebSJed Brown if (!wanted || i == wanted[wnum]) { 450f73e5cebSJed Brown ierr = VecResetArray(vecs[wnum]);CHKERRQ(ierr); 451bee642f7SBarry Smith if (readonly) { 4528860a134SJunchao Zhang ierr = VecLockReadPop(vecs[wnum]);CHKERRQ(ierr); 453bee642f7SBarry Smith } 454f73e5cebSJed Brown ierr = DMRestoreGlobalVector(link->dm,&vecs[wnum]);CHKERRQ(ierr); 455f73e5cebSJed Brown wnum++; 456f73e5cebSJed Brown } 457f73e5cebSJed Brown } 458f73e5cebSJed Brown PetscFunctionReturn(0); 459f73e5cebSJed Brown } 460f73e5cebSJed Brown 4617ac2b803SAlex Fikl /*@C 4627ac2b803SAlex Fikl DMCompositeRestoreLocalAccessArray - Returns the vectors obtained with DMCompositeGetLocalAccessArray(). 4637ac2b803SAlex Fikl 464d083f849SBarry Smith Collective on dm. 4657ac2b803SAlex Fikl 4667ac2b803SAlex Fikl Input Parameters: 4677ac2b803SAlex Fikl + dm - the packer object 4687ac2b803SAlex Fikl . pvec - packed vector 4697ac2b803SAlex Fikl . nwanted - number of vectors wanted 4707ac2b803SAlex Fikl . wanted - sorted array of vectors wanted, or NULL to restore all vectors 4717ac2b803SAlex Fikl - vecs - array of local vectors to return 4727ac2b803SAlex Fikl 4737ac2b803SAlex Fikl Level: advanced 4747ac2b803SAlex Fikl 4757ac2b803SAlex Fikl Notes: 4767ac2b803SAlex Fikl nwanted and wanted must match the values given to DMCompositeGetLocalAccessArray() 4777ac2b803SAlex Fikl otherwise the call will fail. 4787ac2b803SAlex Fikl 4797ac2b803SAlex Fikl .seealso: DMCompositeGetLocalAccessArray(), DMCompositeRestoreAccessArray(), 4807ac2b803SAlex Fikl DMCompositeRestoreAccess(), DMCompositeRestoreEntries(), 4817ac2b803SAlex Fikl DMCompositeScatter(), DMCompositeGather() 4827ac2b803SAlex Fikl @*/ 4837ac2b803SAlex Fikl PetscErrorCode DMCompositeRestoreLocalAccessArray(DM dm,Vec pvec,PetscInt nwanted,const PetscInt *wanted,Vec *vecs) 4847ac2b803SAlex Fikl { 4857ac2b803SAlex Fikl PetscErrorCode ierr; 4867ac2b803SAlex Fikl struct DMCompositeLink *link; 4877ac2b803SAlex Fikl PetscInt i,wnum; 4887ac2b803SAlex Fikl DM_Composite *com = (DM_Composite*)dm->data; 4897ac2b803SAlex Fikl PetscInt readonly; 49071b14b3eSStefano Zampini PetscBool flg; 4917ac2b803SAlex Fikl 4927ac2b803SAlex Fikl PetscFunctionBegin; 4937ac2b803SAlex Fikl PetscValidHeaderSpecific(dm,DM_CLASSID,1); 4947ac2b803SAlex Fikl PetscValidHeaderSpecific(pvec,VEC_CLASSID,2); 49571b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 496*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 4977ac2b803SAlex Fikl if (!com->setup) { 4987ac2b803SAlex Fikl ierr = DMSetUp(dm);CHKERRQ(ierr); 4997ac2b803SAlex Fikl } 5007ac2b803SAlex Fikl 5017ac2b803SAlex Fikl ierr = VecLockGet(pvec,&readonly);CHKERRQ(ierr); 5027ac2b803SAlex Fikl for (i=0,wnum=0,link=com->next; link && wnum<nwanted; i++,link=link->next) { 5037ac2b803SAlex Fikl if (!wanted || i == wanted[wnum]) { 5047ac2b803SAlex Fikl ierr = VecResetArray(vecs[wnum]);CHKERRQ(ierr); 5057ac2b803SAlex Fikl if (readonly) { 5068860a134SJunchao Zhang ierr = VecLockReadPop(vecs[wnum]);CHKERRQ(ierr); 5077ac2b803SAlex Fikl } 5087ac2b803SAlex Fikl ierr = DMRestoreLocalVector(link->dm,&vecs[wnum]);CHKERRQ(ierr); 5097ac2b803SAlex Fikl wnum++; 5107ac2b803SAlex Fikl } 5117ac2b803SAlex Fikl } 5127ac2b803SAlex Fikl PetscFunctionReturn(0); 5137ac2b803SAlex Fikl } 5147ac2b803SAlex Fikl 51547c6ae99SBarry Smith /*@C 51647c6ae99SBarry Smith DMCompositeScatter - Scatters from a global packed vector into its individual local vectors 51747c6ae99SBarry Smith 518d083f849SBarry Smith Collective on dm 51947c6ae99SBarry Smith 5209ae5db72SJed Brown Input Parameters: 52147c6ae99SBarry Smith + dm - the packer object 52247c6ae99SBarry Smith . gvec - the global vector 5230298fd71SBarry Smith - Vec ... - the individual sequential vectors, NULL for those that are not needed 52447c6ae99SBarry Smith 52547c6ae99SBarry Smith Level: advanced 52647c6ae99SBarry Smith 5276f3c3dcfSJed Brown Notes: 5286f3c3dcfSJed Brown DMCompositeScatterArray() is a non-variadic alternative that is often more convenient for library callers and is 5296f3c3dcfSJed Brown accessible from Fortran. 5306f3c3dcfSJed Brown 5319ae5db72SJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(), 5326eb61c8cSJed Brown DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(), 53347c6ae99SBarry Smith DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries() 5346f3c3dcfSJed Brown DMCompositeScatterArray() 53547c6ae99SBarry Smith 53647c6ae99SBarry Smith @*/ 5377087cfbeSBarry Smith PetscErrorCode DMCompositeScatter(DM dm,Vec gvec,...) 53847c6ae99SBarry Smith { 53947c6ae99SBarry Smith va_list Argp; 54047c6ae99SBarry Smith PetscErrorCode ierr; 54147c6ae99SBarry Smith struct DMCompositeLink *next; 5428fd8f222SJed Brown PetscInt cnt; 54347c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 54471b14b3eSStefano Zampini PetscBool flg; 54547c6ae99SBarry Smith 54647c6ae99SBarry Smith PetscFunctionBegin; 54747c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 54847c6ae99SBarry Smith PetscValidHeaderSpecific(gvec,VEC_CLASSID,2); 54971b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 550*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 55147c6ae99SBarry Smith if (!com->setup) { 552d7bf68aeSBarry Smith ierr = DMSetUp(dm);CHKERRQ(ierr); 55347c6ae99SBarry Smith } 55447c6ae99SBarry Smith 55547c6ae99SBarry Smith /* loop over packed objects, handling one at at time */ 55647c6ae99SBarry Smith va_start(Argp,gvec); 5578fd8f222SJed Brown for (cnt=3,next=com->next; next; cnt++,next=next->next) { 5589ae5db72SJed Brown Vec local; 5599ae5db72SJed Brown local = va_arg(Argp, Vec); 5609ae5db72SJed Brown if (local) { 5619ae5db72SJed Brown Vec global; 5625edff71fSBarry Smith const PetscScalar *array; 563999739cfSJacob Faibussowitsch PetscDisableStaticAnalyzerForExpressionUnderstandingThatThisIsDangerousAndBugprone(PetscValidHeaderSpecific(local,VEC_CLASSID,cnt)); 5649ae5db72SJed Brown ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr); 5655edff71fSBarry Smith ierr = VecGetArrayRead(gvec,&array);CHKERRQ(ierr); 5669ae5db72SJed Brown ierr = VecPlaceArray(global,array+next->rstart);CHKERRQ(ierr); 5679ae5db72SJed Brown ierr = DMGlobalToLocalBegin(next->dm,global,INSERT_VALUES,local);CHKERRQ(ierr); 5689ae5db72SJed Brown ierr = DMGlobalToLocalEnd(next->dm,global,INSERT_VALUES,local);CHKERRQ(ierr); 5695edff71fSBarry Smith ierr = VecRestoreArrayRead(gvec,&array);CHKERRQ(ierr); 5709ae5db72SJed Brown ierr = VecResetArray(global);CHKERRQ(ierr); 5719ae5db72SJed Brown ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr); 57247c6ae99SBarry Smith } 57347c6ae99SBarry Smith } 57447c6ae99SBarry Smith va_end(Argp); 57547c6ae99SBarry Smith PetscFunctionReturn(0); 57647c6ae99SBarry Smith } 57747c6ae99SBarry Smith 5786f3c3dcfSJed Brown /*@ 5796f3c3dcfSJed Brown DMCompositeScatterArray - Scatters from a global packed vector into its individual local vectors 5806f3c3dcfSJed Brown 581d083f849SBarry Smith Collective on dm 5826f3c3dcfSJed Brown 5836f3c3dcfSJed Brown Input Parameters: 5846f3c3dcfSJed Brown + dm - the packer object 5856f3c3dcfSJed Brown . gvec - the global vector 586a2b725a8SWilliam Gropp - lvecs - array of local vectors, NULL for any that are not needed 5876f3c3dcfSJed Brown 5886f3c3dcfSJed Brown Level: advanced 5896f3c3dcfSJed Brown 5906f3c3dcfSJed Brown Note: 591907376e6SBarry Smith This is a non-variadic alternative to DMCompositeScatter() 5926f3c3dcfSJed Brown 5936f3c3dcfSJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector() 5946f3c3dcfSJed Brown DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(), 5956f3c3dcfSJed Brown DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries() 5966f3c3dcfSJed Brown 5976f3c3dcfSJed Brown @*/ 5986f3c3dcfSJed Brown PetscErrorCode DMCompositeScatterArray(DM dm,Vec gvec,Vec *lvecs) 5996f3c3dcfSJed Brown { 6006f3c3dcfSJed Brown PetscErrorCode ierr; 6016f3c3dcfSJed Brown struct DMCompositeLink *next; 6026f3c3dcfSJed Brown PetscInt i; 6036f3c3dcfSJed Brown DM_Composite *com = (DM_Composite*)dm->data; 60471b14b3eSStefano Zampini PetscBool flg; 6056f3c3dcfSJed Brown 6066f3c3dcfSJed Brown PetscFunctionBegin; 6076f3c3dcfSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 6086f3c3dcfSJed Brown PetscValidHeaderSpecific(gvec,VEC_CLASSID,2); 60971b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 610*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 6116f3c3dcfSJed Brown if (!com->setup) { 6126f3c3dcfSJed Brown ierr = DMSetUp(dm);CHKERRQ(ierr); 6136f3c3dcfSJed Brown } 6146f3c3dcfSJed Brown 6156f3c3dcfSJed Brown /* loop over packed objects, handling one at at time */ 6166f3c3dcfSJed Brown for (i=0,next=com->next; next; next=next->next,i++) { 6176f3c3dcfSJed Brown if (lvecs[i]) { 6186f3c3dcfSJed Brown Vec global; 619c5d31e75SLisandro Dalcin const PetscScalar *array; 6206f3c3dcfSJed Brown PetscValidHeaderSpecific(lvecs[i],VEC_CLASSID,3); 6216f3c3dcfSJed Brown ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr); 622c5d31e75SLisandro Dalcin ierr = VecGetArrayRead(gvec,&array);CHKERRQ(ierr); 623c5d31e75SLisandro Dalcin ierr = VecPlaceArray(global,(PetscScalar*)array+next->rstart);CHKERRQ(ierr); 6246f3c3dcfSJed Brown ierr = DMGlobalToLocalBegin(next->dm,global,INSERT_VALUES,lvecs[i]);CHKERRQ(ierr); 6256f3c3dcfSJed Brown ierr = DMGlobalToLocalEnd(next->dm,global,INSERT_VALUES,lvecs[i]);CHKERRQ(ierr); 626c5d31e75SLisandro Dalcin ierr = VecRestoreArrayRead(gvec,&array);CHKERRQ(ierr); 6276f3c3dcfSJed Brown ierr = VecResetArray(global);CHKERRQ(ierr); 6286f3c3dcfSJed Brown ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr); 6296f3c3dcfSJed Brown } 6306f3c3dcfSJed Brown } 6316f3c3dcfSJed Brown PetscFunctionReturn(0); 6326f3c3dcfSJed Brown } 6336f3c3dcfSJed Brown 63447c6ae99SBarry Smith /*@C 63547c6ae99SBarry Smith DMCompositeGather - Gathers into a global packed vector from its individual local vectors 63647c6ae99SBarry Smith 637d083f849SBarry Smith Collective on dm 63847c6ae99SBarry Smith 639d8d19677SJose E. Roman Input Parameters: 64047c6ae99SBarry Smith + dm - the packer object 64147c6ae99SBarry Smith . gvec - the global vector 642907376e6SBarry Smith . imode - INSERT_VALUES or ADD_VALUES 6430298fd71SBarry Smith - Vec ... - the individual sequential vectors, NULL for any that are not needed 64447c6ae99SBarry Smith 64547c6ae99SBarry Smith Level: advanced 64647c6ae99SBarry Smith 647f5f57ec0SBarry Smith Not available from Fortran, Fortran users can use DMCompositeGatherArray() 648f5f57ec0SBarry Smith 6499ae5db72SJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(), 6506eb61c8cSJed Brown DMCompositeScatter(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(), 65147c6ae99SBarry Smith DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries() 65247c6ae99SBarry Smith 65347c6ae99SBarry Smith @*/ 6541dac896bSSatish Balay PetscErrorCode DMCompositeGather(DM dm,InsertMode imode,Vec gvec,...) 65547c6ae99SBarry Smith { 65647c6ae99SBarry Smith va_list Argp; 65747c6ae99SBarry Smith PetscErrorCode ierr; 65847c6ae99SBarry Smith struct DMCompositeLink *next; 65947c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 6608fd8f222SJed Brown PetscInt cnt; 66171b14b3eSStefano Zampini PetscBool flg; 66247c6ae99SBarry Smith 66347c6ae99SBarry Smith PetscFunctionBegin; 66447c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 665064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(gvec,VEC_CLASSID,3); 66671b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 667*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 66847c6ae99SBarry Smith if (!com->setup) { 669d7bf68aeSBarry Smith ierr = DMSetUp(dm);CHKERRQ(ierr); 67047c6ae99SBarry Smith } 67147c6ae99SBarry Smith 67247c6ae99SBarry Smith /* loop over packed objects, handling one at at time */ 6731dac896bSSatish Balay va_start(Argp,gvec); 6748fd8f222SJed Brown for (cnt=3,next=com->next; next; cnt++,next=next->next) { 6759ae5db72SJed Brown Vec local; 6769ae5db72SJed Brown local = va_arg(Argp, Vec); 6779ae5db72SJed Brown if (local) { 67847c6ae99SBarry Smith PetscScalar *array; 6799ae5db72SJed Brown Vec global; 680999739cfSJacob Faibussowitsch PetscDisableStaticAnalyzerForExpressionUnderstandingThatThisIsDangerousAndBugprone(PetscValidHeaderSpecific(local,VEC_CLASSID,cnt)); 6819ae5db72SJed Brown ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr); 6829ae5db72SJed Brown ierr = VecGetArray(gvec,&array);CHKERRQ(ierr); 6839ae5db72SJed Brown ierr = VecPlaceArray(global,array+next->rstart);CHKERRQ(ierr); 6849ae5db72SJed Brown ierr = DMLocalToGlobalBegin(next->dm,local,imode,global);CHKERRQ(ierr); 6859ae5db72SJed Brown ierr = DMLocalToGlobalEnd(next->dm,local,imode,global);CHKERRQ(ierr); 6869ae5db72SJed Brown ierr = VecRestoreArray(gvec,&array);CHKERRQ(ierr); 6879ae5db72SJed Brown ierr = VecResetArray(global);CHKERRQ(ierr); 6889ae5db72SJed Brown ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr); 68947c6ae99SBarry Smith } 69047c6ae99SBarry Smith } 69147c6ae99SBarry Smith va_end(Argp); 69247c6ae99SBarry Smith PetscFunctionReturn(0); 69347c6ae99SBarry Smith } 69447c6ae99SBarry Smith 6956f3c3dcfSJed Brown /*@ 6966f3c3dcfSJed Brown DMCompositeGatherArray - Gathers into a global packed vector from its individual local vectors 6976f3c3dcfSJed Brown 698d083f849SBarry Smith Collective on dm 6996f3c3dcfSJed Brown 700d8d19677SJose E. Roman Input Parameters: 7016f3c3dcfSJed Brown + dm - the packer object 7026f3c3dcfSJed Brown . gvec - the global vector 703907376e6SBarry Smith . imode - INSERT_VALUES or ADD_VALUES 7046f3c3dcfSJed Brown - lvecs - the individual sequential vectors, NULL for any that are not needed 7056f3c3dcfSJed Brown 7066f3c3dcfSJed Brown Level: advanced 7076f3c3dcfSJed Brown 7086f3c3dcfSJed Brown Notes: 7096f3c3dcfSJed Brown This is a non-variadic alternative to DMCompositeGather(). 7106f3c3dcfSJed Brown 7116f3c3dcfSJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(), 7126f3c3dcfSJed Brown DMCompositeScatter(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(), 7136f3c3dcfSJed Brown DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries(), 7146f3c3dcfSJed Brown @*/ 7151dac896bSSatish Balay PetscErrorCode DMCompositeGatherArray(DM dm,InsertMode imode,Vec gvec,Vec *lvecs) 7166f3c3dcfSJed Brown { 7176f3c3dcfSJed Brown PetscErrorCode ierr; 7186f3c3dcfSJed Brown struct DMCompositeLink *next; 7196f3c3dcfSJed Brown DM_Composite *com = (DM_Composite*)dm->data; 7206f3c3dcfSJed Brown PetscInt i; 72171b14b3eSStefano Zampini PetscBool flg; 7226f3c3dcfSJed Brown 7236f3c3dcfSJed Brown PetscFunctionBegin; 7246f3c3dcfSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 725064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(gvec,VEC_CLASSID,3); 72671b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 727*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 7286f3c3dcfSJed Brown if (!com->setup) { 7296f3c3dcfSJed Brown ierr = DMSetUp(dm);CHKERRQ(ierr); 7306f3c3dcfSJed Brown } 7316f3c3dcfSJed Brown 7326f3c3dcfSJed Brown /* loop over packed objects, handling one at at time */ 7336f3c3dcfSJed Brown for (next=com->next,i=0; next; next=next->next,i++) { 7346f3c3dcfSJed Brown if (lvecs[i]) { 7356f3c3dcfSJed Brown PetscScalar *array; 7366f3c3dcfSJed Brown Vec global; 737064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(lvecs[i],VEC_CLASSID,4); 7386f3c3dcfSJed Brown ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr); 7396f3c3dcfSJed Brown ierr = VecGetArray(gvec,&array);CHKERRQ(ierr); 7406f3c3dcfSJed Brown ierr = VecPlaceArray(global,array+next->rstart);CHKERRQ(ierr); 7416f3c3dcfSJed Brown ierr = DMLocalToGlobalBegin(next->dm,lvecs[i],imode,global);CHKERRQ(ierr); 7426f3c3dcfSJed Brown ierr = DMLocalToGlobalEnd(next->dm,lvecs[i],imode,global);CHKERRQ(ierr); 7436f3c3dcfSJed Brown ierr = VecRestoreArray(gvec,&array);CHKERRQ(ierr); 7446f3c3dcfSJed Brown ierr = VecResetArray(global);CHKERRQ(ierr); 7456f3c3dcfSJed Brown ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr); 7466f3c3dcfSJed Brown } 7476f3c3dcfSJed Brown } 7486f3c3dcfSJed Brown PetscFunctionReturn(0); 7496f3c3dcfSJed Brown } 7506f3c3dcfSJed Brown 751f5f57ec0SBarry Smith /*@ 752aa219208SBarry Smith DMCompositeAddDM - adds a DM vector to a DMComposite 75347c6ae99SBarry Smith 754d083f849SBarry Smith Collective on dm 75547c6ae99SBarry Smith 756d8d19677SJose E. Roman Input Parameters: 7579b52a9b5SPatrick Sanan + dmc - the DMComposite (packer) object 758f5f57ec0SBarry Smith - dm - the DM object 75947c6ae99SBarry Smith 76047c6ae99SBarry Smith Level: advanced 76147c6ae99SBarry Smith 7620c010503SBarry Smith .seealso DMDestroy(), DMCompositeGather(), DMCompositeAddDM(), DMCreateGlobalVector(), 7636eb61c8cSJed Brown DMCompositeScatter(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(), 76447c6ae99SBarry Smith DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries() 76547c6ae99SBarry Smith 76647c6ae99SBarry Smith @*/ 7677087cfbeSBarry Smith PetscErrorCode DMCompositeAddDM(DM dmc,DM dm) 76847c6ae99SBarry Smith { 76947c6ae99SBarry Smith PetscErrorCode ierr; 77006ebdd98SJed Brown PetscInt n,nlocal; 77147c6ae99SBarry Smith struct DMCompositeLink *mine,*next; 77206ebdd98SJed Brown Vec global,local; 77347c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dmc->data; 77471b14b3eSStefano Zampini PetscBool flg; 77547c6ae99SBarry Smith 77647c6ae99SBarry Smith PetscFunctionBegin; 77747c6ae99SBarry Smith PetscValidHeaderSpecific(dmc,DM_CLASSID,1); 77847c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 77971b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dmc,DMCOMPOSITE,&flg);CHKERRQ(ierr); 780*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 78147c6ae99SBarry Smith next = com->next; 7822c71b3e2SJacob Faibussowitsch PetscCheckFalse(com->setup,PetscObjectComm((PetscObject)dmc),PETSC_ERR_ARG_WRONGSTATE,"Cannot add a DM once you have used the DMComposite"); 78347c6ae99SBarry Smith 78447c6ae99SBarry Smith /* create new link */ 785b00a9115SJed Brown ierr = PetscNew(&mine);CHKERRQ(ierr); 78647c6ae99SBarry Smith ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 78747c6ae99SBarry Smith ierr = DMGetGlobalVector(dm,&global);CHKERRQ(ierr); 78847c6ae99SBarry Smith ierr = VecGetLocalSize(global,&n);CHKERRQ(ierr); 78947c6ae99SBarry Smith ierr = DMRestoreGlobalVector(dm,&global);CHKERRQ(ierr); 79006ebdd98SJed Brown ierr = DMGetLocalVector(dm,&local);CHKERRQ(ierr); 79106ebdd98SJed Brown ierr = VecGetSize(local,&nlocal);CHKERRQ(ierr); 79206ebdd98SJed Brown ierr = DMRestoreLocalVector(dm,&local);CHKERRQ(ierr); 7938865f1eaSKarl Rupp 79447c6ae99SBarry Smith mine->n = n; 79506ebdd98SJed Brown mine->nlocal = nlocal; 79647c6ae99SBarry Smith mine->dm = dm; 7970298fd71SBarry Smith mine->next = NULL; 79847c6ae99SBarry Smith com->n += n; 7997ac2b803SAlex Fikl com->nghost += nlocal; 80047c6ae99SBarry Smith 80147c6ae99SBarry Smith /* add to end of list */ 8028865f1eaSKarl Rupp if (!next) com->next = mine; 8038865f1eaSKarl Rupp else { 80447c6ae99SBarry Smith while (next->next) next = next->next; 80547c6ae99SBarry Smith next->next = mine; 80647c6ae99SBarry Smith } 80747c6ae99SBarry Smith com->nDM++; 80847c6ae99SBarry Smith com->nmine++; 80947c6ae99SBarry Smith PetscFunctionReturn(0); 81047c6ae99SBarry Smith } 81147c6ae99SBarry Smith 8129804daf3SBarry Smith #include <petscdraw.h> 81326887b52SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec,PetscViewer); 8147087cfbeSBarry Smith PetscErrorCode VecView_DMComposite(Vec gvec,PetscViewer viewer) 81547c6ae99SBarry Smith { 81647c6ae99SBarry Smith DM dm; 81747c6ae99SBarry Smith PetscErrorCode ierr; 81847c6ae99SBarry Smith struct DMCompositeLink *next; 81947c6ae99SBarry Smith PetscBool isdraw; 820cef07954SSatish Balay DM_Composite *com; 82147c6ae99SBarry Smith 82247c6ae99SBarry Smith PetscFunctionBegin; 823c688c046SMatthew G Knepley ierr = VecGetDM(gvec, &dm);CHKERRQ(ierr); 824*7a8be351SBarry Smith PetscCheck(dm,PetscObjectComm((PetscObject)gvec),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMComposite"); 82547c6ae99SBarry Smith com = (DM_Composite*)dm->data; 82647c6ae99SBarry Smith next = com->next; 82747c6ae99SBarry Smith 828251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 82947c6ae99SBarry Smith if (!isdraw) { 83047c6ae99SBarry Smith /* do I really want to call this? */ 83147c6ae99SBarry Smith ierr = VecView_MPI(gvec,viewer);CHKERRQ(ierr); 83247c6ae99SBarry Smith } else { 83347c6ae99SBarry Smith PetscInt cnt = 0; 83447c6ae99SBarry Smith 83547c6ae99SBarry Smith /* loop over packed objects, handling one at at time */ 83647c6ae99SBarry Smith while (next) { 83747c6ae99SBarry Smith Vec vec; 838ca4278abSLisandro Dalcin const PetscScalar *array; 83947c6ae99SBarry Smith PetscInt bs; 84047c6ae99SBarry Smith 8419ae5db72SJed Brown /* Should use VecGetSubVector() eventually, but would need to forward the DM for that to work */ 8429ae5db72SJed Brown ierr = DMGetGlobalVector(next->dm,&vec);CHKERRQ(ierr); 843ca4278abSLisandro Dalcin ierr = VecGetArrayRead(gvec,&array);CHKERRQ(ierr); 844ca4278abSLisandro Dalcin ierr = VecPlaceArray(vec,(PetscScalar*)array+next->rstart);CHKERRQ(ierr); 845ca4278abSLisandro Dalcin ierr = VecRestoreArrayRead(gvec,&array);CHKERRQ(ierr); 84647c6ae99SBarry Smith ierr = VecView(vec,viewer);CHKERRQ(ierr); 8479ae5db72SJed Brown ierr = VecResetArray(vec);CHKERRQ(ierr); 848ca4278abSLisandro Dalcin ierr = VecGetBlockSize(vec,&bs);CHKERRQ(ierr); 8499ae5db72SJed Brown ierr = DMRestoreGlobalVector(next->dm,&vec);CHKERRQ(ierr); 85047c6ae99SBarry Smith ierr = PetscViewerDrawBaseAdd(viewer,bs);CHKERRQ(ierr); 85147c6ae99SBarry Smith cnt += bs; 85247c6ae99SBarry Smith next = next->next; 85347c6ae99SBarry Smith } 85447c6ae99SBarry Smith ierr = PetscViewerDrawBaseAdd(viewer,-cnt);CHKERRQ(ierr); 85547c6ae99SBarry Smith } 85647c6ae99SBarry Smith PetscFunctionReturn(0); 85747c6ae99SBarry Smith } 85847c6ae99SBarry Smith 8597087cfbeSBarry Smith PetscErrorCode DMCreateGlobalVector_Composite(DM dm,Vec *gvec) 86047c6ae99SBarry Smith { 86147c6ae99SBarry Smith PetscErrorCode ierr; 86247c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 86347c6ae99SBarry Smith 86447c6ae99SBarry Smith PetscFunctionBegin; 86547c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 86605ec3129SRichard Tran Mills ierr = DMSetFromOptions(dm);CHKERRQ(ierr); 867d7bf68aeSBarry Smith ierr = DMSetUp(dm);CHKERRQ(ierr); 86805ec3129SRichard Tran Mills ierr = VecCreate(PetscObjectComm((PetscObject)dm),gvec);CHKERRQ(ierr); 86905ec3129SRichard Tran Mills ierr = VecSetType(*gvec,dm->vectype);CHKERRQ(ierr); 87005ec3129SRichard Tran Mills ierr = VecSetSizes(*gvec,com->n,com->N);CHKERRQ(ierr); 871c688c046SMatthew G Knepley ierr = VecSetDM(*gvec, dm);CHKERRQ(ierr); 87247c6ae99SBarry Smith ierr = VecSetOperation(*gvec,VECOP_VIEW,(void (*)(void))VecView_DMComposite);CHKERRQ(ierr); 87347c6ae99SBarry Smith PetscFunctionReturn(0); 87447c6ae99SBarry Smith } 87547c6ae99SBarry Smith 8767087cfbeSBarry Smith PetscErrorCode DMCreateLocalVector_Composite(DM dm,Vec *lvec) 87747c6ae99SBarry Smith { 87847c6ae99SBarry Smith PetscErrorCode ierr; 87947c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 88047c6ae99SBarry Smith 88147c6ae99SBarry Smith PetscFunctionBegin; 88247c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 88347c6ae99SBarry Smith if (!com->setup) { 88405ec3129SRichard Tran Mills ierr = DMSetFromOptions(dm);CHKERRQ(ierr); 885d7bf68aeSBarry Smith ierr = DMSetUp(dm);CHKERRQ(ierr); 88647c6ae99SBarry Smith } 88705ec3129SRichard Tran Mills ierr = VecCreate(PETSC_COMM_SELF,lvec);CHKERRQ(ierr); 88805ec3129SRichard Tran Mills ierr = VecSetType(*lvec,dm->vectype);CHKERRQ(ierr); 88905ec3129SRichard Tran Mills ierr = VecSetSizes(*lvec,com->nghost,PETSC_DECIDE);CHKERRQ(ierr); 890c688c046SMatthew G Knepley ierr = VecSetDM(*lvec, dm);CHKERRQ(ierr); 89147c6ae99SBarry Smith PetscFunctionReturn(0); 89247c6ae99SBarry Smith } 89347c6ae99SBarry Smith 89447c6ae99SBarry Smith /*@C 8959ae5db72SJed Brown DMCompositeGetISLocalToGlobalMappings - gets an ISLocalToGlobalMapping for each DM in the DMComposite, maps to the composite global space 89647c6ae99SBarry Smith 89706ebdd98SJed Brown Collective on DM 89847c6ae99SBarry Smith 89947c6ae99SBarry Smith Input Parameter: 90047c6ae99SBarry Smith . dm - the packer object 90147c6ae99SBarry Smith 90247c6ae99SBarry Smith Output Parameters: 9039ae5db72SJed Brown . ltogs - the individual mappings for each packed vector. Note that this includes 9049ae5db72SJed Brown all the ghost points that individual ghosted DMDA's may have. 90547c6ae99SBarry Smith 90647c6ae99SBarry Smith Level: advanced 90747c6ae99SBarry Smith 90847c6ae99SBarry Smith Notes: 9096eb61c8cSJed Brown Each entry of ltogs should be destroyed with ISLocalToGlobalMappingDestroy(), the ltogs array should be freed with PetscFree(). 91047c6ae99SBarry Smith 911f5f57ec0SBarry Smith Not available from Fortran 912f5f57ec0SBarry Smith 9139ae5db72SJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(), 91447c6ae99SBarry Smith DMCompositeGather(), DMCompositeCreate(), DMCompositeGetAccess(), DMCompositeScatter(), 91547c6ae99SBarry Smith DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(),DMCompositeGetEntries() 91647c6ae99SBarry Smith 91747c6ae99SBarry Smith @*/ 9187087cfbeSBarry Smith PetscErrorCode DMCompositeGetISLocalToGlobalMappings(DM dm,ISLocalToGlobalMapping **ltogs) 91947c6ae99SBarry Smith { 92047c6ae99SBarry Smith PetscErrorCode ierr; 92147c6ae99SBarry Smith PetscInt i,*idx,n,cnt; 92247c6ae99SBarry Smith struct DMCompositeLink *next; 92347c6ae99SBarry Smith PetscMPIInt rank; 92447c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 92571b14b3eSStefano Zampini PetscBool flg; 92647c6ae99SBarry Smith 92747c6ae99SBarry Smith PetscFunctionBegin; 92847c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 92971b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 930*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 931728e99d6SJed Brown ierr = DMSetUp(dm);CHKERRQ(ierr); 932854ce69bSBarry Smith ierr = PetscMalloc1(com->nDM,ltogs);CHKERRQ(ierr); 93347c6ae99SBarry Smith next = com->next; 934ffc4695bSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm),&rank);CHKERRMPI(ierr); 93547c6ae99SBarry Smith 93647c6ae99SBarry Smith /* loop over packed objects, handling one at at time */ 93747c6ae99SBarry Smith cnt = 0; 93847c6ae99SBarry Smith while (next) { 9396eb61c8cSJed Brown ISLocalToGlobalMapping ltog; 9406eb61c8cSJed Brown PetscMPIInt size; 94186994e45SJed Brown const PetscInt *suboff,*indices; 9426eb61c8cSJed Brown Vec global; 94347c6ae99SBarry Smith 9446eb61c8cSJed Brown /* Get sub-DM global indices for each local dof */ 9451411c6eeSJed Brown ierr = DMGetLocalToGlobalMapping(next->dm,<og);CHKERRQ(ierr); 9466eb61c8cSJed Brown ierr = ISLocalToGlobalMappingGetSize(ltog,&n);CHKERRQ(ierr); 94786994e45SJed Brown ierr = ISLocalToGlobalMappingGetIndices(ltog,&indices);CHKERRQ(ierr); 948785e854fSJed Brown ierr = PetscMalloc1(n,&idx);CHKERRQ(ierr); 94947c6ae99SBarry Smith 9506eb61c8cSJed Brown /* Get the offsets for the sub-DM global vector */ 9516eb61c8cSJed Brown ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr); 9526eb61c8cSJed Brown ierr = VecGetOwnershipRanges(global,&suboff);CHKERRQ(ierr); 953ffc4695bSBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject)global),&size);CHKERRMPI(ierr); 9546eb61c8cSJed Brown 9556eb61c8cSJed Brown /* Shift the sub-DM definition of the global space to the composite global space */ 9566eb61c8cSJed Brown for (i=0; i<n; i++) { 95786994e45SJed Brown PetscInt subi = indices[i],lo = 0,hi = size,t; 958e333b035SStefano Zampini /* There's no consensus on what a negative index means, 959e333b035SStefano Zampini except for skipping when setting the values in vectors and matrices */ 960e333b035SStefano Zampini if (subi < 0) { idx[i] = subi - next->grstarts[rank]; continue; } 9616eb61c8cSJed Brown /* Binary search to find which rank owns subi */ 9626eb61c8cSJed Brown while (hi-lo > 1) { 9636eb61c8cSJed Brown t = lo + (hi-lo)/2; 9646eb61c8cSJed Brown if (suboff[t] > subi) hi = t; 9656eb61c8cSJed Brown else lo = t; 9666eb61c8cSJed Brown } 9676eb61c8cSJed Brown idx[i] = subi - suboff[lo] + next->grstarts[lo]; 9686eb61c8cSJed Brown } 96986994e45SJed Brown ierr = ISLocalToGlobalMappingRestoreIndices(ltog,&indices);CHKERRQ(ierr); 970f0413b6fSBarry Smith ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm),1,n,idx,PETSC_OWN_POINTER,&(*ltogs)[cnt]);CHKERRQ(ierr); 9716eb61c8cSJed Brown ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr); 97247c6ae99SBarry Smith next = next->next; 97347c6ae99SBarry Smith cnt++; 97447c6ae99SBarry Smith } 97547c6ae99SBarry Smith PetscFunctionReturn(0); 97647c6ae99SBarry Smith } 97747c6ae99SBarry Smith 97887c85e80SJed Brown /*@C 9799ae5db72SJed Brown DMCompositeGetLocalISs - Gets index sets for each component of a composite local vector 98087c85e80SJed Brown 98187c85e80SJed Brown Not Collective 98287c85e80SJed Brown 9834165533cSJose E. Roman Input Parameter: 98487c85e80SJed Brown . dm - composite DM 98587c85e80SJed Brown 9864165533cSJose E. Roman Output Parameter: 98787c85e80SJed Brown . is - array of serial index sets for each each component of the DMComposite 98887c85e80SJed Brown 98987c85e80SJed Brown Level: intermediate 99087c85e80SJed Brown 99187c85e80SJed Brown Notes: 99287c85e80SJed Brown At present, a composite local vector does not normally exist. This function is used to provide index sets for 99387c85e80SJed Brown MatGetLocalSubMatrix(). In the future, the scatters for each entry in the DMComposite may be be merged into a single 9949ae5db72SJed Brown scatter to a composite local vector. The user should not typically need to know which is being done. 99587c85e80SJed Brown 99687c85e80SJed Brown To get the composite global indices at all local points (including ghosts), use DMCompositeGetISLocalToGlobalMappings(). 99787c85e80SJed Brown 99887c85e80SJed Brown To get index sets for pieces of the composite global vector, use DMCompositeGetGlobalISs(). 99987c85e80SJed Brown 100087c85e80SJed Brown Each returned IS should be destroyed with ISDestroy(), the array should be freed with PetscFree(). 100187c85e80SJed Brown 1002f5f57ec0SBarry Smith Not available from Fortran 1003f5f57ec0SBarry Smith 100487c85e80SJed Brown .seealso: DMCompositeGetGlobalISs(), DMCompositeGetISLocalToGlobalMappings(), MatGetLocalSubMatrix(), MatCreateLocalRef() 100587c85e80SJed Brown @*/ 10067087cfbeSBarry Smith PetscErrorCode DMCompositeGetLocalISs(DM dm,IS **is) 100787c85e80SJed Brown { 100887c85e80SJed Brown PetscErrorCode ierr; 100987c85e80SJed Brown DM_Composite *com = (DM_Composite*)dm->data; 101087c85e80SJed Brown struct DMCompositeLink *link; 101187c85e80SJed Brown PetscInt cnt,start; 101271b14b3eSStefano Zampini PetscBool flg; 101387c85e80SJed Brown 101487c85e80SJed Brown PetscFunctionBegin; 101587c85e80SJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 101687c85e80SJed Brown PetscValidPointer(is,2); 101771b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 1018*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 1019785e854fSJed Brown ierr = PetscMalloc1(com->nmine,is);CHKERRQ(ierr); 102006ebdd98SJed Brown for (cnt=0,start=0,link=com->next; link; start+=link->nlocal,cnt++,link=link->next) { 1021520db06cSJed Brown PetscInt bs; 10229ae5db72SJed Brown ierr = ISCreateStride(PETSC_COMM_SELF,link->nlocal,start,1,&(*is)[cnt]);CHKERRQ(ierr); 10231411c6eeSJed Brown ierr = DMGetBlockSize(link->dm,&bs);CHKERRQ(ierr); 1024520db06cSJed Brown ierr = ISSetBlockSize((*is)[cnt],bs);CHKERRQ(ierr); 1025520db06cSJed Brown } 102687c85e80SJed Brown PetscFunctionReturn(0); 102787c85e80SJed Brown } 102887c85e80SJed Brown 102947c6ae99SBarry Smith /*@C 103047c6ae99SBarry Smith DMCompositeGetGlobalISs - Gets the index sets for each composed object 103147c6ae99SBarry Smith 1032d083f849SBarry Smith Collective on dm 103347c6ae99SBarry Smith 103447c6ae99SBarry Smith Input Parameter: 103547c6ae99SBarry Smith . dm - the packer object 103647c6ae99SBarry Smith 103747c6ae99SBarry Smith Output Parameters: 103847c6ae99SBarry Smith . is - the array of index sets 103947c6ae99SBarry Smith 104047c6ae99SBarry Smith Level: advanced 104147c6ae99SBarry Smith 104247c6ae99SBarry Smith Notes: 104347c6ae99SBarry Smith The is entries should be destroyed with ISDestroy(), the is array should be freed with PetscFree() 104447c6ae99SBarry Smith 104547c6ae99SBarry Smith These could be used to extract a subset of vector entries for a "multi-physics" preconditioner 104647c6ae99SBarry Smith 10476eb61c8cSJed Brown Use DMCompositeGetLocalISs() for index sets in the packed local numbering, and 10486eb61c8cSJed Brown DMCompositeGetISLocalToGlobalMappings() for to map local sub-DM (including ghost) indices to packed global 10496eb61c8cSJed Brown indices. 105047c6ae99SBarry Smith 1051f3cb0f7eSJed Brown Fortran Notes: 1052f3cb0f7eSJed Brown 1053f3cb0f7eSJed Brown The output argument 'is' must be an allocated array of sufficient length, which can be learned using DMCompositeGetNumberDM(). 1054f3cb0f7eSJed Brown 10559ae5db72SJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(), 105647c6ae99SBarry Smith DMCompositeGather(), DMCompositeCreate(), DMCompositeGetAccess(), DMCompositeScatter(), 105747c6ae99SBarry Smith DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(),DMCompositeGetEntries() 105847c6ae99SBarry Smith 105947c6ae99SBarry Smith @*/ 10607087cfbeSBarry Smith PetscErrorCode DMCompositeGetGlobalISs(DM dm,IS *is[]) 106147c6ae99SBarry Smith { 106247c6ae99SBarry Smith PetscErrorCode ierr; 106366bb578eSMark Adams PetscInt cnt = 0; 106447c6ae99SBarry Smith struct DMCompositeLink *next; 106547c6ae99SBarry Smith PetscMPIInt rank; 106647c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 106771b14b3eSStefano Zampini PetscBool flg; 106847c6ae99SBarry Smith 106947c6ae99SBarry Smith PetscFunctionBegin; 107047c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 107171b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 1072*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 1073*7a8be351SBarry Smith PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Must call DMSetUp() before"); 1074854ce69bSBarry Smith ierr = PetscMalloc1(com->nDM,is);CHKERRQ(ierr); 107547c6ae99SBarry Smith next = com->next; 1076ffc4695bSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm),&rank);CHKERRMPI(ierr); 107747c6ae99SBarry Smith 107847c6ae99SBarry Smith /* loop over packed objects, handling one at at time */ 107947c6ae99SBarry Smith while (next) { 1080e5e52638SMatthew G. Knepley PetscDS prob; 1081e5e52638SMatthew G. Knepley 108266bb578eSMark Adams ierr = ISCreateStride(PetscObjectComm((PetscObject)dm),next->n,next->grstart,1,&(*is)[cnt]);CHKERRQ(ierr); 1083e5e52638SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1084e5e52638SMatthew G. Knepley if (prob) { 108565c226d8SMatthew G. Knepley MatNullSpace space; 108665c226d8SMatthew G. Knepley Mat pmat; 10870f21e855SMatthew G. Knepley PetscObject disc; 10880f21e855SMatthew G. Knepley PetscInt Nf; 108965c226d8SMatthew G. Knepley 1090e5e52638SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1091f24dd8d2SMatthew G. Knepley if (cnt < Nf) { 1092e5e52638SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, cnt, &disc);CHKERRQ(ierr); 10930f21e855SMatthew G. Knepley ierr = PetscObjectQuery(disc, "nullspace", (PetscObject*) &space);CHKERRQ(ierr); 1094aac2dd2dSMatthew G. Knepley if (space) {ierr = PetscObjectCompose((PetscObject) (*is)[cnt], "nullspace", (PetscObject) space);CHKERRQ(ierr);} 10950f21e855SMatthew G. Knepley ierr = PetscObjectQuery(disc, "nearnullspace", (PetscObject*) &space);CHKERRQ(ierr); 1096aac2dd2dSMatthew G. Knepley if (space) {ierr = PetscObjectCompose((PetscObject) (*is)[cnt], "nearnullspace", (PetscObject) space);CHKERRQ(ierr);} 10970f21e855SMatthew G. Knepley ierr = PetscObjectQuery(disc, "pmat", (PetscObject*) &pmat);CHKERRQ(ierr); 1098aac2dd2dSMatthew G. Knepley if (pmat) {ierr = PetscObjectCompose((PetscObject) (*is)[cnt], "pmat", (PetscObject) pmat);CHKERRQ(ierr);} 109965c226d8SMatthew G. Knepley } 1100f24dd8d2SMatthew G. Knepley } 110147c6ae99SBarry Smith cnt++; 110247c6ae99SBarry Smith next = next->next; 110347c6ae99SBarry Smith } 110447c6ae99SBarry Smith PetscFunctionReturn(0); 110547c6ae99SBarry Smith } 110647c6ae99SBarry Smith 110721c9b008SJed Brown PetscErrorCode DMCreateFieldIS_Composite(DM dm, PetscInt *numFields,char ***fieldNames, IS **fields) 11084d343eeaSMatthew G Knepley { 11094d343eeaSMatthew G Knepley PetscInt nDM; 11104d343eeaSMatthew G Knepley DM *dms; 11114d343eeaSMatthew G Knepley PetscInt i; 11124d343eeaSMatthew G Knepley PetscErrorCode ierr; 11134d343eeaSMatthew G Knepley 11144d343eeaSMatthew G Knepley PetscFunctionBegin; 11154d343eeaSMatthew G Knepley ierr = DMCompositeGetNumberDM(dm, &nDM);CHKERRQ(ierr); 11168865f1eaSKarl Rupp if (numFields) *numFields = nDM; 11174d343eeaSMatthew G Knepley ierr = DMCompositeGetGlobalISs(dm, fields);CHKERRQ(ierr); 11184d343eeaSMatthew G Knepley if (fieldNames) { 1119785e854fSJed Brown ierr = PetscMalloc1(nDM, &dms);CHKERRQ(ierr); 1120785e854fSJed Brown ierr = PetscMalloc1(nDM, fieldNames);CHKERRQ(ierr); 11214d343eeaSMatthew G Knepley ierr = DMCompositeGetEntriesArray(dm, dms);CHKERRQ(ierr); 11224d343eeaSMatthew G Knepley for (i=0; i<nDM; i++) { 11234d343eeaSMatthew G Knepley char buf[256]; 11244d343eeaSMatthew G Knepley const char *splitname; 11254d343eeaSMatthew G Knepley 11264d343eeaSMatthew G Knepley /* Split naming precedence: object name, prefix, number */ 11274d343eeaSMatthew G Knepley splitname = ((PetscObject) dm)->name; 11284d343eeaSMatthew G Knepley if (!splitname) { 11294d343eeaSMatthew G Knepley ierr = PetscObjectGetOptionsPrefix((PetscObject)dms[i],&splitname);CHKERRQ(ierr); 11304d343eeaSMatthew G Knepley if (splitname) { 11314d343eeaSMatthew G Knepley size_t len; 11328caf3d72SBarry Smith ierr = PetscStrncpy(buf,splitname,sizeof(buf));CHKERRQ(ierr); 11338caf3d72SBarry Smith buf[sizeof(buf) - 1] = 0; 11344d343eeaSMatthew G Knepley ierr = PetscStrlen(buf,&len);CHKERRQ(ierr); 11354d343eeaSMatthew G Knepley if (buf[len-1] == '_') buf[len-1] = 0; /* Remove trailing underscore if it was used */ 11364d343eeaSMatthew G Knepley splitname = buf; 11374d343eeaSMatthew G Knepley } 11384d343eeaSMatthew G Knepley } 11394d343eeaSMatthew G Knepley if (!splitname) { 11408caf3d72SBarry Smith ierr = PetscSNPrintf(buf,sizeof(buf),"%D",i);CHKERRQ(ierr); 11414d343eeaSMatthew G Knepley splitname = buf; 11424d343eeaSMatthew G Knepley } 114321c9b008SJed Brown ierr = PetscStrallocpy(splitname,&(*fieldNames)[i]);CHKERRQ(ierr); 11444d343eeaSMatthew G Knepley } 11454d343eeaSMatthew G Knepley ierr = PetscFree(dms);CHKERRQ(ierr); 11464d343eeaSMatthew G Knepley } 11474d343eeaSMatthew G Knepley PetscFunctionReturn(0); 11484d343eeaSMatthew G Knepley } 11494d343eeaSMatthew G Knepley 1150e7c4fc90SDmitry Karpeev /* 1151e7c4fc90SDmitry Karpeev This could take over from DMCreateFieldIS(), as it is more general, 11520298fd71SBarry Smith making DMCreateFieldIS() a special case -- calling with dmlist == NULL; 1153e7c4fc90SDmitry Karpeev At this point it's probably best to be less intrusive, however. 1154e7c4fc90SDmitry Karpeev */ 115516621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition_Composite(DM dm, PetscInt *len,char ***namelist, IS **islist, DM **dmlist) 1156e7c4fc90SDmitry Karpeev { 1157e7c4fc90SDmitry Karpeev PetscInt nDM; 1158e7c4fc90SDmitry Karpeev PetscInt i; 1159e7c4fc90SDmitry Karpeev PetscErrorCode ierr; 1160e7c4fc90SDmitry Karpeev 1161e7c4fc90SDmitry Karpeev PetscFunctionBegin; 1162e7c4fc90SDmitry Karpeev ierr = DMCreateFieldIS_Composite(dm, len, namelist, islist);CHKERRQ(ierr); 1163e7c4fc90SDmitry Karpeev if (dmlist) { 1164e7c4fc90SDmitry Karpeev ierr = DMCompositeGetNumberDM(dm, &nDM);CHKERRQ(ierr); 1165785e854fSJed Brown ierr = PetscMalloc1(nDM, dmlist);CHKERRQ(ierr); 1166e7c4fc90SDmitry Karpeev ierr = DMCompositeGetEntriesArray(dm, *dmlist);CHKERRQ(ierr); 1167e7c4fc90SDmitry Karpeev for (i=0; i<nDM; i++) { 1168e7c4fc90SDmitry Karpeev ierr = PetscObjectReference((PetscObject)((*dmlist)[i]));CHKERRQ(ierr); 1169e7c4fc90SDmitry Karpeev } 1170e7c4fc90SDmitry Karpeev } 1171e7c4fc90SDmitry Karpeev PetscFunctionReturn(0); 1172e7c4fc90SDmitry Karpeev } 1173e7c4fc90SDmitry Karpeev 117447c6ae99SBarry Smith /* -------------------------------------------------------------------------------------*/ 117547c6ae99SBarry Smith /*@C 11769ae5db72SJed Brown DMCompositeGetLocalVectors - Gets local vectors for each part of a DMComposite. 117747c6ae99SBarry Smith Use DMCompositeRestoreLocalVectors() to return them. 117847c6ae99SBarry Smith 117947c6ae99SBarry Smith Not Collective 118047c6ae99SBarry Smith 118147c6ae99SBarry Smith Input Parameter: 118247c6ae99SBarry Smith . dm - the packer object 118347c6ae99SBarry Smith 118447c6ae99SBarry Smith Output Parameter: 11859ae5db72SJed Brown . Vec ... - the individual sequential Vecs 118647c6ae99SBarry Smith 118747c6ae99SBarry Smith Level: advanced 118847c6ae99SBarry Smith 1189f5f57ec0SBarry Smith Not available from Fortran 1190f5f57ec0SBarry Smith 11919ae5db72SJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(), 11926eb61c8cSJed Brown DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(), 119347c6ae99SBarry Smith DMCompositeRestoreLocalVectors(), DMCompositeScatter(), DMCompositeGetEntries() 119447c6ae99SBarry Smith 119547c6ae99SBarry Smith @*/ 11967087cfbeSBarry Smith PetscErrorCode DMCompositeGetLocalVectors(DM dm,...) 119747c6ae99SBarry Smith { 119847c6ae99SBarry Smith va_list Argp; 119947c6ae99SBarry Smith PetscErrorCode ierr; 120047c6ae99SBarry Smith struct DMCompositeLink *next; 120147c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 120271b14b3eSStefano Zampini PetscBool flg; 120347c6ae99SBarry Smith 120447c6ae99SBarry Smith PetscFunctionBegin; 120547c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 120671b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 1207*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 120847c6ae99SBarry Smith next = com->next; 120947c6ae99SBarry Smith /* loop over packed objects, handling one at at time */ 121047c6ae99SBarry Smith va_start(Argp,dm); 121147c6ae99SBarry Smith while (next) { 121247c6ae99SBarry Smith Vec *vec; 121347c6ae99SBarry Smith vec = va_arg(Argp, Vec*); 121406930112SJed Brown if (vec) {ierr = DMGetLocalVector(next->dm,vec);CHKERRQ(ierr);} 121547c6ae99SBarry Smith next = next->next; 121647c6ae99SBarry Smith } 121747c6ae99SBarry Smith va_end(Argp); 121847c6ae99SBarry Smith PetscFunctionReturn(0); 121947c6ae99SBarry Smith } 122047c6ae99SBarry Smith 122147c6ae99SBarry Smith /*@C 12229ae5db72SJed Brown DMCompositeRestoreLocalVectors - Restores local vectors for each part of a DMComposite. 122347c6ae99SBarry Smith 122447c6ae99SBarry Smith Not Collective 122547c6ae99SBarry Smith 122647c6ae99SBarry Smith Input Parameter: 122747c6ae99SBarry Smith . dm - the packer object 122847c6ae99SBarry Smith 122947c6ae99SBarry Smith Output Parameter: 12309ae5db72SJed Brown . Vec ... - the individual sequential Vecs 123147c6ae99SBarry Smith 123247c6ae99SBarry Smith Level: advanced 123347c6ae99SBarry Smith 1234f5f57ec0SBarry Smith Not available from Fortran 1235f5f57ec0SBarry Smith 12369ae5db72SJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(), 12376eb61c8cSJed Brown DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(), 123847c6ae99SBarry Smith DMCompositeGetLocalVectors(), DMCompositeScatter(), DMCompositeGetEntries() 123947c6ae99SBarry Smith 124047c6ae99SBarry Smith @*/ 12417087cfbeSBarry Smith PetscErrorCode DMCompositeRestoreLocalVectors(DM dm,...) 124247c6ae99SBarry Smith { 124347c6ae99SBarry Smith va_list Argp; 124447c6ae99SBarry Smith PetscErrorCode ierr; 124547c6ae99SBarry Smith struct DMCompositeLink *next; 124647c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 124771b14b3eSStefano Zampini PetscBool flg; 124847c6ae99SBarry Smith 124947c6ae99SBarry Smith PetscFunctionBegin; 125047c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 125171b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 1252*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 125347c6ae99SBarry Smith next = com->next; 125447c6ae99SBarry Smith /* loop over packed objects, handling one at at time */ 125547c6ae99SBarry Smith va_start(Argp,dm); 125647c6ae99SBarry Smith while (next) { 125747c6ae99SBarry Smith Vec *vec; 125847c6ae99SBarry Smith vec = va_arg(Argp, Vec*); 125906930112SJed Brown if (vec) {ierr = DMRestoreLocalVector(next->dm,vec);CHKERRQ(ierr);} 126047c6ae99SBarry Smith next = next->next; 126147c6ae99SBarry Smith } 126247c6ae99SBarry Smith va_end(Argp); 126347c6ae99SBarry Smith PetscFunctionReturn(0); 126447c6ae99SBarry Smith } 126547c6ae99SBarry Smith 126647c6ae99SBarry Smith /* -------------------------------------------------------------------------------------*/ 126747c6ae99SBarry Smith /*@C 12689ae5db72SJed Brown DMCompositeGetEntries - Gets the DM for each entry in a DMComposite. 126947c6ae99SBarry Smith 127047c6ae99SBarry Smith Not Collective 127147c6ae99SBarry Smith 127247c6ae99SBarry Smith Input Parameter: 127347c6ae99SBarry Smith . dm - the packer object 127447c6ae99SBarry Smith 127547c6ae99SBarry Smith Output Parameter: 12769ae5db72SJed Brown . DM ... - the individual entries (DMs) 127747c6ae99SBarry Smith 127847c6ae99SBarry Smith Level: advanced 127947c6ae99SBarry Smith 128095452b02SPatrick Sanan Fortran Notes: 128195452b02SPatrick Sanan Available as DMCompositeGetEntries() for one output DM, DMCompositeGetEntries2() for 2, etc 1282f5f57ec0SBarry Smith 12832fa5ba8aSJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(), DMCompositeGetEntriesArray() 12846eb61c8cSJed Brown DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(), 128547c6ae99SBarry Smith DMCompositeRestoreLocalVectors(), DMCompositeGetLocalVectors(), DMCompositeScatter(), 128647c6ae99SBarry Smith DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors() 128747c6ae99SBarry Smith 128847c6ae99SBarry Smith @*/ 12897087cfbeSBarry Smith PetscErrorCode DMCompositeGetEntries(DM dm,...) 129047c6ae99SBarry Smith { 129147c6ae99SBarry Smith va_list Argp; 129247c6ae99SBarry Smith struct DMCompositeLink *next; 129347c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 129471b14b3eSStefano Zampini PetscBool flg; 129571b14b3eSStefano Zampini PetscErrorCode ierr; 129647c6ae99SBarry Smith 129747c6ae99SBarry Smith PetscFunctionBegin; 129847c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 129971b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 1300*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 130147c6ae99SBarry Smith next = com->next; 130247c6ae99SBarry Smith /* loop over packed objects, handling one at at time */ 130347c6ae99SBarry Smith va_start(Argp,dm); 130447c6ae99SBarry Smith while (next) { 130547c6ae99SBarry Smith DM *dmn; 130647c6ae99SBarry Smith dmn = va_arg(Argp, DM*); 13079ae5db72SJed Brown if (dmn) *dmn = next->dm; 130847c6ae99SBarry Smith next = next->next; 130947c6ae99SBarry Smith } 131047c6ae99SBarry Smith va_end(Argp); 131147c6ae99SBarry Smith PetscFunctionReturn(0); 131247c6ae99SBarry Smith } 131347c6ae99SBarry Smith 1314dbab29e1SMark F. Adams /*@C 13152fa5ba8aSJed Brown DMCompositeGetEntriesArray - Gets the DM for each entry in a DMComposite. 13162fa5ba8aSJed Brown 13172fa5ba8aSJed Brown Not Collective 13182fa5ba8aSJed Brown 13192fa5ba8aSJed Brown Input Parameter: 1320907376e6SBarry Smith . dm - the packer object 1321907376e6SBarry Smith 1322907376e6SBarry Smith Output Parameter: 1323907376e6SBarry Smith . dms - array of sufficient length (see DMCompositeGetNumberDM()) to hold the individual DMs 13242fa5ba8aSJed Brown 13252fa5ba8aSJed Brown Level: advanced 13262fa5ba8aSJed Brown 13272fa5ba8aSJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(), DMCompositeGetEntries() 13282fa5ba8aSJed Brown DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(), 13292fa5ba8aSJed Brown DMCompositeRestoreLocalVectors(), DMCompositeGetLocalVectors(), DMCompositeScatter(), 13302fa5ba8aSJed Brown DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors() 13312fa5ba8aSJed Brown 13322fa5ba8aSJed Brown @*/ 13332fa5ba8aSJed Brown PetscErrorCode DMCompositeGetEntriesArray(DM dm,DM dms[]) 13342fa5ba8aSJed Brown { 13352fa5ba8aSJed Brown struct DMCompositeLink *next; 13362fa5ba8aSJed Brown DM_Composite *com = (DM_Composite*)dm->data; 13372fa5ba8aSJed Brown PetscInt i; 133871b14b3eSStefano Zampini PetscBool flg; 133971b14b3eSStefano Zampini PetscErrorCode ierr; 13402fa5ba8aSJed Brown 13412fa5ba8aSJed Brown PetscFunctionBegin; 13422fa5ba8aSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 134371b14b3eSStefano Zampini ierr = PetscObjectTypeCompare((PetscObject)dm,DMCOMPOSITE,&flg);CHKERRQ(ierr); 1344*7a8be351SBarry Smith PetscCheck(flg,PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Not for type %s",((PetscObject)dm)->type_name); 13452fa5ba8aSJed Brown /* loop over packed objects, handling one at at time */ 13462fa5ba8aSJed Brown for (next=com->next,i=0; next; next=next->next,i++) dms[i] = next->dm; 13472fa5ba8aSJed Brown PetscFunctionReturn(0); 13482fa5ba8aSJed Brown } 13492fa5ba8aSJed Brown 1350e10fd815SStefano Zampini typedef struct { 1351e10fd815SStefano Zampini DM dm; 1352e10fd815SStefano Zampini PetscViewer *subv; 1353e10fd815SStefano Zampini Vec *vecs; 1354e10fd815SStefano Zampini } GLVisViewerCtx; 1355e10fd815SStefano Zampini 1356e10fd815SStefano Zampini static PetscErrorCode DestroyGLVisViewerCtx_Private(void *vctx) 1357e10fd815SStefano Zampini { 1358e10fd815SStefano Zampini GLVisViewerCtx *ctx = (GLVisViewerCtx*)vctx; 1359e10fd815SStefano Zampini PetscInt i,n; 1360e10fd815SStefano Zampini PetscErrorCode ierr; 1361e10fd815SStefano Zampini 1362e10fd815SStefano Zampini PetscFunctionBegin; 1363e10fd815SStefano Zampini ierr = DMCompositeGetNumberDM(ctx->dm,&n);CHKERRQ(ierr); 1364e10fd815SStefano Zampini for (i = 0; i < n; i++) { 1365e10fd815SStefano Zampini ierr = PetscViewerDestroy(&ctx->subv[i]);CHKERRQ(ierr); 1366e10fd815SStefano Zampini } 1367e10fd815SStefano Zampini ierr = PetscFree2(ctx->subv,ctx->vecs);CHKERRQ(ierr); 1368e10fd815SStefano Zampini ierr = DMDestroy(&ctx->dm);CHKERRQ(ierr); 1369e10fd815SStefano Zampini ierr = PetscFree(ctx);CHKERRQ(ierr); 1370e10fd815SStefano Zampini PetscFunctionReturn(0); 1371e10fd815SStefano Zampini } 1372e10fd815SStefano Zampini 1373e10fd815SStefano Zampini static PetscErrorCode DMCompositeSampleGLVisFields_Private(PetscObject oX, PetscInt nf, PetscObject oXfield[], void *vctx) 1374e10fd815SStefano Zampini { 1375e10fd815SStefano Zampini Vec X = (Vec)oX; 1376e10fd815SStefano Zampini GLVisViewerCtx *ctx = (GLVisViewerCtx*)vctx; 1377e10fd815SStefano Zampini PetscInt i,n,cumf; 1378e10fd815SStefano Zampini PetscErrorCode ierr; 1379e10fd815SStefano Zampini 1380e10fd815SStefano Zampini PetscFunctionBegin; 1381e10fd815SStefano Zampini ierr = DMCompositeGetNumberDM(ctx->dm,&n);CHKERRQ(ierr); 1382e10fd815SStefano Zampini ierr = DMCompositeGetAccessArray(ctx->dm,X,n,NULL,ctx->vecs);CHKERRQ(ierr); 1383e10fd815SStefano Zampini for (i = 0, cumf = 0; i < n; i++) { 1384e10fd815SStefano Zampini PetscErrorCode (*g2l)(PetscObject,PetscInt,PetscObject[],void*); 1385e10fd815SStefano Zampini void *fctx; 1386e10fd815SStefano Zampini PetscInt nfi; 1387e10fd815SStefano Zampini 1388e10fd815SStefano Zampini ierr = PetscViewerGLVisGetFields_Private(ctx->subv[i],&nfi,NULL,NULL,&g2l,NULL,&fctx);CHKERRQ(ierr); 1389e10fd815SStefano Zampini if (!nfi) continue; 1390e10fd815SStefano Zampini if (g2l) { 1391e10fd815SStefano Zampini ierr = (*g2l)((PetscObject)ctx->vecs[i],nfi,oXfield+cumf,fctx);CHKERRQ(ierr); 1392e10fd815SStefano Zampini } else { 1393e10fd815SStefano Zampini ierr = VecCopy(ctx->vecs[i],(Vec)(oXfield[cumf]));CHKERRQ(ierr); 1394e10fd815SStefano Zampini } 1395e10fd815SStefano Zampini cumf += nfi; 1396e10fd815SStefano Zampini } 1397e10fd815SStefano Zampini ierr = DMCompositeRestoreAccessArray(ctx->dm,X,n,NULL,ctx->vecs);CHKERRQ(ierr); 1398e10fd815SStefano Zampini PetscFunctionReturn(0); 1399e10fd815SStefano Zampini } 1400e10fd815SStefano Zampini 1401e10fd815SStefano Zampini static PetscErrorCode DMSetUpGLVisViewer_Composite(PetscObject odm, PetscViewer viewer) 1402e10fd815SStefano Zampini { 1403e10fd815SStefano Zampini DM dm = (DM)odm, *dms; 1404e10fd815SStefano Zampini Vec *Ufds; 1405e10fd815SStefano Zampini GLVisViewerCtx *ctx; 1406e10fd815SStefano Zampini PetscInt i,n,tnf,*sdim; 1407e10fd815SStefano Zampini char **fecs; 1408e10fd815SStefano Zampini PetscErrorCode ierr; 1409e10fd815SStefano Zampini 1410e10fd815SStefano Zampini PetscFunctionBegin; 1411e10fd815SStefano Zampini ierr = PetscNew(&ctx);CHKERRQ(ierr); 1412e10fd815SStefano Zampini ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 1413e10fd815SStefano Zampini ctx->dm = dm; 1414e10fd815SStefano Zampini ierr = DMCompositeGetNumberDM(dm,&n);CHKERRQ(ierr); 1415e10fd815SStefano Zampini ierr = PetscMalloc1(n,&dms);CHKERRQ(ierr); 1416e10fd815SStefano Zampini ierr = DMCompositeGetEntriesArray(dm,dms);CHKERRQ(ierr); 1417e10fd815SStefano Zampini ierr = PetscMalloc2(n,&ctx->subv,n,&ctx->vecs);CHKERRQ(ierr); 1418e10fd815SStefano Zampini for (i = 0, tnf = 0; i < n; i++) { 1419e10fd815SStefano Zampini PetscInt nf; 1420e10fd815SStefano Zampini 1421e10fd815SStefano Zampini ierr = PetscViewerCreate(PetscObjectComm(odm),&ctx->subv[i]);CHKERRQ(ierr); 1422e10fd815SStefano Zampini ierr = PetscViewerSetType(ctx->subv[i],PETSCVIEWERGLVIS);CHKERRQ(ierr); 1423e10fd815SStefano Zampini ierr = PetscViewerGLVisSetDM_Private(ctx->subv[i],(PetscObject)dms[i]);CHKERRQ(ierr); 1424e10fd815SStefano Zampini ierr = PetscViewerGLVisGetFields_Private(ctx->subv[i],&nf,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr); 1425e10fd815SStefano Zampini tnf += nf; 1426e10fd815SStefano Zampini } 1427e10fd815SStefano Zampini ierr = PetscFree(dms);CHKERRQ(ierr); 1428e10fd815SStefano Zampini ierr = PetscMalloc3(tnf,&fecs,tnf,&sdim,tnf,&Ufds);CHKERRQ(ierr); 1429e10fd815SStefano Zampini for (i = 0, tnf = 0; i < n; i++) { 1430e10fd815SStefano Zampini PetscInt *sd,nf,f; 1431e10fd815SStefano Zampini const char **fec; 1432e10fd815SStefano Zampini Vec *Uf; 1433e10fd815SStefano Zampini 1434e10fd815SStefano Zampini ierr = PetscViewerGLVisGetFields_Private(ctx->subv[i],&nf,&fec,&sd,NULL,(PetscObject**)&Uf,NULL);CHKERRQ(ierr); 1435e10fd815SStefano Zampini for (f = 0; f < nf; f++) { 1436e10fd815SStefano Zampini ierr = PetscStrallocpy(fec[f],&fecs[tnf+f]);CHKERRQ(ierr); 1437e10fd815SStefano Zampini Ufds[tnf+f] = Uf[f]; 1438e10fd815SStefano Zampini sdim[tnf+f] = sd[f]; 1439e10fd815SStefano Zampini } 1440e10fd815SStefano Zampini tnf += nf; 1441e10fd815SStefano Zampini } 1442e10fd815SStefano Zampini ierr = PetscViewerGLVisSetFields(viewer,tnf,(const char**)fecs,sdim,DMCompositeSampleGLVisFields_Private,(PetscObject*)Ufds,ctx,DestroyGLVisViewerCtx_Private);CHKERRQ(ierr); 1443e10fd815SStefano Zampini for (i = 0; i < tnf; i++) { 1444e10fd815SStefano Zampini ierr = PetscFree(fecs[i]);CHKERRQ(ierr); 1445e10fd815SStefano Zampini } 1446e10fd815SStefano Zampini ierr = PetscFree3(fecs,sdim,Ufds);CHKERRQ(ierr); 1447e10fd815SStefano Zampini PetscFunctionReturn(0); 1448e10fd815SStefano Zampini } 1449e10fd815SStefano Zampini 14507087cfbeSBarry Smith PetscErrorCode DMRefine_Composite(DM dmi,MPI_Comm comm,DM *fine) 145147c6ae99SBarry Smith { 145247c6ae99SBarry Smith PetscErrorCode ierr; 145347c6ae99SBarry Smith struct DMCompositeLink *next; 145447c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dmi->data; 145547c6ae99SBarry Smith DM dm; 145647c6ae99SBarry Smith 145747c6ae99SBarry Smith PetscFunctionBegin; 145847c6ae99SBarry Smith PetscValidHeaderSpecific(dmi,DM_CLASSID,1); 1459ce94432eSBarry Smith if (comm == MPI_COMM_NULL) { 1460ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)dmi,&comm);CHKERRQ(ierr); 1461ce94432eSBarry Smith } 14622ce3a92bSJed Brown ierr = DMSetUp(dmi);CHKERRQ(ierr); 146347c6ae99SBarry Smith next = com->next; 146447c6ae99SBarry Smith ierr = DMCompositeCreate(comm,fine);CHKERRQ(ierr); 146547c6ae99SBarry Smith 146647c6ae99SBarry Smith /* loop over packed objects, handling one at at time */ 146747c6ae99SBarry Smith while (next) { 146847c6ae99SBarry Smith ierr = DMRefine(next->dm,comm,&dm);CHKERRQ(ierr); 146947c6ae99SBarry Smith ierr = DMCompositeAddDM(*fine,dm);CHKERRQ(ierr); 147047c6ae99SBarry Smith ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr); 147147c6ae99SBarry Smith next = next->next; 147247c6ae99SBarry Smith } 147347c6ae99SBarry Smith PetscFunctionReturn(0); 147447c6ae99SBarry Smith } 147547c6ae99SBarry Smith 147614354c39SJed Brown PetscErrorCode DMCoarsen_Composite(DM dmi,MPI_Comm comm,DM *fine) 147714354c39SJed Brown { 147814354c39SJed Brown PetscErrorCode ierr; 147914354c39SJed Brown struct DMCompositeLink *next; 148014354c39SJed Brown DM_Composite *com = (DM_Composite*)dmi->data; 148114354c39SJed Brown DM dm; 148214354c39SJed Brown 148314354c39SJed Brown PetscFunctionBegin; 148414354c39SJed Brown PetscValidHeaderSpecific(dmi,DM_CLASSID,1); 14852ce3a92bSJed Brown ierr = DMSetUp(dmi);CHKERRQ(ierr); 14862ee06e3bSJed Brown if (comm == MPI_COMM_NULL) { 148725296bd5SBarry Smith ierr = PetscObjectGetComm((PetscObject)dmi,&comm);CHKERRQ(ierr); 148825296bd5SBarry Smith } 148914354c39SJed Brown next = com->next; 149014354c39SJed Brown ierr = DMCompositeCreate(comm,fine);CHKERRQ(ierr); 149114354c39SJed Brown 149214354c39SJed Brown /* loop over packed objects, handling one at at time */ 149314354c39SJed Brown while (next) { 149414354c39SJed Brown ierr = DMCoarsen(next->dm,comm,&dm);CHKERRQ(ierr); 149514354c39SJed Brown ierr = DMCompositeAddDM(*fine,dm);CHKERRQ(ierr); 149614354c39SJed Brown ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr); 149714354c39SJed Brown next = next->next; 149814354c39SJed Brown } 149914354c39SJed Brown PetscFunctionReturn(0); 150014354c39SJed Brown } 150147c6ae99SBarry Smith 1502e727c939SJed Brown PetscErrorCode DMCreateInterpolation_Composite(DM coarse,DM fine,Mat *A,Vec *v) 150347c6ae99SBarry Smith { 150447c6ae99SBarry Smith PetscErrorCode ierr; 15059ae5db72SJed Brown PetscInt m,n,M,N,nDM,i; 150647c6ae99SBarry Smith struct DMCompositeLink *nextc; 150747c6ae99SBarry Smith struct DMCompositeLink *nextf; 150825296bd5SBarry Smith Vec gcoarse,gfine,*vecs; 150947c6ae99SBarry Smith DM_Composite *comcoarse = (DM_Composite*)coarse->data; 151047c6ae99SBarry Smith DM_Composite *comfine = (DM_Composite*)fine->data; 15119ae5db72SJed Brown Mat *mats; 151247c6ae99SBarry Smith 151347c6ae99SBarry Smith PetscFunctionBegin; 151447c6ae99SBarry Smith PetscValidHeaderSpecific(coarse,DM_CLASSID,1); 151547c6ae99SBarry Smith PetscValidHeaderSpecific(fine,DM_CLASSID,2); 1516f692024eSJed Brown ierr = DMSetUp(coarse);CHKERRQ(ierr); 1517f692024eSJed Brown ierr = DMSetUp(fine);CHKERRQ(ierr); 151847c6ae99SBarry Smith /* use global vectors only for determining matrix layout */ 15199ae5db72SJed Brown ierr = DMGetGlobalVector(coarse,&gcoarse);CHKERRQ(ierr); 15209ae5db72SJed Brown ierr = DMGetGlobalVector(fine,&gfine);CHKERRQ(ierr); 152147c6ae99SBarry Smith ierr = VecGetLocalSize(gcoarse,&n);CHKERRQ(ierr); 152247c6ae99SBarry Smith ierr = VecGetLocalSize(gfine,&m);CHKERRQ(ierr); 152347c6ae99SBarry Smith ierr = VecGetSize(gcoarse,&N);CHKERRQ(ierr); 152447c6ae99SBarry Smith ierr = VecGetSize(gfine,&M);CHKERRQ(ierr); 15259ae5db72SJed Brown ierr = DMRestoreGlobalVector(coarse,&gcoarse);CHKERRQ(ierr); 15269ae5db72SJed Brown ierr = DMRestoreGlobalVector(fine,&gfine);CHKERRQ(ierr); 152747c6ae99SBarry Smith 15289ae5db72SJed Brown nDM = comfine->nDM; 15292c71b3e2SJacob Faibussowitsch PetscCheckFalse(nDM != comcoarse->nDM,PetscObjectComm((PetscObject)fine),PETSC_ERR_ARG_INCOMP,"Fine DMComposite has %D entries, but coarse has %D",nDM,comcoarse->nDM); 15301795a4d1SJed Brown ierr = PetscCalloc1(nDM*nDM,&mats);CHKERRQ(ierr); 153125296bd5SBarry Smith if (v) { 15321795a4d1SJed Brown ierr = PetscCalloc1(nDM,&vecs);CHKERRQ(ierr); 153325296bd5SBarry Smith } 153447c6ae99SBarry Smith 153547c6ae99SBarry Smith /* loop over packed objects, handling one at at time */ 15369ae5db72SJed Brown for (nextc=comcoarse->next,nextf=comfine->next,i=0; nextc; nextc=nextc->next,nextf=nextf->next,i++) { 153725296bd5SBarry Smith if (!v) { 15380298fd71SBarry Smith ierr = DMCreateInterpolation(nextc->dm,nextf->dm,&mats[i*nDM+i],NULL);CHKERRQ(ierr); 153925296bd5SBarry Smith } else { 154025296bd5SBarry Smith ierr = DMCreateInterpolation(nextc->dm,nextf->dm,&mats[i*nDM+i],&vecs[i]);CHKERRQ(ierr); 154125296bd5SBarry Smith } 154247c6ae99SBarry Smith } 1543ce94432eSBarry Smith ierr = MatCreateNest(PetscObjectComm((PetscObject)fine),nDM,NULL,nDM,NULL,mats,A);CHKERRQ(ierr); 154425296bd5SBarry Smith if (v) { 1545ce94432eSBarry Smith ierr = VecCreateNest(PetscObjectComm((PetscObject)fine),nDM,NULL,vecs,v);CHKERRQ(ierr); 154625296bd5SBarry Smith } 15479ae5db72SJed Brown for (i=0; i<nDM*nDM; i++) {ierr = MatDestroy(&mats[i]);CHKERRQ(ierr);} 15489ae5db72SJed Brown ierr = PetscFree(mats);CHKERRQ(ierr); 154925296bd5SBarry Smith if (v) { 155025296bd5SBarry Smith for (i=0; i<nDM; i++) {ierr = VecDestroy(&vecs[i]);CHKERRQ(ierr);} 155125296bd5SBarry Smith ierr = PetscFree(vecs);CHKERRQ(ierr); 155225296bd5SBarry Smith } 155347c6ae99SBarry Smith PetscFunctionReturn(0); 155447c6ae99SBarry Smith } 155547c6ae99SBarry Smith 1556184d77edSJed Brown static PetscErrorCode DMGetLocalToGlobalMapping_Composite(DM dm) 15571411c6eeSJed Brown { 15581411c6eeSJed Brown DM_Composite *com = (DM_Composite*)dm->data; 15591411c6eeSJed Brown ISLocalToGlobalMapping *ltogs; 1560f7efa3c7SJed Brown PetscInt i; 15611411c6eeSJed Brown PetscErrorCode ierr; 15621411c6eeSJed Brown 15631411c6eeSJed Brown PetscFunctionBegin; 15641411c6eeSJed Brown /* Set the ISLocalToGlobalMapping on the new matrix */ 15651411c6eeSJed Brown ierr = DMCompositeGetISLocalToGlobalMappings(dm,<ogs);CHKERRQ(ierr); 1566ce94432eSBarry Smith ierr = ISLocalToGlobalMappingConcatenate(PetscObjectComm((PetscObject)dm),com->nDM,ltogs,&dm->ltogmap);CHKERRQ(ierr); 15679ae5db72SJed Brown for (i=0; i<com->nDM; i++) {ierr = ISLocalToGlobalMappingDestroy(<ogs[i]);CHKERRQ(ierr);} 15681411c6eeSJed Brown ierr = PetscFree(ltogs);CHKERRQ(ierr); 15691411c6eeSJed Brown PetscFunctionReturn(0); 15701411c6eeSJed Brown } 15711411c6eeSJed Brown 1572b412c318SBarry Smith PetscErrorCode DMCreateColoring_Composite(DM dm,ISColoringType ctype,ISColoring *coloring) 157347c6ae99SBarry Smith { 157447c6ae99SBarry Smith PetscErrorCode ierr; 157547c6ae99SBarry Smith PetscInt n,i,cnt; 157647c6ae99SBarry Smith ISColoringValue *colors; 157747c6ae99SBarry Smith PetscBool dense = PETSC_FALSE; 157847c6ae99SBarry Smith ISColoringValue maxcol = 0; 157947c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 158047c6ae99SBarry Smith 158147c6ae99SBarry Smith PetscFunctionBegin; 158247c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 15832c71b3e2SJacob Faibussowitsch PetscCheckFalse(ctype == IS_COLORING_LOCAL,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Only global coloring supported"); 1584e3247f34SBarry Smith else if (ctype == IS_COLORING_GLOBAL) { 158547c6ae99SBarry Smith n = com->n; 1586ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"Unknown ISColoringType"); 1587785e854fSJed Brown ierr = PetscMalloc1(n,&colors);CHKERRQ(ierr); /* freed in ISColoringDestroy() */ 158847c6ae99SBarry Smith 1589c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)dm)->options,((PetscObject)dm)->prefix,"-dmcomposite_dense_jacobian",&dense,NULL);CHKERRQ(ierr); 159047c6ae99SBarry Smith if (dense) { 159147c6ae99SBarry Smith for (i=0; i<n; i++) { 159247c6ae99SBarry Smith colors[i] = (ISColoringValue)(com->rstart + i); 159347c6ae99SBarry Smith } 159447c6ae99SBarry Smith maxcol = com->N; 159547c6ae99SBarry Smith } else { 159647c6ae99SBarry Smith struct DMCompositeLink *next = com->next; 159747c6ae99SBarry Smith PetscMPIInt rank; 159847c6ae99SBarry Smith 1599ffc4695bSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm),&rank);CHKERRMPI(ierr); 160047c6ae99SBarry Smith cnt = 0; 160147c6ae99SBarry Smith while (next) { 160247c6ae99SBarry Smith ISColoring lcoloring; 160347c6ae99SBarry Smith 1604b412c318SBarry Smith ierr = DMCreateColoring(next->dm,IS_COLORING_GLOBAL,&lcoloring);CHKERRQ(ierr); 160547c6ae99SBarry Smith for (i=0; i<lcoloring->N; i++) { 160647c6ae99SBarry Smith colors[cnt++] = maxcol + lcoloring->colors[i]; 160747c6ae99SBarry Smith } 160847c6ae99SBarry Smith maxcol += lcoloring->n; 1609fcfd50ebSBarry Smith ierr = ISColoringDestroy(&lcoloring);CHKERRQ(ierr); 161047c6ae99SBarry Smith next = next->next; 161147c6ae99SBarry Smith } 161247c6ae99SBarry Smith } 1613aaf3ff59SMatthew G. Knepley ierr = ISColoringCreate(PetscObjectComm((PetscObject)dm),maxcol,n,colors,PETSC_OWN_POINTER,coloring);CHKERRQ(ierr); 161447c6ae99SBarry Smith PetscFunctionReturn(0); 161547c6ae99SBarry Smith } 161647c6ae99SBarry Smith 16177087cfbeSBarry Smith PetscErrorCode DMGlobalToLocalBegin_Composite(DM dm,Vec gvec,InsertMode mode,Vec lvec) 161847c6ae99SBarry Smith { 161947c6ae99SBarry Smith PetscErrorCode ierr; 162047c6ae99SBarry Smith struct DMCompositeLink *next; 162147c6ae99SBarry Smith PetscScalar *garray,*larray; 162247c6ae99SBarry Smith DM_Composite *com = (DM_Composite*)dm->data; 162347c6ae99SBarry Smith 162447c6ae99SBarry Smith PetscFunctionBegin; 162547c6ae99SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 162647c6ae99SBarry Smith PetscValidHeaderSpecific(gvec,VEC_CLASSID,2); 162739d35262SVincent Le Chenadec 162847c6ae99SBarry Smith if (!com->setup) { 1629d7bf68aeSBarry Smith ierr = DMSetUp(dm);CHKERRQ(ierr); 163047c6ae99SBarry Smith } 163139d35262SVincent Le Chenadec 163247c6ae99SBarry Smith ierr = VecGetArray(gvec,&garray);CHKERRQ(ierr); 163347c6ae99SBarry Smith ierr = VecGetArray(lvec,&larray);CHKERRQ(ierr); 163447c6ae99SBarry Smith 163547c6ae99SBarry Smith /* loop over packed objects, handling one at at time */ 163639d35262SVincent Le Chenadec next = com->next; 163747c6ae99SBarry Smith while (next) { 163847c6ae99SBarry Smith Vec local,global; 163947c6ae99SBarry Smith PetscInt N; 164047c6ae99SBarry Smith 164147c6ae99SBarry Smith ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr); 164247c6ae99SBarry Smith ierr = VecGetLocalSize(global,&N);CHKERRQ(ierr); 164347c6ae99SBarry Smith ierr = VecPlaceArray(global,garray);CHKERRQ(ierr); 164447c6ae99SBarry Smith ierr = DMGetLocalVector(next->dm,&local);CHKERRQ(ierr); 164547c6ae99SBarry Smith ierr = VecPlaceArray(local,larray);CHKERRQ(ierr); 164647c6ae99SBarry Smith ierr = DMGlobalToLocalBegin(next->dm,global,mode,local);CHKERRQ(ierr); 164747c6ae99SBarry Smith ierr = DMGlobalToLocalEnd(next->dm,global,mode,local);CHKERRQ(ierr); 164847c6ae99SBarry Smith ierr = VecResetArray(global);CHKERRQ(ierr); 164947c6ae99SBarry Smith ierr = VecResetArray(local);CHKERRQ(ierr); 165047c6ae99SBarry Smith ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr); 165139d35262SVincent Le Chenadec ierr = DMRestoreLocalVector(next->dm,&local);CHKERRQ(ierr); 165239d35262SVincent Le Chenadec 165306ebdd98SJed Brown larray += next->nlocal; 165439d35262SVincent Le Chenadec garray += next->n; 165547c6ae99SBarry Smith next = next->next; 165647c6ae99SBarry Smith } 165747c6ae99SBarry Smith 16580298fd71SBarry Smith ierr = VecRestoreArray(gvec,NULL);CHKERRQ(ierr); 16590298fd71SBarry Smith ierr = VecRestoreArray(lvec,NULL);CHKERRQ(ierr); 166047c6ae99SBarry Smith PetscFunctionReturn(0); 166147c6ae99SBarry Smith } 166247c6ae99SBarry Smith 16637087cfbeSBarry Smith PetscErrorCode DMGlobalToLocalEnd_Composite(DM dm,Vec gvec,InsertMode mode,Vec lvec) 16640c010503SBarry Smith { 16650c010503SBarry Smith PetscFunctionBegin; 166639d35262SVincent Le Chenadec PetscValidHeaderSpecific(dm,DM_CLASSID,1); 166739d35262SVincent Le Chenadec PetscValidHeaderSpecific(gvec,VEC_CLASSID,2); 166839d35262SVincent Le Chenadec PetscValidHeaderSpecific(lvec,VEC_CLASSID,4); 166939d35262SVincent Le Chenadec PetscFunctionReturn(0); 167039d35262SVincent Le Chenadec } 167139d35262SVincent Le Chenadec 167239d35262SVincent Le Chenadec PetscErrorCode DMLocalToGlobalBegin_Composite(DM dm,Vec lvec,InsertMode mode,Vec gvec) 167339d35262SVincent Le Chenadec { 167439d35262SVincent Le Chenadec PetscErrorCode ierr; 167539d35262SVincent Le Chenadec struct DMCompositeLink *next; 167639d35262SVincent Le Chenadec PetscScalar *larray,*garray; 167739d35262SVincent Le Chenadec DM_Composite *com = (DM_Composite*)dm->data; 167839d35262SVincent Le Chenadec 167939d35262SVincent Le Chenadec PetscFunctionBegin; 168039d35262SVincent Le Chenadec PetscValidHeaderSpecific(dm,DM_CLASSID,1); 168139d35262SVincent Le Chenadec PetscValidHeaderSpecific(lvec,VEC_CLASSID,2); 168239d35262SVincent Le Chenadec PetscValidHeaderSpecific(gvec,VEC_CLASSID,4); 168339d35262SVincent Le Chenadec 168439d35262SVincent Le Chenadec if (!com->setup) { 168539d35262SVincent Le Chenadec ierr = DMSetUp(dm);CHKERRQ(ierr); 168639d35262SVincent Le Chenadec } 168739d35262SVincent Le Chenadec 168839d35262SVincent Le Chenadec ierr = VecGetArray(lvec,&larray);CHKERRQ(ierr); 168939d35262SVincent Le Chenadec ierr = VecGetArray(gvec,&garray);CHKERRQ(ierr); 169039d35262SVincent Le Chenadec 169139d35262SVincent Le Chenadec /* loop over packed objects, handling one at at time */ 169239d35262SVincent Le Chenadec next = com->next; 169339d35262SVincent Le Chenadec while (next) { 169439d35262SVincent Le Chenadec Vec global,local; 169539d35262SVincent Le Chenadec 169639d35262SVincent Le Chenadec ierr = DMGetLocalVector(next->dm,&local);CHKERRQ(ierr); 169739d35262SVincent Le Chenadec ierr = VecPlaceArray(local,larray);CHKERRQ(ierr); 169839d35262SVincent Le Chenadec ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr); 169939d35262SVincent Le Chenadec ierr = VecPlaceArray(global,garray);CHKERRQ(ierr); 170039d35262SVincent Le Chenadec ierr = DMLocalToGlobalBegin(next->dm,local,mode,global);CHKERRQ(ierr); 170139d35262SVincent Le Chenadec ierr = DMLocalToGlobalEnd(next->dm,local,mode,global);CHKERRQ(ierr); 170239d35262SVincent Le Chenadec ierr = VecResetArray(local);CHKERRQ(ierr); 170339d35262SVincent Le Chenadec ierr = VecResetArray(global);CHKERRQ(ierr); 170439d35262SVincent Le Chenadec ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr); 170539d35262SVincent Le Chenadec ierr = DMRestoreLocalVector(next->dm,&local);CHKERRQ(ierr); 170639d35262SVincent Le Chenadec 170739d35262SVincent Le Chenadec garray += next->n; 170839d35262SVincent Le Chenadec larray += next->nlocal; 170939d35262SVincent Le Chenadec next = next->next; 171039d35262SVincent Le Chenadec } 171139d35262SVincent Le Chenadec 171239d35262SVincent Le Chenadec ierr = VecRestoreArray(gvec,NULL);CHKERRQ(ierr); 171339d35262SVincent Le Chenadec ierr = VecRestoreArray(lvec,NULL);CHKERRQ(ierr); 171439d35262SVincent Le Chenadec 171539d35262SVincent Le Chenadec PetscFunctionReturn(0); 171639d35262SVincent Le Chenadec } 171739d35262SVincent Le Chenadec 171839d35262SVincent Le Chenadec PetscErrorCode DMLocalToGlobalEnd_Composite(DM dm,Vec lvec,InsertMode mode,Vec gvec) 171939d35262SVincent Le Chenadec { 172039d35262SVincent Le Chenadec PetscFunctionBegin; 172139d35262SVincent Le Chenadec PetscValidHeaderSpecific(dm,DM_CLASSID,1); 172239d35262SVincent Le Chenadec PetscValidHeaderSpecific(lvec,VEC_CLASSID,2); 172339d35262SVincent Le Chenadec PetscValidHeaderSpecific(gvec,VEC_CLASSID,4); 172439d35262SVincent Le Chenadec PetscFunctionReturn(0); 172539d35262SVincent Le Chenadec } 172639d35262SVincent Le Chenadec 172739d35262SVincent Le Chenadec PetscErrorCode DMLocalToLocalBegin_Composite(DM dm,Vec vec1,InsertMode mode,Vec vec2) 172839d35262SVincent Le Chenadec { 172939d35262SVincent Le Chenadec PetscErrorCode ierr; 173039d35262SVincent Le Chenadec struct DMCompositeLink *next; 173139d35262SVincent Le Chenadec PetscScalar *array1,*array2; 173239d35262SVincent Le Chenadec DM_Composite *com = (DM_Composite*)dm->data; 173339d35262SVincent Le Chenadec 173439d35262SVincent Le Chenadec PetscFunctionBegin; 173539d35262SVincent Le Chenadec PetscValidHeaderSpecific(dm,DM_CLASSID,1); 173639d35262SVincent Le Chenadec PetscValidHeaderSpecific(vec1,VEC_CLASSID,2); 173739d35262SVincent Le Chenadec PetscValidHeaderSpecific(vec2,VEC_CLASSID,4); 173839d35262SVincent Le Chenadec 173939d35262SVincent Le Chenadec if (!com->setup) { 174039d35262SVincent Le Chenadec ierr = DMSetUp(dm);CHKERRQ(ierr); 174139d35262SVincent Le Chenadec } 174239d35262SVincent Le Chenadec 174339d35262SVincent Le Chenadec ierr = VecGetArray(vec1,&array1);CHKERRQ(ierr); 174439d35262SVincent Le Chenadec ierr = VecGetArray(vec2,&array2);CHKERRQ(ierr); 174539d35262SVincent Le Chenadec 174639d35262SVincent Le Chenadec /* loop over packed objects, handling one at at time */ 174739d35262SVincent Le Chenadec next = com->next; 174839d35262SVincent Le Chenadec while (next) { 174939d35262SVincent Le Chenadec Vec local1,local2; 175039d35262SVincent Le Chenadec 175139d35262SVincent Le Chenadec ierr = DMGetLocalVector(next->dm,&local1);CHKERRQ(ierr); 175239d35262SVincent Le Chenadec ierr = VecPlaceArray(local1,array1);CHKERRQ(ierr); 175339d35262SVincent Le Chenadec ierr = DMGetLocalVector(next->dm,&local2);CHKERRQ(ierr); 175439d35262SVincent Le Chenadec ierr = VecPlaceArray(local2,array2);CHKERRQ(ierr); 175539d35262SVincent Le Chenadec ierr = DMLocalToLocalBegin(next->dm,local1,mode,local2);CHKERRQ(ierr); 175639d35262SVincent Le Chenadec ierr = DMLocalToLocalEnd(next->dm,local1,mode,local2);CHKERRQ(ierr); 175739d35262SVincent Le Chenadec ierr = VecResetArray(local2);CHKERRQ(ierr); 175839d35262SVincent Le Chenadec ierr = DMRestoreLocalVector(next->dm,&local2);CHKERRQ(ierr); 175939d35262SVincent Le Chenadec ierr = VecResetArray(local1);CHKERRQ(ierr); 176039d35262SVincent Le Chenadec ierr = DMRestoreLocalVector(next->dm,&local1);CHKERRQ(ierr); 176139d35262SVincent Le Chenadec 176239d35262SVincent Le Chenadec array1 += next->nlocal; 176339d35262SVincent Le Chenadec array2 += next->nlocal; 176439d35262SVincent Le Chenadec next = next->next; 176539d35262SVincent Le Chenadec } 176639d35262SVincent Le Chenadec 176739d35262SVincent Le Chenadec ierr = VecRestoreArray(vec1,NULL);CHKERRQ(ierr); 176839d35262SVincent Le Chenadec ierr = VecRestoreArray(vec2,NULL);CHKERRQ(ierr); 176939d35262SVincent Le Chenadec 177039d35262SVincent Le Chenadec PetscFunctionReturn(0); 177139d35262SVincent Le Chenadec } 177239d35262SVincent Le Chenadec 177339d35262SVincent Le Chenadec PetscErrorCode DMLocalToLocalEnd_Composite(DM dm,Vec lvec,InsertMode mode,Vec gvec) 177439d35262SVincent Le Chenadec { 177539d35262SVincent Le Chenadec PetscFunctionBegin; 177639d35262SVincent Le Chenadec PetscValidHeaderSpecific(dm,DM_CLASSID,1); 177739d35262SVincent Le Chenadec PetscValidHeaderSpecific(lvec,VEC_CLASSID,2); 177839d35262SVincent Le Chenadec PetscValidHeaderSpecific(gvec,VEC_CLASSID,4); 17790c010503SBarry Smith PetscFunctionReturn(0); 17800c010503SBarry Smith } 178147c6ae99SBarry Smith 17826ae3a549SBarry Smith /*MC 17836ae3a549SBarry Smith DMCOMPOSITE = "composite" - A DM object that is used to manage data for a collection of DMs 17846ae3a549SBarry Smith 17856ae3a549SBarry Smith Level: intermediate 17866ae3a549SBarry Smith 17871abcec8cSBarry Smith .seealso: DMType, DM, DMDACreate(), DMCreate(), DMSetType(), DMCompositeCreate() 17886ae3a549SBarry Smith M*/ 17896ae3a549SBarry Smith 17908cc058d9SJed Brown PETSC_EXTERN PetscErrorCode DMCreate_Composite(DM p) 1791a4121054SBarry Smith { 1792a4121054SBarry Smith PetscErrorCode ierr; 1793a4121054SBarry Smith DM_Composite *com; 1794a4121054SBarry Smith 1795a4121054SBarry Smith PetscFunctionBegin; 1796b00a9115SJed Brown ierr = PetscNewLog(p,&com);CHKERRQ(ierr); 1797a4121054SBarry Smith p->data = com; 1798a4121054SBarry Smith com->n = 0; 17997ac2b803SAlex Fikl com->nghost = 0; 18000298fd71SBarry Smith com->next = NULL; 1801a4121054SBarry Smith com->nDM = 0; 1802a4121054SBarry Smith 1803a4121054SBarry Smith p->ops->createglobalvector = DMCreateGlobalVector_Composite; 1804a4121054SBarry Smith p->ops->createlocalvector = DMCreateLocalVector_Composite; 1805184d77edSJed Brown p->ops->getlocaltoglobalmapping = DMGetLocalToGlobalMapping_Composite; 18064d343eeaSMatthew G Knepley p->ops->createfieldis = DMCreateFieldIS_Composite; 180716621825SDmitry Karpeev p->ops->createfielddecomposition = DMCreateFieldDecomposition_Composite; 1808a4121054SBarry Smith p->ops->refine = DMRefine_Composite; 180914354c39SJed Brown p->ops->coarsen = DMCoarsen_Composite; 181025296bd5SBarry Smith p->ops->createinterpolation = DMCreateInterpolation_Composite; 181125296bd5SBarry Smith p->ops->creatematrix = DMCreateMatrix_Composite; 1812e727c939SJed Brown p->ops->getcoloring = DMCreateColoring_Composite; 1813a4121054SBarry Smith p->ops->globaltolocalbegin = DMGlobalToLocalBegin_Composite; 1814a4121054SBarry Smith p->ops->globaltolocalend = DMGlobalToLocalEnd_Composite; 181539d35262SVincent Le Chenadec p->ops->localtoglobalbegin = DMLocalToGlobalBegin_Composite; 181639d35262SVincent Le Chenadec p->ops->localtoglobalend = DMLocalToGlobalEnd_Composite; 181739d35262SVincent Le Chenadec p->ops->localtolocalbegin = DMLocalToLocalBegin_Composite; 181839d35262SVincent Le Chenadec p->ops->localtolocalend = DMLocalToLocalEnd_Composite; 1819a4121054SBarry Smith p->ops->destroy = DMDestroy_Composite; 1820a4121054SBarry Smith p->ops->view = DMView_Composite; 1821a4121054SBarry Smith p->ops->setup = DMSetUp_Composite; 1822e10fd815SStefano Zampini 1823e10fd815SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)p,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Composite);CHKERRQ(ierr); 1824a4121054SBarry Smith PetscFunctionReturn(0); 1825a4121054SBarry Smith } 1826a4121054SBarry Smith 18271fd49c25SBarry Smith /*@ 18280c010503SBarry Smith DMCompositeCreate - Creates a vector packer, used to generate "composite" 18290c010503SBarry Smith vectors made up of several subvectors. 18300c010503SBarry Smith 1831d083f849SBarry Smith Collective 183247c6ae99SBarry Smith 183347c6ae99SBarry Smith Input Parameter: 18340c010503SBarry Smith . comm - the processors that will share the global vector 18350c010503SBarry Smith 18360c010503SBarry Smith Output Parameters: 18370c010503SBarry Smith . packer - the packer object 183847c6ae99SBarry Smith 183947c6ae99SBarry Smith Level: advanced 184047c6ae99SBarry Smith 18411abcec8cSBarry Smith .seealso DMDestroy(), DMCompositeAddDM(), DMCompositeScatter(), DMCOMPOSITE,DMCreate() 18426eb61c8cSJed Brown DMCompositeGather(), DMCreateGlobalVector(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess() 184347c6ae99SBarry Smith DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries() 184447c6ae99SBarry Smith 184547c6ae99SBarry Smith @*/ 18467087cfbeSBarry Smith PetscErrorCode DMCompositeCreate(MPI_Comm comm,DM *packer) 184747c6ae99SBarry Smith { 18480c010503SBarry Smith PetscErrorCode ierr; 18490c010503SBarry Smith 185047c6ae99SBarry Smith PetscFunctionBegin; 18510c010503SBarry Smith PetscValidPointer(packer,2); 1852a4121054SBarry Smith ierr = DMCreate(comm,packer);CHKERRQ(ierr); 1853a4121054SBarry Smith ierr = DMSetType(*packer,DMCOMPOSITE);CHKERRQ(ierr); 185447c6ae99SBarry Smith PetscFunctionReturn(0); 185547c6ae99SBarry Smith } 1856