Actual source code: ex16.c
2: static char help[] = "Tests DMComposite routines.\n\n";
4: #include <petscdmredundant.h>
5: #include <petscdm.h>
6: #include <petscdmda.h>
7: #include <petscdmcomposite.h>
8: #include <petscpf.h>
10: int main(int argc,char **argv)
11: {
12: PetscInt nredundant1 = 5,nredundant2 = 2,i;
13: ISLocalToGlobalMapping *ltog;
14: PetscMPIInt rank,size;
15: DM packer;
16: Vec global,local1,local2,redundant1,redundant2;
17: PF pf;
18: DM da1,da2,dmred1,dmred2;
19: PetscScalar *redundant1a,*redundant2a;
20: PetscViewer sviewer;
21: PetscBool gather_add = PETSC_FALSE;
23: PetscInitialize(&argc,&argv,(char*)0,help);
24: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
25: MPI_Comm_size(PETSC_COMM_WORLD,&size);
27: PetscOptionsGetBool(NULL,NULL,"-gather_add",&gather_add,NULL);
29: DMCompositeCreate(PETSC_COMM_WORLD,&packer);
31: DMRedundantCreate(PETSC_COMM_WORLD,0,nredundant1,&dmred1);
32: DMCreateLocalVector(dmred1,&redundant1);
33: DMCompositeAddDM(packer,dmred1);
35: DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,8,1,1,NULL,&da1);
36: DMSetFromOptions(da1);
37: DMSetUp(da1);
38: DMCreateLocalVector(da1,&local1);
39: DMCompositeAddDM(packer,da1);
41: DMRedundantCreate(PETSC_COMM_WORLD,1%size,nredundant2,&dmred2);
42: DMCreateLocalVector(dmred2,&redundant2);
43: DMCompositeAddDM(packer,dmred2);
45: DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,6,1,1,NULL,&da2);
46: DMSetFromOptions(da2);
47: DMSetUp(da2);
48: DMCreateLocalVector(da2,&local2);
49: DMCompositeAddDM(packer,da2);
51: DMCreateGlobalVector(packer,&global);
52: PFCreate(PETSC_COMM_WORLD,1,1,&pf);
53: PFSetType(pf,PFIDENTITY,NULL);
54: PFApplyVec(pf,NULL,global);
55: PFDestroy(&pf);
56: VecView(global,PETSC_VIEWER_STDOUT_WORLD);
58: DMCompositeScatter(packer,global,redundant1,local1,redundant2,local2);
59: PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD);
60: PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] My part of redundant1 vector\n",rank);
61: PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
62: VecView(redundant1,sviewer);
63: PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
64: PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
65: PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] My part of da1 vector\n",rank);
66: PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
67: VecView(local1,sviewer);
68: PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
69: PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
70: PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] My part of redundant2 vector\n",rank);
71: PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
72: VecView(redundant2,sviewer);
73: PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
74: PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
75: PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] My part of da2 vector\n",rank);
76: PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
77: VecView(local2,sviewer);
78: PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
79: PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
80: PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD);
82: VecGetArray(redundant1,&redundant1a);
83: VecGetArray(redundant2,&redundant2a);
84: for (i=0; i<nredundant1; i++) redundant1a[i] = (rank+2)*i;
85: for (i=0; i<nredundant2; i++) redundant2a[i] = (rank+10)*i;
86: VecRestoreArray(redundant1,&redundant1a);
87: VecRestoreArray(redundant2,&redundant2a);
89: DMCompositeGather(packer,gather_add ? ADD_VALUES : INSERT_VALUES,global,redundant1,local1,redundant2,local2);
90: VecView(global,PETSC_VIEWER_STDOUT_WORLD);
92: /* get the global numbering for each subvector element */
93: DMCompositeGetISLocalToGlobalMappings(packer,<og);
95: PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"Local to global mapping of redundant1 vector\n");
96: ISLocalToGlobalMappingView(ltog[0],PETSC_VIEWER_STDOUT_WORLD);
97: PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"Local to global mapping of local1 vector\n");
98: ISLocalToGlobalMappingView(ltog[1],PETSC_VIEWER_STDOUT_WORLD);
99: PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"Local to global mapping of redundant2 vector\n");
100: ISLocalToGlobalMappingView(ltog[2],PETSC_VIEWER_STDOUT_WORLD);
101: PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"Local to global mapping of local2 vector\n");
102: ISLocalToGlobalMappingView(ltog[3],PETSC_VIEWER_STDOUT_WORLD);
104: for (i=0; i<4; i++) ISLocalToGlobalMappingDestroy(<og[i]);
105: PetscFree(ltog);
107: DMDestroy(&da1);
108: DMDestroy(&dmred1);
109: DMDestroy(&dmred2);
110: DMDestroy(&da2);
111: VecDestroy(&redundant1);
112: VecDestroy(&redundant2);
113: VecDestroy(&local1);
114: VecDestroy(&local2);
115: VecDestroy(&global);
116: DMDestroy(&packer);
117: PetscFinalize();
118: return 0;
119: }
121: /*TEST
123: build:
124: requires: !complex
126: test:
127: nsize: 3
129: test:
130: suffix: 2
131: nsize: 3
132: args: -gather_add
134: TEST*/