	program deref
c
#include "mpi.fort.h"
c
       integer size, i, index_array(2)
       integer deref_array(2)
       integer local(2), proc(2)
       integer build_translation_table
       integer itable, my_size
c initialize CHAOS environment
       call PARTI_setup()

       my_size = 0
c   Assign indices 1 and 4 to processor 0
       if(MPI_mynode().eq.0) then
         my_size = 2
         index_array(1) = 1
         index_array(2) = 4
       endif
c   Assign indices 2 and 3 to processor 1
       if(MPI_mynode().eq.1) then
         my_size = 2
         index_array(1) = 2
         index_array(2) = 3
       endif
c   set up a translation table
       itable = build_translation_table(1,index_array,my_size)
c    Processor 0 seeks processor and local indices
c     for global array indices 0 and 1 */
       ndata = 0
       if(MPI_mynode().eq.0) then
         ndata = 2
         deref_array(1) = 1
         deref_array(2) = 2
       endif
c    Processor 1 seeks processor and local indices
c     for global array indices 2 and 3 */
       if(MPI_mynode().eq.1) then
         ndata = 2
         deref_array(1) = 3
         deref_array(2) = 4
       endif

c   Dereference a set of global variables
       call dereference(itable,deref_array,local,proc, ndata)

c local and proc return the processors and local indices where
c global array indices are stored.
c In processor 0, proc(0) = 0, proc(1) = 1, local(0) = 0 , local(1) = 0
c In processor 1, proc(0) = 1, proc(1) = 0, local(0) = 1 , local(1) = 1

       if (MPI_mynode().eq. 0) then
        write(*,*) 'Processor 0'
        write (*,*) 'proc', (proc(i), i = 1, my_size)
        write (*,*) 'local', (local(i), i = 1, my_size)
       end if
       call MPI_gsync()
       if (MPI_mynode().eq. 1) then
        write(*,*) 'Processor 1'
        write (*,*) 'proc', (proc(i), i = 1, my_size)
        write (*,*) 'local', (local(i), i = 1, my_size)
       end if

       stop
       end
