From 27caca9ef26aa2a22fe2f19bd657d7b3f144d711 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 14 Mar 2008 19:55:55 +0000 Subject: [PATCH] svn path=/trunk/boinc_samples/; revision=14915 --- checkin_notes | 273 +++++++++++---------- multi_thread/multi_thread.C | 86 +++++-- win_build/multi_thread.vcproj | 437 ++++++++++++++++++++++++++++++++++ win_build/samples.sln | 34 ++- 4 files changed, 673 insertions(+), 157 deletions(-) create mode 100644 win_build/multi_thread.vcproj diff --git a/checkin_notes b/checkin_notes index 1159584b30..0020c4eaf5 100644 --- a/checkin_notes +++ b/checkin_notes @@ -24,9 +24,9 @@ David 12 Apr 2006 uppercase/ uc_graphics.C (new) - upper_case.C - win_build/ - upper_case.vcproj + upper_case.C + win_build/ + upper_case.vcproj David 13 Apr 2006 - got graphics to work under Linux; added a Makefile @@ -37,10 +37,10 @@ David 13 Apr 2006 upper_case.C David 13 Apr 2006 - - handle mouse events + - handle mouse events - uppercase/ - uc_graphics.C + uppercase/ + uc_graphics.C David 21 Apr 2006 - resolve logo filename @@ -62,17 +62,17 @@ Charlie 27 April 2006 project.pbxproj David 29 Apr 2006 - - use project preferences () for graphics color - - remove -exit, -signal options from upper_case + - use project preferences () for graphics color + - remove -exit, -signal options from upper_case - uppercase/ + uppercase/ uc_graphics.C - upper_case.C + upper_case.C David 1 May 2006 - - implement boinc_reread_prefs() + - implement boinc_reread_prefs() - uppercase/ + uppercase/ uc_graphics.C David 26 May 2006 @@ -85,9 +85,9 @@ David 26 May 2006 David 29 May 2006 - moved wrapper application here from boinc/apps - win_build - sleeper.vcproj - wrapper.vcproj + win_build + sleeper.vcproj + wrapper.vcproj wrapper/ wrapper.C Makefile @@ -95,7 +95,7 @@ David 29 May 2006 Rom 31 May 2006 - Demonstrate delay load functionality on graphics libraries for Windows. - uppercase/ + uppercase/ uc_graphics.C David 13 June 2006 @@ -106,33 +106,33 @@ David 13 June 2006 worker.C David 13 June 2006 - - added Win project file for worker, - fixed names in project files, got stuff to compile + - added Win project file for worker, + fixed names in project files, got stuff to compile - uppercase/ - upper_case.C - win_build/ - samples.sln - sleeper.vcproj - worker.vcproj (new) - worker/ - worker.C - wrapper/ - wrapper.C + uppercase/ + upper_case.C + win_build/ + samples.sln + sleeper.vcproj + worker.vcproj (new) + worker/ + worker.C + wrapper/ + wrapper.C David 14 June 2006 - - debugged and updated "wrapper" program. - It works now, more or less, on Windows. - - wrapper no longer copies input/output files. - You must use the attribute - in the file reference instead. - - attempted to have wrapper pass stderr handle to worker - (Windows). Doesn't work yet. + - debugged and updated "wrapper" program. + It works now, more or less, on Windows. + - wrapper no longer copies input/output files. + You must use the attribute + in the file reference instead. + - attempted to have wrapper pass stderr handle to worker + (Windows). Doesn't work yet. - worker/ - worker.C - wrapper/ - wrapper.C + worker/ + worker.C + wrapper/ + wrapper.C David 16 June 2006 - generalized wrapper.C so task descriptions are kept in structs; @@ -144,20 +144,20 @@ David 16 June 2006 wrapper.C David 12 Aug 2006 - - wrapper: add support for stdin/stdout files - (adapted from Eric Myers) - - changed project files to VS 2005 + - wrapper: add support for stdin/stdout files + (adapted from Eric Myers) + - changed project files to VS 2005 - win_build/ - *.vcproj - wrapper/ - wrapper.C + win_build/ + *.vcproj + wrapper/ + wrapper.C David 16 Aug 2006 - - wrapper: get it to compile on Win, and fix XML parsing + - wrapper: get it to compile on Win, and fix XML parsing - wrapper/ - wrapper.C + wrapper/ + wrapper.C David 9 Sept 2006 - change wrapper to use new XML parser @@ -176,9 +176,9 @@ David 11 Sept 2006 wrapper/Makefile David 11 Sept 2006 - - include all apps in VS solution + - include all apps in VS solution - win_build/samples.sln + win_build/samples.sln David 26 Sept 2006 - close state file in uppercase (from Ian/Tigher) @@ -198,17 +198,17 @@ Rom 6 Nov 2006 - Checkin VS 2003 project files. David 8 Nov 2006 - - The upper_case project file (but not the others) - needs GDI32.DLL;OPENGL32.DLL;GLU32.DLL in its list - of Delay Loaded DLLs (Linker properties), - and delayimp.lib in its Additional Dependencies + - The upper_case project file (but not the others) + needs GDI32.DLL;OPENGL32.DLL;GLU32.DLL in its list + of Delay Loaded DLLs (Linker properties), + and delayimp.lib in its Additional Dependencies - uppercase/ - logo.jpg (new) + uppercase/ + logo.jpg (new) David 23 Nov 2006 - don't include util.h in worker.C! - - fix win compile + - fix win compile worker/ worker.C @@ -224,61 +224,61 @@ David 24 Nov 2006 job.xml David 25 Nov 2006 - - wrapper: redirection of stdin/stdout wasn't - working correctly on Windows. - Need to open files with CreateFile(). + - wrapper: redirection of stdin/stdout wasn't + working correctly on Windows. + Need to open files with CreateFile(). - win_build/ - samples.sln - wrapper/ - wrapper.C + win_build/ + samples.sln + wrapper/ + wrapper.C David 5 Dec 2006 - - changed upper_case options to -early_crash - -early_exit + - changed upper_case options to -early_crash + -early_exit - uppercase/ - upper_case.C + uppercase/ + upper_case.C David 8 Dec 2006 - - added -early_sleep option to upper_case - (for debugging core client) + - added -early_sleep option to upper_case + (for debugging core client) - uppercase/ - uc_graphics.C - upper_case.C + uppercase/ + uc_graphics.C + upper_case.C David 18 Dec 2006 - - compile fix + - compile fix - win_build/ - libboinc_vcproj + win_build/ + libboinc_vcproj David 27 Dec 2006 - comment out boinc_graphics_possible() call - uppercase/ - upper_case.C + uppercase/ + upper_case.C David 11 Jan 2007 - upper_case: if checkpoint found, truncate the output file to the indicated size. Otherwise a checkpoint/restart generally produces a bad result. - uppercase/ - upper_case.C + uppercase/ + upper_case.C David 21 Feb 2007 - - compile fixes + - compile fixes - sleeper/ - sleeper.C - uppercase/ - upper_case.C - win_build/ - libboinc.vcproj - wrapper/ - wrapper.C + sleeper/ + sleeper.C + uppercase/ + upper_case.C + win_build/ + libboinc.vcproj + wrapper/ + wrapper.C David 20 Mar 2007 - wrapper (Unix) get actual CPU time of child (not wall time) @@ -318,38 +318,38 @@ Rom 9 May 2007 *.vcproj David 22 May 2007 - - add version 6-style app + - add version 6-style app - example_app/ - Makefile - uc2.C - uc2_graphics.C + example_app/ + Makefile + uc2.C + uc2_graphics.C David 22 May 2007 - - first stab at getting it to work on Win + - first stab at getting it to work on Win - win_build/ - libboincapi.vcproj - libgraphics2.vcproj (new) - samples.sln - uc2.vcproj (new) - uc2_graphics.vcproj (new) + win_build/ + libboincapi.vcproj + libgraphics2.vcproj (new) + samples.sln + uc2.vcproj (new) + uc2_graphics.vcproj (new) David 23 May 2007 - - version 6 example app works on Win + - version 6 example app works on Win - example_app/ - uc2.C - uc2.h (new) - uc2_graphics.C - win_build/ - libgraphics2.vcproj - samples.sln - uc2.vcproj - uc2_graphics.vcproj + example_app/ + uc2.C + uc2.h (new) + uc2_graphics.C + win_build/ + libgraphics2.vcproj + samples.sln + uc2.vcproj + uc2_graphics.vcproj David 23 May 2007 - - version 6 example app works on Linux + - version 6 example app works on Linux example_app/ uc2.h @@ -370,23 +370,23 @@ David 8 June 2007 wrapper.C David 8 June 2007 - example_app/ - uc2.C,h - uc2_graphics.C + example_app/ + uc2.C,h + uc2_graphics.C David 13 June 2007 - - wrapper: set thread priority of worker processes - to IDLE class (Win); - set prior of worker processes to PROCESS_IDLE_PRIORITY (Unix) + - wrapper: set thread priority of worker processes + to IDLE class (Win); + set prior of worker processes to PROCESS_IDLE_PRIORITY (Unix) - uppercase/ - upper_case.C - wrapper/ - wrapper.C + uppercase/ + upper_case.C + wrapper/ + wrapper.C David 19 June 2007 - example_app/ - uc2_graphics.C + example_app/ + uc2_graphics.C David 28 June 2007 - wrapper: report CPU time correctly when >1 job @@ -416,10 +416,10 @@ Charlie 29 Aug 2007 project.pbxproj David 17 Aug 2007 - - project file fix + - project file fix - win_build/ - libgraphics2.vcproj + win_build/ + libgraphics2.vcproj Charlie 29 Aug 2007 - Mac: new XCode project for version 6 example_app. @@ -483,11 +483,11 @@ David 26 Sept 2007 uc2.C David 26 Sept 2007 - - minor stuff + - minor stuff - example_app - uc2.C - uc2_graphics.C + example_app + uc2.C + uc2_graphics.C Charlie 27 Sep 2007 Mac: remove "-DAPP_GRAPHICS" from XCode project C flags to fix a @@ -595,3 +595,12 @@ David 14 Mar 2008 multi_thread/ Makefile multi_thread.C + +David 14 Mar 2008 + - get multi_thread to work on Windows, and fix it up + + multi_thread/ + multi_thread.C + win_build/ + multi_thread.vcproj (new) + samples.sln diff --git a/multi_thread/multi_thread.C b/multi_thread/multi_thread.C index 96ff5b9c1a..241dc1f04c 100644 --- a/multi_thread/multi_thread.C +++ b/multi_thread/multi_thread.C @@ -17,24 +17,42 @@ // or write to the Free Software Foundation, Inc., // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// Example multi-thread BOINC application -// The main thread spawns N threads, each of which computers for X seconds -// It handles BOINC's process control messages itself. +// 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. #include #include +#ifdef _WIN32 +#else #include #include +#endif +#include "util.h" +#include "str_util.h" #include "boinc_api.h" using std::vector; -int ngflops = 10; +#define DEFAULT_NTHREADS 4 +#define TOTAL_GFLOPS 64 + +int gflops_per_thread; #ifdef _WIN32 -typedef UINT THREAD_ID; +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 @@ -43,7 +61,6 @@ typedef pthread_t THREAD_ID; // A thread function is passed a pointer to its own object, // and sets its ID to THREAD_ID_NULL when it's finished. // -typedef void* (*THREAD_FUNC)(void*); struct THREAD { THREAD_ID id; @@ -71,19 +88,26 @@ struct THREAD_SET { }; #ifdef _WIN32 -THREAD_ID create_thread(void *func()) { - uintptr_t thread; - UINT thread_id; - thread = _beginthreadex( +void THREAD::start(THREAD_FUNC func) { + id = (HANDLE) _beginthreadex( NULL, 16384, func, + this, 0, - 0, - &thread_id + NULL ); - if (!thread) return THREAD_ID_NULL; - return thread_id; + 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); + } } #else void THREAD::start(THREAD_FUNC func) { @@ -99,6 +123,8 @@ void THREAD::suspend(bool if_susp) { } #endif +// do a billion floating-point ops +// static void giga_flop() { double j = 3.14159; int i; @@ -108,27 +134,39 @@ static void giga_flop() { } } +#ifdef _WIN32 +UINT WINAPI worker(void* p) { +#else void* worker(void* p) { +#endif THREAD* t = (THREAD*)p; - for (int i=0; iindex, i); } t->id = THREAD_ID_NULL; +#ifdef _WIN32 + return 0; +#endif } -int main() { +int main(int argc, char** argv) { int i; THREAD_SET thread_set; BOINC_OPTIONS options; BOINC_STATUS status; APP_INIT_DATA aid; - int nthreads = 4; + int nthreads = DEFAULT_NTHREADS; + boinc_options_defaults(options); options.direct_process_action = false; boinc_init_options(&options); boinc_get_status(&status); boinc_get_init_data(aid); + if (strlen(aid.opaque)) { + parse_int(aid.opaque, "", nthreads); + } + gflops_per_thread = TOTAL_GFLOPS/nthreads; for (i=0; i + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/win_build/samples.sln b/win_build/samples.sln index b2a3e90e44..73dec0161b 100644 --- a/win_build/samples.sln +++ b/win_build/samples.sln @@ -36,8 +36,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "wrapper", "wrapper.vcproj", Release.AspNetCompiler.Debug = "False" EndProjectSection ProjectSection(ProjectDependencies) = postProject - {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} = {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} {0BC1DB36-030A-4321-B387-1CEE2611E329} = {0BC1DB36-030A-4321-B387-1CEE2611E329} + {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} = {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sleeper", "sleeper.vcproj", "{A9647CEA-644D-4C0A-8733-D916CD344859}" @@ -46,8 +46,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sleeper", "sleeper.vcproj", Release.AspNetCompiler.Debug = "False" EndProjectSection ProjectSection(ProjectDependencies) = postProject - {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} = {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} {0BC1DB36-030A-4321-B387-1CEE2611E329} = {0BC1DB36-030A-4321-B387-1CEE2611E329} + {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} = {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "worker", "worker.vcproj", "{F1BE6109-586D-448E-8C5B-D5C2CB874EA2}" @@ -65,9 +65,9 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "uc2", "uc2.vcproj", "{CCB9A Release.AspNetCompiler.Debug = "False" EndProjectSection ProjectSection(ProjectDependencies) = postProject - {0BC1DB36-030A-4321-B387-1CEE2611E329} = {0BC1DB36-030A-4321-B387-1CEE2611E329} - {814EBFD3-3CE6-4933-A580-C1FE3147ACB4} = {814EBFD3-3CE6-4933-A580-C1FE3147ACB4} {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} = {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} + {814EBFD3-3CE6-4933-A580-C1FE3147ACB4} = {814EBFD3-3CE6-4933-A580-C1FE3147ACB4} + {0BC1DB36-030A-4321-B387-1CEE2611E329} = {0BC1DB36-030A-4321-B387-1CEE2611E329} EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "uc2_graphics", "uc2_graphics.vcproj", "{3CF31288-A44D-4C78-A3AA-B05B6E32DF11}" @@ -76,11 +76,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "uc2_graphics", "uc2_graphic Release.AspNetCompiler.Debug = "False" EndProjectSection ProjectSection(ProjectDependencies) = postProject - {814EBFD3-3CE6-4933-A580-C1FE3147ACB4} = {814EBFD3-3CE6-4933-A580-C1FE3147ACB4} - {5F065EAC-B881-4E9A-9E34-7A21D7A01D98} = {5F065EAC-B881-4E9A-9E34-7A21D7A01D98} - {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} = {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} - {C4165626-F68F-4F66-A126-3B82DDBB7480} = {C4165626-F68F-4F66-A126-3B82DDBB7480} {D3D21F11-A7E7-4EA2-8518-E24695133BFF} = {D3D21F11-A7E7-4EA2-8518-E24695133BFF} + {C4165626-F68F-4F66-A126-3B82DDBB7480} = {C4165626-F68F-4F66-A126-3B82DDBB7480} + {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} = {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} + {5F065EAC-B881-4E9A-9E34-7A21D7A01D98} = {5F065EAC-B881-4E9A-9E34-7A21D7A01D98} + {814EBFD3-3CE6-4933-A580-C1FE3147ACB4} = {814EBFD3-3CE6-4933-A580-C1FE3147ACB4} EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libgraphics2", "libgraphics2.vcproj", "{814EBFD3-3CE6-4933-A580-C1FE3147ACB4}" @@ -89,6 +89,16 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libgraphics2", "libgraphics Release.AspNetCompiler.Debug = "False" EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "multi_thread", "multi_thread.vcproj", "{BFE833C6-840F-4F2E-A1FA-A4DE9B9277D6}" + ProjectSection(WebsiteProperties) = preProject + Debug.AspNetCompiler.Debug = "True" + Release.AspNetCompiler.Debug = "False" + EndProjectSection + ProjectSection(ProjectDependencies) = postProject + {0BC1DB36-030A-4321-B387-1CEE2611E329} = {0BC1DB36-030A-4321-B387-1CEE2611E329} + {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} = {E8F6BD7E-461A-4733-B7D8-37B09A099ED8} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -185,6 +195,14 @@ Global {814EBFD3-3CE6-4933-A580-C1FE3147ACB4}.Release|Win32.Build.0 = Release|Win32 {814EBFD3-3CE6-4933-A580-C1FE3147ACB4}.Release|x64.ActiveCfg = Release|x64 {814EBFD3-3CE6-4933-A580-C1FE3147ACB4}.Release|x64.Build.0 = Release|x64 + {BFE833C6-840F-4F2E-A1FA-A4DE9B9277D6}.Debug|Win32.ActiveCfg = Debug|Win32 + {BFE833C6-840F-4F2E-A1FA-A4DE9B9277D6}.Debug|Win32.Build.0 = Debug|Win32 + {BFE833C6-840F-4F2E-A1FA-A4DE9B9277D6}.Debug|x64.ActiveCfg = Debug|x64 + {BFE833C6-840F-4F2E-A1FA-A4DE9B9277D6}.Debug|x64.Build.0 = Debug|x64 + {BFE833C6-840F-4F2E-A1FA-A4DE9B9277D6}.Release|Win32.ActiveCfg = Release|Win32 + {BFE833C6-840F-4F2E-A1FA-A4DE9B9277D6}.Release|Win32.Build.0 = Release|Win32 + {BFE833C6-840F-4F2E-A1FA-A4DE9B9277D6}.Release|x64.ActiveCfg = Release|x64 + {BFE833C6-840F-4F2E-A1FA-A4DE9B9277D6}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE