[petsc-users] assemble the matrix on proc 0

Barry Smith bsmith at mcs.anl.gov
Thu Dec 18 07:17:40 CST 2014


   Likely when you set the sizes for the matrix you did not set the local size properly on each process. You need to set the local size to be the complete matrix size on process 0 and 0 on all the other processes.

  Barry

> On Dec 18, 2014, at 4:42 AM, Hoang Giang Bui <hgbk2008 at gmail.com> wrote:
> 
> Hello
> 
> I want to assemble petsc matrix from csr matrix on proc 0. I did that like:
> 
> ierr = MatSetType(A, MATMPIAIJ);
> ierr = MatMPIAIJSetPreallocation(A, PETSC_DEFAULT, PETSC_NULL, PETSC_DEFAULT, PETSC_NULL);
> 
> on proc 0, I assemble the matrix
> 
>        for(Ii = Istart; Ii < Iend; ++Ii)
>        {
>            int nz = ia[Ii + 1] - ia[Ii];
>            ierr = MatSetValues(A, 1, &Ii, nz, &ja[ia[Ii]], &v[ia[Ii]], INSERT_VALUES);
>        }
> 
> the other proc also called this code, but input matrix only exists in proc 0.
> 
> The matrix print out correctly:
> MatView(A, PETSC_VIEWER_STDOUT_WORLD);
> row 0: (0, 1)  (3, 6)
> row 1: (1, 10.5)
> row 2: (2, 0.015)
> row 3: (1, 250.5)  (3, -280)  (4, 33.32)
> row 4: (4, 12)
> 
> However, when solved by ksp, it created an error:
> [3]PETSC ERROR: Invalid argument
> [3]PETSC ERROR: Must be square matrix, rows 0 columns 1
> 
> What should be wrong in this case?
> 
> Regards,
> Bui
> 



More information about the petsc-users mailing list