IBM Books

Administration Guide


Put (C)

This C program is an example of the use of LAPI_Put and LAPI_Get:

/* Put.c
** Example program illustrating the use of LAPI_Put and LAPI_Get
*/
 
#include <lapi.h>
 
#define A_MAX     2
#define I_MAX     10
 
int
main(int argc, char **argv)                                               main
{
    lapi_handle_t t_hndl;               /* LAPI context handle - */
                                        /* returned */
    lapi_info_t   t_info;               /* LAPI info structure */
    int           task_id,              /* My task id */
                  num_tasks;            /* Number of tasks in my job */
    int           t_buf[I_MAX];         /* Buffer to manipulate */
    lapi_cntr_t   l_cntr;               /* Origin counter */
    lapi_cntr_t   t_cntr;               /* Target counter */
    lapi_cntr_t   c_cntr;               /* Completion counter */
    void          *global_addr[A_MAX];  /* Array to store t_buf */
                                        /* addr from all the tasks */
                                        /* The size of this array */
                                        /* needs to each number of tasks */
    void          *tgt_addr[A_MAX];     /* Array to store target */
                                        /* counter ad from all the */
                                        /* tasks. */
    int           loop, rc, tgt, val, cur_val;
    char          err_msg_buf[LAPI_MAX_ERR_STRING];
 
    bzero(&t_info, sizeof(lapi_info_t));
    t_info.err_hndlr = NULL;   /* Not registering error handler function */
    if ((rc = LAPI_Init(&t_hndl, &t_info)) != LAPI_SUCCESS) {
        LAPI_Msg_string(rc, err_msg_buf);
        printf("Error Message: %s, rc = %d\n", err_msg_buf, rc);
        exit (rc);
    }
    rc = LAPI_Qenv(t_hndl, TASK_ID, &task_id);     /* Get task number */
                                                   /* within job */
    rc = LAPI_Qenv(t_hndl, NUM_TASKS, &num_tasks); /* Get number of */
                                                   /* tasks in job */
 
    if (num_tasks != 2) }
        printf("Error Message: Run with MP_PROCS set to 2\n");
        exit(1);
    }
 
    /* Turn off parameter checking - default is on */
    rc = LAPI_Senv(t_hndl, ERROR_CHK, 0);
 
    /* Initialize counters to be zero at the start */
    rc = LAPI_Setcntr(t_hndl, &l_cntr, 0);
    rc = LAPI_Setcntr(t_hndl, &t_cntr, 0);
    rc = LAPI_Setcntr(t_hndl, &c_cntr, 0);
 
    /* Exchange buffer address and target counter address of every task */
    rc = LAPI_Address_init(t_hndl,t_buf,global_addr);    /* Collective call */
    rc = LAPI_Address_init(t_hndl,&t_cntr,tgt_addr);     /* Collective call */
 
    if (task_id == 0) { /* Task id is 0 , Origin */
        tgt = task_id + 1;
        for (loop=0; loop < I_MAX; loop++) { /* Update buffer */
            t_buf[loop] = task_id - loop;
        }
        rc = LAPI_Gfence(t_hndl);  /* Global fence to sync before starting */
        rc = LAPI_Put(t_hndl,tgt,I_MAX*sizeof(int), global_addr[tgt],
                        (void *)t_buf,tgt_addr[tgt],,&l_cntr,&c_cntr);
        /* Wait for local Put completion */
        rc = LAPI_Waitcntr(t_hndl, &l_cntr, 1, NULL);
 
        /* Can now change local buffer */
        for (loop=0; loop < I_MAX; loop++) { /* Update buffer */
            t_buf[loop] = loop * task_id;
        }
 
        /* Wait for target Put completion at task 1 as well */
        rc = LAPI_Waitcntr(t_hndl, &c_cntr, 1, NULL);
        printf("Node %d, done issuing Put to node %d\n", task_id, tgt);
 
        rc = LAPI_Get(t_hndl,tgt,I_MAX*sizeof(int), global_addr[tgt],
                                   (void *)t_buf,tgt_addr[tgt],&l_cntr);
        /* Wait for local Get completion */
        rc = LAPI_Waitcntr(t_hndl, &l_cntr, 1, NULL);
        printf("Node %d, done issuing Get from node %d\n", task_id, tgt);
        printf("Result of Get after the Put from node %d:\n", tgt);
        for (loop=0; loop < I_MAX; loop++) { /* Update buffer */
            printf("Val[%d] = %d\n", loop, t_buf[loop]);
        }
    } else { /* Task id is 1 , Target */
        tgt = task_id - 1;
        for (loop=0; loop < I_MAX; loop++) { /* Zero out buffer */
            t_buf[loop] = 0;
        }
        rc = LAPI_Gfence(t_hndl);  /* Global fence to sync before starting */
        /* Process Put */
        rc = LAPI_Getcntr(t_hndl, &t_cntr, &val);
        while (val < 1) {
            sleep(1); /* Do some work */
            rc = LAPI_Probe(t_hndl); /* Poll the adapter once */
            rc = LAPI_Getcntr(t_hndl, &t_cntr, &val);
        }
        /* To clear the t_cntr value */
        rc = LAPI_Waitcntr(t_hndl, &t_cntr, 1, &cur_val);
        printf("Node %d, done doing work and processing Put\n", task_id);
        printf("Result of Put from %d:\n", tgt);
        for (loop=0; loop < I_MAX; loop++) { /* Update buffer */
            printf("Val[%d] = %d\n", loop, t_buf[loop]);
        }
        /* Process Get */
        rc = LAPI_Getcntr(t_hndl, &t_cntr, &val);
        while (val < 1) {
            sleep(1); /* Do some work */
            rc = LAPI_Probe(t_hndl); /* Poll the adapter once */
            rc = LAPI_Getcntr(t_hndl, &t_cntr, &val);
        }
 
        /* To clear the t_cntr value */
        rc = LAPI_Waitcntr(t_hndl, &t_cntr, 1, &cur_val);
        printf("Node %d, done doing work and processing Get\n", task_id);
    }
    rc = LAPI_Gfence(t_hndl); /* Global fence to sync before terminating job */
    rc = LAPI_Term(t_hndl);
}


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]