Commit 4efb4396 authored by Simon Brass's avatar Simon Brass
Browse files

emulate VEGAS' box coords

parent b3590e87
program main
use mpi_f08
implicit none
integer :: k, ierr, nprocs, myrank
! Box coord
integer :: n_boxes
logical :: success
real, dimension(5) :: box
! Results
real :: total, total_sq
real :: root_total, root_total_sq
integer :: imsg, result
! --------------------------------------------------
call MPI_init(ierr)
if (ierr /= MPI_SUCCESS) then
print *, "MPI_init error"
end if
call MPI_Comm_size(MPI_Comm_world, nprocs, ierr)
if (ierr /= MPI_SUCCESS) then
print *, "MPI_Comm_size error"
end if
call MPI_Comm_rank(MPI_Comm_world, myrank, ierr)
if (ierr /= MPI_SUCCESS) then
print *, "MPI_Comm_rank error"
end if
print *, "nprocs=", nprocs, " myrank=", myrank
! --------------------------------------------------
n_boxes = 3
box = 1
success = .true.
print *, "total number of boxes = ", n_boxes**5
do k = 1, myrank
call increment_box_coord (box(1:2), success)
end do
if (.not. success) then
print *, "Could not find a valid box coord as starting point."
end if
total = 0.
total_sq = 0.
root_total = 0.
root_total_sq = 0.
par: do while (success)
print *, "box (par) = ", box(1:2)
perp: do while (success)
print *, "box (perp) = ", box(3:5)
total = total + 1.5
total_sq = total_sq + 1.5**2
call increment_box_coord (box(3:5), success)
end do perp
do k = 1, nprocs
call increment_box_coord (box(1:2), success)
if (.not. success) exit
end do
end do par
call MPI_Reduce (total, root_total, 1, MPI_REAL, MPI_SUM, 0, MPI_COMM_WORLD)
call MPI_Reduce (total_sq, root_total_sq, 1, MPI_REAL, MPI_SUM, 0, MPI_COMM_WORLD)
if (myrank == 0) then
print *, "total = ", root_total, " (", n_boxes**5 * 1.5, " )"
print *, "total_sq = ", root_total_sq, " (", n_boxes**5 * 1.5**2, " )"
end if
! --------------------------------------------------
call MPI_finalize(ierr)
if (ierr /= MPI_SUCCESS) then
print *, "MPI_finalize error"
end if
contains
subroutine increment_box_coord (box, success)
real, dimension(:), intent(inout) :: box
logical, intent(out) :: success
integer :: j
success = .true.
do j = size(box), 1, -1
box(j) = box(j) + 1
if (box(j) <= n_boxes) return
box(j) = 1
end do
success = .false.
end subroutine increment_box_coord
end program main
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment