1 #if HAVE_CONFIG_H
2 #   include "config.h"
3 #endif
4 
5 /* $Id: shmalloc.c,v 1.10 2002-06-20 23:34:17 vinod Exp $ */
6 #include <stdio.h>
7 #include <string.h>
8 #include "armcip.h"
9 #include "message.h"
10 #include "kr_malloc.h"
11 
12 static long *offset_arr;
13 
armci_shmalloc_exchange_offsets(context_t * ctx_local)14 void armci_shmalloc_exchange_offsets(context_t *ctx_local)
15 {
16     void **ptr_arr;
17     void *ptr;
18     armci_size_t bytes = 128;
19     int i;
20 
21     ptr_arr    = (void**)malloc(armci_nproc*sizeof(void*));
22     offset_arr = (long*) malloc(armci_nproc*sizeof(long));
23     if(!ptr_arr || !offset_arr) armci_die("armci_shmalloc_get_offsets: malloc failed", 0);
24 
25     /* get memory with same size on all procs */
26     ptr = kr_malloc(bytes, ctx_local);
27     if(!ptr) armci_die("armci_shmalloc_get_offsets: kr_malloc failed",bytes);
28 
29     bzero((char*)ptr_arr,armci_nproc*sizeof(void*));
30     ptr_arr[armci_me] = ptr;
31 
32     /* now combine individual addresses into a single array */
33     armci_exchange_address(ptr_arr, armci_nproc);
34 
35     /* identify offets */
36     for (i=0; i<armci_nproc; i++)
37     {
38        offset_arr[i] = (long) ((char*)ptr - (char*)ptr_arr[i]);
39     }
40 
41     /* release memory */
42     kr_free(ptr, ctx_local);
43 }
44 
armci_shmalloc_exchange_address(void ** ptr_arr)45 void armci_shmalloc_exchange_address(void **ptr_arr)
46 {
47     int i;
48 
49     /* now combine individual addresses into a single array */
50     armci_exchange_address(ptr_arr, armci_nproc);
51 
52     /* since shmalloc may not give symmetric addresses (especially on Linux),
53      * adjust addresses based on offset calculated during initialization */
54     for (i=0; i<armci_nproc; i++)
55     {
56        ptr_arr[i] = (char*)ptr_arr[i] + offset_arr[i];
57     }
58 }
59 
60 #ifdef MSG_COMMS_MPI
61 
62 extern int ARMCI_Absolute_id(ARMCI_Group *group,int group_rank);
63 
64 /* group based exchange address */
armci_shmalloc_exchange_address_grp(void ** ptr_arr,ARMCI_Group * group)65 void armci_shmalloc_exchange_address_grp(void **ptr_arr, ARMCI_Group *group)
66 {
67     int i, world_rank;
68     int grp_nproc;
69 
70     ARMCI_Group_size(group, &grp_nproc);
71 
72     /* now combine individual addresses into a single array */
73     armci_exchange_address_grp(ptr_arr, grp_nproc, group);
74 
75     /* since shmalloc may not give symmetric addresses (especially on Linux),
76      * adjust addresses based on offset calculated during initialization */
77     for (i=0; i<grp_nproc; i++)
78     {
79        world_rank = ARMCI_Absolute_id(group,i);
80        ptr_arr[i] = (char*)ptr_arr[i] + offset_arr[world_rank];
81     }
82 }
83 #endif
84 
85 /* get the remote process's pointer */
armci_shmalloc_remote_addr(void * ptr,int proc)86 void* armci_shmalloc_remote_addr(void *ptr, int proc)
87 {
88     return (void*)((char*)ptr - offset_arr[proc]);
89 }
90