2008-03-14 17:35:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2008 University of California
|
|
|
|
//
|
|
|
|
// This 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 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This software 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.
|
|
|
|
//
|
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
2008-03-14 19:55:55 +00:00
|
|
|
// Example multi-thread BOINC application.
|
|
|
|
// It does 64 "blocks" of computation, where each block is about 1 GFLOP.
|
|
|
|
// It divides this among a number N of "worker" threads.
|
|
|
|
// N is passed in through init_data.xml, and defaults to 4.
|
|
|
|
//
|
|
|
|
// The main issue is how to suspend/resume the threads.
|
|
|
|
// The standard BOINC API doesn't work - it assumes that
|
|
|
|
// the initial thread is the only one.
|
|
|
|
// What we do instead is to have our initial thread launch the worker threads,
|
|
|
|
// then go into a polling loop where it checks for suspend/resume messages
|
|
|
|
// from the BOINC client, and handles them itself.
|
2008-03-14 17:35:22 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <vector>
|
2008-03-14 19:55:55 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#else
|
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;
|
|
|
|
|
2008-03-18 21:15:21 +00:00
|
|
|
#define DEFAULT_NTHREADS 1
|
2008-03-14 19:55:55 +00:00
|
|
|
#define TOTAL_GFLOPS 64
|
|
|
|
|
|
|
|
int gflops_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;
|
|
|
|
|
|
|
|
void start(THREAD_FUNC);
|
|
|
|
void suspend(bool);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct THREAD_SET {
|
|
|
|
vector<THREAD*> threads;
|
|
|
|
void suspend(bool if_susp) {
|
|
|
|
for (unsigned int i=0; i<threads.size(); i++) {
|
|
|
|
THREAD* t = threads[i];
|
|
|
|
if (t->id != THREAD_ID_NULL) t->suspend(if_susp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool done() {
|
|
|
|
for (unsigned int i=0; i<threads.size(); i++) {
|
|
|
|
THREAD* t = threads[i];
|
|
|
|
if (t->id != THREAD_ID_NULL) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2008-03-14 19:55:55 +00:00
|
|
|
void THREAD::start(THREAD_FUNC func) {
|
|
|
|
id = (HANDLE) _beginthreadex(
|
2008-03-14 17:35:22 +00:00
|
|
|
NULL,
|
|
|
|
16384,
|
|
|
|
func,
|
2008-03-14 19:55:55 +00:00
|
|
|
this,
|
2008-03-14 17:35:22 +00:00
|
|
|
0,
|
2008-03-14 19:55:55 +00:00
|
|
|
NULL
|
2008-03-14 17:35:22 +00:00
|
|
|
);
|
2008-03-14 19:55:55 +00:00
|
|
|
if (!id) {
|
|
|
|
fprintf(stderr, "Can't start thread\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void THREAD::suspend(bool if_susp) {
|
|
|
|
if (if_susp) {
|
|
|
|
SuspendThread(id);
|
|
|
|
} else {
|
|
|
|
ResumeThread(id);
|
|
|
|
}
|
2008-03-14 17:35:22 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void THREAD::start(THREAD_FUNC func) {
|
|
|
|
int retval;
|
|
|
|
retval = pthread_create(&id, 0, func, (void*)this);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "can't start thread\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void THREAD::suspend(bool if_susp) {
|
|
|
|
pthread_kill(id, if_susp?SIGSTOP:SIGCONT);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-03-14 19:55:55 +00:00
|
|
|
// do a billion floating-point ops
|
|
|
|
//
|
2008-03-14 17:35:22 +00:00
|
|
|
static void giga_flop() {
|
|
|
|
double j = 3.14159;
|
|
|
|
int i;
|
|
|
|
for (i=0; i<500000000; i++) {
|
|
|
|
j += 5.12313123;
|
|
|
|
j *= 0.5398394834;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2008-03-14 17:35:22 +00:00
|
|
|
THREAD* t = (THREAD*)p;
|
2008-03-14 19:55:55 +00:00
|
|
|
for (int i=0; i<gflops_per_thread; i++) {
|
2008-03-14 17:35:22 +00:00
|
|
|
giga_flop();
|
|
|
|
fprintf(stderr, "thread %d finished %d\n", t->index, i);
|
|
|
|
}
|
|
|
|
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-14 19:55:55 +00:00
|
|
|
int main(int argc, char** argv) {
|
2008-03-14 17:35:22 +00:00
|
|
|
int i;
|
|
|
|
THREAD_SET thread_set;
|
|
|
|
BOINC_OPTIONS options;
|
|
|
|
BOINC_STATUS status;
|
|
|
|
APP_INIT_DATA aid;
|
2008-03-14 19:55:55 +00:00
|
|
|
int nthreads = DEFAULT_NTHREADS;
|
2008-03-18 21:15:21 +00:00
|
|
|
double start_time = dtime();
|
2008-03-14 17:35:22 +00:00
|
|
|
|
2008-03-14 19:55:55 +00:00
|
|
|
boinc_options_defaults(options);
|
2008-03-14 17:35:22 +00:00
|
|
|
options.direct_process_action = false;
|
|
|
|
boinc_init_options(&options);
|
|
|
|
boinc_get_status(&status);
|
|
|
|
boinc_get_init_data(aid);
|
2008-03-14 19:55:55 +00:00
|
|
|
if (strlen(aid.opaque)) {
|
|
|
|
parse_int(aid.opaque, "<nthreads>", nthreads);
|
|
|
|
}
|
|
|
|
gflops_per_thread = TOTAL_GFLOPS/nthreads;
|
2008-03-14 17:35:22 +00:00
|
|
|
|
|
|
|
for (i=0; i<nthreads; i++) {
|
|
|
|
THREAD* t = new THREAD;
|
|
|
|
t->index = i;
|
|
|
|
t->start(worker);
|
|
|
|
thread_set.threads.push_back(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (thread_set.done()) break;
|
|
|
|
bool old_susp = status.suspended;
|
|
|
|
boinc_get_status(&status);
|
|
|
|
if (status.suspended != old_susp) {
|
|
|
|
thread_set.suspend(status.suspended);
|
|
|
|
}
|
2008-03-14 19:55:55 +00:00
|
|
|
boinc_sleep(0.1);
|
2008-03-14 17:35:22 +00:00
|
|
|
}
|
2008-03-18 21:15:21 +00:00
|
|
|
double elapsed_time = dtime()-start_time;
|
|
|
|
fprintf(stderr,
|
|
|
|
"All done. Used %d threads. Elapsed time %f\n",
|
|
|
|
nthreads, elapsed_time
|
|
|
|
);
|
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
|