Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static char help[] = "Introduction to the Sparse Matrix environment.\n\n";
- #include "petsc.h"
- #include <stdlib.h>
- #define NR 5
- #define NC 8
- int main(int argc, char *argv[])
- {
- PetscInt i,j,nr=NR,nc=NC,idxm[NR],idxn[NC],high,low;
- PetscInt rank,size;
- Mat A;/*This is going to be the main working matrix of the tutorial.*/
- PetscScalar values[NR*NC];
- PetscInt *d_nnz,*o_nnz;
- PetscInitialize(&argc,&argv,(char*)0,help);
- MPI_Comm_size(PETSC_COMM_WORLD,&size);
- MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
- /**************************************************************************
- * Setting Up the Layout and the type of the Matrix *
- **************************************************************************/
- MatCreate(PETSC_COMM_WORLD,&A);
- MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,nr,nc);
- MatSetType(A,MATMPIAIJ);
- MatSetFromOptions(A);
- MatSetUp(A);
- MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);/*Starting the Matrxi Assembly*/
- MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
- MatGetOwnershipRange(A,&low,&high);
- /**************************************************************************
- * Setting the Values of the Matrix *
- **************************************************************************/
- PetscMalloc((high-low)*sizeof(PetscInt),&d_nnz);
- PetscMalloc((high-low)*sizeof(PetscInt),&o_nnz);
- for(i=0;i<nc;i++)
- {
- idxn[i]=i;
- }
- for(i=low;i<high;i++)
- {
- idxm[i-low]=i;
- }
- printf("Low %d High %d\n",low,high);
- for(i=low;i<high;i++)
- {
- d_nnz[i-low]=0;
- o_nnz[i-low]=0;
- for(j=0;j<nc;j++)
- {
- if((j-i<=3) && (j>=i))
- values[(i-low)*nc+j]=1;
- else
- values[(i-low)*nc+j]=0;
- if(values[(i-low)*nc+j]!=0)
- {
- if((j>=low)&&(j<high))
- d_nnz[i-low]++;
- else
- o_nnz[i-low]++;
- }
- }
- }
- PetscBarrier(PETSC_NULL);/*Just introducing a barrier so that all the processes are to the same level*/
- for(i=low;i<high;i++)
- {
- 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]);
- }
- MatMPIAIJSetPreallocation(A,0,d_nnz,0,o_nnz);
- MatSetValues(A,(high-low),idxm,nc,idxn,values,INSERT_VALUES);/*Setting the value for the assigned idxm[] and idxn[]*/
- MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);/*Starting the Matrix Assembly*/
- MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
- MatView(A,PETSC_VIEWER_STDOUT_WORLD);/*For checking the value assigned to the matrix A*/
- /**************************************************************************
- * Getting the Values of the Matrix *
- **************************************************************************/
- for(i=0;i<nc;i++)
- {
- idxn[i]=i;
- }
- for(i=low;i<high;i++)
- {
- idxm[i-low]=i;
- }
- PetscScalar Result[(high-low)*nc];
- MatGetValues(A,(high-low),idxm,nc,idxn,Result);
- PetscBarrier(PETSC_NULL);/*Just introducing a barrier so that all the processes are to the same level*/
- for(i=low;i<high;i++)
- {
- for(j=0;j<nc;j++)
- {
- PetscSynchronizedPrintf(PETSC_COMM_SELF,"%.2f\t",Result[(i-low)*nc+j]);
- }
- PetscSynchronizedPrintf(PETSC_COMM_SELF,"\n");
- }
- //PetscPrintf(PETSC_COMM_SELF,"The local matrix size for the process %d is %d cross %d\n",rank+1,M,N);
- //PetscPrintf(PETSC_COMM_SELF,"The local matrix size for the process %d is %d to %d\n",rank+1,low,high);
- MatDestroy(&A);
- PetscFinalize();
- return 0;
- }
Add Comment
Please, Sign In to add comment