Skip to content

Commit 75ff06e

Browse files
Cleanup some Pthread code. (#294)
Signed-off-by: Samuel K. Gutierrez <[email protected]>
1 parent ad45a42 commit 75ff06e

7 files changed

+42
-48
lines changed

src/quo-vadis-pthread.cc

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode: C++; c-basic-offset:4; indent-tabs-mode:nil -*- */
22
/*qv_policy_t
3-
* Copyright (c) 2022-2024 Triad National Security, LLC
3+
* Copyright (c) 2022-2025 Triad National Security, LLC
44
* All rights reserved.
55
*
66
* Copyright (c) 2022-2024 Inria
@@ -24,7 +24,6 @@
2424
#include "qvi-scope.h"
2525
#include "qvi-utils.h"
2626

27-
2827
struct qvi_pthread_args_s {
2928
qv_scope_t *scope = nullptr;
3029
qvi_pthread_routine_fun_ptr_t th_routine = nullptr;
@@ -143,13 +142,12 @@ qv_pthread_colors_fill(
143142
qv_policy_t policy,
144143
int stride,
145144
int npieces
146-
){
145+
) {
147146
int rc = QV_SUCCESS;
148147

149-
if(stride < 1)
150-
return QV_ERR_INVLD_ARG;
151-
152-
switch(policy){
148+
if (stride < 1) return QV_ERR_INVLD_ARG;
149+
150+
switch(policy) {
153151
case QV_POLICY_SPREAD:
154152
{
155153
break;
@@ -164,8 +162,6 @@ qv_pthread_colors_fill(
164162

165163
case QV_POLICY_SCATTER:
166164
{
167-
168-
169165
break;
170166
}
171167

@@ -186,7 +182,6 @@ qv_pthread_colors_fill(
186182
break;
187183
}
188184
}
189-
190185
return rc;
191186
}
192187

src/qvi-group-pthread.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode: C++; c-basic-offset:4; indent-tabs-mode:nil -*- */
22
/*
3-
* Copyright (c) 2020-2024 Triad National Security, LLC
3+
* Copyright (c) 2020-2025 Triad National Security, LLC
44
* All rights reserved.
55
*
66
* This file is part of the quo-vadis project. See the LICENSE file at the

src/qvi-group-pthread.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
struct qvi_group_pthread : public qvi_group {
2323
/** Underlying group instance. */
24-
qvi_pthread_group_t *thgroup = nullptr;
24+
qvi_pthread_group *thgroup = nullptr;
2525
/** Constructor. */
2626
qvi_group_pthread(void) = default;
2727
/** Constructor. */

src/qvi-pthread.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ qvi_pthread_group::qvi_pthread_group(
3333
*/
3434

3535
m_data_g = new qvi_bbuff *[m_size]();
36-
for(int i = 0 ; i < group_size ; i++){
36+
for (int i = 0 ; i < group_size ; i++) {
3737
const int rc = qvi_bbuff_new(&m_data_g[i]);
3838
if (qvi_unlikely(rc != 0)) throw qvi_runtime_error();
3939
}
@@ -47,7 +47,7 @@ qvi_pthread_group::call_first_from_pthread_create(
4747
) {
4848
const pid_t mytid = qvi_gettid();
4949
auto args = (qvi_pthread_group_pthread_create_args_s *)arg;
50-
qvi_pthread_group_t *const group = args->group;
50+
qvi_pthread_group *const group = args->group;
5151
const qvi_pthread_routine_fun_ptr_t thread_routine = args->throutine;
5252
void *const th_routine_argp = args->throutine_argp;
5353
// Let the threads add their TIDs to the vector.
@@ -204,9 +204,9 @@ int
204204
qvi_pthread_group::split(
205205
int color,
206206
int key,
207-
qvi_pthread_group_t **child
207+
qvi_pthread_group **child
208208
) {
209-
qvi_pthread_group_t *ichild = nullptr;
209+
qvi_pthread_group *ichild = nullptr;
210210

211211
qvi_subgroup_info sginfo;
212212
int rc = m_subgroup_info(color, key, &sginfo);

src/qvi-pthread.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,21 @@
1919
#include "qvi-bbuff.h"
2020

2121
typedef void *(*qvi_pthread_routine_fun_ptr_t)(void *);
22-
22+
// Foward declaration.
2323
struct qvi_pthread_group;
24-
typedef struct qvi_pthread_group qvi_pthread_group_t;
2524

2625
struct qvi_pthread_group_pthread_create_args_s {
2726
/** Thread group. */
28-
qvi_pthread_group_t *group = nullptr;
27+
qvi_pthread_group *group = nullptr;
2928
/** The routine to call after group construction. */
3029
qvi_pthread_routine_fun_ptr_t throutine = nullptr;
3130
/** Thread routine arguments. */
3231
void *throutine_argp = nullptr;
33-
/** Constructor. */
32+
/** Default constructor. */
3433
qvi_pthread_group_pthread_create_args_s(void) = delete;
3534
/** Constructor. */
3635
qvi_pthread_group_pthread_create_args_s(
37-
qvi_pthread_group_t *group_a,
36+
qvi_pthread_group *group_a,
3837
qvi_pthread_routine_fun_ptr_t throutine_a,
3938
void *throutine_argp_a
4039
) : group(group_a)
@@ -73,7 +72,7 @@ struct qvi_pthread_group {
7372
qvi_subgroup_info *sginfo
7473
);
7574
public:
76-
/** Constructor. */
75+
/** Default constructor. */
7776
qvi_pthread_group(void) = delete;
7877
/**
7978
* Constructor. This is called by the parent process to construct the
@@ -113,7 +112,7 @@ struct qvi_pthread_group {
113112
split(
114113
int color,
115114
int key,
116-
qvi_pthread_group_t **child
115+
qvi_pthread_group **child
117116
);
118117

119118
int

tests/common-test-utils.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ ctu_scope_report(
9090
}
9191

9292
printf(
93-
"[%d] %s sgrank is %d\n"
94-
"[%d] %s sgsize is %d\n",
93+
"[%d] %s scope group rank is %d\n"
94+
"[%d] %s scope group size is %d\n",
9595
pid, scope_name, sgrank,
9696
pid, scope_name, sgsize
9797
);

tests/test-pthread-split.c

+23-23
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ thread_work(
2929
ers = "qv_scope_group_rank failed";
3030
ctu_panic("%s (rc=%s)", ers, qv_strerr(rc));
3131
}
32-
32+
3333
ctu_scope_report(scope, "thread_scope_in_thread_routine");
3434
ctu_emit_task_bind(scope);
35-
36-
fprintf(stdout,"[%d] ============ Thread %d splitting in two pieces\n", tid, rank);
35+
36+
printf("[%d] ============ Thread %d splitting in two pieces\n", tid, rank);
3737
qv_scope_t *pthread_subscope = NULL;
3838
rc = qv_scope_split(scope, 2, rank, &pthread_subscope);
3939
if (rc != QV_SUCCESS) {
@@ -44,7 +44,7 @@ thread_work(
4444
ctu_scope_report(pthread_subscope, "thread_subscope");
4545
ctu_emit_task_bind(pthread_subscope);
4646

47-
47+
4848
rc = qv_scope_free(pthread_subscope);
4949
if (rc != QV_SUCCESS) {
5050
ers = "qv_scope_free failed";
@@ -73,6 +73,11 @@ main(void)
7373
ers = "MPI_Comm_size() failed";
7474
ctu_panic("%s (rc=%d)", ers, rc);
7575
}
76+
// TODO: As Edgar pointed out, this will work only in the single process
77+
// case. The mpi_scope needs to be split to get a new single MPI scope.
78+
if (wsize != 1) {
79+
ctu_panic("!!! This test works only with one MPI process!");
80+
}
7681

7782
rc = MPI_Comm_rank(comm, &wrank);
7883
if (rc != MPI_SUCCESS) {
@@ -100,46 +105,41 @@ main(void)
100105

101106
ctu_scope_report(mpi_scope, "mpi_scope");
102107
ctu_emit_task_bind(mpi_scope);
103-
104-
//As Edgar pointed out, this will work only in the
105-
//single process case.
106-
//The mpi_scope need to be plsit in order to get
107-
//a new mpi_single_process_scope
108-
109108
//
110109
// Test qv_pthread_scope_split
111110
//
112111
int npieces = 2;
113112
int nthreads = ncores;
114113
int stride = 1;
115114
int colors[nthreads];
116-
117-
printf("[%d] Testing thread_scope_split (nthreads=%i, npieces=%i)\n", tid, nthreads, npieces);
118-
115+
116+
printf(
117+
"[%d] Testing thread_scope_split (nthreads=%d, npieces=%d)\n",
118+
tid, nthreads, npieces
119+
);
120+
119121
for (int i = 0 ; i < nthreads ; i++) {
120122
colors[i] = i % npieces;
121123
}
122124

123-
fprintf(stdout,"Array values :");
125+
printf("Manual values: ");
124126
for (int i = 0 ; i < nthreads ; i++) {
125-
fprintf(stdout,"val[%i]: %i |",i,colors[i]);
127+
printf("val[%i]:%i | ",i,colors[i]);
126128
}
127-
fprintf(stdout,"\n");
128-
129+
printf("\n");
129130

130131
rc = qv_pthread_colors_fill(colors, nthreads, QV_POLICY_PACKED, stride, npieces);
131132
if (rc != QV_SUCCESS) {
132133
ers = "qv_pthread_colors_fill() failed";
133134
ctu_panic("%s (rc=%s)", ers, qv_strerr(rc));
134135
}
135136

136-
fprintf(stdout,"Array values :");
137+
fprintf(stdout,"Filled values: ");
137138
for (int i = 0 ; i < nthreads ; i++) {
138-
fprintf(stdout,"val[%i]: %i |",i,colors[i]);
139+
fprintf(stdout,"val[%i]:%i | ",i,colors[i]);
139140
}
140141
fprintf(stdout,"\n");
141-
142-
142+
143143
qv_scope_t **th_scopes = NULL;
144144
rc = qv_pthread_scope_split(
145145
mpi_scope, npieces, colors, nthreads, &th_scopes
@@ -189,7 +189,7 @@ main(void)
189189

190190
printf("[%d] Testing thread_scope_split_at (nthreads=%i)\n", tid, nthreads);
191191

192-
int colors2[nthreads];
192+
int colors2[nthreads];
193193
for (int i = 0 ; i < nthreads ; i++) {
194194
colors2[i] = i % ncores;
195195
}
@@ -200,7 +200,7 @@ main(void)
200200
}
201201
fprintf(stdout,"\n");
202202

203-
203+
204204
rc = qv_pthread_colors_fill(colors2, nthreads, QV_POLICY_PACKED, stride, ncores);
205205
if (rc != QV_SUCCESS) {
206206
ers = "qv_pthread_colors_fill() failed";

0 commit comments

Comments
 (0)