Guest User

PETSc Matrix Doubt

a guest
Feb 19th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1.     MatCreate(PETSC_COMM_WORLD,&A);
  2.     MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,nr,nc);
  3.     MatSetType(A,MATMPIAIJ);
  4.     MatSetFromOptions(A);
  5.  
  6.     MatSetUp(A);
  7.     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);/*Starting the Matrxi Assembly*/
  8.     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
  9.  
  10.     MatGetOwnershipRange(A,&low,&high);
  11.  
  12.     /**************************************************************************
  13.     *                     Setting the Values of the Matrix                    *
  14.     **************************************************************************/
  15.  
  16.  
  17.     PetscMalloc((high-low)*sizeof(PetscInt),&d_nnz);
  18.     PetscMalloc((high-low)*sizeof(PetscInt),&o_nnz);
  19.     for(i=0;i<nc;i++)
  20.     {
  21.         idxn[i]=i;
  22.     }
  23.     for(i=low;i<high;i++)
  24.     {
  25.         idxm[i-low]=i;
  26.     }
  27.  
  28.     for(i=low;i<high;i++)
  29.     {
  30.         d_nnz[i-low]=0;
  31.         o_nnz[i-low]=0;
  32.         for(j=0;j<nc;j++)
  33.         {
  34.             if((j-i<=3) && (j>=i))
  35.                 values[(i-low)*nc+j]=1;
  36.             else
  37.                 values[(i-low)*nc+j]=0;
  38.             if(values[(i-low)*nc+j]!=0)
  39.             {
  40.                 if((j>=low)&&(j<high))
  41.                     d_nnz[i-low]++;
  42.                 else
  43.                     o_nnz[i-low]++;
  44.             }
  45.         }
  46.     }
  47.  
  48.     PetscBarrier(PETSC_NULL);/*Just introducing a barrier so that all the processes are to the same level*/
  49.  
  50.     MatMPIAIJSetPreallocation(A,0,d_nnz,0,o_nnz);
  51.  
  52.     MatSetValues(A,(high-low),idxm,nc,idxn,values,INSERT_VALUES);/*Setting the value for the assigned idxm[] and idxn[]*/
  53.  
  54.     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);/*Starting the Matrix Assembly*/
  55.     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
  56.  
  57.     MatView(A,PETSC_VIEWER_STDOUT_WORLD);/*For checking the value assigned to the matrix A*/
Add Comment
Please, Sign In to add comment