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 void* xmalloc(size_t n)
44 fprintf(stderr, "You are about to die\n");
51 void check_dgemm(double *a, double *b, double *result, double *expected, blasint n)
55 double zerod = 0, oned = 1;
57 BLASFUNC(dgemm)(&trans1, &trans2, &n, &n, &n, &oned, a, &n, b, &n, &zerod, result, &n);
58 for(i = 0; i < n * n; ++i) {
59 ASSERT_DBL_NEAR_TOL(expected[i], result[i], DOUBLE_EPS);
68 double *a, *b, *c, *d;
72 pid_t fork_pid_nested;
74 n_bytes = sizeof(*a) * n * n;
81 // Put ones in a and b
82 for(i = 0; i < n * n; ++i) {
87 // Compute a DGEMM product in the parent process prior to forking to
88 // ensure that the OpenBLAS thread pool is initialized.
91 double zerod = 0, oned = 1;
92 BLASFUNC(dgemm)(&trans1, &trans2, &n, &n, &n, &oned, a, &n, b, &n, &zerod, c, &n);
96 CTEST_ERR("Failed to fork process.");
97 } else if (fork_pid == 0) {
98 // Compute a DGEMM product in the child process to check that the
99 // thread pool as been properly been reinitialized after the fork.
100 check_dgemm(a, b, d, c, n);
102 // Nested fork to check that the pthread_atfork protection can work
104 fork_pid_nested = fork();
105 if (fork_pid_nested == -1) {
106 CTEST_ERR("Failed to fork process.");
108 } else if (fork_pid_nested == 0) {
109 check_dgemm(a, b, d, c, n);
112 check_dgemm(a, b, d, c, n);
113 int child_status = 0;
114 pid_t wait_pid = wait(&child_status);
115 ASSERT_EQUAL(wait_pid, fork_pid_nested);
116 ASSERT_EQUAL(0, WEXITSTATUS (child_status));
120 check_dgemm(a, b, d, c, n);
121 // Wait for the child to finish and check the exit code.
122 int child_status = 0;
123 pid_t wait_pid = wait(&child_status);
124 ASSERT_EQUAL(wait_pid, fork_pid);
125 ASSERT_EQUAL(0, WEXITSTATUS (child_status));