#include <petsc/finclude/petscmat.h>
program main
  use petscmat
  implicit none

  Mat A, B
  PetscErrorCode ierr
  PetscScalar, pointer :: km(:, :)
  PetscInt, parameter :: three = 3, one = 1
  PetscInt idxm(1), idxmj(1), i, j
  PetscMPIInt rank, size

  PetscCallA(PetscInitialize(ierr))

  PetscCallMPIA(MPI_Comm_rank(PETSC_COMM_WORLD, rank, ierr))
  PetscCallMPIA(MPI_Comm_size(PETSC_COMM_WORLD, size, ierr))

  PetscCallA(MatCreate(PETSC_COMM_WORLD, A, ierr))
  PetscCallA(MatSetSizes(A, three, three, PETSC_DECIDE, PETSC_DECIDE, ierr))
  PetscCallA(MatSetBlockSize(A, three, ierr))
  PetscCallA(MatSetUp(A, ierr))
  PetscCallA(MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, B, ierr))
  idxm(1) = 0
  allocate (km(three, three))
  do i = 1, 3
    do j = 1, 3
      km(1, 1) = i + j
      idxm(1) = i - 1 + 3*rank
      idxmj(1) = j - 1 + 3*rank
      PetscCallA(MatSetValues(B, one, idxm, one, idxmj, reshape(km, [three**2]), ADD_VALUES, ierr))
    end do
  end do

  PetscCallA(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY, ierr))
  PetscCallA(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY, ierr))
  PetscCallA(MatView(B, PETSC_VIEWER_STDOUT_WORLD, ierr))

  PetscCallA(MatDestroy(A, ierr))
  PetscCallA(MatDestroy(B, ierr))

  deallocate (km)
  PetscCallA(PetscFinalize(ierr))
end

!/*TEST
!
!   test:
!     nsize: 2
!
!TEST*/
