





















#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "bsp_lib_user.h"
#include "bsp_register.h"
#include "../library_shared/bsp_types.h"







#ifdef  CONVEXSPP
#define BSP_ARCH  "CONVEXSPP"
#endif

#ifdef  CRAYC90
#define BSP_ARCH  "CRAYC90"
#endif

#ifdef  CRAYJ90
#define BSP_ARCH  "CRAYJ90"
#endif

#ifdef  CRAYT3D
#define BSP_ARCH  "CRAYT3D"
#endif

#ifdef  CRAYT3E
#define BSP_ARCH  "CRAYT3E"
#endif

#ifdef  CRAYYMP
#define BSP_ARCH  "CRAYYMP"
#endif

#ifdef  FUJIAP1000
#define BSP_ARCH  "FUJIAP1000"
#endif

#ifdef  HITACHIMPP
#define BSP_ARCH  "HITACHIMPP"
#endif

#ifdef  HP
#define BSP_ARCH  "HP"
#endif

#ifdef  LINUX
#define BSP_ARCH  "LINUX"
#endif

#ifdef  OSF1
#define BSP_ARCH  "OSF1"
#endif

#ifdef  PARAMID
#define BSP_ARCH  "PARAMID"
#endif

#ifdef  PARSYTEC
#define BSP_ARCH  "PARSYTEC"
#endif

#ifdef  RS6000
#define BSP_ARCH  "RS6000"
#endif

#ifdef  SGI
#define BSP_ARCH  "SGI"
#endif

#ifdef SGIN32
#define BSP_ARCH  "SGIN32"
#endif

#ifdef  SGICHALL64
#define BSP_ARCH  "SGICHALL64"
#endif

#ifdef  SGIORIGIN
#define BSP_ARCH  "SGIORIGIN"
#endif

#ifdef  SP2
#define BSP_ARCH  "SP2"
#endif

#ifdef  Solaris
#define BSP_ARCH  "Solaris"
#endif

#ifdef  SolarisX86
#define BSP_ARCH  "SolarisX86"
#endif

#ifdef  SunOS
#define BSP_ARCH  "SunOS"
#endif

#ifdef  WindowsNT
#define BSP_ARCH  "WindowsNT"
#endif

#ifdef  WIN32
#define BSP_ARCH  "WIN32"
#endif



#ifdef  SHMEM_SYSV
#define BSP_DEVICE  "SHMEM_SYSV"
#endif
#ifdef  SHMEM_SGI
#define BSP_DEVICE  "SHMEM_SGI"
#endif
#ifdef  SHMEM_NULL
#define BSP_DEVICE  "SHMEM_NULL"
#endif
#ifdef  SHMEM_WIN32
#define BSP_DEVICE  "SHMEM_WIN32"
#endif
#ifdef  MPASS_MPI
#define BSP_DEVICE  "MPASS_MPI"
#endif
#ifdef  MPASS_TCPIP
#define BSP_DEVICE  "MPASS_TCPIP"
#endif
#ifdef  MPASS_UDPIP
#define BSP_DEVICE  "MPASS_UDPIP"
#endif
#ifdef  MPASS_3C905B
#define BSP_DEVICE  "MPASS_3C905B"
#endif
#ifdef  MPASS_MPL
#define BSP_DEVICE  "MPASS_MPL"
#endif
#ifdef  MPASS_PARIX
#define BSP_DEVICE  "MPASS_PARIX"
#endif
#ifdef  MPASS_EXPRESS
#define BSP_DEVICE  "MPASS_EXPRESS"
#endif
#ifdef  MPASS_PARMACS
#define BSP_DEVICE  "MPASS_PARMACS"
#endif
#ifdef  MPASS_FUJICELL
#define BSP_DEVICE  "MPASS_FUJICELL"
#endif
#ifdef  DRMA_SHMEM
#define BSP_DEVICE  "DRMA_SHMEM"
#endif






#define BSMP_PADDING_SIZE 8







#define SIZEOF(x) ((int) sizeof(x))























#if defined(SGICHALL64) || defined(MPASS_UDPIP) || defined(MPASS_TCPIP) || defined(MPASS_3C905B)
#define MAX_FCOMBINE_MESSAGE_SIZE 888
#else
#if defined(CRAYT3D) || defined(CRAYT3E)
#define MAX_FCOMBINE_MESSAGE_SIZE 32
#else
#define MAX_FCOMBINE_MESSAGE_SIZE 400
#endif
#endif










#define BSMP_MIN_BUFFER_SIZE 100
#define BSMP_INFINITE_BUFFERS (-1)












#define CONTENTION_REMOVAL_NONE()   (_bsp_opt_contention_level==0)
#define CONTENTION_REMOVAL_SOME()   (_bsp_opt_contention_level==2)
#define CONTENTION_REMOVAL_ALL()    (_bsp_opt_contention_level>0)











#define FAST_MEMCPY(dst,src,count)                          \
do {                                                        \
      char *srcnew=(char*)(src);                            \
      char *dstnew=(char*)(dst);                            \
      if ((count==sizeof(double)) &&                        \
          ((((long int)dstnew)&(sizeof(double)-1))==0) &&   \
          ((((long int)srcnew)&(sizeof(double)-1))==0)) {  \
        *((double*)(dstnew))=*((double*)(srcnew));          \
      } else if ((count==sizeof(int)) &&                    \
          ((((long int)dstnew)&(sizeof(int)-1))==0) &&      \
          ((((long int)srcnew)&(sizeof(int)-1))==0)) {      \
        *((int*)(dstnew))=*((int*)(srcnew));                \
      } else {                                              \
        memcpy(dstnew,srcnew,count);                        \
      }                                                     \
} while(0)



#ifdef USE_BCOPY
#define MEMCPY(dst,src,nbytes) bcopy(src,dst,nbytes)
#else
#define MEMCPY(dst,src,nbytes) memcpy(dst,src,nbytes)
#endif



#ifdef SANITY_CHECK
#define MALLOC(size)       calloc(size,SIZEOF(char))
#define CALLOC(nelem,size) calloc(nelem,size)
#else
#define MALLOC(size)       malloc(size)
#define CALLOC(nelem,size) malloc((nelem)*(size))
#endif










#define RAND_SEED(m) srand(m)
#define RAND()       (rand()>>2)







#define CEILING_DIV(x,y) (((x) != 0) ? ((((x) - 1)/(y)) + 1) : 0)
#ifdef TURBO
#define MOD_POW2(x,y) ((x) & ((y)-1))
#else
#define MOD_POW2(x,y) ((x)%(y))
#endif

#define PAIRMAX(x,y) (((x)>(y))?(x):(y))
#define PAIRMIN(x,y) (((x)<(y))?(x):(y))






#if defined(REALDEBUG) || defined(CALLGRAPH_PROFILE)
#define STATIC /* static */
#else
#define STATIC static
#endif



















#define COMM_IS_PUT(pid)      ((pid & 3)==0) 
#define COMM_IS_SEND(pid)     ((pid & 3)==2) 
#define COMM_IS_PUTLIKE(pid)  ((pid & 1)==0)

#define COMM_IS_GET(pid)      ((pid & 3)==1)
#define COMM_IS_HPGET(pid)    ((pid & 3)==3)
#define COMM_IS_GETLIKE(pid)  ((pid & 1)==1)

#define COMM_SET_PUT(pid)     (pid<<2)
#define COMM_SET_SEND(pid)    ((pid<<2) +2)
#define COMM_SET_GET(pid)     ((pid<<2) +1)
#define COMM_SET_HPGET(pid)   ((pid<<2) +3)
#define COMM_GET_PID(pid)     (pid>>2)










#define UNKNOWN_SENDER (-1)











typedef struct {
   bsp_int32 comm_pid; /* See put, get, and  send smashed with pid macros */
   bsp_int32 addr_global;
   bsp_int32 offset;
   void *addr_real;
   bsp_int32 nbytes;
 } _BSPcomm_thunk;








extern int _bsp_throttle_procs;
extern int _bsp_check_syncs;
extern int _bsp_do_stat;
extern int _bsp_buffer_size;
extern int _bsp_buffer_stalls;
extern int _bsp_nbuffers;
extern int _bsp_comm_fifo_size;
extern int _bsp_opt_fcombine_puts;
extern int _bsp_opt_fcombine_puts_max;
extern int _bsp_opt_fcombine_puts_min;
extern int _bsp_opt_contention_level;
extern int _bsp_opt_flibrary_level;
extern char _bsp_library_type;
extern char *_bsp_arch;
extern char *_bsp_device;
extern char *_bsp_exec_file;
extern int _bsp_do_prof;
extern int _bsp_do_cgprof;


#ifdef BSP_SHMEM
#include "bsp_lib_shmem.h"
#else
#ifdef BSP_MPASS
#include "bsp_lib_mpass.h"
#else
#include "bsp_lib_drma.h"
#endif
#endif

