Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static char help[] = "Introduction to the Vec environment.\n\n";
- #include "petsc.h"
- int main(int argc, char *argv[])
- {
- PetscInt i,rank,n; /*Two variables for storing the rank and the total number of processes.*/
- PetscInt N; /*This stores the length of each vector.*/
- Vec x; /*This would be the main working vector of this program.*/
- PetscScalar one=1.0;
- PetscInitialize(&argc,&argv,(char*)0,help);
- MPI_Comm_rank (PETSC_COMM_WORLD, &rank);/*Stores the values of rank of each process in the variable rank.*/
- MPI_Comm_size (PETSC_COMM_WORLD, &n); /*Similarly stores the total number of processes.*/
- VecCreate(PETSC_COMM_SELF,&x);
- VecSetSizes(x,rank+1,PETSC_DECIDE); /*Now setting the sizes.*/
- VecSetFromOptions(x);
- VecGetSize(x,&N);
- VecSet(x,one);
- PetscInt ix[N];
- PetscScalar y[N],Sum=0.0; /*This would be the storage term for the Sum and Current Element.*/
- for(i=0;i<N;i++)
- ix[i]=i;
- VecAssemblyBegin(x);
- VecAssemblyEnd(x);
- VecGetValues(x,N,ix,y);
- for(i=0;i<N;i++)
- {
- Sum+=y[i];
- PetscPrintf(PETSC_COMM_SELF,"Process:%d Y[%d]=%d\n",rank,i,y[i]);
- }
- VecDestroy(&x);
- PetscPrintf(PETSC_COMM_SELF,"The sum of the vector elements from process %d is %d\n",rank,Sum);
- PetscFinalize();
- return 0;
- }
Add Comment
Please, Sign In to add comment