12
12
13
13
static int init_shm_initialized ;
14
14
15
+ int MPIDU_Init_shm_local_size ;
16
+ int MPIDU_Init_shm_local_rank ;
17
+
15
18
#ifdef ENABLE_NO_LOCAL
16
19
/* shared memory disabled, just stubs */
17
20
@@ -55,8 +58,6 @@ typedef struct Init_shm_barrier {
55
58
MPL_atomic_int_t wait ;
56
59
} Init_shm_barrier_t ;
57
60
58
- static int local_size ;
59
- static int my_local_rank ;
60
61
static MPIDU_shm_seg_t memory ;
61
62
static Init_shm_barrier_t * barrier ;
62
63
static void * baseaddr ;
@@ -88,12 +89,12 @@ static int Init_shm_barrier(void)
88
89
89
90
MPIR_FUNC_ENTER ;
90
91
91
- if (local_size == 1 )
92
+ if (MPIDU_Init_shm_local_size == 1 )
92
93
goto fn_exit ;
93
94
94
95
MPIR_ERR_CHKINTERNAL (!barrier_init , mpi_errno , "barrier not initialized" );
95
96
96
- if (MPL_atomic_fetch_add_int (& barrier -> val , 1 ) == local_size - 1 ) {
97
+ if (MPL_atomic_fetch_add_int (& barrier -> val , 1 ) == MPIDU_Init_shm_local_size - 1 ) {
97
98
MPL_atomic_store_int (& barrier -> val , 0 );
98
99
MPL_atomic_store_int (& barrier -> wait , 1 - sense );
99
100
} else {
@@ -112,39 +113,35 @@ static int Init_shm_barrier(void)
112
113
int MPIDU_Init_shm_init (void )
113
114
{
114
115
int mpi_errno = MPI_SUCCESS , mpl_err = 0 ;
115
- MPIR_CHKPMEM_DECL ();
116
116
MPIR_CHKLMEM_DECL ();
117
117
118
118
MPIR_FUNC_ENTER ;
119
119
120
- local_size = MPIR_Process .local_size ;
121
- my_local_rank = MPIR_Process .local_rank ;
122
-
123
- size_t segment_len = MPIDU_SHM_CACHE_LINE_LEN + sizeof (MPIDU_Init_shm_block_t ) * local_size ;
124
-
125
- char * serialized_hnd = NULL ;
126
- int serialized_hnd_size = 0 ;
120
+ MPIDU_Init_shm_local_size = MPIR_Process .local_size ;
121
+ MPIDU_Init_shm_local_rank = MPIR_Process .local_rank ;
127
122
128
- mpl_err = MPL_shm_hnd_init ( & ( memory . hnd ));
129
- MPIR_ERR_CHKANDJUMP ( mpl_err , mpi_errno , MPI_ERR_OTHER , "**alloc_shar_mem" );
123
+ if ( MPIDU_Init_shm_local_size == 1 ) {
124
+ /* We'll special case this trivial case */
130
125
131
- memory .segment_len = segment_len ;
126
+ /* All processes need call MPIR_pmi_bcast. This is because we may need call MPIR_pmi_barrier
127
+ * inside depend on PMI versions, and all processes need participate.
128
+ */
129
+ int dummy ;
130
+ mpi_errno = MPIR_pmi_bcast (& dummy , sizeof (int ), MPIR_PMI_DOMAIN_LOCAL );
131
+ MPIR_ERR_CHECK (mpi_errno );
132
+ } else {
133
+ size_t segment_len = MPIDU_SHM_CACHE_LINE_LEN +
134
+ sizeof (MPIDU_Init_shm_block_t ) * MPIDU_Init_shm_local_size ;
132
135
133
- if ( local_size == 1 ) {
134
- char * addr ;
136
+ char * serialized_hnd = NULL ;
137
+ int serialized_hnd_size = 0 ;
135
138
136
- MPIR_CHKPMEM_MALLOC (addr , segment_len + MPIDU_SHM_CACHE_LINE_LEN , MPL_MEM_SHM );
139
+ mpl_err = MPL_shm_hnd_init (& (memory .hnd ));
140
+ MPIR_ERR_CHKANDJUMP (mpl_err , mpi_errno , MPI_ERR_OTHER , "**alloc_shar_mem" );
137
141
138
- memory .base_addr = addr ;
139
- baseaddr =
140
- (char * ) (((uintptr_t ) addr + (uintptr_t ) MPIDU_SHM_CACHE_LINE_LEN - 1 ) &
141
- (~((uintptr_t ) MPIDU_SHM_CACHE_LINE_LEN - 1 )));
142
- memory .symmetrical = 0 ;
142
+ memory .segment_len = segment_len ;
143
143
144
- mpi_errno = Init_shm_barrier_init (TRUE);
145
- MPIR_ERR_CHECK (mpi_errno );
146
- } else {
147
- if (my_local_rank == 0 ) {
144
+ if (MPIDU_Init_shm_local_rank == 0 ) {
148
145
/* root prepare shm segment */
149
146
mpl_err = MPL_shm_seg_create_and_attach (memory .hnd , memory .segment_len ,
150
147
(void * * ) & (memory .base_addr ), 0 );
@@ -164,15 +161,13 @@ int MPIDU_Init_shm_init(void)
164
161
serialized_hnd_size = MPIR_pmi_max_val_size ();
165
162
MPIR_CHKLMEM_MALLOC (serialized_hnd , serialized_hnd_size );
166
163
}
167
- }
168
- /* All processes need call MPIR_pmi_bcast. This is because we may need call MPIR_pmi_barrier
169
- * inside depend on PMI versions, and all processes need participate.
170
- */
171
- mpi_errno = MPIR_pmi_bcast (serialized_hnd , serialized_hnd_size , MPIR_PMI_DOMAIN_LOCAL );
172
- MPIR_ERR_CHECK (mpi_errno );
173
- if (local_size != 1 ) {
174
- MPIR_Assert (local_size > 1 );
175
- if (my_local_rank > 0 ) {
164
+ /* All processes need call MPIR_pmi_bcast. This is because we may need call MPIR_pmi_barrier
165
+ * inside depend on PMI versions, and all processes need participate.
166
+ */
167
+ mpi_errno = MPIR_pmi_bcast (serialized_hnd , serialized_hnd_size , MPIR_PMI_DOMAIN_LOCAL );
168
+ MPIR_ERR_CHECK (mpi_errno );
169
+
170
+ if (MPIDU_Init_shm_local_rank > 0 ) {
176
171
/* non-root attach shm segment */
177
172
mpl_err = MPL_shm_hnd_deserialize (memory .hnd , serialized_hnd , strlen (serialized_hnd ));
178
173
MPIR_ERR_CHKANDJUMP (mpl_err , mpi_errno , MPI_ERR_OTHER , "**alloc_shar_mem" );
@@ -188,17 +183,17 @@ int MPIDU_Init_shm_init(void)
188
183
mpi_errno = Init_shm_barrier ();
189
184
MPIR_ERR_CHECK (mpi_errno );
190
185
191
- if (my_local_rank == 0 ) {
186
+ if (MPIDU_Init_shm_local_rank == 0 ) {
192
187
/* memory->hnd no longer needed */
193
188
mpl_err = MPL_shm_seg_remove (memory .hnd );
194
189
MPIR_ERR_CHKANDJUMP (mpl_err , mpi_errno , MPI_ERR_OTHER , "**remove_shar_mem" );
195
190
}
196
191
197
192
baseaddr = memory .base_addr + MPIDU_SHM_CACHE_LINE_LEN ;
198
193
memory .symmetrical = 0 ;
199
- }
200
194
201
- mpi_errno = Init_shm_barrier ();
195
+ mpi_errno = Init_shm_barrier ();
196
+ }
202
197
203
198
init_shm_initialized = 1 ;
204
199
@@ -207,7 +202,6 @@ int MPIDU_Init_shm_init(void)
207
202
MPIR_FUNC_EXIT ;
208
203
return mpi_errno ;
209
204
fn_fail :
210
- MPIR_CHKPMEM_REAP ();
211
205
goto fn_exit ;
212
206
}
213
207
@@ -217,16 +211,12 @@ int MPIDU_Init_shm_finalize(void)
217
211
218
212
MPIR_FUNC_ENTER ;
219
213
220
- if (!init_shm_initialized ) {
214
+ if (!init_shm_initialized || MPIDU_Init_shm_local_size == 1 ) {
221
215
goto fn_exit ;
222
216
}
223
217
224
- if (local_size == 1 )
225
- MPL_free (memory .base_addr );
226
- else {
227
- mpl_err = MPL_shm_seg_detach (memory .hnd , (void * * ) & (memory .base_addr ), memory .segment_len );
228
- MPIR_ERR_CHKANDJUMP (mpl_err , mpi_errno , MPI_ERR_OTHER , "**detach_shar_mem" );
229
- }
218
+ mpl_err = MPL_shm_seg_detach (memory .hnd , (void * * ) & (memory .base_addr ), memory .segment_len );
219
+ MPIR_ERR_CHKANDJUMP (mpl_err , mpi_errno , MPI_ERR_OTHER , "**detach_shar_mem" );
230
220
231
221
MPL_shm_hnd_finalize (& (memory .hnd ));
232
222
@@ -245,7 +235,9 @@ int MPIDU_Init_shm_barrier(void)
245
235
246
236
MPIR_FUNC_ENTER ;
247
237
248
- mpi_errno = Init_shm_barrier ();
238
+ if (MPIDU_Init_shm_local_size > 1 ) {
239
+ mpi_errno = Init_shm_barrier ();
240
+ }
249
241
250
242
MPIR_FUNC_EXIT ;
251
243
@@ -258,8 +250,11 @@ int MPIDU_Init_shm_put(void *orig, size_t len)
258
250
259
251
MPIR_FUNC_ENTER ;
260
252
261
- MPIR_Assert (len <= sizeof (MPIDU_Init_shm_block_t ));
262
- MPIR_Memcpy ((char * ) baseaddr + my_local_rank * sizeof (MPIDU_Init_shm_block_t ), orig , len );
253
+ if (MPIDU_Init_shm_local_size > 1 ) {
254
+ MPIR_Assert (len <= sizeof (MPIDU_Init_shm_block_t ));
255
+ MPIR_Memcpy ((char * ) baseaddr + MPIDU_Init_shm_local_rank * sizeof (MPIDU_Init_shm_block_t ),
256
+ orig , len );
257
+ }
263
258
264
259
MPIR_FUNC_EXIT ;
265
260
@@ -272,7 +267,10 @@ int MPIDU_Init_shm_get(int local_rank, size_t len, void *target)
272
267
273
268
MPIR_FUNC_ENTER ;
274
269
275
- MPIR_Assert (local_rank < local_size && len <= sizeof (MPIDU_Init_shm_block_t ));
270
+ /* a single process should not get its own put */
271
+ MPIR_Assert (MPIDU_Init_shm_local_size > 1 );
272
+
273
+ MPIR_Assert (local_rank < MPIDU_Init_shm_local_size && len <= sizeof (MPIDU_Init_shm_block_t ));
276
274
MPIR_Memcpy (target , (char * ) baseaddr + local_rank * sizeof (MPIDU_Init_shm_block_t ), len );
277
275
278
276
MPIR_FUNC_EXIT ;
@@ -286,7 +284,10 @@ int MPIDU_Init_shm_query(int local_rank, void **target_addr)
286
284
287
285
MPIR_FUNC_ENTER ;
288
286
289
- MPIR_Assert (local_rank < local_size );
287
+ /* a single process should not get its own put */
288
+ MPIR_Assert (MPIDU_Init_shm_local_size > 1 );
289
+
290
+ MPIR_Assert (local_rank < MPIDU_Init_shm_local_size );
290
291
* target_addr = (char * ) baseaddr + local_rank * sizeof (MPIDU_Init_shm_block_t );
291
292
292
293
MPIR_FUNC_EXIT ;
0 commit comments