| 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 <pthread.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
|
| 14 |
/* |
| 15 |
* Create an exodus file with NUM_NODES nodes and 0 elements. |
| 16 |
* Initial mesh is created on main thread. |
| 17 |
* There are |
| 18 |
* Nodal var data and names is written on multiple threads |
| 19 |
* -- Each thread handles a portion of the nodes for each nodal variable |
| 20 |
* -- Each thread handles a portion of the nodes for each coordinate variable |
| 21 |
*/ |
| 22 |
|
| 23 |
#define NUM_THREADS 8 |
| 24 |
#define NUM_NODES 64 /* should be multiple of NUM_THREADS */ |
| 25 |
#define NUM_NODAL_VAR 4 |
| 26 |
|
| 27 |
typedef struct |
| 28 |
{ |
| 29 |
long threadid; |
| 30 |
int exoid; |
| 31 |
int timestep; |
| 32 |
} param; |
| 33 |
|
| 34 |
void *output_nodal_var(void *varg) |
| 35 |
{ |
| 36 |
param *arg = (param *)varg; |
| 37 |
int num_node = ex_inquire_int(arg->exoid, EX_INQ_NODES); |
| 38 |
int segment = num_node / NUM_THREADS; |
| 39 |
int begin = arg->threadid * segment; |
| 40 |
|
| 41 |
float *data = malloc(segment * sizeof(float)); |
| 42 |
int i, var; |
| 43 |
|
| 44 |
if (arg->timestep == 1) { |
| 45 |
for (i = 0; i < segment; i++) { |
| 46 |
data[i] = (float)(begin + i) / 100.0; |
| 47 |
} |
| 48 |
ex_put_partial_coord(arg->exoid, begin + 1, segment, data, data, data); |
| 49 |
} |
| 50 |
|
| 51 |
if (arg->threadid < NUM_NODAL_VAR) { |
| 52 |
char name[33]; |
| 53 |
snprintf(name, 33, "NodalVar%ld", arg->threadid + 1); |
| 54 |
ex_put_variable_name(arg->exoid, EX_NODAL, arg->threadid + 1, name); |
| 55 |
} |
| 56 |
|
| 57 |
for (var = 1; var <= NUM_NODAL_VAR; var++) { |
| 58 |
for (i = 0; i < segment; i++) { |
| 59 |
data[i] = (arg->timestep - 1) * 10 + var + (float)(begin + i) / 100.0; |
| 60 |
} |
| 61 |
ex_put_partial_var(arg->exoid, arg->timestep, EX_NODAL, var, 1, begin + 1, segment, data); |
| 62 |
} |
| 63 |
free(data); |
| 64 |
return NULL; |
| 65 |
} |
| 66 |
|
| 67 |
int init_file(int num_nodal_vars) |
| 68 |
{ |
| 69 |
/* Specify compute and i/o word size */ |
| 70 |
int CPU_word_size = 0; /* sizeof(float) */ |
| 71 |
int IO_word_size = 4; /* (4 bytes) */ |
| 72 |
|
| 73 |
ex_opts(EX_VERBOSE); |
| 74 |
|
| 75 |
/* create EXODUS II file */ |
| 76 |
int exoid = ex_create("test.exo", /* filename path */ |
| 77 |
EX_CLOBBER, /* create mode */ |
| 78 |
&CPU_word_size, /* CPU float word size in bytes */ |
| 79 |
&IO_word_size); /* I/O float word size in bytes */ |
| 80 |
printf("after ex_create for test.exo, exoid = %d\n", exoid); |
| 81 |
printf(" cpu word size: %d io word size: %d\n", CPU_word_size, IO_word_size); |
| 82 |
|
| 83 |
if (!ex_inquire_int(exoid, EX_INQ_THREADSAFE)) { |
| 84 |
fprintf(stderr, |
| 85 |
"ERROR: This exodus library is not compiled to allow thread-safe operations.\n"); |
| 86 |
exit(1); |
| 87 |
} |
| 88 |
|
| 89 |
/* initialize file with parameters */ |
| 90 |
int num_dim = 3; |
| 91 |
int num_nodes = NUM_NODES; |
| 92 |
int num_elem = 0; |
| 93 |
int num_elem_blk = 0; |
| 94 |
int num_node_sets = 0; |
| 95 |
int num_side_sets = 0; |
| 96 |
|
| 97 |
int error = ex_put_init(exoid, "This is a test", num_dim, num_nodes, num_elem, num_elem_blk, |
| 98 |
num_node_sets, num_side_sets); |
| 99 |
|
| 100 |
printf("after ex_put_init, error = %d\n", error); |
| 101 |
|
| 102 |
error = ex_put_variable_param(exoid, EX_NODAL, num_nodal_vars); |
| 103 |
printf("after ex_put_variable_param, error = %d\n", error); |
| 104 |
if (error) { |
| 105 |
ex_close(exoid); |
| 106 |
exit(-1); |
| 107 |
} |
| 108 |
|
| 109 |
float time_value = 0.0; |
| 110 |
error = ex_put_time(exoid, 1, &time_value); |
| 111 |
printf("after ex_put_time, error = %d\n", error); |
| 112 |
|
| 113 |
return exoid; |
| 114 |
} |
| 115 |
|
| 116 |
int main(int argc, char *argv[]) |
| 117 |
{ |
| 118 |
|
| 119 |
int exoid = init_file(NUM_NODAL_VAR); |
| 120 |
|
| 121 |
param arg[NUM_THREADS]; |
| 122 |
pthread_t threads[NUM_THREADS]; |
| 123 |
|
| 124 |
printf("Running on %d threads\n", NUM_THREADS); |
| 125 |
for (long t = 0; t < NUM_THREADS; t++) { |
| 126 |
arg[t].exoid = exoid; |
| 127 |
arg[t].threadid = t; |
| 128 |
arg[t].timestep = 1; |
| 129 |
|
| 130 |
int rc = pthread_create(&threads[t], NULL, output_nodal_var, (void *)(arg + t)); |
| 131 |
if (rc) { |
| 132 |
printf("ERROR; return code from pthread_create() is %d\n", rc); |
| 133 |
exit(-1); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
for (long t = 0; t < NUM_THREADS; t++) { |
| 138 |
pthread_join(threads[t], NULL); |
| 139 |
} |
| 140 |
|
| 141 |
ex_close(exoid); |
| 142 |
} |