Actual source code: ex16.c

  1: /*$Id: ex16.c,v 1.12 2001/08/07 03:04:42 balay Exp $*/

  3: static char help[] = "Tests VecPack routines.\n\n";

 5:  #include petscda.h
 6:  #include petscpf.h

 10: int main(int argc,char **argv)
 11: {
 12:   int         ierr,nredundant1 = 5,nredundant2 = 2,rank,i,*ridx1,*ridx2,*lidx1,*lidx2,nlocal;
 13:   PetscScalar *redundant1,*redundant2;
 14:   VecPack     packer;
 15:   Vec         global,local1,local2;
 16:   PF          pf;
 17:   DA          da1,da2;
 18:   PetscViewer sviewer;

 20:   PetscInitialize(&argc,&argv,(char*)0,help);
 21:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 23:   VecPackCreate(PETSC_COMM_WORLD,&packer);

 25:   PetscMalloc(nredundant1*sizeof(PetscScalar),&redundant1);
 26:   VecPackAddArray(packer,nredundant1);

 28:   DACreate1d(PETSC_COMM_WORLD,DA_NONPERIODIC,8,1,1,PETSC_NULL,&da1);
 29:   DACreateLocalVector(da1,&local1);
 30:   VecPackAddDA(packer,da1);

 32:   PetscMalloc(nredundant2*sizeof(PetscScalar),&redundant2);
 33:   VecPackAddArray(packer,nredundant2);

 35:   DACreate1d(PETSC_COMM_WORLD,DA_NONPERIODIC,6,1,1,PETSC_NULL,&da2);
 36:   DACreateLocalVector(da2,&local2);
 37:   VecPackAddDA(packer,da2);

 39:   VecPackCreateGlobalVector(packer,&global);
 40:   PFCreate(PETSC_COMM_WORLD,1,1,&pf);
 41:   PFSetType(pf,PFIDENTITY,PETSC_NULL);
 42:   PFApplyVec(pf,PETSC_NULL,global);
 43:   PFDestroy(pf);
 44:   VecView(global,PETSC_VIEWER_STDOUT_WORLD);

 46:   VecPackScatter(packer,global,redundant1,local1,redundant2,local2);
 47:   PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] My part of redundant1 array\n",rank);
 48:   PetscScalarView(nredundant1,redundant1,PETSC_VIEWER_STDOUT_WORLD);
 49:   PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] My part of da1 vector\n",rank);
 50:   PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
 51:   VecView(local1,sviewer);
 52:   PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
 53:   PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] My part of redundant2 array\n",rank);
 54:   PetscScalarView(nredundant2,redundant2,PETSC_VIEWER_STDOUT_WORLD);
 55:   PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] My part of da2 vector\n",rank);
 56:   PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
 57:   VecView(local2,sviewer);
 58:   PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);

 60:   for (i=0; i<nredundant1; i++) redundant1[i] = (rank+2)*i;
 61:   for (i=0; i<nredundant2; i++) redundant2[i] = (rank+10)*i;

 63:   VecPackGather(packer,global,redundant1,local1,redundant2,local2);
 64:   VecView(global,PETSC_VIEWER_STDOUT_WORLD);

 66:   /* get the global numbering for each subvector/array element */
 67:   VecPackGetGlobalIndices(packer,&ridx1,&lidx1,&ridx2,&lidx2);
 68: 
 69:   PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] Global numbering of redundant1 array\n",rank);
 70:   PetscIntView(nredundant1,ridx1,PETSC_VIEWER_STDOUT_WORLD);
 71:   PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] Global numbering of local1 vector\n",rank);
 72:   VecGetSize(local1,&nlocal);
 73:   PetscIntView(nlocal,lidx1,PETSC_VIEWER_STDOUT_WORLD);
 74:   PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] Global numbering of redundant2 array\n",rank);
 75:   PetscIntView(nredundant2,ridx2,PETSC_VIEWER_STDOUT_WORLD);
 76:   PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] Global numbering of local2 vector\n",rank);
 77:   VecGetSize(local2,&nlocal);
 78:   PetscIntView(nlocal,lidx2,PETSC_VIEWER_STDOUT_WORLD);
 79: 
 80:   PetscFree(ridx1);
 81:   PetscFree(lidx1);
 82:   PetscFree(ridx2);
 83:   PetscFree(lidx2);

 85:   DADestroy(da1);
 86:   DADestroy(da2);
 87:   VecDestroy(local1);
 88:   VecDestroy(local2);
 89:   VecDestroy(global);
 90:   VecPackDestroy(packer);
 91:   PetscFree(redundant1);
 92:   PetscFree(redundant2);
 93:   PetscFinalize();
 94:   return 0;
 95: }
 96: