| 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 |
* Nodal var data and names is written on multiple threads |
| 21 |
* -- Each thread handles a single nodal variable |
| 22 |
* -- Coordinate data is written by first 3 threads |
| 23 |
*/ |
| 24 |
|
| 25 |
#define NUM_THREADS 8 |
| 26 |
#define NUM_NODES 64 |
| 27 |
|
| 28 |
static int ulpsDistance(float a, float b) |
| 29 |
{ |
| 30 |
int ia, ib; |
| 31 |
if (a == b) |
| 32 |
return 0; |
| 33 |
memcpy(&ia, &a, sizeof(a)); |
| 34 |
memcpy(&ib, &b, sizeof(b)); |
| 35 |
|
| 36 |
if ((ia < 0) != (ib < 0)) |
| 37 |
return INT_MAX; |
| 38 |
int distance = ia - ib; |
| 39 |
if (distance < 0) |
| 40 |
distance = -distance; |
| 41 |
return distance; |
| 42 |
} |
| 43 |
static int approx_equal(float f1, float f2) |
| 44 |
{ |
| 45 |
if (f1 == f2) |
| 46 |
return 1; |
| 47 |
|
| 48 |
return ulpsDistance(f1, f2) <= 2; |
| 49 |
} |
| 50 |
|
| 51 |
typedef struct |
| 52 |
{ |
| 53 |
long threadid; |
| 54 |
int exoid; |
| 55 |
int timestep; |
| 56 |
} param; |
| 57 |
|
| 58 |
void *input_nodal_var(void *varg) |
| 59 |
{ |
| 60 |
char db_name[33]; |
| 61 |
char ex_name[33]; |
| 62 |
param *arg = (param *)varg; |
| 63 |
int num_node = ex_inquire_int(arg->exoid, EX_INQ_NODES); |
| 64 |
float *data = malloc(num_node * sizeof(float)); |
| 65 |
int i; |
| 66 |
|
| 67 |
if (arg->timestep == 1) { |
| 68 |
switch (arg->threadid) { |
| 69 |
case 0: |
| 70 |
ex_get_coord(arg->exoid, data, NULL, NULL); |
| 71 |
for (i = 0; i < num_node; i++) { |
| 72 |
if (!approx_equal(data[i], arg->threadid + 1 + (float)i / 100.0)) { |
| 73 |
fprintf(stderr, |
| 74 |
"ERROR: Thread %ld: X Coordinate mismatch at node %d: Got: %f, expected %f\n", |
| 75 |
arg->threadid, i + 1, data[i], arg->threadid + 1 + (float)i / 100.0); |
| 76 |
} |
| 77 |
} |
| 78 |
break; |
| 79 |
case 1: |
| 80 |
ex_get_coord(arg->exoid, NULL, data, NULL); |
| 81 |
for (i = 0; i < num_node; i++) { |
| 82 |
if (!approx_equal(data[i], arg->threadid + 1 + (float)i / 100.0)) { |
| 83 |
fprintf(stderr, |
| 84 |
"ERROR: Thread %ld: Y Coordinate mismatch at node %d: Got: %f, expected %f\n", |
| 85 |
arg->threadid, i + 1, data[i], arg->threadid + 1 + (float)i / 100.0); |
| 86 |
} |
| 87 |
} |
| 88 |
break; |
| 89 |
case 2: |
| 90 |
ex_get_coord(arg->exoid, NULL, NULL, data); |
| 91 |
for (i = 0; i < num_node; i++) { |
| 92 |
if (!approx_equal(data[i], arg->threadid + 1 + (float)i / 100.0)) { |
| 93 |
fprintf(stderr, |
| 94 |
"ERROR: Thread %ld: Z Coordinate mismatch at node %d: Got: %f, expected %f\n", |
| 95 |
arg->threadid, i + 1, data[i], arg->threadid + 1 + (float)i / 100.0); |
| 96 |
} |
| 97 |
} |
| 98 |
break; |
| 99 |
default: break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
snprintf(ex_name, 33, "NodalVar%ld", arg->threadid + 1); |
| 104 |
ex_get_variable_name(arg->exoid, EX_NODAL, arg->threadid + 1, db_name); |
| 105 |
if (strcmp(db_name, ex_name) != 0) { |
| 106 |
fprintf(stderr, |
| 107 |
"ERROR: Thread %ld: Incorrect variable name for variable %d: Got: %s, expected %s\n", |
| 108 |
arg->threadid, (int)arg->threadid + 1, db_name, ex_name); |
| 109 |
} |
| 110 |
|
| 111 |
ex_get_var(arg->exoid, arg->timestep, EX_NODAL, arg->threadid + 1, 1, num_node, data); |
| 112 |
for (i = 0; i < num_node; i++) { |
| 113 |
if (!approx_equal(data[i], (arg->timestep - 1) * 10 + arg->threadid + 1 + (float)i / 100.0)) { |
| 114 |
fprintf(stderr, |
| 115 |
"ERROR: Thread %ld: Nodal variable %d mismatch at node %d: Got: %f, expected %f\n", |
| 116 |
arg->threadid, (int)arg->threadid + 1, i + 1, data[i], |
| 117 |
(arg->timestep - 1) * 10 + arg->threadid + 1 + (float)i / 100.0); |
| 118 |
} |
| 119 |
} |
| 120 |
free(data); |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
|
| 124 |
int init_file(int num_nodal_vars) |
| 125 |
{ |
| 126 |
int exoid; |
| 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 |
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 |
|
| 149 |
return exoid; |
| 150 |
} |
| 151 |
|
| 152 |
int main(int argc, char *argv[]) |
| 153 |
{ |
| 154 |
int exoid = init_file(NUM_THREADS); |
| 155 |
|
| 156 |
param arg[NUM_THREADS]; |
| 157 |
|
| 158 |
pthread_t threads[NUM_THREADS]; |
| 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 |
} |