Skip to content

Commit 86d7426

Browse files
Cleanup more Pthread code. (#299)
Signed-off-by: Samuel K. Gutierrez <[email protected]>
1 parent 19d1577 commit 86d7426

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

src/qvi-pthread.cc

+33-29
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ qvi_pthread_group::m_start_init_by_a_single_thread(
2929
int rc = pthread_barrier_init(&m_barrier, nullptr, m_size);
3030
if (qvi_unlikely(rc != 0)) return rc;
3131

32-
m_gather_data = new qvi_bbuff *[m_size]();
32+
m_gather_data = new qvi_bbuff *[m_size];
3333
for (int i = 0 ; i < group_size ; i++) {
3434
rc = qvi_bbuff_new(&m_gather_data[i]);
3535
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
3636
}
37-
m_scatter_data = new qvi_bbuff**();
38-
m_ckrs = new qvi_subgroup_color_key_rank[m_size]();
37+
m_scatter_data = new qvi_bbuff**;
38+
m_ckrs.resize(m_size);
3939

4040
return QV_SUCCESS;
4141
}
@@ -108,23 +108,6 @@ qvi_pthread_group::qvi_pthread_group(
108108
m_context->groupid2thgroup.insert({mygid, this});
109109
}
110110

111-
void *
112-
qvi_pthread_group::call_first_from_pthread_create(
113-
void *arg
114-
) {
115-
auto args = (qvi_pthread_group_pthread_create_args *)arg;
116-
const qvi_pthread_routine_fun_ptr_t thread_routine = args->throutine;
117-
void *const th_routine_argp = args->throutine_argp;
118-
119-
const int rc = m_finish_init_by_all_threads(args->group);
120-
// TODO(skg) Is this the correct thing to do? Shall we return something?
121-
if (qvi_unlikely(rc != QV_SUCCESS)) throw qvi_runtime_error();
122-
// Free the provided argument container.
123-
qvi_delete(&args);
124-
// Finally, call the specified thread routine.
125-
return thread_routine(th_routine_argp);
126-
}
127-
128111
qvi_pthread_group::~qvi_pthread_group(void)
129112
{
130113
std::lock_guard<std::mutex> guard(m_mutex);
@@ -141,11 +124,32 @@ qvi_pthread_group::~qvi_pthread_group(void)
141124
}
142125

143126
delete m_scatter_data;
144-
delete[] m_ckrs;
145127

146128
pthread_barrier_destroy(&m_barrier);
147129
}
148130

131+
void *
132+
qvi_pthread_group::call_first_from_pthread_create(
133+
void *arg
134+
) {
135+
auto args = (qvi_pthread_group_pthread_create_args *)arg;
136+
const qvi_pthread_routine_fun_ptr_t thread_routine = args->throutine;
137+
void *const th_routine_argp = args->throutine_argp;
138+
139+
const int rc = m_finish_init_by_all_threads(args->group);
140+
if (qvi_unlikely(rc != QV_SUCCESS)) {
141+
qvi_log_error(
142+
"An error occurred in m_finish_init_by_all_threads(): {} ({})",
143+
rc, qv_strerr(rc)
144+
);
145+
throw qvi_runtime_error();
146+
}
147+
// Free the provided argument container.
148+
qvi_delete(&args);
149+
// Finally, call the specified thread routine.
150+
return thread_routine(th_routine_argp);
151+
}
152+
149153
int
150154
qvi_pthread_group::size(void)
151155
{
@@ -201,9 +205,9 @@ qvi_pthread_group::m_subgroup_info(
201205
// Sort the color/key/rank array. First according to color, then by key,
202206
// but in the same color realm. If color and key are identical, sort by
203207
// the rank from given group.
204-
std::sort(m_ckrs, m_ckrs + m_size, qvi_subgroup_color_key_rank::by_color);
205-
std::sort(m_ckrs, m_ckrs + m_size, qvi_subgroup_color_key_rank::by_key);
206-
std::sort(m_ckrs, m_ckrs + m_size, qvi_subgroup_color_key_rank::by_rank);
208+
std::sort(m_ckrs.begin(), m_ckrs.end(), qvi_subgroup_color_key_rank::by_color);
209+
std::sort(m_ckrs.begin(), m_ckrs.end(), qvi_subgroup_color_key_rank::by_key);
210+
std::sort(m_ckrs.begin(), m_ckrs.end(), qvi_subgroup_color_key_rank::by_rank);
207211
// Calculate the number of distinct colors provided.
208212
std::set<int> color_set;
209213
for (int i = 0; i < m_size; ++i) {
@@ -293,21 +297,21 @@ qvi_pthread_group::gather(
293297
qvi_bbuff_alloc_type_t *alloc_type,
294298
qvi_bbuff ***rxbuffs
295299
) {
300+
int rc = QV_SUCCESS;
296301
const int myrank = rank();
297-
// I'm not sure why this barrier is needed, but it seems to help...
302+
298303
barrier();
299-
int rc = QV_SUCCESS;
300304
{
301305
std::lock_guard<std::mutex> guard(m_mutex);
302306
rc = qvi_bbuff_copy(*txbuff, m_gather_data[myrank]);
303307
*alloc_type = QVI_BBUFF_ALLOC_SHARED_GLOBAL;
304308
}
305-
// Need to ensure that all threads have contributed to m_data_g
309+
// Ensure that all threads have contributed to m_gather_data.
306310
barrier();
307311

308312
if (qvi_unlikely(rc != QV_SUCCESS)) {
309313
*rxbuffs = nullptr;
310-
return QV_ERR_INTERNAL;
314+
return rc;
311315
}
312316
*rxbuffs = m_gather_data;
313317
return rc;
@@ -336,7 +340,7 @@ qvi_pthread_group::scatter(
336340

337341
if (qvi_unlikely(rc != QV_SUCCESS)) {
338342
qvi_bbuff_delete(&mybbuff);
339-
return QV_ERR_INTERNAL;
343+
return rc;
340344
}
341345
*rxbuff = mybbuff;
342346
return rc;

src/qvi-pthread.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,14 @@ struct qvi_pthread_group : qvi_refc {
6565
std::map<pid_t, qvi_task *> m_tid2task;
6666
/** Used for mutexy things. */
6767
std::mutex m_mutex;
68-
/** Used for monitory things. */
69-
std::condition_variable m_condition;
7068
/** Used for barrier things. */
7169
pthread_barrier_t m_barrier;
7270
/** Used for gather exchanges. */
7371
qvi_bbuff **m_gather_data = nullptr;
7472
/** Used for scatter exchanges. */
7573
qvi_bbuff ***m_scatter_data = nullptr;
7674
/** Shared color, key, rank data used for splitting. */
77-
qvi_subgroup_color_key_rank *m_ckrs = nullptr;
75+
std::vector<qvi_subgroup_color_key_rank> m_ckrs;
7876
/** Shared sub-group IDs. */
7977
std::vector<qvi_group_id_t> m_subgroup_gids;
8078
/** */

0 commit comments

Comments
 (0)