Skip to content

Commit 9ee8b22

Browse files
committed
add pthreads stress-test for MPI_Remove_error_*
Signed-off-by: Thomas Naughton <[email protected]>
1 parent 4dd8c4e commit 9ee8b22

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

environ-mgmt/configure.ac

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,22 @@ AC_CHECK_LIB([mpi], [MPI_Remove_error_string],
128128

129129
AC_SUBST([MPI_LIBS])
130130

131+
dnl
132+
dnl Check for pthreads support
133+
dnl
134+
135+
AC_SEARCH_LIBS([pthread_create], [pthread], [have_pthread=yes], [have_pthread=no])
136+
if test "$have_pthread" = "yes"; then
137+
AC_DEFINE([HAVE_PTHREAD], [1], [Define if have pthreads])
138+
fi
139+
131140
AC_LANG_POP([C])
132141

133142
dnl
134143
dnl Party on
135144
dnl
136145

146+
AC_CONFIG_HEADERS([config.h])
137147
AC_CONFIG_FILES([
138148
Makefile
139149
src/Makefile

environ-mgmt/src/Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
#
88

99
noinst_PROGRAMS = test_add_del_err_codes \
10+
test_add_del_err_codes_pthreads \
1011
test_add_err_codes_usempi \
1112
test_add_del_err_codes_usempi \
1213
test_add_del_err_codes_usempif08
1314

1415
test_add_del_err_codes_SOURCES = \
1516
test_add_del_err_codes.c
1617

18+
test_add_del_err_codes_pthreads_SOURCES = \
19+
test_add_del_err_codes_pthreads.c
20+
1721
test_add_err_codes_usempi_SOURCES = \
1822
test_add_err_codes_usempi.f90
1923

environ-mgmt/src/test_add_del_err_codes.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* - example from Lisandro Dalcin
1212
* https://www.open-mpi.org/community/lists/devel/2014/04/14578.php
1313
*/
14+
#include "config.h"
15+
1416
#include <stdio.h>
1517
#include <stdlib.h>
1618

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2025 UT-Battelle, LLC. All rights reserved.
3+
* $COPYRIGHT$
4+
*/
5+
6+
#include "config.h"
7+
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <mpi.h>
11+
12+
#ifdef HAVE_PTHREAD
13+
#include <pthread.h>
14+
#include <unistd.h>
15+
#include <time.h>
16+
17+
#define NUM_THREADS 10
18+
#define MAX_SLEEP_MS 100
19+
20+
typedef struct {
21+
int thread_id;
22+
} thread_arg_t;
23+
24+
void *thread_func(void *arg)
25+
{
26+
thread_arg_t *targ = (thread_arg_t *)arg;
27+
int thread_id = targ->thread_id;
28+
int errorclass, errorcode;
29+
char errorstring[MPI_MAX_ERROR_STRING];
30+
char code_string[MPI_MAX_ERROR_STRING];
31+
int len;
32+
unsigned int sleep_time;
33+
34+
/* Seed random number generator with thread id and time */
35+
unsigned int seed = (unsigned int)(time(NULL) ^ (thread_id << 16));
36+
37+
/* Add error class */
38+
MPI_Add_error_class(&errorclass);
39+
printf("Thread %d: Added error class %d\n", thread_id, errorclass);
40+
41+
/* Add error code */
42+
MPI_Add_error_code(errorclass, &errorcode);
43+
printf("Thread %d: Added error code %d\n", thread_id, errorcode);
44+
45+
/* Add error string */
46+
snprintf(errorstring, MPI_MAX_ERROR_STRING,
47+
"Error string for thread %d", thread_id);
48+
MPI_Add_error_string(errorcode, errorstring);
49+
printf("Thread %d: Added error string '%s'\n", thread_id, errorstring);
50+
51+
/* Verify the error string was added correctly */
52+
MPI_Error_string(errorcode, code_string, &len);
53+
printf("Thread %d: Verified error string: '%s'\n", thread_id, code_string);
54+
55+
/* Sleep for random time to stress concurrent operations */
56+
sleep_time = rand_r(&seed) % MAX_SLEEP_MS;
57+
usleep(sleep_time * 1000);
58+
printf("Thread %d: Slept for %u ms\n", thread_id, sleep_time);
59+
60+
/* Remove error string */
61+
MPI_Remove_error_string(errorcode);
62+
printf("Thread %d: Removed error string for code %d\n", thread_id, errorcode);
63+
64+
/* Remove error code */
65+
MPI_Remove_error_code(errorcode);
66+
printf("Thread %d: Removed error code %d\n", thread_id, errorcode);
67+
68+
/* Remove error class */
69+
MPI_Remove_error_class(errorclass);
70+
printf("Thread %d: Removed error class %d\n", thread_id, errorclass);
71+
72+
printf("Thread %d: Completed successfully\n", thread_id);
73+
return NULL;
74+
}
75+
#endif
76+
77+
int main(int argc, char **argv)
78+
{
79+
int rank, size;
80+
81+
MPI_Init(&argc, &argv);
82+
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
83+
MPI_Comm_size(MPI_COMM_WORLD, &size);
84+
85+
if (rank == 0) {
86+
printf("MPI initialized with %d processes\n", size);
87+
88+
#ifdef HAVE_PTHREAD
89+
pthread_t threads[NUM_THREADS];
90+
thread_arg_t args[NUM_THREADS];
91+
int i;
92+
93+
printf("pthread support available - creating %d threads\n", NUM_THREADS);
94+
95+
/* Create threads */
96+
for (i = 0; i < NUM_THREADS; i++) {
97+
args[i].thread_id = i;
98+
if (pthread_create(&threads[i], NULL, thread_func, &args[i]) != 0) {
99+
fprintf(stderr, "Failed to create thread %d\n", i);
100+
MPI_Abort(MPI_COMM_WORLD, 1);
101+
}
102+
}
103+
104+
/* Join threads */
105+
for (i = 0; i < NUM_THREADS; i++) {
106+
pthread_join(threads[i], NULL);
107+
}
108+
109+
printf("All threads completed successfully\n");
110+
#else
111+
printf("pthread support not available - test running without threads\n");
112+
#endif
113+
}
114+
115+
MPI_Finalize();
116+
return 0;
117+
}

0 commit comments

Comments
 (0)