-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
169 lines (145 loc) · 4.17 KB
/
main.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
#if NUM_THREADS > 1
#include <pthread.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include "simulazione.h"
#include "hex.h"
struct thread_data thread_data_array[NUM_THREADS];
static void usage(int argc, char *argv[]) {
printf ("Usage: %s { start | resume } N outstring inconf.gz inlpl inloc "
"big_sigma beta_uniform beta_localized conf_volume\n"
"\n"
"Suggested parameters: start 192 ciao RAND NULL NULL 0.05 0 0 1.0\n"
"\n",
argv[0]);
exit (-1);
}
static int error_arguments(int argc, char *argv[]) {
return (argc < 2) || !((!strcmp(argv[1], "start") && (argc == 11)) ||
(!strcmp(argv[1], "resume") && (argc == 11)));
}
void sigusr1_handler(int sig) {
fprintf(stderr,
"TOTAL STEPS: %llu\n"
"STEPS DONE: %llu\n"
"STEPS TO GO: %llu\n",
mc_time.DYN_STEPS, mc_time.DYN_STEPS - mc_time.t, mc_time.t);
flushfiles();
}
void sigusr2_handler(int sig) {
rewind(simufiles.chkfile);
ftruncate(fileno(simufiles.chkfile), 0);
print_buffer(simufiles.chkfile);
flushfiles();
}
void sigterm_handler(int sig) {
fputs("Terminated\n", stderr);
fflush(stderr);
fflush(stdout);
closefiles();
exit(143);
}
static void install_sighandlers() {
struct sigaction sausr1;
sausr1.sa_handler = sigusr1_handler;
sausr1.sa_flags = 0; // or SA_RESTART
sigemptyset(&sausr1.sa_mask);
if (sigaction(SIGUSR1, &sausr1, NULL) == -1) {
perror("couldn't set USR1 signal");
}
struct sigaction sausr2;
sausr2.sa_handler = sigusr2_handler;
sausr2.sa_flags = 0; // or SA_RESTART
sigemptyset(&sausr2.sa_mask);
if (sigaction(SIGUSR2, &sausr2, NULL) == -1) {
perror("couldn't set USR2 signal");
}
// install the sigterm and sigint signal
struct sigaction saterm;
saterm.sa_handler = sigterm_handler;
saterm.sa_flags = 0; // or SA_RESTART
sigemptyset(&saterm.sa_mask);
if (sigaction(SIGTERM, &saterm, NULL) == -1) {
perror("couldn't set TERM signal");
}
struct sigaction saint;
saint.sa_handler = sigterm_handler;
saint.sa_flags = 0; // or SA_RESTART
sigemptyset(&saint.sa_mask);
if (sigaction(SIGINT, &saint, NULL) == -1) {
perror("couldn't set INT signal");
}
}
int main (int argc, char **argv) {
if (error_arguments(argc, argv))
usage(argc, argv);
// install the sigusr signal
install_sighandlers();
// create simulation thread
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;
int rc;
long t;
void *status;
/* Initialize and set thread detached attribute */
#if NUM_THREADS > 1
pthread_spin_init(&spinsum, 0);
pthread_barrier_init(&startbarr, NULL, NUM_THREADS);
pthread_barrier_init(&firstbarr, NULL, NUM_THREADS);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
#endif
for(t=0; t<NUM_THREADS; t++) {
thread_data_array[t].thread_id = t;
thread_data_array[t].argc = argc;
thread_data_array[t].argv = argv;
if(t == 0) { // simulation thread
#if NUM_THREADS > 1
rc = pthread_create(&thread[t], &attr, simulazione,
(void *) &thread_data_array[t]);
#else
simulazione((void *)&thread_data_array[t]);
rc = 0;
#endif
} else if(t == 1) { // input thread
#if NUM_THREADS > 1
void *inputmain(void *threadarg);
rc = pthread_create(&thread[t], &attr, inputmain,
(void *) &thread_data_array[t]);
#endif
} else if(t == 2) { // glut thread
#if NUM_THREADS > 2
void *glumain(void *threadarg);
rc = pthread_create(&thread[t], &attr, glumain,
(void *) &thread_data_array[t]);
#endif
} /* else { */
/* rc = pthread_create(&thread[t], &attr, interactThread, */
/* (void *) &thread_data_array[t]); */
/* } */
if (rc) {
fprintf(stderr, "ERROR; return code from pthread_create()"
" is %d\n", rc);
exit(-1);
}
}
/* Free attribute and wait for the other threads */
#if NUM_THREADS > 1
pthread_attr_destroy(&attr);
for(t=0; t<NUM_THREADS; t++) {
rc = pthread_join(thread[t], &status);
if (rc) {
fprintf(stderr, "ERROR; return code from pthread_join()"
" is %d\n", rc);
exit(-1);
}
}
pthread_spin_destroy(&spinsum);
pthread_exit(NULL);
#endif
return 0;
}