Guest User

PETSc Doubt

a guest
Feb 7th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. static char help[] = "Introduction to the Vec environment.\n\n";
  2.  
  3.  
  4. #include "petsc.h"
  5.  
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     PetscInt     i,rank,n;      /*Two variables for storing the rank and the total number of processes.*/
  10.     PetscInt     N;              /*This stores the length of each vector.*/
  11.     Vec          x;              /*This would be the main working vector of this program.*/
  12.     PetscScalar  one=1.0;
  13.  
  14.     PetscInitialize(&argc,&argv,(char*)0,help);
  15.     MPI_Comm_rank (PETSC_COMM_WORLD, &rank);/*Stores the values of rank of each process in the variable rank.*/
  16.     MPI_Comm_size (PETSC_COMM_WORLD, &n);   /*Similarly stores the total number of processes.*/
  17.     VecCreate(PETSC_COMM_SELF,&x);
  18.     VecSetSizes(x,rank+1,PETSC_DECIDE);     /*Now setting the sizes.*/
  19.     VecSetFromOptions(x);
  20.     VecGetSize(x,&N);
  21.     VecSet(x,one);
  22.     PetscInt ix[N];
  23.     PetscScalar  y[N],Sum=0.0;          /*This would be the storage term for the Sum and Current Element.*/
  24.  
  25.     for(i=0;i<N;i++)
  26.         ix[i]=i;
  27.     VecAssemblyBegin(x);
  28.     VecAssemblyEnd(x);
  29.  
  30.     VecGetValues(x,N,ix,y);
  31.  
  32.     for(i=0;i<N;i++)
  33.     {
  34.         Sum+=y[i];
  35.  
  36.         PetscPrintf(PETSC_COMM_SELF,"Process:%d Y[%d]=%d\n",rank,i,y[i]);
  37.     }
  38.  
  39.     VecDestroy(&x);
  40.     PetscPrintf(PETSC_COMM_SELF,"The sum of the vector elements from process %d is %d\n",rank,Sum);
  41.  
  42.  
  43.     PetscFinalize();
  44.  
  45.     return 0;
  46. }
Add Comment
Please, Sign In to add comment