Guest User

Main Code

a guest
Feb 19th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. static char help[] = "Introduction to the Sparse Matrix environment.\n\n";
  2.  
  3.  
  4. #include "petsc.h"
  5. #include <stdlib.h>
  6.  
  7. #define     NR  5
  8. #define     NC  8
  9.  
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.     PetscInt    i,j,nr=NR,nc=NC,idxm[NR],idxn[NC],high,low;
  14.     PetscInt    rank,size;
  15.     Mat         A;/*This is going to be the main working matrix of the tutorial.*/
  16.     PetscScalar values[NR*NC];
  17.     PetscInt    *d_nnz,*o_nnz;
  18.  
  19.     PetscInitialize(&argc,&argv,(char*)0,help);
  20.  
  21.     MPI_Comm_size(PETSC_COMM_WORLD,&size);
  22.     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
  23.  
  24.     /**************************************************************************
  25.     *          Setting Up the Layout and the type of the  Matrix              *
  26.     **************************************************************************/
  27.  
  28.     MatCreate(PETSC_COMM_WORLD,&A);
  29.     MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,nr,nc);
  30.     MatSetType(A,MATMPIAIJ);
  31.     MatSetFromOptions(A);
  32.  
  33.     MatSetUp(A);
  34.     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);/*Starting the Matrxi Assembly*/
  35.     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
  36.  
  37.     MatGetOwnershipRange(A,&low,&high);
  38.  
  39.  
  40.  
  41.  
  42.     /**************************************************************************
  43.     *                     Setting the Values of the Matrix                    *
  44.     **************************************************************************/
  45.  
  46.  
  47.     PetscMalloc((high-low)*sizeof(PetscInt),&d_nnz);
  48.     PetscMalloc((high-low)*sizeof(PetscInt),&o_nnz);
  49.     for(i=0;i<nc;i++)
  50.     {
  51.         idxn[i]=i;
  52.     }
  53.     for(i=low;i<high;i++)
  54.     {
  55.         idxm[i-low]=i;
  56.     }
  57.  
  58.     printf("Low %d High %d\n",low,high);
  59.  
  60.     for(i=low;i<high;i++)
  61.     {
  62.         d_nnz[i-low]=0;
  63.         o_nnz[i-low]=0;
  64.         for(j=0;j<nc;j++)
  65.         {
  66.             if((j-i<=3) && (j>=i))
  67.                 values[(i-low)*nc+j]=1;
  68.             else
  69.                 values[(i-low)*nc+j]=0;
  70.             if(values[(i-low)*nc+j]!=0)
  71.             {
  72.                 if((j>=low)&&(j<high))
  73.                     d_nnz[i-low]++;
  74.                 else
  75.                     o_nnz[i-low]++;
  76.             }
  77.         }
  78.     }
  79.  
  80.     PetscBarrier(PETSC_NULL);/*Just introducing a barrier so that all the processes are to the same level*/
  81.  
  82.  
  83.     for(i=low;i<high;i++)
  84.     {
  85.         PetscSynchronizedPrintf(PETSC_COMM_SELF,"Proc: %d o_nnz[%d] %d d_nnz[%d] %d\n",rank,i-low,o_nnz[i-low],i-low,d_nnz[i-low]);
  86.     }
  87.  
  88.     MatMPIAIJSetPreallocation(A,0,d_nnz,0,o_nnz);
  89.  
  90.     MatSetValues(A,(high-low),idxm,nc,idxn,values,INSERT_VALUES);/*Setting the value for the assigned idxm[] and idxn[]*/
  91.  
  92.     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);/*Starting the Matrix Assembly*/
  93.     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
  94.  
  95.     MatView(A,PETSC_VIEWER_STDOUT_WORLD);/*For checking the value assigned to the matrix A*/
  96.     /**************************************************************************
  97.     *                     Getting the Values of the Matrix                    *
  98.     **************************************************************************/
  99.  
  100.     for(i=0;i<nc;i++)
  101.     {
  102.         idxn[i]=i;
  103.     }
  104.     for(i=low;i<high;i++)
  105.     {
  106.         idxm[i-low]=i;
  107.     }
  108.  
  109.     PetscScalar Result[(high-low)*nc];
  110.  
  111.     MatGetValues(A,(high-low),idxm,nc,idxn,Result);
  112.  
  113.     PetscBarrier(PETSC_NULL);/*Just introducing a barrier so that all the processes are to the same level*/
  114.     for(i=low;i<high;i++)
  115.     {
  116.         for(j=0;j<nc;j++)
  117.         {
  118.             PetscSynchronizedPrintf(PETSC_COMM_SELF,"%.2f\t",Result[(i-low)*nc+j]);
  119.         }
  120.         PetscSynchronizedPrintf(PETSC_COMM_SELF,"\n");
  121.     }
  122.  
  123.     //PetscPrintf(PETSC_COMM_SELF,"The local matrix size for the process %d is %d cross %d\n",rank+1,M,N);
  124.     //PetscPrintf(PETSC_COMM_SELF,"The local matrix size for the process %d is %d to %d\n",rank+1,low,high);
  125.  
  126.     MatDestroy(&A);
  127.  
  128.     PetscFinalize();
  129.  
  130.     return 0;
  131. }
Add Comment
Please, Sign In to add comment