|
| 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