@@ -138,6 +138,124 @@ int MPIDU_Init_shm_alloc(size_t len, void **ptr)
138138 /* --END ERROR HANDLING-- */
139139}
140140
141+ int MPIDU_Init_shm_comm_alloc (MPIR_Comm * comm , size_t len , void * * ptr )
142+ {
143+ int mpi_errno = MPI_SUCCESS , mpl_err = 0 ;
144+ void * current_addr ;
145+ size_t segment_len = len ;
146+ struct memory_seg * memory = NULL ;
147+ memory_list_t * memory_node = NULL ;
148+ MPIR_CHKPMEM_DECL ();
149+
150+ MPIR_FUNC_ENTER ;
151+
152+ if (MPIDU_Init_shm_local_size == 1 ) {
153+ * ptr = MPL_aligned_alloc (MPL_CACHELINE_SIZE , len , MPL_MEM_SHM );
154+ MPIR_ERR_CHKANDJUMP (!* ptr , mpi_errno , MPI_ERR_OTHER , "**nomem" );
155+ goto fn_exit ;
156+ }
157+
158+ MPIR_Comm * node_comm = comm -> node_comm ;
159+ bool is_root ;
160+ if (node_comm ) {
161+ is_root = (node_comm -> rank == 0 );
162+ } else {
163+ is_root = true;
164+ }
165+
166+ MPIR_Assert (segment_len > 0 );
167+ MPIR_CHKPMEM_MALLOC (memory , sizeof (* memory ), MPL_MEM_OTHER );
168+ mpl_err = MPL_shm_hnd_init (& (memory -> hnd ));
169+ MPIR_ERR_CHKANDJUMP (mpl_err , mpi_errno , MPI_ERR_OTHER , "**alloc_shar_mem" );
170+
171+ memory -> segment_len = segment_len ;
172+
173+ char * serialized_hnd = NULL ;
174+ int serialized_hnd_size = 0 ;
175+ char serialized_hnd_buffer [MPIDU_INIT_SHM_BLOCK_SIZE ];
176+ bool need_attach ;
177+ bool need_remove ;
178+ if (is_root ) {
179+ if (!MPIDU_Init_shm_atomic_key_exist ()) {
180+ /* We need to create the shm segment */
181+ mpl_err = MPL_shm_seg_create_and_attach (memory -> hnd , memory -> segment_len ,
182+ (void * * ) & (memory -> base_addr ), 0 );
183+ MPIR_ERR_CHKANDJUMP (mpl_err , mpi_errno , MPI_ERR_OTHER , "**alloc_shar_mem" );
184+
185+ mpl_err = MPL_shm_hnd_get_serialized_by_ref (memory -> hnd , & serialized_hnd );
186+ MPIR_ERR_CHKANDJUMP (mpl_err , mpi_errno , MPI_ERR_OTHER , "**alloc_shar_mem" );
187+ serialized_hnd_size = strlen (serialized_hnd ) + 1 ; /* add 1 for null char */
188+
189+ MPIDU_Init_shm_atomic_put (serialized_hnd , serialized_hnd_size );
190+ need_attach = false;
191+ need_remove = true;
192+ } else {
193+ /* Just retrieve the existing serialized handle */
194+ MPIDU_Init_shm_atomic_get (serialized_hnd_buffer , MPIDU_INIT_SHM_BLOCK_SIZE );
195+ serialized_hnd = serialized_hnd_buffer ;
196+ serialized_hnd_size = strlen (serialized_hnd ) + 1 ; /* add 1 for null char */
197+ need_attach = true;
198+ need_remove = false;
199+ }
200+ MPIR_Assert (serialized_hnd_size <= MPIDU_INIT_SHM_BLOCK_SIZE );
201+ if (node_comm ) {
202+ mpi_errno = MPIR_Bcast_impl (serialized_hnd , MPIDU_INIT_SHM_BLOCK_SIZE ,
203+ MPIR_BYTE_INTERNAL , 0 , node_comm , MPIR_ERR_NONE );
204+ MPIR_ERR_CHECK (mpi_errno );
205+ }
206+ } else {
207+ mpi_errno = MPIR_Bcast_impl (serialized_hnd_buffer , MPIDU_INIT_SHM_BLOCK_SIZE ,
208+ MPIR_BYTE_INTERNAL , 0 , node_comm , MPIR_ERR_NONE );
209+ MPIR_ERR_CHECK (mpi_errno );
210+ serialized_hnd = serialized_hnd_buffer ;
211+ serialized_hnd_size = strlen (serialized_hnd ) + 1 ; /* add 1 for null char */
212+ need_attach = true;
213+ need_remove = false;
214+ }
215+ if (need_attach ) {
216+ mpl_err = MPL_shm_hnd_deserialize (memory -> hnd , serialized_hnd , strlen (serialized_hnd ));
217+ MPIR_ERR_CHKANDJUMP (mpl_err , mpi_errno , MPI_ERR_OTHER , "**alloc_shar_mem" );
218+
219+ mpl_err = MPL_shm_seg_attach (memory -> hnd , memory -> segment_len ,
220+ (void * * ) & memory -> base_addr , 0 );
221+ MPIR_ERR_CHKANDJUMP (mpl_err , mpi_errno , MPI_ERR_OTHER , "**attach_shar_mem" );
222+ }
223+
224+ if (node_comm ) {
225+ mpi_errno = MPIR_Barrier_impl (node_comm , MPIR_ERR_NONE );
226+ MPIR_ERR_CHECK (mpi_errno );
227+ }
228+ if (need_remove ) {
229+ /* memory->hnd no longer needed */
230+ mpl_err = MPL_shm_seg_remove (memory -> hnd );
231+ MPIR_ERR_CHKANDJUMP (mpl_err , mpi_errno , MPI_ERR_OTHER , "**remove_shar_mem" );
232+ }
233+
234+ current_addr = memory -> base_addr ;
235+ memory -> symmetrical = false;
236+ memory -> is_shm = true;
237+
238+ mpi_errno = check_alloc (memory );
239+ MPIR_ERR_CHECK (mpi_errno );
240+
241+ /* assign sections of the shared memory segment to their pointers */
242+ * ptr = current_addr ;
243+
244+ MPIR_CHKPMEM_MALLOC (memory_node , sizeof (* memory_node ), MPL_MEM_OTHER );
245+ memory_node -> ptr = * ptr ;
246+ memory_node -> memory = memory ;
247+ LL_APPEND (memory_head , memory_tail , memory_node );
248+
249+ fn_exit :
250+ MPIR_FUNC_EXIT ;
251+ return mpi_errno ;
252+ fn_fail :
253+ MPL_shm_seg_remove (memory -> hnd );
254+ MPL_shm_hnd_finalize (& (memory -> hnd ));
255+ MPIR_CHKPMEM_REAP ();
256+ goto fn_exit ;
257+ }
258+
141259/* MPIDU_SHM_Seg_free() free the shared memory segment */
142260int MPIDU_Init_shm_free (void * ptr )
143261{
0 commit comments