(bind-func main
(let ((limit 10))
(lambda ()
(MPI_Init null null)
(let ((world_size_ptr:i32* (salloc))
(world_size (begin
(MPI_Comm_size MPI_COMM_WORLD world_size_ptr)
(pref world_size_ptr 0)))
(world_rank_ptr:i32* (salloc))
(world_rank (begin
(MPI_Comm_rank MPI_COMM_WORLD world_rank_ptr)
(pref world_rank_ptr 0)))
(number:i32* (salloc)))
(if (< world_size 2)
(begin
(println "World size must be greater than 1")
(MPI_Abort MPI_COMM_WORLD 1)))
(let ((count_ptr:i32* (salloc))
(partner_rank (% (+ world_rank 1) 2)))
(pset! count_ptr 0 0)
(while (< (pref count_ptr 0) limit)
(if (= world_rank (% (pref count_ptr 0) 2))
(begin
(pset! count_ptr 0 (+ (pref count_ptr 0) 1))
(MPI_Send (convert count_ptr) 1 MPI_INT partner_rank 0 MPI_COMM_WORLD)
(printf "%d sent and incremented count %d to %d\n" world_rank (pref count_ptr 0) partner_rank))
(begin
(MPI_Recv (convert count_ptr) 1 MPI_INT partner_rank 0 MPI_COMM_WORLD MPI_STATUS_IGNORE)
(printf "%d received count %d from %d\n" world_rank (pref count_ptr 0) partner_rank)))))
(MPI_Finalize)))))