The entire code for the broadcasting algorithms can be found here.
#include "bsp.h" #include <stdlib.h> #define OVERSAMPLE_ONE 250 #define OVERSAMPLE_TWO 500 #define SIZE 100000 int pid; int nprocs;
void bcast_onestage(int frompid, void *src, void *dst, int nbytes) { int i; if (pid==frompid) for(i=0;i<nprocs;i++) bsp_hpput(i,src,dst,0,nbytes); bsp_sync(); }
void bcast_twostage(int frompid, void *src, void *dst, int nbytes) { int i,n_over_p, last_n_over_p; n_over_p = nbytes / nprocs; last_n_over_p = nbytes - (n_over_p * (nprocs-1)); if (frompid==pid) { for(i=0;i<nprocs;i++) { bsp_hpput(i, ((char*) src)+(n_over_p*i), dst, (n_over_p*i), (i==(nprocs-1))?last_n_over_p:n_over_p); } } bsp_sync(); for(i=0;i<nprocs;i++) bsp_hpput(i, ((char*) dst)+(n_over_p*pid), dst, (n_over_p*pid), (pid==(nprocs-1))?last_n_over_p:n_over_p); bsp_sync(); }
void bar() { int i, *xs = calloc(SIZE,sizeof(int)); if (xs==NULL) bsp_abort("Unable to allocate storage"); bsp_pushregister(xs,sizeof(int)*SIZE); bsp_sync(); for (i=0;i<OVERSAMPLE_ONE;i++) bcast_onestage(0,xs,xs,sizeof(int)*SIZE); for (i=0;i<OVERSAMPLE_TWO;i++) bcast_twostage(0,xs,xs,sizeof(int)*SIZE); bsp_popregister(xs); } void foo() { int i, *xs = calloc(SIZE,sizeof(int)); if (xs==NULL) bsp_abort("Unable to allocate storage"); bsp_pushregister(xs,sizeof(int)*SIZE); bsp_sync(); for (i=0;i<OVERSAMPLE_ONE;i++) bcast_onestage(0,xs,xs,sizeof(int)*SIZE); bsp_popregister(xs); } void main() { bsp_begin(bsp_nprocs()); pid =bsp_pid(); nprocs=bsp_nprocs(); foo(); bar(); bsp_end(); }