       program reglocal
c
#include "mpi.fort.h"
c
       integer i,ndata,indirection, BUFSIZE, global_size, BLOCK
       integer my_index(4)
       parameter(BUFSIZE = 4)
       integer iglobal_ref(4),ilocal_ref(4)
       real buffer(3), aloc(4)
       double precision x(5),y(5+BUFSIZE)
       integer itabptr, ischedptr, build_translation_table

       BLOCK = 1
       global_size = 8
       if (MPI_numnodes() .le. global_size)  then
          my_size = global_size/MPI_numnodes()
       else
          if (MPI_mynode() .lt. global_size) then
             my_size = 1
          else
             my_size = 0
          endif
       endif
       if(MPI_mynode() .eq. 0) then
         iglobal_ref(1) = 4
         iglobal_ref(2) = 8
         iglobal_ref(3) = 2
         ndata = 3
       else if (MPI_mynode() .eq. 1) then
         iglobal_ref(1) = 5
         iglobal_ref(2) = 3
         iglobal_ref(3) = 4
         iglobal_ref(4) = 1
         ndata = 4
        else
         ndata  = 0
        end if

C initialize Chaos environment
        call PARTI_setup()
c the following is the inspector code
         call reglocalize(BLOCK,global_size,ischedptr,iglobal_ref,
     $                  ilocal_ref,ndata,n_off_proc,my_size,1)
        do 10 i=1,ndata
           iglobal_ref(i) = ilocal_ref(i)
10       continue


        do i=1, ndata
           x(i) =  MPI_mynode()*100 + i
        enddo
        do 20 i=1, my_size 
           y(i) =  MPI_mynode()*100 + 2*i
20      continue

c  end of the inspector and the following is the executor code
         call dgather(y(my_size+1),y,ischedptr)

         do 30 i=1,ndata
           indirection = iglobal_ref(i)
           x(i) = x(i) +  y(indirection)
30       continue
          
c end of the executor code
         if (MPI_mynode() .le. 1) then
         WRITE(*,*)
     $    '[',MPI_mynode(),'] x:',(X(I), I=1,ndata)
         WRITE(*,*) 
     $     '[',MPI_mynode(),'] gref:',(iglobal_ref(I), I=1,ndata)
         WRITE(*,*) 
     $     '[',MPI_mynode(),'] y:',(Y(iglobal_ref(I)), I=1,ndata)
         end if
         call MPI_gsync()
         

C  Gather example
        do 11 i=1,my_size
            aloc(i) =  float(MPI_mynode())+0.1*i
11       continue

         call fgather(buffer,aloc,ischedptr)
	 if (MPI_mynode() .le. 1) then
          write (*,*) 'Gather results'
          WRITE(*,*) MPI_mynode(), (buffer(I), I=1,n_off_proc)
         endif

         end
