


























#ifdef DRMA_SHMEM
#include <mpp/shmem.h>
#endif





#ifdef BSP_ONLINE_PROCS
#define BSP_MAX_PROCS BSP_ONLINE_PROCS
#else
#define BSP_MAX_PROCS 1024
#endif













































#ifdef DRMA_SHMEM
#ifdef CRAYMPP
#define cwhatis_nprocs_mypid(refargc,refargv,refnprocs,refpid) \
  (*(refnprocs) = shmem_n_pes(),                               \
  (*(refpid)    = shmem_my_pe(),                               \
  (shmem_set_cache_inv(),0)))
#else
#define cwhatis_nprocs_mypid(refargc,refargv,refnprocs,refpid) \
  (*(refnprocs) = _num_pes(),                                  \
  (*(refpid)    = _my_pe(),0))
#endif

#define f77whatis_nprocs_mypid(refnprocs,refpid) \
  cwhatis_nprocs_mypid(ohshit,ohshit,refnprocs,refpid)








#ifdef CRAYMPP
#define drma_word_size() (SIZEOF(long))
#else
#define drma_word_size() (SIZEOF(int))
#endif

#ifdef SGIORIGIN
#define shmem_put(tgt,src,len,pe) shmem_put32(tgt,src,len,pe)
#define shmem_get(tgt,src,len,pe) shmem_get32(tgt,src,len,pe)
#endif

#ifdef SANITY_CHECK
#define drma_put(pid,src,dst,nbytes)                                 \
  do {                                                               \
    if (nbytes % drma_word_size())                                   \
      bsp_abort("{drma_put} %d bytes not word chunked line %d of %s",\
                nbytes,__LINE__,__FILE__);                           \
    if (((long int) ((char*) src)) % drma_word_size())               \
      bsp_abort("{drma_put} source not word aligned: line %d of %s", \
                __LINE__,__FILE__);                                  \
    if (((long int) ((char*) dst)) % drma_word_size())               \
      bsp_abort("{drma_put} dest not word aligned: line %d of %s",   \
                __LINE__,__FILE__);                                  \
    shmem_put((long*)(dst),(long*)(src),                             \
              nbytes/drma_word_size(),pid_mapping(pid));             \
  } while(0)
#else
#define drma_put(pid,src,dst,nbytes)   \
  shmem_put((long*)(dst),(long*)(src), \
            (nbytes)/drma_word_size(),pid_mapping(pid))
#endif

#ifdef SANITY_CHECK
#define drma_put_int(pid,src,dst) drma_put(pid,src,dst,SIZEOF(int))
#else
#define drma_put_int(pid,src,dst) shmem_int_p(dst,*(src),pid_mapping(pid))
#endif







#ifdef SANITY_CHECK
#define drma_get(pid,src,dst,nbytes) \
  do {                                                               \
    if (nbytes % drma_word_size())                                   \
      bsp_abort("{drma_get} %d bytes not word chunked line %d of %s",\
               nbytes,__LINE__,__FILE__);                            \
    if (((long int) ((char*) src)) % drma_word_size())               \
      bsp_abort("{drma_get} source not word aligned: line %d of %s", \
                __LINE__,__FILE__);                                  \
    if (((long int) ((char*) dst)) % drma_word_size())               \
      bsp_abort("{drma_get} dest not word aligned: line %d of %s",   \
                __LINE__,__FILE__);                                  \
    shmem_get((long*)(dst),(long*)(src),                             \
              nbytes/drma_word_size(),pid_mapping(pid));             \
  } while(0)
#else
#define drma_get(pid,src,dst,nbytes)   \
   shmem_get((long*)(dst),(long*)(src),\
             (nbytes)/drma_word_size(),pid_mapping(pid))
#endif

#ifdef SANITY_CHECK
#define drma_get_int(pid,src,dst) drma_get(pid,src,dst,SIZEOF(int))
#else
#define drma_get_int(pid,src,dst) *(dst)=shmem_int_g(src,pid_mapping(pid))
#endif











#define init_barrier()                        \
  do {                                        \
    _drma.syncs=0;                            \
  } while(0)



#ifdef CRAYPVP
#define barrier_sync()                                \
  do {                                                \
    if (_bsp_fork_nprocs>1) {                         \
      _drma.syncs++;                                  \
      shmem_barrier_all();                            \
    }                                                 \
  } while(0)

#else
#define barrier_sync()                                \
  do {                                                \
    if (_bsp_fork_nprocs>1) {                         \
      _drma.syncs++;                                  \
      barrier();                                      \
    }                                                 \
  } while(0)
#endif





#define drma_exit(status) exit(status)
#ifdef CRAYMPP
#define drma_stopall()    \
  do {                    \
    tracebk(10,stderr);   \
    globalexit(1);        \
  } while(0)

#else
#define drma_stopall()    error-should-never-inline-this
#endif
#endif






#define drma_lock_type long int
#define init_drma_lock(i) i=0;
#define drma_set_lock(i) shmem_set_lock(&i)










typedef struct {
   int              syncs;
   drma_lock_type   mutex;
   _BSPcomm_thunk **proc_comm_put_fifo;    /* pending comms per proc  */
   int             *proc_comm_put_next;    /* next position out       */ 
   int             *proc_comm_put_size;    /* next position out       */ 
   _BSPcomm_thunk **proc_comm_get_fifo;    /* pending comms per proc  */
   int             *proc_comm_get_next;    /* next position out       */ 
   int             *proc_comm_get_size;    /* next position out       */ 
   int             *bsmp_sizes;
   int             *pid_map;           
} _BSPinfo;
  
extern _BSPinfo _drma;























#ifdef CRAYMPP
#define BSP_RANDOMISE_PIDS
#endif

#ifdef BSP_RANDOMISE_PIDS
#define init_pid_mapping(nprocs,refpid)                            \
  do {                                                             \
    int i_macro, temp_macro, idx;                                  \
    _drma.pid_map=CALLOC(nprocs,SIZEOF(int));                      \
    if (_drma.pid_map==NULL)                                       \
      bsp_abort("{init_pid_mapping} failed to allocate storage");  \
    for(i_macro=0;i_macro<nprocs;i_macro++)                        \
      _drma.pid_map[i_macro]=i_macro;                              \
    RAND_SEED(nprocs);                                             \
    for(i_macro=1;i_macro<nprocs;i_macro++) {                      \
      idx        = 1+ (RAND()%(nprocs-1));                         \
      temp_macro = _drma.pid_map[idx];                             \
      _drma.pid_map[idx]=_drma.pid_map[i_macro];                   \
      _drma.pid_map[i_macro]=temp_macro;                           \
    }                                                              \
    for(i_macro=0;i_macro<nprocs;i_macro++) {                      \
      if (_drma.pid_map[i_macro]==*(refpid)) {                     \
        *(refpid)=i_macro;                                         \
        break;                                                     \
      }                                                            \
    }                                                              \
  } while(0) 

#define pid_mapping(realpid) _drma.pid_map[realpid]
#else
#define init_pid_mapping(nprocs,refpid) /* do nothing */
#define pid_mapping(realpid) realpid
#endif


