Skip to content

Commit 7750d50

Browse files
committed
chore: add test case for exec_blas_async after fork
most functions are using exec_blas but dgetrf uses exec_blas_async directly and the behavior after fork is not the same. It's currently deadlocking.
1 parent d6b25c4 commit 7750d50

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

utest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ if (NOT USE_OPENMP)
101101
set(OpenBLAS_utest_src
102102
${OpenBLAS_utest_src}
103103
test_fork.c
104+
test_post_fork_async.c
104105
)
105106
endif()
106107
set(OpenBLAS_utest_src

utest/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ endif
4545
# FIXME TBD if this works on OSX, SunOS, POWER and zarch
4646
ifeq ($(OSNAME), $(filter $(OSNAME),Linux CYGWIN_NT))
4747
ifneq ($(USE_OPENMP), 1)
48-
OBJS += test_fork.o
48+
OBJS += test_fork.o test_post_fork_async.o
4949
endif
5050
OBJS += test_post_fork.o
5151
endif

utest/test_post_fork_async.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*****************************************************************************
2+
Copyright (c) 2011-2025, The OpenBLAS Project
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are
7+
met:
8+
9+
1. Redistributions of source code must retain the above copyright
10+
notice, this list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright
13+
notice, this list of conditions and the following disclaimer in
14+
the documentation and/or other materials provided with the
15+
distribution.
16+
3. Neither the name of the OpenBLAS project nor the names of
17+
its contributors may be used to endorse or promote products
18+
derived from this software without specific prior written
19+
permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30+
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
**********************************************************************************/
33+
34+
#include <sys/types.h>
35+
#include <sys/wait.h>
36+
#include <errno.h>
37+
#include <cblas.h>
38+
#include "openblas_utest.h"
39+
40+
static void* xmalloc(size_t n)
41+
{
42+
void* tmp;
43+
tmp = calloc(1, n);
44+
if (tmp == NULL) {
45+
fprintf(stderr, "Failed to allocate memory for the testcase.\n");
46+
exit(1);
47+
} else {
48+
return tmp;
49+
}
50+
}
51+
52+
CTEST(fork, safety_after_fork_async)
53+
{
54+
#ifdef __UCLIBC__
55+
#if !defined __UCLIBC_HAS_STUBS__ && !defined __ARCH_USE_MMU__
56+
exit(0);
57+
#endif
58+
#endif
59+
#ifndef BUILD_DOUBLE
60+
exit(0);
61+
#else
62+
blasint n = 200;
63+
blasint info;
64+
blasint i;
65+
66+
blasint *ipiv;
67+
double *arr;
68+
69+
pid_t fork_pid;
70+
71+
arr = xmalloc(sizeof(*arr) * n * n);
72+
ipiv = xmalloc(sizeof(*ipiv) * n);
73+
74+
// array is an identity matrix
75+
for (i = 0; i < n*n; i += n + 1) {
76+
arr[i] = 1.0;
77+
}
78+
79+
fork_pid = fork();
80+
if (fork_pid == -1) {
81+
perror("fork");
82+
CTEST_ERR("Failed to fork process.");
83+
} else if (fork_pid == 0) {
84+
exit(0);
85+
} else {
86+
// Wait for the child to finish and check the exit code.
87+
int child_status = 0;
88+
pid_t wait_pid = wait(&child_status);
89+
ASSERT_EQUAL(wait_pid, fork_pid);
90+
ASSERT_EQUAL(0, WEXITSTATUS (child_status));
91+
}
92+
BLASFUNC(dgetrf)(&n, &n, arr, &n, ipiv, &info);
93+
#endif
94+
}

0 commit comments

Comments
 (0)