simplify assignment of neighboring tasks
p-costa opened this issue · comments
The determination of neighboring tasks can be simplified by using the following functions I drafted:
function get_id(coords,dims,periods) result(id)
use mpi, only: MPI_PROC_NULL
implicit none
integer :: id
integer, intent(in), dimension(3) :: coords,dims
logical, intent(in), dimension(3), optional :: periods
integer, dimension(3) :: coords_aux,shift
coords_aux(:) = coords(:)
if(present(periods)) then
shift(:) = 0
where(periods(:))
where(coords_aux(:)>dims(:)-1) shift(:) = (0 -coords_aux(:))/dims(:)
where(coords_aux(:)<0 ) shift(:) = (dims(:)-1-coords_aux(:))/dims(:)
coords_aux(:) = coords_aux(:) + shift(:)*dims(:)
end where
endif
if(all(coords_aux(:)<=dims(:)-1).and.all(coords_aux(:)>=0)) then
id = coords_aux(1)+coords_aux(2)*dims(1)+coords_aux(3)*dims(2)
else
id = MPI_PROC_NULL
endif
end function get_id
function get_coords(id,dims) result(coords)
integer :: coords(3)
integer, intent(in) :: id, dims(3)
coords(:) = [mod(id,dims(1)),mod(id/dims(1),dims(2)),mod(id/(dims(1)*dims(2)),dims(3))]
end function get_coords