#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 8
#define NUM_NODES 64
typedef struct
{
long threadid;
int exoid;
int timestep;
} param;
void *output_nodal_var(void *varg)
{
char name[33];
param *arg = (param *)varg;
float *data = malloc(num_node * sizeof(float));
int i;
for (i = 0; i < num_node; i++) {
data[i] = (arg->timestep - 1) * 10 + arg->threadid + 1 + (float)i / 100.0;
}
if (arg->timestep == 1) {
switch (arg->threadid) {
default: break;
}
}
sprintf(name, "NodalVar%ld", arg->threadid + 1);
free(data);
return NULL;
}
int init_file(int num_nodal_vars)
{
int CPU_word_size = 0;
int IO_word_size = 4;
&CPU_word_size,
&IO_word_size);
printf("after ex_create for test.exo, exoid = %d\n", exoid);
printf(" cpu word size: %d io word size: %d\n", CPU_word_size, IO_word_size);
fprintf(stderr,
"ERROR: This exodus library is not compiled to allow thread-safe operations.\n");
exit(1);
}
int num_dim = 3;
int num_nodes = NUM_NODES;
int num_elem = 0;
int num_elem_blk = 0;
int num_node_sets = 0;
int num_side_sets = 0;
int error =
ex_put_init(exoid,
"This is a test", num_dim, num_nodes, num_elem, num_elem_blk,
num_node_sets, num_side_sets);
printf("after ex_put_init, error = %d\n", error);
printf("after ex_put_variable_param, error = %d\n", error);
if (error) {
exit(-1);
}
float time_value = 0.0;
printf("after ex_put_time, error = %d\n", error);
return exoid;
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
int exoid = init_file(NUM_THREADS);
param arg[NUM_THREADS];
printf("Running on %d threads\n", NUM_THREADS);
for (t = 0; t < NUM_THREADS; t++) {
arg[t].exoid = exoid;
arg[t].threadid = t;
arg[t].timestep = 1;
rc = pthread_create(&threads[t], NULL, output_nodal_var, (void *)(arg + t));
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for (t = 0; t < NUM_THREADS; t++) {
pthread_join(threads[t], NULL);
}
}