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

Address some TODO items. #236

Merged
merged 1 commit into from
Jul 21, 2024
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
15 changes: 5 additions & 10 deletions src/quo-vadis-pthread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ qv_pthread_scope_split(
int nthreads,
qv_scope_t ***subscope
) {
const bool invld_args = !scope || npieces < 0 ||
!color_array || nthreads < 0 || !subscope;
const bool invld_args = !scope || npieces < 0 || !color_array ||
nthreads < 0 || !subscope;
if (qvi_unlikely(invld_args)) {
return QV_ERR_INVLD_ARG;
}
Expand Down Expand Up @@ -107,19 +107,14 @@ qv_pthread_create(
// Since this is meant to behave similarly to
// pthread_create(), return a reasonable errno.
if (qvi_unlikely(rc != QV_SUCCESS)) return ENOMEM;
// TODO(skg) Cleanup.

auto group = dynamic_cast<qvi_group_pthread_t *>(qvi_scope_group_get(scope));
qvi_pthread_group_pthread_create_args_s *cargs = nullptr;
rc = qvi_new(&cargs);
rc = qvi_new(&cargs, group->thgroup, qvi_pthread_routine, arg_ptr);
if (qvi_unlikely(rc != QV_SUCCESS)) {
qvi_delete(&arg_ptr);
return ENOMEM;
}
// TODO(skg) Cleanup.
auto gt = dynamic_cast<qvi_group_pthread_t *>(qvi_scope_group_get(scope));
cargs->group = gt->thgroup;
cargs->th_routine = qvi_pthread_routine;
cargs->th_routine_argp = arg_ptr;

return pthread_create(
thread, attr, qvi_pthread_group_s::call_first_from_pthread_create, cargs
);
Expand Down
24 changes: 17 additions & 7 deletions src/qvi-pthread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ void *
qvi_pthread_group_s::call_first_from_pthread_create(
void *arg
) {
// TODO(skg) Cleanup.
const pid_t mytid = qvi_gettid();
auto args = (qvi_pthread_group_pthread_create_args_s *)arg;
auto group = args->group;
auto thread_routine = args->th_routine;
auto th_routine_argp = args->th_routine_argp;
qvi_pthread_group_t *const group = args->group;
const qvi_pthread_routine_fun_ptr_t thread_routine = args->throutine;
void *const th_routine_argp = args->throutine_argp;
// Let the threads add their TIDs to the vector.
{
std::lock_guard<std::mutex> guard(group->m_mutex);
group->m_tids.push_back(qvi_gettid());
group->m_tids.push_back(mytid);
}
// Make sure they all contribute before continuing.
pthread_barrier_wait(&group->m_barrier);
// Elect one thread to be the worker.
bool worker = false;
{
std::lock_guard<std::mutex> guard(group->m_mutex);
worker = group->m_tids.at(0) == qvi_gettid();
worker = (group->m_tids.at(0) == mytid);
}
// The worker populates the TID to rank mapping, while the others wait.
if (worker) {
Expand All @@ -57,7 +57,7 @@ qvi_pthread_group_s::call_first_from_pthread_create(
qvi_task_t *task = nullptr;
const int rc = qvi_new(&task);
if (qvi_unlikely(rc != QV_SUCCESS)) throw qvi_runtime_error();
group->m_tid2task.insert({qvi_gettid(), task});
group->m_tid2task.insert({mytid, task});
}
// Make sure they all finish before continuing.
pthread_barrier_wait(&group->m_barrier);
Expand All @@ -76,6 +76,16 @@ qvi_pthread_group_s::~qvi_pthread_group_s(void)
pthread_barrier_destroy(&m_barrier);
}

int
qvi_pthread_group_s::barrier(void)
{
const int rc = pthread_barrier_wait(&(m_barrier));
if (qvi_unlikely((rc != 0) && (rc != PTHREAD_BARRIER_SERIAL_THREAD))) {
return QV_ERR_INTERNAL;
}
return QV_SUCCESS;
}

/*
* vim: ft=cpp ts=4 sts=4 sw=4 expandtab
*/
24 changes: 16 additions & 8 deletions src/qvi-pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,28 @@
#include "qvi-common.h"
#include "qvi-utils.h"

// TODO(skg) Rename
typedef void *(*qvi_pthread_routine_fun_ptr_t)(void *);

struct qvi_pthread_group_s;
typedef struct qvi_pthread_group_s qvi_pthread_group_t;

struct qvi_pthread_group_pthread_create_args_s {
/** Thread group. */
qvi_pthread_group_t *group = nullptr;
qvi_pthread_routine_fun_ptr_t th_routine = nullptr;
void *th_routine_argp = nullptr;
/** The routine to call after group construction. */
qvi_pthread_routine_fun_ptr_t throutine = nullptr;
/** Thread routine arguments. */
void *throutine_argp = nullptr;
/** Constructor. */
qvi_pthread_group_pthread_create_args_s(void) = delete;
/** Constructor. */
qvi_pthread_group_pthread_create_args_s(
qvi_pthread_group_t *group_a,
qvi_pthread_routine_fun_ptr_t throutine_a,
void *throutine_argp_a
) : group(group_a)
, throutine(throutine_a)
, throutine_argp(throutine_argp_a) { }
};

struct qvi_pthread_group_s {
Expand Down Expand Up @@ -93,11 +105,7 @@ struct qvi_pthread_group_s {
}

int
barrier(void)
{
pthread_barrier_wait(&m_barrier);
return QV_SUCCESS;
}
barrier(void);

int
create_from_split(
Expand Down
Loading