From e9ecb9c04a300313b0ef4f7b6e34ba5164ffb79d Mon Sep 17 00:00:00 2001 From: "Eric J. Korpela" Date: Tue, 7 Jun 2011 01:46:14 +0000 Subject: [PATCH] Safe exit checking for CUDA applications under windows. In application you could use the following for safe exit checking. #ifdef _WIN32 //Jason: Safe exit check macro to play nicer with Cuda & MS-CRT #ifdef USE_CUDA #define SAFE_EXIT_CHECK do { \ if (worker_thread_exit_request) { \ fprintf(stderr,"-> Worker received exit request, syncing Cuda..."); cudaThreadSynchronize(); fprintf(stderr,"Done.\n"); \ fprintf(stderr," Worker Freeing Cuda data..."); cudaAcc_free(); fprintf(stderr,"Done.\n"); \ fprintf(stderr," Worker Acknowledging exit request, spinning->\n"); worker_thread_exit_ack = true; \ while (1) Sleep(10); \ } \ } while (0); #else #define SAFE_EXIT_CHECK do { \ if (worker_thread_exit_request) { \ fprintf(stderr," Worker Acknowledging exit request, spinning-> "); worker_thread_exit_ack = true; \ while (1) Sleep(10); \ } \ } while (0); #endif #else // Linux or other probably have their own safe exit handling, defined as blank, do nothing #define SAFE_EXIT_CHECK #endif and install at the top of the cffft loop, and more locations if desired: SAFE_EXIT_CHECK; I'd like to implement these as BOINC API functions, but have not yet done so. svn path=/trunk/boinc/; revision=23646 --- api/boinc_api.cpp | 20 +++++++++++++++++++- api/boinc_api.h | 6 ++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/api/boinc_api.cpp b/api/boinc_api.cpp index e37a255598..781092f99e 100644 --- a/api/boinc_api.cpp +++ b/api/boinc_api.cpp @@ -164,6 +164,9 @@ static HANDLE hSharedMem; HANDLE worker_thread_handle; // used to suspend worker thread, and to measure its CPU time DWORD timer_thread_id; +//Jason: New windows exit handling to play more nicely with cuda & MS-CRT +volatile bool worker_thread_exit_request = false; +volatile bool worker_thread_exit_ack = false; #else static volatile bool worker_thread_exit_flag = false; static volatile int worker_thread_exit_status; @@ -654,7 +657,22 @@ void boinc_exit(int status) { BOINCINFO("Exit Status: %d", status); fflush(NULL); -#if defined(_WIN32) +#if defined(_WIN32) + //Jason: Windows exit handling to play nicer with cuda & MS-CRT + worker_thread_exit_request = true; + fprintf(stderr,"boinc_exit(): requesting safe worker shutdown ->\n"); + int exitcounter = 0; + while ( !worker_thread_exit_ack && exitcounter < 20 ) + { + Sleep(100); + exitcounter++; + } + if (worker_thread_exit_ack) + fprintf(stderr,"boinc_exit(): received safe worker shutdown acknowledge ->\n"); + else + fprintf(stderr,"boinc_exit(): worker didn't respond to exit request within 2 seconds, exiting anyway.\n"); + fflush(NULL); + // ... Continue original nasty exit code here // Halt all the threads and cleans up. TerminateProcess(GetCurrentProcess(), status); // note: the above CAN return! diff --git a/api/boinc_api.h b/api/boinc_api.h index 64f87622db..989b43eca0 100644 --- a/api/boinc_api.h +++ b/api/boinc_api.h @@ -119,6 +119,12 @@ extern int setMacPList(void); extern int setMacIcon(char *filename, char *iconData, long iconSize); #endif +#ifdef _WIN32 + //Jason: New windows exit handling to play more nicely with cuda & MS-CRT + extern volatile bool worker_thread_exit_request; + extern volatile bool worker_thread_exit_ack; +#endif + #ifdef __cplusplus } // extern "C" { #endif