File: armci.c

package info (click to toggle)
netpipe 3.7.2-7
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,184 kB
  • ctags: 912
  • sloc: ansic: 7,588; makefile: 276; sh: 63; perl: 45; csh: 25
file content (393 lines) | stat: -rw-r--r-- 7,590 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*  This module is basically the rewritten ARMCI module written by Xuehua
*/
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>

#include <mpi.h>
#include "armci.h"
#define USE_VOLATILE_RPTR /* need for polling on receive buffer */
#include "netpipe.h"

extern double *pTime;
extern int    *pNrepeat;


int npes, mype;
int nbor_r_buff_offset;

struct mnode_t {
    void *ptrs[2];   /* Will only use 2 pointers */
    int nbytes;
    struct mnode_t* next;
};

/* Pointers to first element of the linked list */
struct mnode_t *m_first = 0;


void mlink_front(struct mnode_t* node) {
    if (m_first != 0) node->next = m_first;
    else node->next = 0;
    m_first = node;
}


struct mnode_t *mfind(void* ptr, struct mnode_t** prev) {
    struct mnode_t *p, *n;

    if (m_first != 0) {
        if (m_first->ptrs[mype] != ptr) {
            for (p=m_first, n=p->next; n != 0; p=n, n=n->next) {
                if (n->ptrs[mype] == ptr) {
                   *prev = p;
                   return n;
                }
            }
        }
        else {
            *prev = 0;
            return m_first;
        }
    }
    else {
        *prev = 0;
        return 0;
    }
    
    ARMCI_Error("Cannot find pointer in linked list", -1);
    
}


void* munlink(struct mnode_t *node, struct mnode_t *prev) {

    if (node != 0) {
        if (prev != 0) prev->next = node->next;
        else m_first = node->next;
    }

    return node;
}



void* armci_malloc(int nbytes) {
    struct mnode_t *node = malloc(sizeof(struct mnode_t));

    if (node == 0) {
        ARMCI_Error("Cannot allocate memory", -1);
    }

    ARMCI_Malloc(node->ptrs, nbytes);

    node->nbytes = nbytes;
    mlink_front(node);

    return node->ptrs[mype];
}


void armci_free(void* ptr) {
    struct mnode_t *n, *p;

    n = mfind(ptr, &p);
    if (n != 0) munlink(n, p);

    /* XXX if n = 0, then we have a problem */

    ARMCI_Free(ptr);
}


void* remote_ptr(void* local) {
    struct mnode_t *n, *p;

    n = mfind(local, &p);  /* ignore p */
    if (n != 0) {
        return n->ptrs[1-mype];
    }
    else {
        return 0;
    }
}

/* aro */
int is_host_local(char* hostname)
{
  struct hostent* hostinfo;
  char* addr;
  char buf[1024];
  char cmd[80];
  FILE* output;

  hostinfo = gethostbyname(hostname);

  if(hostinfo == NULL) {
    fprintf(stderr, "Could not resolve hostname [%s] to IP address", hostname);
    fprintf(stderr, "Reason: ");

    switch(h_errno)
      {
      case HOST_NOT_FOUND:
        printf("host not found\n");
        break;

      case NO_ADDRESS:
        printf("no IP address available\n");
        break;

      case NO_RECOVERY:
        printf("name server error\n");
        break;

      case TRY_AGAIN:
        printf("temporary error on name server, try again later\n");
        break;

      }

    return -1;
  }

  addr = (char*)inet_ntoa(*(struct in_addr *)hostinfo->h_addr_list[0]);

  sprintf(cmd, "/sbin/ifconfig | grep %s", addr);

  output = popen(cmd, "r");
   
  if(output == NULL) {
    fprintf(stderr, "running /sbin/ifconfig failed\n");
    return -1;
  }
  
  if(fgets(buf, 1024, output) == NULL) {
    pclose(output);
    return 0;
  } else {
    pclose(output);
    return 1;
  } 
}

void chop(char* s)
{
  int i;
  for(i=0; s[i]!='\0'; s++)
    if(s[i]=='\n')
      s[i]='\0';
    
}

void set_armci_hostname()
{
  char buf[1024];
  FILE* hostfile = fopen("armci_hosts", "r");

  if(hostfile == NULL)
    return;

  while(fgets(buf, 1024, hostfile) != NULL) {
    chop(buf);/* remove trailing newline */
    if(is_host_local(buf)==1) {
      fprintf(stderr,"Setting ARMCI_HOSTNAME=%s\n", buf);
      if(setenv("ARMCI_HOSTNAME", buf, 1)==-1)
      fprintf(stderr, "Insufficient space in environment\n");
    }
  }

  fclose(hostfile);

}

void Init(ArgStruct *p, int* pargc, char*** pargv)
{
    MPI_Init(pargc, pargv);
}

void Setup(ArgStruct *p) {
    int e;

    set_armci_hostname(); /* aro */
    ARMCI_Init(); /* aro */

    e = MPI_Comm_size(MPI_COMM_WORLD, &npes);
    if (e != MPI_SUCCESS) {
        ARMCI_Error("Cannot obtain number of PEs", e);
    }
    else if (npes != 2) {
        ARMCI_Error("This program must be run on 2 PEs", -1);
    }

    e = MPI_Comm_rank(MPI_COMM_WORLD, &mype);
    if (e != MPI_SUCCESS) {
        ARMCI_Error("Cannot obtain PE rank", e);
    }

    if (npes != 2) {
        ARMCI_Error("You must run on 2 nodes", -1);
        /* ARMCI_Error terminates everything */
    }

    p->prot.flag = armci_malloc(sizeof(int));
    pTime = armci_malloc(sizeof(double));
    pNrepeat = armci_malloc(sizeof(int));

    p->tr = p->rcv = 0;
    if ((p->prot.ipe = mype) == 0) {
        p->tr = 1;
        p->prot.nbor = 1;
        *p->prot.flag = 1;
    }
    else {
        p->rcv = 1;
        p->prot.nbor = 0;
        *p->prot.flag = 0;
    }
}


void Sync(ArgStruct *p) {
    MPI_Barrier(MPI_COMM_WORLD);
}


void PrepareToReceive(ArgStruct *p) {
}


void SendData(ArgStruct *p) {
    int p_bytes;
    void *remote_buff;
    int buf_offset=0;

    p_bytes = p->bufflen;

    buf_offset  = nbor_r_buff_offset;
    buf_offset += p->s_ptr - p->s_buff;
    remote_buff = remote_ptr(p->r_buff_orig) + buf_offset;

    ARMCI_Put(p->s_ptr, remote_buff, p_bytes, p->prot.nbor);
    ARMCI_AllFence();  /* may be necessary or not */
}


void RecvData(ArgStruct *p) {

    while (p->r_ptr[p->bufflen-1] != 'a' + (p->cache ? 1 - p->tr : 1)) 
    {
       /* BUSY WAIT */
    }

    p->r_ptr[p->bufflen-1] = 'a' + (p->cache ? p->tr : 0);
}


void SendTime(ArgStruct *p, double *t) {
    int p_bytes;
    void *remote_buff;

    *pTime = *t;
    
    p_bytes = sizeof(double);
    remote_buff = remote_ptr(pTime);
    ARMCI_Put(pTime, remote_buff, p_bytes, p->prot.nbor);

    p_bytes = sizeof(int);
    remote_buff = remote_ptr((void*)p->prot.flag);
    ARMCI_Put((void*)p->prot.flag, remote_buff, p_bytes, p->prot.nbor);
}


void RecvTime(ArgStruct *p, double *t) {

    while (*p->prot.flag != p->prot.ipe) 
    {
       /* BUSY WAIT */   
    }

    *t = *pTime; 
    *p->prot.flag = p->prot.nbor;
}


void SendRepeat(ArgStruct *p, int rpt) {
    void *remote_buff;
    int p_bytes;

    *pNrepeat = rpt;

    p_bytes = sizeof(int);
    remote_buff = remote_ptr(pNrepeat);
    ARMCI_Put(pNrepeat, remote_buff, p_bytes, p->prot.nbor);

    p_bytes = sizeof(int);
    remote_buff = remote_ptr((void*)p->prot.flag);
    ARMCI_Put((void*)p->prot.flag, remote_buff, p_bytes, p->prot.nbor);

}


void RecvRepeat(ArgStruct *p, int *rpt) {
    void *remote_buff;

    while (*p->prot.flag != p->prot.ipe)
    {
       /* BUSY WAIT */
    }
    
    *rpt = *pNrepeat;
    *p->prot.flag = p->prot.nbor;
}


void  CleanUp(ArgStruct *p) {
    ARMCI_Finalize();

}



void Reset(ArgStruct *p)
{

}

void AfterAlignmentInit(ArgStruct *p)
{
  MPI_Status s;

  /* Calculate difference between malloc'ed buffer and aligned buffer */

  int my_r_buff_offset = p->r_buff - p->r_buff_orig;

  /* Exchange offset data */

  MPI_Send(&my_r_buff_offset, 1, MPI_INT, p->prot.nbor, 0, MPI_COMM_WORLD);

  MPI_Recv(&nbor_r_buff_offset, 1, MPI_INT, p->prot.nbor,0,MPI_COMM_WORLD, &s);
  
}

void MyMalloc(ArgStruct *p, int bufflen, int soffset, int roffset)
{

/* the MAX() is easier than another if clause and the offsets should be
   small enough for this to never matter */

    p->r_buff = armci_malloc(bufflen+MAX(soffset,roffset));

    if(!p->cache)
      p->s_buff = armci_malloc(bufflen+soffset);

}
void FreeBuff(char *buff1, char* buff2)
{

  if(buff1 != NULL)
    armci_free(buff1);

  if(buff2 != NULL)
    armci_free(buff2);
}