-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjonswap_multithread.c
181 lines (141 loc) · 5.17 KB
/
jonswap_multithread.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
/*
Mock-up implementation using multithreading to benchmark performance.
This does not do anything specific rather than calculating various
spectra and timetraces with very refined steps.
Compile command:
gcc jonswap_multithread.c -o jonswap_multithread -lm -lpthread -Wno-implicit-function-declaration
#+AUTHOR Rafael Rossi
#+DATE 25-Oct-2019
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <pthread.h>
#include <time.h>
#include "libjonswap.h"
#define MAX_NUM_THREADS 99
void* worker_thread(void *vargp);
double randbetween(double a, double b);
struct WorkerThreadArgs {
double hs;
double tp;
double tf;
double ts;
int seed;
} thread_args[MAX_NUM_THREADS];
int main(int argc, char *argv[]) {
pthread_t thread_id[MAX_NUM_THREADS];
int num_threads;
struct timespec start, finish;
double elapsed;
srandom(time(NULL));
if(argc == 1) {
// default option
num_threads = 3;
} else {
int n = atoi(argv[1]);
num_threads = n < MAX_NUM_THREADS ? n : MAX_NUM_THREADS;
}
// define a queue of random args for the threads
for(int i=0; i<num_threads; ++i) {
thread_args[i] = (struct WorkerThreadArgs) {
.hs=randbetween(1.5, 3.5),
.tp=randbetween(6, 14),
.tf=10000,
.ts=randbetween(0.01, 0.25),
.seed=random()
};
}
////////////////////////////////////////////////////////////////////////
/////////// Running multi threaded /////////////////////////////////////
////////////////////////////////////////////////////////////////////////
printf("Running %d thread%c.\n", num_threads, num_threads > 1 ? 's':' ');
clock_gettime(CLOCK_MONOTONIC, &start);
// create and launch all threads
for(int i=0; i<num_threads; ++i)
pthread_create(&thread_id[i], NULL, worker_thread, thread_args+i);
// wait until all threads are done
for(int i=0; i<num_threads; ++i)
pthread_join(thread_id[i], NULL);
clock_gettime(CLOCK_MONOTONIC, &finish);
elapsed = (finish.tv_sec - start.tv_sec);
elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
printf("Done with all threads - %.3fs\n", elapsed);
////////////////////////////////////////////////////////////////////////
/////////// Running single threaded/////////////////////////////////////
////////////////////////////////////////////////////////////////////////
printf("Running sequentially.\n");
clock_gettime(CLOCK_MONOTONIC, &start);
// create and launch all threads
for(int i=0; i<num_threads; ++i) {
pthread_create(&thread_id[i], NULL, worker_thread, thread_args+i);
pthread_join(thread_id[i], NULL); // join after create will wait until it is done
}
clock_gettime(CLOCK_MONOTONIC, &finish);
elapsed = (finish.tv_sec - start.tv_sec);
elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
printf("Done sequentially - %.3fs\n", elapsed);
// printf("Press Enter to exit.\n");
// char input[5];
// fgets(input, sizeof input, stdin);
exit(0);
}
double randbetween(double a, double b) {
return a + (double) rand() /(double) (RAND_MAX/(b - a));
}
void* worker_thread(void *vargp) {
struct WorkerThreadArgs* args = (struct WorkerThreadArgs *) vargp;
double hs = args->hs;
double tp = args->tp;
double tf = args->tf;
double ts = args->ts;
long int seed = args->seed;
double to;
double w1, w2;
double *t, *w, *pm, *js, *amp, *phi, *tt, *eta;
double (*fgamma)(double, double);
int nharms, tt_size;
short show_spectrum, show_timetrace;
// hard coded inits
w1 = 0.2;
w2 = 6.28;
to = 0.0;
nharms = 200;
fgamma = jonswap_gamma;
show_timetrace = 0;
show_spectrum = 0;
if (verbose) printf("Hs %.2f m\nTp %.2f s \nDuration %.2f s \nStep %.2f s \nnharms %d\nseed %ld\n",
hs, tp, tf, ts, nharms, seed);
w = freq_domain(w1, w2, nharms);
t = period_domain(w, nharms);
pm = pierson_moskowitz_spectrum(hs, tp, nharms, w);
js = jonswap_spectrum(hs, tp, fgamma, nharms, w, pm);
amp = jonswap_component_amplitude(js, w, nharms);
phi = phases(nharms, seed);
tt = time_domain(to, tf, ts, &tt_size);
eta = wave_elevation(amp, w, phi, tt, nharms, tt_size);
if (verbose)
// checking using the relationship zero-th moment is Hs squared divided by 16
// m0 = Hs**2 / 16 ==> Hs = 4*sqrt(m0)
printf("Check Hs %.2f m\n", 4*sqrt(spectral_moment(0, js, w, nharms)));
if (show_spectrum){
if (verbose) {
printf("\nSpectrum\n\n");
printf("%10s %10s %12s %12s %12s %10s \n", "T", "w", "PM", "JS", "amp", "phi");
printf("%10s %10s %12s %12s %12s %10s \n", "[s]", "[rd/s]", "[m2s/rd]", "[m2s/rd]", "[m]", "[rd]");
for (int i = 0; i < nharms; ++i)
printf("%10.3f %10.3f %12.5f %12.5f %12.5f %10.3f\n", t[i], w[i], pm[i], js[i], amp[i], phi[i]);
}
}
if (show_timetrace) {
if (verbose) {
printf("\nTime History\n\n");
printf("%10s %10s \n", "Time", "Elevation");
for (int j = 0; j < tt_size; ++j)
printf("%10.2f %10.3f\n", tt[j], eta[j]);
}
}
printf("Done with Hs %.1f Tp %4.1f dt %.2f seed %ld\n", hs, tp, ts, seed);
return NULL;
}