[petsc-users] assemble the matrix on proc 0

Barry Smith bsmith at mcs.anl.gov
Thu Dec 18 11:31:16 CST 2014


> ierr = MatSetSizes(A, my_size, PETSC_DECIDE, n, n);

should be

> ierr = MatSetSizes(A, my_size, my_size, n, n);


> On Dec 18, 2014, at 8:21 AM, Matthew Knepley <knepley at gmail.com> wrote:
> 
> On Thu, Dec 18, 2014 at 8:03 AM, Hoang Giang Bui <hgbk2008 at gmail.com> wrote:
> Yes, I did it, like:
> 
>         int my_size;
>         if(rank == 0)
>             my_size = n;
>         else
>             my_size = 0;
>         PetscErrorCode ierr;
>         Mat A;
>         ierr = MatCreate(PETSC_COMM_WORLD, &A);
>         ierr = MatSetSizes(A, my_size, PETSC_DECIDE, n, n);
> 
> View the matrix first
> 
>   ierr = MatView(A, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
> 
> There are a lot of possible mistakes to make. This can help sort it out.
> 
>   Thanks,
> 
>     Matt
>  
> Regards,
> Bui
> 
> 
> On 12/18/2014 02:17 PM, Barry Smith wrote:
>     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
> 
> 
> 
> 
> -- 
> What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.
> -- Norbert Wiener



More information about the petsc-users mailing list