Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential races in Pthread gather/scatter. #297

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/qvi-pthread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ qvi_pthread_group::m_subgroup_info(
int rc = QV_SUCCESS;
const int master_rank = 0;
const int my_rank = rank();
// TODO(skg)
//qvi_log_debug("color={}, key={}, rank={}", color, key, my_rank);
// Gather colors and keys from ALL threads in the parent group.
// NOTE: this (i.e., the caller) is a member of the parent group).
m_ckrs[my_rank].color = color;
Expand Down Expand Up @@ -277,9 +275,10 @@ qvi_pthread_group::split(
const qvi_group_id_t mygid = m_subgroup_gids[sginfo.index];
ichild = m_context->groupid2thgroup.at(mygid);
}
// Now we can check if errors happened above.
if (qvi_unlikely(rc != QV_SUCCESS)) goto out;

rc = m_finish_init_by_all_threads(ichild);

out:
if (qvi_unlikely(rc != QV_SUCCESS)) {
qvi_delete(&ichild);
Expand All @@ -295,16 +294,22 @@ qvi_pthread_group::gather(
qvi_bbuff_alloc_type_t *alloc_type,
qvi_bbuff ***rxbuffs
) {
const int rc = qvi_bbuff_copy(*txbuff, m_data_g[rank()]);
const int myrank = rank();
// I'm not sure why this barrier is needed, but it seems to help...
barrier();
int rc = QV_SUCCESS;
{
std::lock_guard<std::mutex> guard(m_mutex);
rc = qvi_bbuff_copy(*txbuff, m_data_g[myrank]);
*alloc_type = QVI_BBUFF_ALLOC_SHARED_GLOBAL;
}
// Need to ensure that all threads have contributed to m_data_g
pthread_barrier_wait(&m_barrier);
*alloc_type = QVI_BBUFF_ALLOC_SHARED_GLOBAL;
barrier();

if (qvi_unlikely(rc != QV_SUCCESS)) {
*rxbuffs = nullptr;
return QV_ERR_INTERNAL;
}

*rxbuffs = m_data_g;
return rc;
}
Expand All @@ -315,16 +320,20 @@ qvi_pthread_group::scatter(
int rootid,
qvi_bbuff **rxbuff
) {
int rc = QV_SUCCESS;
const int myrank = rank();
qvi_bbuff *mybbuff = nullptr;

if (rootid == myrank) {
*m_data_s = txbuffs;
}
pthread_barrier_wait(&m_barrier);

qvi_bbuff *mybbuff = nullptr;
const int rc = qvi_bbuff_dup( *((*m_data_s)[myrank]), &mybbuff);
pthread_barrier_wait(&m_barrier);
barrier();
{
std::lock_guard<std::mutex> guard(m_mutex);
rc = qvi_bbuff_dup(*((*m_data_s)[myrank]), &mybbuff);
}
barrier();

if (qvi_unlikely(rc != QV_SUCCESS)) {
qvi_bbuff_delete(&mybbuff);
Expand Down
4 changes: 0 additions & 4 deletions src/qvi-scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,6 @@ qv_scope::split(
);
rc = chwsplit.split(&colorp, &hwpool);
if (rc != QV_SUCCESS) goto out;
// TODO(skg) In the threaded case it looks like there is a race here or
// something else is wrong. See colorp.
//qvi_log_debug("SCOPE SPLIT npieces={}, color={} colorp={}", npieces, color, colorp);

// Split underlying group. Notice the use of colorp here.
rc = m_group->split(
colorp, m_group->rank(), &group
Expand Down