2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2008-03-14 17:35:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2008 University of California
|
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// 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.
|
2008-03-14 17:35:22 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2008-03-14 17:35:22 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2008-03-14 17:35:22 +00:00
|
|
|
|
2008-03-14 19:55:55 +00:00
|
|
|
// Example multi-thread BOINC application.
|
2010-01-27 03:55:46 +00:00
|
|
|
// 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.
|
2008-03-28 17:39:00 +00:00
|
|
|
// It divides this among N "worker" threads.
|
|
|
|
// N is passed in the command line, and defaults to 1.
|
2008-03-27 18:00:40 +00:00
|
|
|
//
|
2008-03-28 17:39:00 +00:00
|
|
|
// Doesn't do checkpointing.
|
2008-03-14 17:35:22 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <vector>
|
2008-03-14 19:55:55 +00:00
|
|
|
#ifdef _WIN32
|
2010-01-27 03:55:46 +00:00
|
|
|
#include "boinc_win.h"
|
2008-03-14 19:55:55 +00:00
|
|
|
#else
|
2008-03-28 17:39:00 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2008-03-14 17:35:22 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <pthread.h>
|
2008-03-14 19:55:55 +00:00
|
|
|
#endif
|
2008-03-14 17:35:22 +00:00
|
|
|
|
2008-03-14 19:55:55 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "str_util.h"
|
2008-03-14 17:35:22 +00:00
|
|
|
#include "boinc_api.h"
|
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
2010-01-01 06:03:08 +00:00
|
|
|
#define DEFAULT_NTHREADS 4
|
|
|
|
#define TOTAL_UNITS 16
|
2008-03-14 19:55:55 +00:00
|
|
|
|
2008-03-28 17:39:00 +00:00
|
|
|
int units_per_thread;
|
2008-03-14 17:35:22 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2008-03-14 19:55:55 +00:00
|
|
|
typedef HANDLE THREAD_ID;
|
|
|
|
typedef UINT (__stdcall *THREAD_FUNC)(void*);
|
2008-03-14 17:35:22 +00:00
|
|
|
#else
|
2008-03-14 19:55:55 +00:00
|
|
|
typedef void* (*THREAD_FUNC)(void*);
|
2008-03-14 17:35:22 +00:00
|
|
|
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;
|
2008-03-27 18:00:40 +00:00
|
|
|
int units_done;
|
2008-03-14 17:35:22 +00:00
|
|
|
|
2008-03-28 17:39:00 +00:00
|
|
|
THREAD(THREAD_FUNC func, int i) {
|
2010-06-21 23:29:37 +00:00
|
|
|
char buf[256];
|
|
|
|
|
2008-03-28 17:39:00 +00:00
|
|
|
index = i;
|
|
|
|
units_done = 0;
|
|
|
|
#ifdef _WIN32
|
|
|
|
id = (HANDLE) _beginthreadex(
|
|
|
|
NULL,
|
|
|
|
16384,
|
|
|
|
func,
|
|
|
|
this,
|
|
|
|
0,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
if (!id) {
|
2010-09-15 23:03:30 +00:00
|
|
|
fprintf(stderr, "%s Can't start thread\n",
|
|
|
|
boinc_msg_prefix(buf, sizeof(buf))
|
|
|
|
);
|
2008-03-28 17:39:00 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int retval;
|
|
|
|
retval = pthread_create(&id, 0, func, (void*)this);
|
|
|
|
if (retval) {
|
2010-09-15 23:03:30 +00:00
|
|
|
fprintf(stderr, "%s can't start thread\n",
|
|
|
|
boinc_msg_prefix(buf, sizeof(buf))
|
|
|
|
);
|
2008-03-28 17:39:00 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2008-03-14 17:35:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct THREAD_SET {
|
|
|
|
vector<THREAD*> threads;
|
2008-03-28 17:39:00 +00:00
|
|
|
bool all_done() {
|
2008-03-14 17:35:22 +00:00
|
|
|
for (unsigned int i=0; i<threads.size(); i++) {
|
2008-03-28 17:39:00 +00:00
|
|
|
if (threads[i]->id != THREAD_ID_NULL) return false;
|
2008-03-14 17:35:22 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2008-03-27 18:00:40 +00:00
|
|
|
int units_done() {
|
|
|
|
int count = 0;
|
|
|
|
for (unsigned int i=0; i<threads.size(); i++) {
|
2008-03-28 17:39:00 +00:00
|
|
|
count += threads[i]->units_done;
|
2008-03-27 18:00:40 +00:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2008-03-14 17:35:22 +00:00
|
|
|
};
|
|
|
|
|
2008-03-14 19:55:55 +00:00
|
|
|
// do a billion floating-point ops
|
2008-03-20 22:52:16 +00:00
|
|
|
// (note: I needed to add an arg to this;
|
|
|
|
// otherwise the MS C++ compiler optimizes away
|
|
|
|
// all but the first call to it!)
|
2008-03-14 19:55:55 +00:00
|
|
|
//
|
2008-03-28 17:39:00 +00:00
|
|
|
static double do_a_giga_flop(int foo) {
|
2008-03-20 22:52:16 +00:00
|
|
|
double x = 3.14159*foo;
|
2008-03-14 17:35:22 +00:00
|
|
|
int i;
|
|
|
|
for (i=0; i<500000000; i++) {
|
2008-03-20 22:52:16 +00:00
|
|
|
x += 5.12313123;
|
|
|
|
x *= 0.5398394834;
|
2008-03-14 17:35:22 +00:00
|
|
|
}
|
2008-03-20 22:52:16 +00:00
|
|
|
return x;
|
2008-03-14 17:35:22 +00:00
|
|
|
}
|
|
|
|
|
2008-03-14 19:55:55 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
UINT WINAPI worker(void* p) {
|
|
|
|
#else
|
2008-03-14 17:35:22 +00:00
|
|
|
void* worker(void* p) {
|
2008-03-14 19:55:55 +00:00
|
|
|
#endif
|
2010-06-21 23:29:37 +00:00
|
|
|
char buf[256];
|
2008-03-14 17:35:22 +00:00
|
|
|
THREAD* t = (THREAD*)p;
|
2008-03-28 17:39:00 +00:00
|
|
|
for (int i=0; i<units_per_thread; i++) {
|
|
|
|
double x = do_a_giga_flop(i);
|
2008-03-27 18:00:40 +00:00
|
|
|
t->units_done++;
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s thread %d finished %d: %f\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), t->index, i, x
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2008-03-14 17:35:22 +00:00
|
|
|
}
|
|
|
|
t->id = THREAD_ID_NULL;
|
2008-03-14 19:55:55 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return 0;
|
|
|
|
#endif
|
2008-03-14 17:35:22 +00:00
|
|
|
}
|
|
|
|
|
2008-03-28 17:39:00 +00:00
|
|
|
int main(int argc, char** argv) {
|
2010-01-01 06:03:08 +00:00
|
|
|
int i, nthreads = DEFAULT_NTHREADS;
|
2008-03-18 21:15:21 +00:00
|
|
|
double start_time = dtime();
|
2010-06-21 23:29:37 +00:00
|
|
|
char buf[256];
|
2008-03-14 17:35:22 +00:00
|
|
|
|
2012-12-12 23:12:40 +00:00
|
|
|
BOINC_OPTIONS options;
|
|
|
|
boinc_options_defaults(options);
|
|
|
|
options.multi_thread = true;
|
2011-05-07 17:20:56 +00:00
|
|
|
boinc_init_options(&options);
|
2008-03-27 18:00:40 +00:00
|
|
|
|
2010-01-01 06:03:08 +00:00
|
|
|
for (i=1; i<argc; i++) {
|
2008-03-27 18:00:40 +00:00
|
|
|
if (!strcmp(argv[i], "--nthreads")) {
|
|
|
|
nthreads = atoi(argv[++i]);
|
|
|
|
} else {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s unrecognized arg: %s\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), argv[i]
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2008-03-27 18:00:40 +00:00
|
|
|
}
|
2008-03-14 19:55:55 +00:00
|
|
|
}
|
2008-03-27 18:00:40 +00:00
|
|
|
|
2008-03-28 17:39:00 +00:00
|
|
|
units_per_thread = TOTAL_UNITS/nthreads;
|
2008-03-14 17:35:22 +00:00
|
|
|
|
2010-01-01 06:03:08 +00:00
|
|
|
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);
|
2008-03-14 17:35:22 +00:00
|
|
|
}
|
2008-03-28 17:39:00 +00:00
|
|
|
|
2008-03-18 21:15:21 +00:00
|
|
|
double elapsed_time = dtime()-start_time;
|
|
|
|
fprintf(stderr,
|
2009-03-27 23:42:47 +00:00
|
|
|
"%s All done. Used %d threads. Elapsed time %f\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), nthreads, elapsed_time
|
2008-03-18 21:15:21 +00:00
|
|
|
);
|
2008-03-14 19:55:55 +00:00
|
|
|
boinc_finish(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
|
|
|
|
LPSTR command_line;
|
|
|
|
char* argv[100];
|
|
|
|
int argc;
|
|
|
|
|
|
|
|
command_line = GetCommandLine();
|
|
|
|
argc = parse_command_line( command_line, argv );
|
|
|
|
return main(argc, argv);
|
2008-03-14 17:35:22 +00:00
|
|
|
}
|
2008-03-18 21:15:21 +00:00
|
|
|
#endif
|