xref: /petsc/src/vec/is/is/tutorials/ex5.c (revision 19a20e4ccaa848451342107d37410bc58500e44e)
1 
2 static char help[] = "Demonstrates using ISLocalToGlobalMappings with block size.\n\n";
3 
4 /*T
5     Concepts: local to global mappings
6     Concepts: global to local mappings
7 
8     Description:  Creates an index set based on blocks of integers. Views that index set
9     and then destroys it.
10 T*/
11 
12 #include <petscis.h>
13 #include <petscviewer.h>
14 
15 int main(int argc,char **argv)
16 {
17   PetscErrorCode         ierr;
18   PetscInt               i,n = 4,indices[] = {0,3,9,12},m = 2,input[] = {0,2};
19   PetscInt               output[2],inglobals[13],outlocals[13];
20   ISLocalToGlobalMapping mapping;
21 
22   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
23 
24   /*
25       Create a local to global mapping. Each processor independently
26      creates a mapping
27   */
28   ierr = PetscIntView(n,indices,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
29   ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_WORLD,2,n,indices,PETSC_COPY_VALUES,&mapping);CHKERRQ(ierr);
30 
31   /*
32      Map a set of local indices to their global values
33   */
34   ierr = PetscIntView(m,input,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
35   ierr = ISLocalToGlobalMappingApply(mapping,m,input,output);CHKERRQ(ierr);
36   ierr = PetscIntView(m,output,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
37 
38   /*
39      Map some global indices to local, retaining the ones without a local index by -1
40   */
41   for (i=0; i<13; i++) inglobals[i] = i;
42   ierr = PetscIntView(13,inglobals,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
43   ierr = ISGlobalToLocalMappingApply(mapping,IS_GTOLM_MASK,13,inglobals,NULL,outlocals);CHKERRQ(ierr);
44   ierr = PetscIntView(13,outlocals,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
45 
46   /*
47      Map some block global indices to local, dropping the ones without a local index.
48   */
49   ierr = PetscIntView(13,inglobals,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
50   ierr = ISGlobalToLocalMappingApplyBlock(mapping,IS_GTOLM_DROP,13,inglobals,&m,outlocals);CHKERRQ(ierr);
51   ierr = PetscIntView(m,outlocals,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
52 
53   /*
54      Free the space used by the local to global mapping
55   */
56   ierr = ISLocalToGlobalMappingDestroy(&mapping);CHKERRQ(ierr);
57 
58   ierr = PetscFinalize();
59   return ierr;
60 }
61 
62 /*TEST
63 
64    test:
65 
66 TEST*/
67