-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.c
278 lines (251 loc) · 8.61 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "utils.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#ifdef _WIN32
#include <Windows.h>
#include <process.h>
#else
#include <unistd.h>
#endif
#ifdef _WIN32
static const unsigned __int64 EPOCH = ((unsigned __int64)116444736000000000ULL);
#endif
nodeval_t **alloc_2d(const int m, const int n) {
nodeval_t **arr = malloc(m * sizeof(*arr));
int i;
for (i = 0; i < m; ++i) {
arr[i] = malloc(n * sizeof(nodeval_t));
}
return arr;
}
nodeval_t ****alloc_4d(const int m, const int n, const int o, const int p) {
int i, j, k;
nodeval_t ****arr = malloc(m * sizeof(***arr));
for (i = 0; i < m; i++) {
arr[i] = malloc(n * sizeof(**arr));
for (j = 0; j < n; j++) {
arr[i][j] = malloc(o * sizeof(*arr));
for (k = 0; k < o; k++) {
arr[i][j][k] = malloc(p * sizeof(nodeval_t));
}
}
}
return arr;
}
void init_zeros_2d(nodeval_t **nodes, int number_nodes_x, int number_nodes_y) {
//initialize all nodes with 0
for (int i = 0; i < number_nodes_x; i++) {
for (int j = 0; j < number_nodes_y; j++) {
nodes[i][j] = 0.0;
}
}
}
const unsigned int system_processor_online_count() {
#ifdef _WIN32
SYSTEM_INFO info;
GetSystemInfo(&info);
return info.dwNumberOfProcessors;
#else
return sysconf(_SC_NPROCESSORS_ONLN);
#endif
}
threadhandle_t *
create_and_run_simulation_thread(unsigned int(*callback)(partialsimulationcontext_t *),
partialsimulationcontext_t *context) {
threadhandle_t *handle = malloc(sizeof(threadhandle_t));
#ifdef _WIN32
*handle = (threadhandle_t) _beginthreadex(0, 0, callback, context, 0, 0);
#else
int ret = pthread_create(handle, NULL, (void *(*)(void *)) callback, context);
if (ret) {
printf("Error initializing thread. Error code %d.\n", ret);
return NULL;
}
#endif
return handle;
}
void join_and_close_simulation_threads(threadhandle_t **handles, const int num_threads) {
for (int i = 0; i < num_threads; i++) {
#ifdef _WIN32
WaitForSingleObject(*handles[i], INFINITE);
#else
pthread_join(*handles[i], NULL);
#endif
}
for (int i = 0; i < num_threads; i++) {
#ifdef _WIN32
CloseHandle(*handles[i]);
#endif
free(handles[i]);
}
}
void init_thread_barrier(threadbarrier_t *barrier, const unsigned int number_threads) {
#ifdef _WIN32
InitializeSynchronizationBarrier(barrier, number_threads, -1);
#else
pthread_barrier_init(barrier, NULL, number_threads);
#endif
}
void destroy_thread_barrier(threadbarrier_t *barrier) {
#ifdef _WIN32
DeleteSynchronizationBarrier(barrier);
#else
pthread_barrier_destroy(barrier);
#endif
}
unsigned int wait_at_barrier(threadbarrier_t *barrier) {
#ifdef _WIN32
return EnterSynchronizationBarrier(barrier, 0);
#else
if (pthread_barrier_wait(barrier) == PTHREAD_BARRIER_SERIAL_THREAD) {
return 1;
} else {
return 0;
}
#endif
}
void init_partial_simulation_context(partialsimulationcontext_t *context, int num_ticks, double tick_ms,
int number_nodes_x, int number_nodes_y,
int num_global_obervationnodes, nodetimeseries_t *global_observationnodes,
nodeval_t **old_state,
nodeval_t **new_state, nodeval_t **slopes, nodeval_t ****kernels,
kernelfunc_t d_ptr, kernelfunc_t id_ptr,
int number_global_inputs, nodeinputseries_t *global_inputs,
int thread_start_x, int thread_end_x, threadbarrier_t *barrier) {
context->num_ticks = num_ticks;
context->tick_ms = tick_ms;
context->number_nodes_x = number_nodes_x;
context->number_nodes_y = number_nodes_y;
context->num_global_obervationnodes = num_global_obervationnodes;
context->global_observationnodes = global_observationnodes;
context->old_state = old_state;
context->new_state = new_state;
context->slopes = slopes;
context->kernels = kernels;
context->d_ptr = d_ptr;
context->id_ptr = id_ptr;
context->number_global_inputs = number_global_inputs;
context->global_inputs = global_inputs;
context->thread_start_x = thread_start_x;
context->thread_end_x = thread_end_x;
context->barrier = barrier;
//derive the partial observation nodes
context->num_partial_obervationnodes = 0;
for (int i = 0; i < num_global_obervationnodes; i++) { //count partial array elements
if (global_observationnodes != NULL
&& global_observationnodes[i].x_index >= thread_start_x && global_observationnodes[i].x_index < thread_end_x) {
context->num_partial_obervationnodes++;
}
}
context->partial_observationnodes = malloc(context->num_partial_obervationnodes * sizeof(nodetimeseries_t *));
int j = 0;
for (int i = 0; i < num_global_obervationnodes; i++) {
if (global_observationnodes != NULL
&& global_observationnodes[i].x_index >= thread_start_x && global_observationnodes[i].x_index < thread_end_x) {
context->partial_observationnodes[j] = &(global_observationnodes[i]);
j++;
}
}
//derive the partial inputs
context->number_partial_inputs = 0;
for (int i = 0; i < number_global_inputs; i++) { //count partial array elements
if (global_inputs != NULL
&& global_inputs[i].x_index >= thread_start_x && global_inputs[i].x_index < thread_end_x) {
context->number_partial_inputs++;
}
}
context->partial_inputs = malloc(context->number_partial_inputs * sizeof(nodeinputseries_t *));
j = 0;
for (int i = 0; i < number_global_inputs; i++) {
if (global_inputs != NULL
&& global_inputs[i].x_index >= thread_start_x && global_inputs[i].x_index < thread_end_x) {
context->partial_inputs[j] = &(global_inputs[i]);
j++;
}
}
}
void init_executioncontext(executioncontext_t *context) {
if (MULTITHREADING) {
context->num_threads = (int) (THREADFACTOR * system_processor_online_count());
} else {
context->num_threads = 1;
}
context->handles = malloc(context->num_threads * sizeof(threadhandle_t *));
context->contexts = malloc(context->num_threads * sizeof(partialsimulationcontext_t));
}
void output_to_csv(char *filename, int length, nodeval_t *values) {
printf("Creating %s file\n", filename);
FILE *fp = fopen(filename, "w+");
if (fp == NULL) {
printf("File is null.\n Error: %s\n", strerror(errno));
} else {
fprintf(fp, "Energy-value,");
int i;
for (i = 0; i < length; i++) {
fprintf(fp, "\n%f,", values[i]);
}
fclose(fp);
printf("%s file created.\n", filename);
}
}
int get_daytime(struct timeval *tp) {
#ifdef _WIN32
/*
* Taken from PostgerSQL's gettimeofday.c
* Win32 gettimeofday() replacement
*
* src/port/gettimeofday.c
*
* Copyright (c) 2003 SRA, Inc.
* Copyright (c) 2003 SKC, Inc.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose, without fee, and without a
* written agreement is hereby granted, provided that the above
* copyright notice and this paragraph and the following two
* paragraphs appear in all copies.
*
* IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
* DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
* IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
* SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
FILETIME file_time;
SYSTEMTIME system_time;
ULARGE_INTEGER ularge;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
ularge.LowPart = file_time.dwLowDateTime;
ularge.HighPart = file_time.dwHighDateTime;
tp->tv_sec = (long)((ularge.QuadPart - EPOCH) / 10000000L);
tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
return 0;
#else
return gettimeofday(tp, NULL);
#endif
}
void parse_file(nodeinputseries_t timeseries, const char *filename) {
FILE *stream = fopen(filename, "r");
int i = 0;
char line[1024];
while (fgets(line, 1024, stream)) {
char *tmp = strdup(line);
timeseries.timeseries[i++] = get_field(tmp, 1);
free(tmp);
}
printf("Read %d lines for file %s.\n", i, filename);
}
int get_field(char *line, int num) {
// parsing to integer
int integer = atoi(line);
return integer;
}