static inline int32_t create_send_tag(ompi_osc_pt2pt_module_t *module) { #if OMPI_HAVE_THREAD_SUPPORT && OPAL_HAVE_ATOMIC_CMPSET_32 int32_t newval, oldval; do { oldval = module->p2p_tag_counter; newval = (oldval + 1) % mca_pml.pml_max_tag; } while (0 == opal_atomic_cmpset_32(&module->p2p_tag_counter, oldval, newval)); return newval; #else int32_t ret; /* no compare and swap - have to lock the module */ OPAL_THREAD_LOCK(&module->p2p_lock); module->p2p_tag_counter = (module->p2p_tag_counter + 1) % mca_pml.pml_max_tag; ret = module->p2p_tag_counter; OPAL_THREAD_UNLOCK(&module->p2p_lock); return ret; #endif }
int oshmem_shmem_finalize(void) { int ret = OSHMEM_SUCCESS; static int32_t finalize_has_already_started = 0; if (opal_atomic_cmpset_32(&finalize_has_already_started, 0, 1) && oshmem_shmem_initialized && !oshmem_shmem_aborted) { /* Should be called first because ompi_mpi_finalize makes orte and opal finalization */ ret = _shmem_finalize(); if ((OSHMEM_SUCCESS == ret) && ompi_mpi_initialized && !ompi_mpi_finalized) { MPI_Comm_free(&oshmem_comm_world); ret = ompi_mpi_finalize(); } if (OSHMEM_SUCCESS == ret) { oshmem_shmem_initialized = false; } } return ret; }
int ompi_mpi_finalize(void) { int ret; static int32_t finalize_has_already_started = 0; opal_list_item_t *item; struct timeval ompistart, ompistop; ompi_rte_collective_t *coll; ompi_proc_t** procs; size_t nprocs; /* Be a bit social if an erroneous program calls MPI_FINALIZE in two different threads, otherwise we may deadlock in ompi_comm_free() (or run into other nasty lions, tigers, or bears) */ if (! opal_atomic_cmpset_32(&finalize_has_already_started, 0, 1)) { /* Note that if we're already finalized, we cannot raise an MPI exception. The best that we can do is write something to stderr. */ char hostname[MAXHOSTNAMELEN]; pid_t pid = getpid(); gethostname(hostname, sizeof(hostname)); opal_show_help("help-mpi-runtime.txt", "mpi_finalize:invoked_multiple_times", true, hostname, pid); return MPI_ERR_OTHER; } ompi_mpiext_fini(); /* Per MPI-2:4.8, we have to free MPI_COMM_SELF before doing anything else in MPI_FINALIZE (to include setting up such that MPI_FINALIZED will return true). */ if (NULL != ompi_mpi_comm_self.comm.c_keyhash) { ompi_attr_delete_all(COMM_ATTR, &ompi_mpi_comm_self, ompi_mpi_comm_self.comm.c_keyhash); OBJ_RELEASE(ompi_mpi_comm_self.comm.c_keyhash); ompi_mpi_comm_self.comm.c_keyhash = NULL; } /* Proceed with MPI_FINALIZE */ ompi_mpi_finalized = true; /* As finalize is the last legal MPI call, we are allowed to force the release * of the user buffer used for bsend, before going anywhere further. */ (void)mca_pml_base_bsend_detach(NULL, NULL); nprocs = 0; procs = ompi_proc_all(&nprocs); MCA_PML_CALL(del_procs(procs, nprocs)); free(procs); #if OMPI_ENABLE_PROGRESS_THREADS == 0 opal_progress_set_event_flag(OPAL_EVLOOP_ONCE | OPAL_EVLOOP_NONBLOCK); #endif /* Redo ORTE calling opal_progress_event_users_increment() during MPI lifetime, to get better latency when not using TCP */ opal_progress_event_users_increment(); /* check to see if we want timing information */ if (ompi_enable_timing != 0 && 0 == OMPI_PROC_MY_NAME->vpid) { gettimeofday(&ompistart, NULL); } /* NOTE: MPI-2.1 requires that MPI_FINALIZE is "collective" across *all* connected processes. This only means that all processes have to call it. It does *not* mean that all connected processes need to synchronize (either directly or indirectly). For example, it is quite easy to construct complicated scenarios where one job is "connected" to another job via transitivity, but have no direct knowledge of each other. Consider the following case: job A spawns job B, and job B later spawns job C. A "connectedness" graph looks something like this: A <--> B <--> C So what are we *supposed* to do in this case? If job A is still connected to B when it calls FINALIZE, should it block until jobs B and C also call FINALIZE? After lengthy discussions many times over the course of this project, the issue was finally decided at the Louisville Feb 2009 meeting: no. Rationale: - "Collective" does not mean synchronizing. It only means that every process call it. Hence, in this scenario, every process in A, B, and C must call FINALIZE. - KEY POINT: if A calls FINALIZE, then it is erroneous for B or C to try to communicate with A again. - Hence, OMPI is *correct* to only effect a barrier across each jobs' MPI_COMM_WORLD before exiting. Specifically, if A calls FINALIZE long before B or C, it's *correct* if A exits at any time (and doesn't notify B or C that it is exiting). - Arguably, if B or C do try to communicate with the now-gone A, OMPI should try to print a nice error ("you tried to communicate with a job that is already gone...") instead of segv or other Badness. However, that is an *extremely* difficult problem -- sure, it's easy for A to tell B that it is finalizing, but how can A tell C? A doesn't even know about C. You'd need to construct a "connected" graph in a distributed fashion, which is fraught with race conditions, etc. Hence, our conclusion is: OMPI is *correct* in its current behavior (of only doing a barrier across its own COMM_WORLD) before exiting. Any problems that occur are as a result of erroneous MPI applications. We *could* tighten up the erroneous cases and ensure that we print nice error messages / don't crash, but that is such a difficult problem that we decided we have many other, much higher priority issues to handle that deal with non-erroneous cases. */ /* wait for everyone to reach this point This is a grpcomm barrier instead of an MPI barrier because an MPI barrier doesn't ensure that all messages have been transmitted before exiting, so the possibility of a stranded message exists. */ coll = OBJ_NEW(ompi_rte_collective_t); coll->id = ompi_process_info.peer_fini_barrier; coll->active = true; if (OMPI_SUCCESS != (ret = ompi_rte_barrier(coll))) { OMPI_ERROR_LOG(ret); return ret; } /* wait for barrier to complete */ OMPI_LAZY_WAIT_FOR_COMPLETION(coll->active); OBJ_RELEASE(coll); /* check for timing request - get stop time and report elapsed time if so */ if (ompi_enable_timing && 0 == OMPI_PROC_MY_NAME->vpid) { gettimeofday(&ompistop, NULL); opal_output(0, "ompi_mpi_finalize[%ld]: time to execute barrier %ld usec", (long)OMPI_PROC_MY_NAME->vpid, (long int)((ompistop.tv_sec - ompistart.tv_sec)*1000000 + (ompistop.tv_usec - ompistart.tv_usec))); } /* * Shutdown the Checkpoint/Restart Mech. */ if (OMPI_SUCCESS != (ret = ompi_cr_finalize())) { OMPI_ERROR_LOG(ret); } /* Shut down any bindings-specific issues: C++, F77, F90 */ /* Remove all memory associated by MPI_REGISTER_DATAREP (per MPI-2:9.5.3, there is no way for an MPI application to *un*register datareps, but we don't want the OMPI layer causing memory leaks). */ while (NULL != (item = opal_list_remove_first(&ompi_registered_datareps))) { OBJ_RELEASE(item); } OBJ_DESTRUCT(&ompi_registered_datareps); /* Remove all F90 types from the hash tables. As the OBJ_DESTRUCT will * call a special destructor able to release predefined types, we can * simply call the OBJ_DESTRUCT on the hash table and all memory will * be correctly released. */ OBJ_DESTRUCT( &ompi_mpi_f90_integer_hashtable ); OBJ_DESTRUCT( &ompi_mpi_f90_real_hashtable ); OBJ_DESTRUCT( &ompi_mpi_f90_complex_hashtable ); /* Free communication objects */ /* free file resources */ if (OMPI_SUCCESS != (ret = ompi_file_finalize())) { return ret; } /* free window resources */ if (OMPI_SUCCESS != (ret = ompi_win_finalize())) { return ret; } if (OMPI_SUCCESS != (ret = ompi_osc_base_finalize())) { return ret; } /* free pml resource */ if(OMPI_SUCCESS != (ret = mca_pml_base_finalize())) { return ret; } /* free communicator resources */ if (OMPI_SUCCESS != (ret = ompi_comm_finalize())) { return ret; } /* free requests */ if (OMPI_SUCCESS != (ret = ompi_request_finalize())) { return ret; } if (OMPI_SUCCESS != (ret = ompi_message_finalize())) { return ret; } /* If requested, print out a list of memory allocated by ALLOC_MEM but not freed by FREE_MEM */ if (0 != ompi_debug_show_mpi_alloc_mem_leaks) { mca_mpool_base_tree_print(); } /* Now that all MPI objects dealing with communications are gone, shut down MCA types having to do with communications */ if (OMPI_SUCCESS != (ret = mca_base_framework_close(&ompi_pml_base_framework) ) ) { OMPI_ERROR_LOG(ret); return ret; } /* shut down buffered send code */ mca_pml_base_bsend_fini(); #if OPAL_ENABLE_FT_CR == 1 /* * Shutdown the CRCP Framework, must happen after PML shutdown */ if (OMPI_SUCCESS != (ret = mca_base_framework_close(&ompi_crcp_base_framework) ) ) { OMPI_ERROR_LOG(ret); return ret; } #endif /* Free secondary resources */ /* free attr resources */ if (OMPI_SUCCESS != (ret = ompi_attr_finalize())) { return ret; } /* free group resources */ if (OMPI_SUCCESS != (ret = ompi_group_finalize())) { return ret; } /* free proc resources */ if ( OMPI_SUCCESS != (ret = ompi_proc_finalize())) { return ret; } /* finalize the pubsub functions */ if (OMPI_SUCCESS != (ret = mca_base_framework_close(&ompi_pubsub_base_framework) ) ) { return ret; } /* finalize the DPM framework */ if ( OMPI_SUCCESS != (ret = mca_base_framework_close(&ompi_dpm_base_framework))) { return ret; } /* free internal error resources */ if (OMPI_SUCCESS != (ret = ompi_errcode_intern_finalize())) { return ret; } /* free error code resources */ if (OMPI_SUCCESS != (ret = ompi_mpi_errcode_finalize())) { return ret; } /* free errhandler resources */ if (OMPI_SUCCESS != (ret = ompi_errhandler_finalize())) { return ret; } /* Free all other resources */ /* free op resources */ if (OMPI_SUCCESS != (ret = ompi_op_finalize())) { return ret; } /* free ddt resources */ if (OMPI_SUCCESS != (ret = ompi_datatype_finalize())) { return ret; } /* free info resources */ if (OMPI_SUCCESS != (ret = ompi_info_finalize())) { return ret; } /* Close down MCA modules */ /* io is opened lazily, so it's only necessary to close it if it was actually opened */ if (0 < ompi_io_base_framework.framework_refcnt) { /* May have been "opened" multiple times. We want it closed now */ ompi_io_base_framework.framework_refcnt = 1; if (OMPI_SUCCESS != mca_base_framework_close(&ompi_io_base_framework)) { return ret; } } (void) mca_base_framework_close(&ompi_topo_base_framework); if (OMPI_SUCCESS != (ret = mca_base_framework_close(&ompi_osc_base_framework))) { return ret; } if (OMPI_SUCCESS != (ret = mca_base_framework_close(&ompi_coll_base_framework))) { return ret; } if (OMPI_SUCCESS != (ret = mca_base_framework_close(&ompi_bml_base_framework))) { return ret; } if (OMPI_SUCCESS != (ret = mca_base_framework_close(&ompi_mpool_base_framework))) { return ret; } if (OMPI_SUCCESS != (ret = mca_base_framework_close(&ompi_rcache_base_framework))) { return ret; } if (OMPI_SUCCESS != (ret = mca_base_framework_close(&ompi_allocator_base_framework))) { return ret; } if (NULL != ompi_mpi_main_thread) { OBJ_RELEASE(ompi_mpi_main_thread); ompi_mpi_main_thread = NULL; } /* Leave the RTE */ if (OMPI_SUCCESS != (ret = ompi_rte_finalize())) { return ret; } /* now close the rte framework */ if (OMPI_SUCCESS != (ret = mca_base_framework_close(&ompi_rte_base_framework) ) ) { OMPI_ERROR_LOG(ret); return ret; } if (OPAL_SUCCESS != (ret = opal_finalize_util())) { return ret; } /* All done */ return MPI_SUCCESS; }
int main(int argc, char *argv[]) { #if OPAL_HAVE_POSIX_THREADS int tid; pthread_t *th; #endif if (argc != 2) { printf("*** Incorrect number of arguments. Skipping test\n"); return 77; } nthreads = atoi(argv[1]); /* first test single-threaded functionality */ /* -- cmpset 32-bit tests -- */ vol32 = 42, old32 = 42, new32 = 50; assert(opal_atomic_cmpset_32(&vol32, old32, new32) == 1); opal_atomic_rmb(); assert(vol32 == new32); vol32 = 42, old32 = 420, new32 = 50; assert(opal_atomic_cmpset_32(&vol32, old32, new32) == 0); opal_atomic_rmb(); assert(vol32 == 42); vol32 = 42, old32 = 42, new32 = 50; assert(opal_atomic_cmpset_acq_32(&vol32, old32, new32) == 1); assert(vol32 == new32); vol32 = 42, old32 = 420, new32 = 50; assert(opal_atomic_cmpset_acq_32(&vol32, old32, new32) == 0); assert(vol32 == 42); vol32 = 42, old32 = 42, new32 = 50; assert(opal_atomic_cmpset_rel_32(&vol32, old32, new32) == 1); opal_atomic_rmb(); assert(vol32 == new32); vol32 = 42, old32 = 420, new32 = 50; assert(opal_atomic_cmpset_rel_32(&vol32, old32, new32) == 0); opal_atomic_rmb(); assert(vol32 == 42); /* -- cmpset 64-bit tests -- */ #if OPAL_HAVE_ATOMIC_MATH_64 vol64 = 42, old64 = 42, new64 = 50; assert(1 == opal_atomic_cmpset_64(&vol64, old64, new64)); opal_atomic_rmb(); assert(new64 == vol64); vol64 = 42, old64 = 420, new64 = 50; assert(opal_atomic_cmpset_64(&vol64, old64, new64) == 0); opal_atomic_rmb(); assert(vol64 == 42); vol64 = 42, old64 = 42, new64 = 50; assert(opal_atomic_cmpset_acq_64(&vol64, old64, new64) == 1); assert(vol64 == new64); vol64 = 42, old64 = 420, new64 = 50; assert(opal_atomic_cmpset_acq_64(&vol64, old64, new64) == 0); assert(vol64 == 42); vol64 = 42, old64 = 42, new64 = 50; assert(opal_atomic_cmpset_rel_64(&vol64, old64, new64) == 1); opal_atomic_rmb(); assert(vol64 == new64); vol64 = 42, old64 = 420, new64 = 50; assert(opal_atomic_cmpset_rel_64(&vol64, old64, new64) == 0); opal_atomic_rmb(); assert(vol64 == 42); #endif /* -- cmpset int tests -- */ volint = 42, oldint = 42, newint = 50; assert(opal_atomic_cmpset(&volint, oldint, newint) == 1); opal_atomic_rmb(); assert(volint ==newint); volint = 42, oldint = 420, newint = 50; assert(opal_atomic_cmpset(&volint, oldint, newint) == 0); opal_atomic_rmb(); assert(volint == 42); volint = 42, oldint = 42, newint = 50; assert(opal_atomic_cmpset_acq(&volint, oldint, newint) == 1); assert(volint == newint); volint = 42, oldint = 420, newint = 50; assert(opal_atomic_cmpset_acq(&volint, oldint, newint) == 0); assert(volint == 42); volint = 42, oldint = 42, newint = 50; assert(opal_atomic_cmpset_rel(&volint, oldint, newint) == 1); opal_atomic_rmb(); assert(volint == newint); volint = 42, oldint = 420, newint = 50; assert(opal_atomic_cmpset_rel(&volint, oldint, newint) == 0); opal_atomic_rmb(); assert(volint == 42); /* -- cmpset ptr tests -- */ volptr = (void *) 42, oldptr = (void *) 42, newptr = (void *) 50; assert(opal_atomic_cmpset_ptr(&volptr, oldptr, newptr) == 1); opal_atomic_rmb(); assert(volptr == newptr); volptr = (void *) 42, oldptr = (void *) 420, newptr = (void *) 50; assert(opal_atomic_cmpset_ptr(&volptr, oldptr, newptr) == 0); opal_atomic_rmb(); assert(volptr == (void *) 42); volptr = (void *) 42, oldptr = (void *) 42, newptr = (void *) 50; assert(opal_atomic_cmpset_acq_ptr(&volptr, oldptr, newptr) == 1); assert(volptr == newptr); volptr = (void *) 42, oldptr = (void *) 420, newptr = (void *) 50; assert(opal_atomic_cmpset_acq_ptr(&volptr, oldptr, newptr) == 0); assert(volptr == (void *) 42); volptr = (void *) 42, oldptr = (void *) 42, newptr = (void *) 50; assert(opal_atomic_cmpset_rel_ptr(&volptr, oldptr, newptr) == 1); opal_atomic_rmb(); assert(volptr == newptr); volptr = (void *) 42, oldptr = (void *) 420, newptr = (void *) 50; assert(opal_atomic_cmpset_rel_ptr(&volptr, oldptr, newptr) == 0); opal_atomic_rmb(); assert(volptr == (void *) 42); /* -- add_32 tests -- */ val32 = 42; assert(opal_atomic_add_32(&val32, 5) == (42 + 5)); opal_atomic_rmb(); assert((42 + 5) == val32); /* -- add_64 tests -- */ #if OPAL_HAVE_ATOMIC_MATH_64 val64 = 42; assert(opal_atomic_add_64(&val64, 5) == (42 + 5)); opal_atomic_rmb(); assert((42 + 5) == val64); #endif /* -- add_int tests -- */ valint = 42; opal_atomic_add(&valint, 5); opal_atomic_rmb(); assert((42 + 5) == valint); /* threaded tests */ val32 = 0; #if OPAL_HAVE_ATOMIC_MATH_64 val64 = 0ul; #endif valint = 0; /* -- create the thread set -- */ #if OPAL_HAVE_POSIX_THREADS th = (pthread_t *) malloc(nthreads * sizeof(pthread_t)); if (!th) { perror("malloc"); exit(EXIT_FAILURE); } for (tid = 0; tid < nthreads; tid++) { if (pthread_create(&th[tid], NULL, thread_main, (void *) (unsigned long) tid) != 0) { perror("pthread_create"); exit(EXIT_FAILURE); } } /* -- wait for the thread set to finish -- */ for (tid = 0; tid < nthreads; tid++) { void *thread_return; if (pthread_join(th[tid], &thread_return) != 0) { perror("pthread_join"); exit(EXIT_FAILURE); } } free(th); opal_atomic_rmb(); assert((5 * nthreads * nreps) == val32); #if OPAL_HAVE_ATOMIC_MATH_64 opal_atomic_rmb(); assert((5 * nthreads * nreps) == val64); #endif opal_atomic_rmb(); assert((5 * nthreads * nreps) == valint); #endif return 0; }
/* progress */ int mca_btl_ugni_smsg_process (mca_btl_base_endpoint_t *ep) { mca_btl_active_message_callback_t *reg; mca_btl_ugni_base_frag_t frag; mca_btl_base_segment_t seg; bool disconnect = false; uintptr_t data_ptr; gni_return_t rc; uint32_t len; int count = 0; if (!opal_atomic_cmpset_32 (&ep->smsg_progressing, 0, 1)) { /* already progressing (we can't support reentry here) */ return 0; } /* per uGNI documentation we loop until the mailbox is empty */ do { uint8_t tag = GNI_SMSG_ANY_TAG; rc = GNI_SmsgGetNextWTag (ep->smsg_ep_handle, (void **) &data_ptr, &tag); if (GNI_RC_NOT_DONE == rc) { BTL_VERBOSE(("no smsg message waiting. rc = %d", rc)); ep->smsg_progressing = 0; return count; } if (OPAL_UNLIKELY(GNI_RC_SUCCESS != rc)) { fprintf (stderr, "Unhandled Smsg error: %d\n", rc); assert (0); return OMPI_ERROR; } if (OPAL_UNLIKELY(0 == data_ptr)) { BTL_ERROR(("null data ptr!")); assert (0); return OMPI_ERROR; } count++; BTL_VERBOSE(("got smsg fragment. tag = %d\n", tag)); switch (tag) { case MCA_BTL_UGNI_TAG_SEND: frag.hdr.send = ((mca_btl_ugni_send_frag_hdr_t *) data_ptr)[0]; tag = frag.hdr.send.lag >> 24; len = frag.hdr.send.lag & 0x00ffffff; BTL_VERBOSE(("received smsg fragment. hdr = {len = %u, tag = %d}", len, tag)); reg = mca_btl_base_active_message_trigger + tag; frag.base.des_dst = &seg; frag.base.des_dst_cnt = 1; seg.seg_addr.pval = (void *)((uintptr_t)data_ptr + sizeof (mca_btl_ugni_send_frag_hdr_t)); seg.seg_len = len; assert (NULL != reg->cbfunc); reg->cbfunc(&ep->btl->super, tag, &(frag.base), reg->cbdata); break; case MCA_BTL_UGNI_TAG_GET_INIT: frag.hdr.eager_ex = ((mca_btl_ugni_eager_ex_frag_hdr_t *) data_ptr)[0]; mca_btl_ugni_start_eager_get (ep, frag.hdr.eager_ex, NULL); break; case MCA_BTL_UGNI_TAG_RDMA_COMPLETE: frag.hdr.rdma = ((mca_btl_ugni_rdma_frag_hdr_t *) data_ptr)[0]; mca_btl_ugni_frag_complete (frag.hdr.rdma.ctx, OMPI_SUCCESS); break; case MCA_BTL_UGNI_TAG_DISCONNECT: /* remote endpoint has disconnected */ disconnect = true; break; default: BTL_ERROR(("unknown tag %d\n", tag)); break; } rc = GNI_SmsgRelease (ep->smsg_ep_handle); if (OPAL_UNLIKELY(GNI_RC_SUCCESS != rc)) { BTL_ERROR(("Smsg release failed! rc = %d", rc)); return OMPI_ERROR; } } while (!disconnect); ep->smsg_progressing = false; /* disconnect if we get here */ mca_btl_ugni_ep_disconnect (ep, false); return count; }