static char help[]= "This example shows 1) how to transfer vectors from a parent communicator to vectors on a child communicator and vice versa;\n\ 2) how to transfer vectors from a subcommunicator to vectors on another subcommunicator. The two subcommunicators are not\n\ required to cover all processes in PETSC_COMM_WORLD; 3) how to copy a vector from a parent communicator to vectors on its child communicators.\n\ To run any example with VECCUDA vectors, add -vectype cuda to the argument list\n\n"; #include int main(int argc,char **argv) { PetscErrorCode ierr; PetscMPIInt nproc,grank,mycolor; PetscInt i,n,N = 20,low,high; MPI_Comm subcomm; Vec x = PETSC_NULL; /* global vectors on PETSC_COMM_WORLD */ Vec yg = PETSC_NULL; /* global vectors on PETSC_COMM_WORLD */ VecScatter vscat; IS ix,iy; PetscBool iscuda = PETSC_FALSE; /* Option to use VECCUDA vectors */ PetscBool optionflag, compareflag; char vectypename[PETSC_MAX_PATH_LEN]; PetscBool world2sub = PETSC_FALSE; /* Copy a vector from WORLD to a subcomm? */ PetscBool sub2sub = PETSC_FALSE; /* Copy a vector from a subcomm to another subcomm? */ PetscBool world2subs = PETSC_FALSE; /* Copy a vector from WORLD to multiple subcomms? */ ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; ierr = MPI_Comm_size(PETSC_COMM_WORLD,&nproc);CHKERRMPI(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&grank);CHKERRMPI(ierr); if (nproc < 2) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_SIZ,"This test must have at least two processes to run"); ierr = PetscOptionsGetBool(NULL,0,"-world2sub",&world2sub,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(NULL,0,"-sub2sub",&sub2sub,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(NULL,0,"-world2subs",&world2subs,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetString(NULL,NULL,"-vectype",vectypename,sizeof(vectypename),&optionflag);CHKERRQ(ierr); if (optionflag) { ierr = PetscStrncmp(vectypename, "cuda", (size_t)4, &compareflag);CHKERRQ(ierr); if (compareflag) iscuda = PETSC_TRUE; } /* Split PETSC_COMM_WORLD into three subcomms. Each process can only see the subcomm it belongs to */ mycolor = grank % 3; ierr = MPI_Comm_split(PETSC_COMM_WORLD,mycolor,grank,&subcomm);CHKERRMPI(ierr); /*=========================================================================== * Transfer a vector x defined on PETSC_COMM_WORLD to a vector y defined on * a subcommunicator of PETSC_COMM_WORLD and vice versa. *===========================================================================*/ if (world2sub) { ierr = VecCreate(PETSC_COMM_WORLD, &x);CHKERRQ(ierr); ierr = VecSetSizes(x, PETSC_DECIDE, N);CHKERRQ(ierr); if (iscuda) { ierr = VecSetType(x, VECCUDA);CHKERRQ(ierr); } else { ierr = VecSetType(x, VECSTANDARD);CHKERRQ(ierr); } ierr = VecSetUp(x);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject)x,"x_commworld");CHKERRQ(ierr); /* Give a name to view x clearly */ /* Initialize x to [-0.0, -1.0, -2.0, ..., -19.0] */ ierr = VecGetOwnershipRange(x,&low,&high);CHKERRQ(ierr); for (i=low; i