#include int main(int argc,char **argv) { PetscErrorCode ierr; Vec x,xseq,perm_vecseq,perm_vecmpi; const PetscReal *xdata,*perm_vecmpi_data; PetscInt i,n,N,*iperm; PetscReal *rperm; PetscRandom rnd; VecScatter vscat; ierr = PetscInitialize(&argc,&argv,NULL,NULL);if (ierr) return ierr; /* Build a random vector of length 10 */ ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr); ierr = VecSetSizes(x,PETSC_DECIDE,10);CHKERRQ(ierr); ierr = VecSetFromOptions(x);CHKERRQ(ierr); ierr = PetscRandomCreate(PETSC_COMM_WORLD,&rnd);CHKERRQ(ierr); ierr = VecSetRandom(x,rnd);CHKERRQ(ierr); ierr = PetscRandomDestroy(&rnd);CHKERRQ(ierr); /* perm_vecmpi has the same layout as x. It will store permutation of x (in real) */ ierr = VecDuplicate(x,&perm_vecmpi);CHKERRQ(ierr); /* Gather x to xseq on rank 0. xseq on other ranks are actually empty */ ierr = VecScatterCreateToZero(x,&vscat,&xseq);CHKERRQ(ierr); ierr = VecScatterBegin(vscat,x,xseq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterEnd(vscat,x,xseq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); /* Do PetscSortRealWithPermutation on xseq */ /* If want descending order, you need ierr = VecScale(xseq,-1.0);CHKERRQ(ierr); */ ierr = VecGetLocalSize(xseq,&N);CHKERRQ(ierr); /* N=0 on ranks other than rank 0 */ ierr = PetscMalloc2(N,&iperm,N,&rperm);CHKERRQ(ierr); for (i=0; i