forked from BOINC/boinc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_thread.cpp
More file actions
194 lines (174 loc) · 4.87 KB
/
Copy pathmulti_thread.cpp
File metadata and controls
194 lines (174 loc) · 4.87 KB
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
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
// Example multi-thread BOINC application.
// This app defines its own classes (THREAD, THREAD_SET) for managing threads.
// You can also use libraries such as OpenMP.
// Just make sure you call boinc_init_parallel().
//
// This app does 64 "units" of computation, where each units is about 1 GFLOP.
// It divides this among N "worker" threads.
// N is passed in the command line, and defaults to 1.
//
// Doesn't do checkpointing.
#include <stdio.h>
#include <vector>
#ifdef _WIN32
#include "boinc_win.h"
#else
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>
#endif
#include "util.h"
#include "str_util.h"
#include "boinc_api.h"
using std::vector;
#define DEFAULT_NTHREADS 4
#define TOTAL_UNITS 16
int units_per_thread;
#ifdef _WIN32
typedef HANDLE THREAD_ID;
typedef UINT (__stdcall *THREAD_FUNC)(void*);
#else
typedef void* (*THREAD_FUNC)(void*);
typedef pthread_t THREAD_ID;
#endif
#define THREAD_ID_NULL 0
// An abstraction of threads.
// A thread function is passed a pointer to its own object,
// and sets its ID to THREAD_ID_NULL when it's finished.
//
struct THREAD {
THREAD_ID id;
int index;
int units_done;
THREAD(THREAD_FUNC func, int i) {
char buf[256];
index = i;
units_done = 0;
#ifdef _WIN32
id = (HANDLE) _beginthreadex(
NULL,
16384,
func,
this,
0,
NULL
);
if (!id) {
fprintf(stderr, "%s Can't start thread\n",
boinc_msg_prefix(buf, sizeof(buf))
);
exit(1);
}
#else
int retval;
retval = pthread_create(&id, 0, func, (void*)this);
if (retval) {
fprintf(stderr, "%s can't start thread\n",
boinc_msg_prefix(buf, sizeof(buf))
);
exit(1);
}
#endif
}
};
struct THREAD_SET {
vector<THREAD*> threads;
bool all_done() {
for (unsigned int i=0; i<threads.size(); i++) {
if (threads[i]->id != THREAD_ID_NULL) return false;
}
return true;
}
int units_done() {
int count = 0;
for (unsigned int i=0; i<threads.size(); i++) {
count += threads[i]->units_done;
}
return count;
}
};
// do a billion floating-point ops
// (note: I needed to add an arg to this;
// otherwise the MS C++ compiler optimizes away
// all but the first call to it!)
//
static double do_a_giga_flop(int foo) {
double x = 3.14159*foo;
int i;
for (i=0; i<500000000; i++) {
x += 5.12313123;
x *= 0.5398394834;
}
return x;
}
#ifdef _WIN32
UINT WINAPI worker(void* p) {
#else
void* worker(void* p) {
#endif
char buf[256];
THREAD* t = (THREAD*)p;
for (int i=0; i<units_per_thread; i++) {
double x = do_a_giga_flop(i);
t->units_done++;
fprintf(stderr, "%s thread %d finished %d: %f\n",
boinc_msg_prefix(buf, sizeof(buf)), t->index, i, x
);
}
t->id = THREAD_ID_NULL;
#ifdef _WIN32
return 0;
#endif
}
int main(int argc, char** argv) {
int i, nthreads = DEFAULT_NTHREADS;
double start_time = dtime();
char buf[256];
BOINC_OPTIONS options;
boinc_options_defaults(options);
options.multi_thread = true;
boinc_init_options(&options);
for (i=1; i<argc; i++) {
if (!strcmp(argv[i], "--nthreads")) {
nthreads = atoi(argv[++i]);
} else {
fprintf(stderr, "%s unrecognized arg: %s\n",
boinc_msg_prefix(buf, sizeof(buf)), argv[i]
);
}
}
units_per_thread = TOTAL_UNITS/nthreads;
THREAD_SET thread_set;
for (i=0; i<nthreads; i++) {
thread_set.threads.push_back(new THREAD(worker, i));
}
while (1) {
double f = thread_set.units_done()/((double)TOTAL_UNITS);
boinc_fraction_done(f);
if (thread_set.all_done()) break;
boinc_sleep(1.0);
}
double elapsed_time = dtime()-start_time;
fprintf(stderr,
"%s All done. Used %d threads. Elapsed time %f\n",
boinc_msg_prefix(buf, sizeof(buf)), nthreads, elapsed_time
);
boinc_finish(0);
}