Skip to content

Commit 3617c22

Browse files
committed
Refs #294. Used pthread_atfork to avoid hang after a Unix fork.
The problem is the mutex we used in blas_server. Thus, we must clear the mutex before the fork and re-init them at parent and child process. If you used OpenMP, GOMP has the same problem by now. Please try other OpenMP implemantation.
1 parent f9daebb commit 3617c22

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

driver/others/blas_server.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8383

8484
#define ATTRIBUTE_SIZE 128
8585

86+
extern void openblas_warning(int verbose, const char * msg);
87+
8688
/* This is a thread server model implementation. The threads are */
8789
/* spawned at first access to blas library, and still remains until */
8890
/* destruction routine is called. The number of threads are */
@@ -921,5 +923,17 @@ int BLASFUNC(blas_thread_shutdown)(void){
921923
return 0;
922924
}
923925

926+
/*
927+
https://github.com/xianyi/OpenBLAS/issues/294
928+
Use pthread_atfork to close blas_thread_server before fork.
929+
Then, re-init blas_thread_server after fork at child and parent.
930+
*/
931+
void openblas_fork_handler()
932+
{
933+
int err;
934+
err = pthread_atfork (BLASFUNC(blas_thread_shutdown), blas_thread_init, blas_thread_init);
935+
if(err != 0)
936+
openblas_warning(0, "OpenBLAS cannot install fork handler. You may meet hang after fork.\n");
937+
}
924938
#endif
925939

driver/others/blas_server_omp.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,9 @@ int exec_blas(BLASLONG num, blas_queue_t *queue){
315315
return 0;
316316
}
317317

318+
void openblas_fork_handler()
319+
{
320+
321+
}
322+
318323
#endif

driver/others/blas_server_win32.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,8 @@ void openblas_set_num_threads(int num)
498498
{
499499
goto_set_num_threads(num);
500500
}
501+
502+
void openblas_fork_handler()
503+
{
504+
505+
}

driver/others/memory.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,11 @@ void CONSTRUCTOR gotoblas_init(void) {
12881288
#ifdef SMP
12891289
if (blas_cpu_number == 0) blas_get_cpu_number();
12901290
#ifdef SMP_SERVER
1291-
if (blas_server_avail == 0) blas_thread_init();
1291+
if (blas_server_avail == 0) {
1292+
blas_thread_init();
1293+
//deal with pthread and fork.
1294+
openblas_fork_handler();
1295+
}
12921296
#endif
12931297
#endif
12941298

0 commit comments

Comments
 (0)