| 1 |
/* |
| 2 |
* Copyright(C) 1999-2021, 2023 National Technology & Engineering Solutions |
| 3 |
* of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 4 |
* NTESS, the U.S. Government retains certain rights in this software. |
| 5 |
* |
| 6 |
* See packages/seacas/LICENSE for details |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <exodusII.h> |
| 10 |
#include <limits.h> |
| 11 |
#include <math.h> |
| 12 |
#include <pthread.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
/* |
| 18 |
* Create an exodus file with NUM_NODES nodes and 0 elements. |
| 19 |
* Initial mesh is created on main thread. |
| 20 |
* There are |
| 21 |
* Nodal var data and names is written on multiple threads |
| 22 |
* -- Each thread handles a portion of the nodes for each nodal variable |
| 23 |
* -- Each thread handles a portion of the nodes for each coordinate variable |
| 24 |
*/ |
| 25 |
|
| 26 |
#define NUM_THREADS 8 |
| 27 |
#define NUM_NODES 64 /* should be multiple of NUM_THREADS */ |
| 28 |
#define NUM_NODAL_VAR 4 |
| 29 |
|
| 30 |
static int ulpsDistance(float a, float b) |
| 31 |
{ |
| 32 |
int ia, ib; |
| 33 |
if (a == b) |
| 34 |
return 0; |
| 35 |
memcpy(&ia, &a, sizeof(a)); |
| 36 |
memcpy(&ib, &b, sizeof(b)); |
| 37 |
|
| 38 |
if ((ia < 0) != (ib < 0)) |
| 39 |
return INT_MAX; |
| 40 |
int distance = ia - ib; |
| 41 |
if (distance < 0) |
| 42 |
distance = -distance; |
| 43 |
return distance; |
| 44 |
} |
| 45 |
static int approx_equal(float f1, float f2) { return ulpsDistance(f1, f2) <= 2; } |
| 46 |
|
| 47 |
typedef struct |
| 48 |
{ |
| 49 |
long threadid; |
| 50 |
int exoid; |
| 51 |
int timestep; |
| 52 |
} param; |
| 53 |
|
| 54 |
void *input_nodal_var(void *varg) |
| 55 |
{ |
| 56 |
param *arg = (param *)varg; |
| 57 |
int num_node = ex_inquire_int(arg->exoid, EX_INQ_NODES); |
| 58 |
int segment = num_node / NUM_THREADS; |
| 59 |
int begin = arg->threadid * segment; |
| 60 |
|
| 61 |
float *data = malloc(segment * sizeof(float)); |
| 62 |
int i, var; |
| 63 |
|
| 64 |
if (arg->timestep == 1) { |
| 65 |
ex_get_partial_coord(arg->exoid, begin + 1, segment, data, NULL, NULL); |
| 66 |
|
| 67 |
for (i = 0; i < segment; i++) { |
| 68 |
if (!approx_equal(data[i], (float)(begin + i) / 100.0)) { |
| 69 |
fprintf(stderr, |
| 70 |
"ERROR: Thread %ld: X Coordinate mismatch at node %d: Got: %f, expected %f\n", |
| 71 |
arg->threadid, i + 1, data[i], (float)(begin + i) / 100.0); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
ex_get_partial_coord(arg->exoid, begin + 1, segment, NULL, data, NULL); |
| 76 |
|
| 77 |
for (i = 0; i < segment; i++) { |
| 78 |
if (!approx_equal(data[i], (float)(begin + i) / 100.0)) { |
| 79 |
fprintf(stderr, |
| 80 |
"ERROR: Thread %ld: Y Coordinate mismatch at node %d: Got: %f, expected %f\n", |
| 81 |
arg->threadid, i + 1, data[i], (float)(begin + i) / 100.0); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
ex_get_partial_coord(arg->exoid, begin + 1, segment, NULL, NULL, data); |
| 86 |
|
| 87 |
for (i = 0; i < segment; i++) { |
| 88 |
if (!approx_equal(data[i], (float)(begin + i) / 100.0)) { |
| 89 |
fprintf(stderr, |
| 90 |
"ERROR: Thread %ld: Z Coordinate mismatch at node %d: Got: %f, expected %f\n", |
| 91 |
arg->threadid, i + 1, data[i], (float)(begin + i) / 100.0); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (arg->threadid < NUM_NODAL_VAR) { |
| 97 |
char db_name[33]; |
| 98 |
char ex_name[33]; |
| 99 |
|
| 100 |
snprintf(ex_name, 33, "NodalVar%ld", arg->threadid + 1); |
| 101 |
ex_get_variable_name(arg->exoid, EX_NODAL, arg->threadid + 1, db_name); |
| 102 |
if (strcmp(db_name, ex_name) != 0) { |
| 103 |
fprintf(stderr, |
| 104 |
"ERROR: Thread %ld: Incorrect variable name for variable %ld: Got: %s, expected %s\n", |
| 105 |
arg->threadid, arg->threadid + 1, db_name, ex_name); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
for (var = 1; var <= NUM_NODAL_VAR; var++) { |
| 110 |
ex_get_partial_var(arg->exoid, arg->timestep, EX_NODAL, var, 1, begin + 1, segment, data); |
| 111 |
for (i = 0; i < segment; i++) { |
| 112 |
if (!approx_equal(data[i], (arg->timestep - 1) * 10 + var + (float)(begin + i) / 100.0)) { |
| 113 |
fprintf(stderr, |
| 114 |
"ERROR: Thread %ld: Nodal variable data mismatch in variable %d at node %d: " |
| 115 |
"Got: %f, expected %f\n", |
| 116 |
arg->threadid, var, i + 1, data[i], |
| 117 |
(arg->timestep - 1) * 10 + var + (float)(begin + i) / 100.0); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
free(data); |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
|
| 125 |
int init_file(int num_nodal_vars) |
| 126 |
{ |
| 127 |
float version; |
| 128 |
int CPU_word_size = 0; /* sizeof(float) */ |
| 129 |
int IO_word_size = 0; /* use what is stored in file */ |
| 130 |
|
| 131 |
ex_opts(EX_VERBOSE | EX_ABORT); |
| 132 |
|
| 133 |
/* open EXODUS II files */ |
| 134 |
int exoid = ex_open("test.exo", /* filename path */ |
| 135 |
EX_READ, /* access mode = READ */ |
| 136 |
&CPU_word_size, /* CPU word size */ |
| 137 |
&IO_word_size, /* IO word size */ |
| 138 |
&version); /* ExodusII library version */ |
| 139 |
|
| 140 |
if (exoid < 0) |
| 141 |
exit(1); |
| 142 |
|
| 143 |
if (!ex_inquire_int(exoid, EX_INQ_THREADSAFE)) { |
| 144 |
fprintf(stderr, |
| 145 |
"ERROR: This exodus library is not compiled to allow thread-safe operations.\n"); |
| 146 |
exit(1); |
| 147 |
} |
| 148 |
return exoid; |
| 149 |
} |
| 150 |
|
| 151 |
int main(int argc, char *argv[]) |
| 152 |
{ |
| 153 |
pthread_t threads[NUM_THREADS]; |
| 154 |
|
| 155 |
int exoid = init_file(NUM_NODAL_VAR); |
| 156 |
|
| 157 |
param arg[NUM_THREADS]; |
| 158 |
|
| 159 |
printf("Running on %d threads\n", NUM_THREADS); |
| 160 |
for (long t = 0; t < NUM_THREADS; t++) { |
| 161 |
arg[t].exoid = exoid; |
| 162 |
arg[t].threadid = t; |
| 163 |
arg[t].timestep = 1; |
| 164 |
|
| 165 |
int rc = pthread_create(&threads[t], NULL, input_nodal_var, (void *)(arg + t)); |
| 166 |
if (rc) { |
| 167 |
printf("ERROR; return code from pthread_create() is %d\n", rc); |
| 168 |
exit(-1); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
for (long t = 0; t < NUM_THREADS; t++) { |
| 173 |
pthread_join(threads[t], NULL); |
| 174 |
} |
| 175 |
|
| 176 |
ex_close(exoid); |
| 177 |
} |