#include <limits.h>
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_THREADS 8
#define NUM_NODES 64
static int ulpsDistance(float a, float b)
{
int ia, ib;
if (a == b)
return 0;
memcpy(&ia, &a, sizeof(a));
memcpy(&ib, &b, sizeof(b));
if ((ia < 0) != (ib < 0))
return INT_MAX;
int distance = ia - ib;
if (distance < 0)
distance = -distance;
return distance;
}
static int approx_equal(float f1, float f2)
{
if (f1 == f2)
return 1;
return ulpsDistance(f1, f2) <= 2;
}
typedef struct
{
long threadid;
int exoid;
int timestep;
} param;
void *input_nodal_var(void *varg)
{
char db_name[33];
char ex_name[33];
param *arg = (param *)varg;
float *data = malloc(num_node * sizeof(float));
int i;
if (arg->timestep == 1) {
switch (arg->threadid) {
case 0:
for (i = 0; i < num_node; i++) {
if (!approx_equal(data[i], arg->threadid + 1 + (float)i / 100.0)) {
fprintf(stderr,
"ERROR: Thread %ld: X Coordinate mismatch at node %d: Got: %f, expected %f\n",
arg->threadid, i + 1, data[i], arg->threadid + 1 + (float)i / 100.0);
}
}
break;
case 1:
for (i = 0; i < num_node; i++) {
if (!approx_equal(data[i], arg->threadid + 1 + (float)i / 100.0)) {
fprintf(stderr,
"ERROR: Thread %ld: Y Coordinate mismatch at node %d: Got: %f, expected %f\n",
arg->threadid, i + 1, data[i], arg->threadid + 1 + (float)i / 100.0);
}
}
break;
case 2:
for (i = 0; i < num_node; i++) {
if (!approx_equal(data[i], arg->threadid + 1 + (float)i / 100.0)) {
fprintf(stderr,
"ERROR: Thread %ld: Z Coordinate mismatch at node %d: Got: %f, expected %f\n",
arg->threadid, i + 1, data[i], arg->threadid + 1 + (float)i / 100.0);
}
}
break;
default: break;
}
}
snprintf(ex_name, 33, "NodalVar%ld", arg->threadid + 1);
if (strcmp(db_name, ex_name) != 0) {
fprintf(stderr,
"ERROR: Thread %ld: Incorrect variable name for variable %d: Got: %s, expected %s\n",
arg->threadid, (int)arg->threadid + 1, db_name, ex_name);
}
for (i = 0; i < num_node; i++) {
if (!approx_equal(data[i], (arg->timestep - 1) * 10 + arg->threadid + 1 + (float)i / 100.0)) {
fprintf(stderr,
"ERROR: Thread %ld: Nodal variable %d mismatch at node %d: Got: %f, expected %f\n",
arg->threadid, (int)arg->threadid + 1, i + 1, data[i],
(arg->timestep - 1) * 10 + arg->threadid + 1 + (float)i / 100.0);
}
}
free(data);
return NULL;
}
int init_file(int num_nodal_vars)
{
int exoid;
float version;
int CPU_word_size = 0;
int IO_word_size = 0;
&CPU_word_size,
&IO_word_size,
&version);
if (exoid < 0)
exit(1);
fprintf(stderr,
"ERROR: This exodus library is not compiled to allow thread-safe operations.\n");
exit(1);
}
return exoid;
}
int main(int argc, char *argv[])
{
int exoid = init_file(NUM_THREADS);
param arg[NUM_THREADS];
pthread_t threads[NUM_THREADS];
printf("Running on %d threads\n", NUM_THREADS);
for (long t = 0; t < NUM_THREADS; t++) {
arg[t].exoid = exoid;
arg[t].threadid = t;
arg[t].timestep = 1;
int rc = pthread_create(&threads[t], NULL, input_nodal_var, (void *)(arg + t));
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for (long t = 0; t < NUM_THREADS; t++) {
pthread_join(threads[t], NULL);
}
}
@ EX_NODAL
Definition exodusII.h:268
@ EX_ABORT
Definition exodusII.h:392
@ EX_VERBOSE
Definition exodusII.h:390
@ EX_INQ_NODES
Definition exodusII.h:149
@ EX_INQ_THREADSAFE
Definition exodusII.h:205
#define EX_READ
Definition exodusII.h:98
int ex_get_coord(int exoid, void *x_coor, void *y_coor, void *z_coor)
Definition ex_get_coord.c:71
int ex_get_var(int exoid, int time_step, ex_entity_type var_type, int var_index, ex_entity_id obj_id, int64_t num_entry_this_obj, void *var_vals)
Definition ex_get_var.c:68
int ex_get_variable_name(int exoid, ex_entity_type obj_type, int var_num, char *var_name)
Definition ex_get_variable_name.c:35
int64_t ex_inquire_int(int exoid, ex_inquiry req_info)
Definition ex_inquire.c:1029
int ex_opts(int options)
Definition ex_opts.c:56
#define ex_open(path, mode, comp_ws, io_ws, version)
Definition exodusII.h:597
int ex_close(int exoid)
Definition ex_close.c:47