Debian Codegraph @

packages/seacas/libraries/exodus/test/test_ts_nvar.c

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