Exodus 7.22

/test/test_ts_partial_nvar.c
/*
* Copyright(c) 2005-2017 National Technology &Engineering Solutions
* of Sandia, LLC(NTESS).Under the terms of Contract DE - NA0003525 with
* NTESS, the U.S.Government retains certain rights in this software.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and / or other materials provided
* with the distribution.
*
* * Neither the name of NTESS nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <exodusII.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create an exodus file with NUM_NODES nodes and 0 elements.
* Initial mesh is created on main thread.
* There are
* Nodal var data and names is written on multiple threads
* -- Each thread handles a portion of the nodes for each nodal variable
* -- Each thread handles a portion of the nodes for each coordinate variable
*/
#define NUM_THREADS 8
#define NUM_NODES 64 /* should be multiple of NUM_THREADS */
#define NUM_NODAL_VAR 4
typedef struct
{
long threadid;
int exoid;
int timestep;
} param;
void *output_nodal_var(void *varg)
{
char name[33];
param *arg = (param *)varg;
int num_node = ex_inquire_int(arg->exoid, EX_INQ_NODES);
int segment = num_node / NUM_THREADS;
int begin = arg->threadid * segment;
float *data = malloc(segment * sizeof(float));
int i, var;
if (arg->timestep == 1) {
for (i = 0; i < segment; i++) {
data[i] = (float)(begin + i) / 100.0;
}
ex_put_partial_coord(arg->exoid, begin + 1, segment, data, data, data);
}
if (arg->threadid < NUM_NODAL_VAR) {
sprintf(name, "NodalVar%ld", arg->threadid + 1);
ex_put_variable_name(arg->exoid, EX_NODAL, arg->threadid + 1, name);
}
for (var = 1; var <= NUM_NODAL_VAR; var++) {
for (i = 0; i < segment; i++) {
data[i] = (arg->timestep - 1) * 10 + var + (float)(begin + i) / 100.0;
}
ex_put_partial_var(arg->exoid, arg->timestep, EX_NODAL, var, 1, begin + 1, segment, data);
}
free(data);
return NULL;
}
int init_file(int num_nodal_vars)
{
/* Specify compute and i/o word size */
int CPU_word_size = 0; /* sizeof(float) */
int IO_word_size = 4; /* (4 bytes) */
ex_opts(EX_VERBOSE);
/* create EXODUS II file */
int exoid = ex_create("test.exo", /* filename path */
EX_CLOBBER, /* create mode */
&CPU_word_size, /* CPU float word size in bytes */
&IO_word_size); /* I/O float word size in bytes */
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);
if (!ex_inquire_int(exoid, EX_INQ_THREADSAFE)) {
fprintf(stderr,
"ERROR: This exodus library is not compiled to allow thread-safe operations.\n");
exit(1);
}
/* initialize file with parameters */
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);
error = ex_put_variable_param(exoid, EX_NODAL, num_nodal_vars);
printf("after ex_put_variable_param, error = %d\n", error);
if (error) {
ex_close(exoid);
exit(-1);
}
float time_value = 0.0;
error = ex_put_time(exoid, 1, &time_value);
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_NODAL_VAR);
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);
}
ex_close(exoid);
}
ex_inquire_int
int64_t ex_inquire_int(int exoid, ex_inquiry req_info)
Definition: ex_inquire.c:954
ex_opts
int ex_opts(int options)
Definition: ex_opts.c:83
error
error
Definition: ex_put_elem_var_tab.c:115
EX_INQ_NODES
Definition: exodusII.h:162
ex_put_variable_param
int ex_put_variable_param(int exoid, ex_entity_type obj_type, int num_vars)
Definition: ex_put_variable_param.c:152
ex_put_time
int ex_put_time(int exoid, int time_step, const void *time_value)
Definition: ex_put_time.c:78
ex_create
#define ex_create(path, mode, comp_ws, io_ws)
Definition: exodusII.h:444
ex_close
int ex_close(int exoid)
Definition: ex_close.c:73
ex_put_partial_coord
int ex_put_partial_coord(int exoid, int64_t start_node_num, int64_t num_nodes, const void *x_coor, const void *y_coor, const void *z_coor)
Definition: ex_put_partial_coord.c:71
EX_CLOBBER
#define EX_CLOBBER
Definition: exodusII.h:114
EX_NODAL
Definition: exodusII.h:254
ex_put_variable_name
int ex_put_variable_name(int exoid, ex_entity_type obj_type, int var_num, const char *var_name)
Definition: ex_put_variable_name.c:65
EX_INQ_THREADSAFE
Definition: exodusII.h:219
exodusII.h
ex_put_init
int ex_put_init(int exoid, const char *title, int64_t num_dim, int64_t num_nodes, int64_t num_elem, int64_t num_elem_blk, int64_t num_node_sets, int64_t num_side_sets)
Definition: ex_put_init.c:80
EX_VERBOSE
Definition: exodusII.h:282
ex_put_partial_var
int ex_put_partial_var(int exoid, int time_step, ex_entity_type var_type, int var_index, ex_entity_id obj_id, int64_t start_index, int64_t num_entities, const void *var_vals)
Definition: ex_put_partial_var.c:54