Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- 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*/
- 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*/
Add Comment
Please, Sign In to add comment