[petsc-users] Use MatConvert

Barry Smith bsmith at mcs.anl.gov
Tue Mar 17 12:43:20 CDT 2015


> On Mar 17, 2015, at 11:40 AM, Chung-Kan Huang <ckhuangf at gmail.com> wrote:
> 
> Hi,
> 
> I wonder what I did wrong when using MATConvert...
> 
>   MatCreate(* comm_, &J_);
> 
>   PetscInt * d_nnz = NULL;
>   PetscInt * o_nnz = NULL;
>   PetscMalloc(local_size * sizeof(PetscInt), & d_nnz);
>   PetscMalloc(local_size * sizeof(PetscInt), & o_nnz);
>   for (int i = 0; i < local_size; i++) {
>     d_nnz[i] = d_nnz_v[i];
>     o_nnz[i] = o_nnz_v[i];
>   }
>   MatSetSizes(J_,
>           local_size,
>           local_size,
>           PETSC_DECIDE,
>           PETSC_DECIDE);
>   
>   MatSetBlockSize(J_, M_SIZE); // So MatSetValuesBlocked can be used
>   MatSetType(J_, MATAIJ);
>   
>   if (comm_->Get_size() > 1) {

You don't need to have this if here; just call both MatMPIAIJSetPreallocation and MatSeqAIJSetPreallocation() and PETSc will use the correct one and ignore the other one automatically.

>     // MPI
>     MatMPIAIJSetPreallocation(J_,
>                   max_d_nz,
>                   d_nnz,
>                   max_o_nz,
>                   o_nnz);
>   
>   } else {
>     // Seq
>     MatSeqAIJSetPreallocation(J_,
>                   PETSC_DEFAULT, 
>                   d_nnz);
>   }
>   
>   PetscFree(d_nnz);
>   PetscFree(o_nnz);
> 
>   
>   // Column oriented
>   MatSetOption(J_, MAT_ROW_ORIENTED, PETSC_FALSE);
>   // Test code to check if J_ has right structure
>   preAssembeJ(); 
> 
> <image.png>
>   MatAssemblyBegin(J_, MAT_FINAL_ASSEMBLY);
>   MatAssemblyEnd(J_, MAT_FINAL_ASSEMBLY);
> 
> 
>  MatConvert(J_, MATBAIJ, MAT_REUSE_MATRIX, & J_);

   I cannot understand what is wrong. Is it producing the wrong matrix? Please send the entire code so we can run it ourselves.

  Barry

>   
>   MatZeroEntries(J_);     
> // Test code to check if J_ has right structure  
>   preAssembeJ();
> <image.png>
> Thanks,
> Kan
> Cheers
> 



More information about the petsc-users mailing list