Skip to content

Commit 9f30d49

Browse files
committed
introduce TST_THREAD_MASTER thread Id for master, num_threads counts only workers
1 parent dffd000 commit 9f30d49

File tree

6 files changed

+33
-40
lines changed

6 files changed

+33
-40
lines changed

one-sided/tst_one_sided_simple_ring_get.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ int tst_one_sided_simple_ring_get_cleanup (struct tst_env * env)
9696
comm = tst_comm_getcomm (env->comm);
9797
MPI_CHECK (MPI_Comm_rank (comm, &comm_rank));
9898

99-
//if (tst_global_rank == 0 && tst_thread_get_num() == 0)
10099
MPI_Win_free (&send_win);
101100

102101
tst_type_freevalues (env->type, env->send_buffer, env->values_num);

p2p/tst_p2p_simple_ring_bsend.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
int tst_p2p_simple_ring_bsend_init (struct tst_env * env)
2020
{
2121
int comm_rank;
22-
int num_threads = 1;
22+
int num = 1;
2323
MPI_Comm comm;
2424

2525
tst_output_printf (DEBUG_LOG, TST_REPORT_MAX, "(Rank:%d) env->comm:%d env->type:%d env->values_num:%d\n",
@@ -37,14 +37,12 @@ int tst_p2p_simple_ring_bsend_init (struct tst_env * env)
3737
tst_type_setstandardarray (env->type, env->values_num, env->send_buffer, comm_rank);
3838

3939
#ifdef HAVE_MPI2_THREADS
40-
num_threads = tst_thread_num_threads ();
41-
#endif
42-
env->mpi_buffer_size = num_threads * (tst_type_gettypesize (env->type) * env->values_num + MPI_BSEND_OVERHEAD);
43-
if ( NULL == (env->mpi_buffer = malloc (env->mpi_buffer_size)) )
44-
ERROR (errno, "malloc");
45-
#ifdef HAVE_MPI2_THREADS
46-
if ( tst_thread_get_num() == 0 ) {
40+
if (tst_thread_get_num() == TST_THREAD_MASTER) {
4741
#endif
42+
num = 1 + tst_thread_num_threads ();
43+
env->mpi_buffer_size = num * (tst_type_gettypesize (env->type) * env->values_num + MPI_BSEND_OVERHEAD);
44+
if ( NULL == (env->mpi_buffer = malloc (env->mpi_buffer_size)) )
45+
ERROR (errno, "malloc");
4846
MPI_CHECK (MPI_Buffer_attach (env->mpi_buffer, env->mpi_buffer_size));
4947
#ifdef HAVE_MPI2_THREADS
5048
}
@@ -129,7 +127,7 @@ int tst_p2p_simple_ring_bsend_run (struct tst_env * env)
129127
int tst_p2p_simple_ring_bsend_cleanup (struct tst_env * env)
130128
{
131129
#ifdef HAVE_MPI2_THREADS
132-
if ( tst_thread_get_num() == 0 ) {
130+
if (tst_thread_get_num() == TST_THREAD_MASTER) {
133131
#endif
134132
MPI_CHECK (MPI_Buffer_detach (&env->mpi_buffer, &env->mpi_buffer_size));
135133
#ifdef HAVE_MPI2_THREADS

p2p/tst_p2p_simple_ring_ibsend.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static int mpi_buffer_size = 0;
3131
int tst_p2p_simple_ring_ibsend_init (struct tst_env * env)
3232
{
3333
int comm_rank;
34-
int num_threads = 1;
34+
int num = 1;
3535
MPI_Comm comm;
3636

3737
tst_output_printf (DEBUG_LOG, TST_REPORT_MAX, "(Rank:%d) env->comm:%d env->type:%d env->values_num:%d\n",
@@ -52,10 +52,10 @@ int tst_p2p_simple_ring_ibsend_init (struct tst_env * env)
5252
* Actually, the standard doesn't even say so.
5353
*/
5454
#ifdef HAVE_MPI2_THREADS
55-
if ( tst_thread_get_num() == 0 ) {
55+
if ( tst_thread_get_num() == TST_THREAD_MASTER ) {
5656
#endif
57-
num_threads = tst_thread_num_threads ();
58-
env->mpi_buffer_size = num_threads * (tst_type_gettypesize (env->type) * env->values_num + MPI_BSEND_OVERHEAD);
57+
num = 1 + tst_thread_num_threads();
58+
env->mpi_buffer_size = num * (tst_type_gettypesize (env->type) * env->values_num + MPI_BSEND_OVERHEAD);
5959
if ((env->mpi_buffer = malloc (env->mpi_buffer_size)) == NULL)
6060
ERROR (errno, "malloc");
6161
MPI_CHECK (MPI_Buffer_attach (env->mpi_buffer, env->mpi_buffer_size));
@@ -134,7 +134,7 @@ int tst_p2p_simple_ring_ibsend_run (struct tst_env * env)
134134
int tst_p2p_simple_ring_ibsend_cleanup (struct tst_env * env)
135135
{
136136
#ifdef HAVE_MPI2_THREADS
137-
if ( tst_thread_get_num () == 0 ) {
137+
if ( tst_thread_get_num () == TST_THREAD_MASTER ) {
138138
#endif
139139
void *buf;
140140
int size = -1;

tst_comm.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -632,15 +632,13 @@ int tst_comm_cleanup () {
632632

633633

634634
MPI_Comm tst_comm_getcomm (int i) {
635-
636635
CHECK_ARG (i, MPI_COMM_NULL);
637-
638-
if (tst_thread_running() && tst_thread_get_num() > 0) {
639-
int threadId = tst_thread_get_num();
640-
return comms[i].mpi_thread_comms[threadId - 1];
636+
int threadId = tst_thread_get_num();
637+
if (threadId == TST_THREAD_MASTER) {
638+
return comms[i].mpi_comm;
641639
}
642640
else {
643-
return comms[i].mpi_comm;
641+
return comms[i].mpi_thread_comms[threadId];
644642
}
645643
}
646644

tst_threads.c

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "mpi_test_suite.h"
99

1010

11+
1112
static int num_threads; /**< Number of available threads - 1 */
1213
static int working;
1314
static pthread_mutex_t working_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -136,7 +137,7 @@ int tst_thread_init(int max_threads, struct tst_thread_env_t ***thread_env) {
136137
int i;
137138
struct tst_thread_env_t ** env;
138139

139-
assert(max_threads > 0);
140+
assert(max_threads >= 0);
140141
assert(thread_env != NULL);
141142
assert(num_threads == 0);
142143

@@ -146,17 +147,12 @@ int tst_thread_init(int max_threads, struct tst_thread_env_t ***thread_env) {
146147
tst_output_printf(DEBUG_LOG, TST_REPORT_MAX, "(Rank:%d) tst_thread_init: Initializing %d threads\n",
147148
tst_global_rank, max_threads);
148149

149-
tst_thread_tid_array = malloc ( max_threads * sizeof (pthread_t));
150-
memset (tst_thread_tid_array, 0, max_threads * sizeof (pthread_t));
151-
env = malloc ( max_threads * sizeof (struct tst_thread_env_t *));
152-
memset (env, 0, max_threads * sizeof (struct tst_thread_env_t*));
150+
tst_thread_tid_array = malloc(max_threads * sizeof (pthread_t));
151+
memset(tst_thread_tid_array, 0, max_threads * sizeof (pthread_t));
152+
env = malloc( max_threads * sizeof (struct tst_thread_env_t *));
153+
memset(env, 0, max_threads * sizeof (struct tst_thread_env_t*));
153154

154-
/*
155-
* Master thread also doing some work
156-
*/
157-
tst_thread_tid_array[0] = pthread_self();
158-
159-
for (i = 0; i < max_threads - 1; i++) {
155+
for (i = 0; i < max_threads; i++) {
160156
int ret;
161157
env[i] = malloc (sizeof (struct tst_thread_env_t));
162158
memset (env[i], 0, sizeof (struct tst_thread_env_t));
@@ -165,11 +161,11 @@ int tst_thread_init(int max_threads, struct tst_thread_env_t ***thread_env) {
165161
ret = pthread_create (&(env[i]->tid), NULL, tst_thread_dispatcher, env[i]);
166162
if (ret != 0)
167163
ERROR (errno, "tst_thread_init: pthread_create");
168-
tst_thread_tid_array[i + 1] = env[i]->tid;
164+
tst_thread_tid_array[i] = env[i]->tid;
169165
num_threads++;
170166
}
171167

172-
assert(num_threads == max_threads - 1);
168+
assert(num_threads == max_threads);
173169
*thread_env = env;
174170
return 0;
175171
}
@@ -202,19 +198,18 @@ int tst_thread_cleanup (struct tst_thread_env_t ** thread_env)
202198

203199
/** \brief Return thread ID of the current thread
204200
*
205-
* \return Success: thread ID, Fail: -1
201+
* \return thread ID
206202
*/
207203
inline int tst_thread_get_num() {
208-
assert(num_threads >= 0);
209-
assert(NULL != tst_thread_tid_array);
204+
assert(num_threads == 0 || ((num_threads > 0) && (NULL != tst_thread_tid_array)));
210205
int i = 0;
211206
pthread_t self = pthread_self();
212-
for (i = 0; i <= num_threads; i++) {
207+
for (i = 0; i < num_threads; i++) {
213208
if (pthread_equal(tst_thread_tid_array[i], self)) {
214209
return i;
215210
}
216211
}
217-
return -1;
212+
return TST_THREAD_MASTER;
218213
}
219214

220215
/** \brief Reset test environment of all running threads
@@ -337,7 +332,7 @@ int tst_thread_execute_cleanup (struct tst_env * env)
337332

338333

339334
inline int tst_thread_num_threads () {
340-
return num_threads + 1; /* +1 because the main thread also is doing some work */
335+
return num_threads;
341336
}
342337

343338

tst_threads.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include "mpi_test_suite.h"
88

99

10+
#define TST_THREAD_MASTER -1
11+
12+
1013
typedef enum {
1114
TST_THREAD_CMD_NULL = 0,
1215
TST_THREAD_CMD_INIT,

0 commit comments

Comments
 (0)