1 /*****************************************************************************
2 Copyright (c) 2011-2014, The OpenBLAS Project
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
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
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
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.
32 **********************************************************************************/
34 #include <sys/types.h>
37 #include "openblas_utest.h"
39 static void* xmalloc(size_t n)
44 fprintf(stderr, "You are about to die\n");
52 static void check_dgemm(double *a, double *b, double *result, double *expected, blasint n)
56 double zerod = 0, oned = 1;
58 BLASFUNC(dgemm)(&trans1, &trans2, &n, &n, &n, &oned, a, &n, b, &n, &zerod, result, &n);
59 for(i = 0; i < n * n; ++i) {
60 ASSERT_DBL_NEAR_TOL(expected[i], result[i], DOUBLE_EPS);
73 double *a, *b, *c, *d;
77 pid_t fork_pid_nested;
79 n_bytes = sizeof(*a) * n * n;
86 // Put ones in a and b
87 for(i = 0; i < n * n; ++i) {
92 // Compute a DGEMM product in the parent process prior to forking to
93 // ensure that the OpenBLAS thread pool is initialized.
96 double zerod = 0, oned = 1;
97 BLASFUNC(dgemm)(&trans1, &trans2, &n, &n, &n, &oned, a, &n, b, &n, &zerod, c, &n);
100 if (fork_pid == -1) {
101 CTEST_ERR("Failed to fork process.");
102 } else if (fork_pid == 0) {
103 // Compute a DGEMM product in the child process to check that the
104 // thread pool as been properly been reinitialized after the fork.
105 check_dgemm(a, b, d, c, n);
107 // Nested fork to check that the pthread_atfork protection can work
109 fork_pid_nested = fork();
110 if (fork_pid_nested == -1) {
111 CTEST_ERR("Failed to fork process.");
113 } else if (fork_pid_nested == 0) {
114 check_dgemm(a, b, d, c, n);
117 check_dgemm(a, b, d, c, n);
118 int child_status = 0;
119 pid_t wait_pid = wait(&child_status);
120 ASSERT_EQUAL(wait_pid, fork_pid_nested);
121 ASSERT_EQUAL(0, WEXITSTATUS (child_status));
125 check_dgemm(a, b, d, c, n);
126 // Wait for the child to finish and check the exit code.
127 int child_status = 0;
128 pid_t wait_pid = wait(&child_status);
129 ASSERT_EQUAL(wait_pid, fork_pid);
130 ASSERT_EQUAL(0, WEXITSTATUS (child_status));