"Fossies" - the Fresh Open Source Software Archive

Member "NetPIPE-3.7.2/src/lapi.c" (19 Aug 2010, 6703 Bytes) of package /linux/privat/old/NetPIPE-3.7.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "lapi.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 4.x_vs_3.7.2.

    1 #include "netpipe.h"
    2 #include <lapi.h>
    3 
    4 lapi_handle_t  t_hndl;
    5 lapi_cntr_t    l_cntr;
    6 lapi_cntr_t    t_cntr;
    7 lapi_cntr_t    c_cntr;
    8 lapi_info_t    t_info;  /* LAPI info structure */
    9 void           *global_addr[2];
   10 void           *tgt_addr[2];
   11 void           *rpt_addr[2];
   12 void           *time_addr[2];
   13 void           *offset_addr[2];
   14 int            npRepeat;
   15 int            nbor_r_buff_offset;
   16 
   17 void Init(ArgStruct *p, int* pargc, char*** pargv)
   18 {
   19 
   20 }
   21 
   22 void Setup(ArgStruct *p)
   23 {
   24         int   one=1, loop, rc, val, cur_val;
   25         int   task_id;       /* My task id */
   26         int   num_tasks;     /* Number of tasks in my job */
   27         char* t_buf;         /* Buffer to manipulate */
   28         char  err_msg_buf[LAPI_MAX_ERR_STRING];
   29 
   30         bzero(&t_info, sizeof(lapi_info_t));
   31 
   32         t_info.err_hndlr = NULL;   /* Not registering error handler function */
   33 
   34         if ((rc = LAPI_Init(&t_hndl, &t_info)) != LAPI_SUCCESS) {
   35                 LAPI_Msg_string(rc, err_msg_buf);
   36                 printf("Error Message: %s, rc = %d\n", err_msg_buf, rc);
   37                 exit (rc);
   38         }
   39 
   40         /* Get task number within job */
   41 
   42         rc = LAPI_Qenv(t_hndl, TASK_ID, &task_id);
   43 
   44         /* Get number of tasks in job */
   45 
   46         rc = LAPI_Qenv(t_hndl, NUM_TASKS, &num_tasks);
   47 
   48         if (num_tasks != 2) {
   49                 printf("Error Message: Run with MP_PROCS set to 2\n");
   50                 exit(1);
   51         }
   52 
   53         /* Turn off parameter checking - default is on */
   54 
   55         rc = LAPI_Senv(t_hndl, ERROR_CHK, 0);
   56 
   57         /* Initialize counters to be zero at the start */
   58 
   59         rc = LAPI_Setcntr(t_hndl, &l_cntr, 0);
   60 
   61         rc = LAPI_Setcntr(t_hndl, &t_cntr, 0);
   62 
   63         rc = LAPI_Setcntr(t_hndl, &c_cntr, 0);
   64 
   65         /* Exchange addresses for target counter, repeats, and rbuff offset */
   66 
   67         rc = LAPI_Address_init(t_hndl,&t_cntr,tgt_addr);
   68 
   69         rc = LAPI_Address_init(t_hndl,&npRepeat,rpt_addr); 
   70 
   71         rc = LAPI_Address_init(t_hndl,&nbor_r_buff_offset,offset_addr);
   72 
   73         p->tr = p->rcv = 0;
   74         if (task_id ==0)
   75         {
   76                 p->tr = 1;
   77                 p->prot.nbor=1;
   78         }
   79         else
   80         {
   81                 p->rcv = 1;
   82                 p->prot.nbor=0;
   83         }
   84 }
   85 
   86 void Sync(ArgStruct *p)
   87 {
   88         LAPI_Gfence(t_hndl);
   89 }
   90 
   91 void PrepareToReceive(ArgStruct *p)
   92 {
   93  /* Nothing to do */
   94 }
   95 
   96 void SendData(ArgStruct *p)
   97 {
   98         int rc;
   99         int offset = p->s_ptr - p->s_buff;
  100         void* dest = global_addr[p->prot.nbor] + nbor_r_buff_offset + offset;
  101 
  102         /* We calculate the destination address because buffer alignment most
  103          * likely changed the start of the buffer from what malloc returned
  104          */
  105         rc = LAPI_Put(t_hndl, p->prot.nbor, p->bufflen*sizeof(char), dest,
  106                       (void *)p->s_ptr,tgt_addr[p->prot.nbor], 
  107                       &l_cntr,&c_cntr);
  108 
  109         /* Wait for local Put completion */
  110 
  111         rc = LAPI_Waitcntr(t_hndl, &l_cntr, 1, NULL); 
  112 
  113 }
  114 
  115 void RecvData(ArgStruct *p)
  116 {
  117         int rc,val,cur_val;
  118 
  119         /* Poll for receive.  We have to use polling
  120          * as LAPI_Waitcntr does not guarantee making progress
  121          * on receives.
  122          */
  123         rc = LAPI_Getcntr(t_hndl, &t_cntr, &val);
  124         while (val < 1) {
  125             rc = LAPI_Probe(t_hndl); /* Poll the adapter once */
  126             rc = LAPI_Getcntr(t_hndl, &t_cntr, &val);
  127         }
  128 
  129         /* To clear the t_cntr value */
  130         rc = LAPI_Waitcntr(t_hndl, &t_cntr, 1, &cur_val); 
  131 
  132 }
  133 
  134 void SendTime(ArgStruct *p, double *t)
  135 {
  136         int rc;
  137         rc = LAPI_Address_init(t_hndl,t,time_addr);
  138         rc = LAPI_Put(t_hndl,p->prot.nbor,sizeof(double),
  139                time_addr[p->prot.nbor],(void *)t,tgt_addr[p->prot.nbor],
  140                &l_cntr,&c_cntr);
  141         /* Wait for local Put completion */
  142         rc = LAPI_Waitcntr(t_hndl, &l_cntr, 1, NULL);
  143 
  144 }
  145 
  146 void RecvTime(ArgStruct *p, double *t)
  147 {
  148         int rc, val, cur_val;
  149         rc = LAPI_Address_init(t_hndl,t,time_addr);
  150         rc = LAPI_Getcntr(t_hndl, &t_cntr, &val);
  151         while (val < 1) {
  152             rc = LAPI_Probe(t_hndl); /* Poll the adapter once */
  153             rc = LAPI_Getcntr(t_hndl, &t_cntr, &val);
  154         }
  155         /* To clear the t_cntr value */
  156         rc = LAPI_Waitcntr(t_hndl, &t_cntr, 1, &cur_val);
  157 }
  158 
  159 void SendRepeat(ArgStruct *p, int rpt)
  160 {
  161         int rc;
  162 
  163         rc = LAPI_Put(t_hndl,p->prot.nbor,sizeof(int), rpt_addr[p->prot.nbor],
  164                         (void *)&rpt,tgt_addr[p->prot.nbor],&l_cntr,&c_cntr);
  165 
  166         /* Wait for local Put completion */
  167         rc = LAPI_Waitcntr(t_hndl, &l_cntr, 1, NULL);    
  168 }
  169 
  170 void RecvRepeat(ArgStruct *p, int *rpt) 
  171 {
  172         int rc,val,cur_val;
  173 
  174         rc = LAPI_Getcntr(t_hndl, &t_cntr, &val);
  175         while (val < 1) {
  176             rc = LAPI_Probe(t_hndl); /* Poll the adapter once */
  177             rc = LAPI_Getcntr(t_hndl, &t_cntr, &val);
  178         }
  179 
  180         *rpt = npRepeat;
  181 
  182         /* To clear the t_cntr value */
  183         rc = LAPI_Waitcntr(t_hndl, &t_cntr, 1, &cur_val);  
  184 }
  185 
  186 void  CleanUp(ArgStruct *p)
  187 {
  188     int rc;
  189     rc = LAPI_Gfence(t_hndl); /* Global fence to sync before terminating job */
  190     rc = LAPI_Term(t_hndl);   
  191 }        
  192 
  193 
  194 void Reset(ArgStruct *p)
  195 {
  196 
  197 }
  198 
  199 
  200 void AfterAlignmentInit(ArgStruct* p)
  201 {
  202     int rc, val, cur_val;
  203     int my_r_buff_offset = p->r_buff - p->r_buff_orig;
  204 
  205     /* Send my receive buffer offset to other guy */
  206     rc = LAPI_Put(t_hndl,p->prot.nbor,sizeof(int), offset_addr[p->prot.nbor],
  207                   (void *)&my_r_buff_offset,tgt_addr[p->prot.nbor],&l_cntr,&c_cntr);
  208 
  209     /* Wait for local Put completion */
  210     rc = LAPI_Waitcntr(t_hndl, &l_cntr, 1, NULL);
  211 
  212     /* Wait for incoming Put completion (We poll because receive progress not 
  213      * guaranteed in LAPI_Waitcntr() 
  214      */
  215     rc = LAPI_Getcntr(t_hndl, &t_cntr, &val);
  216     while (val < 1) {
  217       rc = LAPI_Probe(t_hndl); /* Poll the adapter once, make progress */
  218       rc = LAPI_Getcntr(t_hndl, &t_cntr, &val);
  219     }
  220 
  221     /* To clear the t_cntr value */
  222     rc = LAPI_Waitcntr(t_hndl, &t_cntr, 1, &cur_val);  
  223 }
  224 
  225 void MyMalloc(ArgStruct *p, int bufflen, int soffset, int roffset)
  226 {
  227     int rc;
  228 
  229     if((p->r_buff=(char *)malloc(bufflen+MAX(soffset,roffset)))==(char *)NULL)
  230     {
  231         fprintf(stderr,"couldn't allocate memory for receive buffer\n");
  232         exit(-1);
  233     }
  234     rc = LAPI_Address_init(t_hndl,p->r_buff,global_addr);
  235 
  236     if(!p->cache)
  237       if((p->s_buff=(char *)malloc(bufflen+soffset))==(char *)NULL)
  238       {
  239           fprintf(stderr,"Couldn't allocate memory for send buffer\n");
  240           exit(-1);
  241       }
  242 
  243 }
  244 
  245 void FreeBuff(char *buff1, char *buff2)
  246 {
  247     if(buff1 != NULL)
  248       free(buff1);
  249 
  250     if(buff2 != NULL)
  251       free(buff2);
  252 }
  253 
  254