Upgrade to version 0.3.21
[platform/upstream/openblas.git] / utest / test_fork.c
1 /*****************************************************************************
2 Copyright (c) 2011-2014, 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 <cblas.h>
37 #include "openblas_utest.h"
38
39 static void* xmalloc(size_t n)
40 {
41     void* tmp;
42     tmp = malloc(n);
43     if (tmp == NULL) {
44         fprintf(stderr, "You are about to die\n");
45         exit(1);
46     } else {
47         return tmp;
48     }
49 }
50
51 #ifdef BUILD_DOUBLE
52 static void check_dgemm(double *a, double *b, double *result, double *expected, blasint n)
53 {
54     char trans1 = 'T';
55     char trans2 = 'N';
56     double zerod = 0, oned = 1;
57     int i;
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);
61     }
62 }
63 #endif
64
65 CTEST(fork, safety)
66 {
67 #ifndef BUILD_DOUBLE
68 exit(0);
69 #else
70     blasint n = 1000;
71     int i;
72
73     double *a, *b, *c, *d;
74     size_t n_bytes;
75
76     pid_t fork_pid;
77     pid_t fork_pid_nested;
78
79     n_bytes = sizeof(*a) * n * n;
80
81     a = xmalloc(n_bytes);
82     b = xmalloc(n_bytes);
83     c = xmalloc(n_bytes);
84     d = xmalloc(n_bytes);
85
86     // Put ones in a and b
87     for(i = 0; i < n * n; ++i) {
88         a[i] = 1;
89         b[i] = 1;
90     }
91
92     // Compute a DGEMM product in the parent process prior to forking to
93     // ensure that the OpenBLAS thread pool is initialized.
94     char trans1 = 'T';
95     char trans2 = 'N';
96     double zerod = 0, oned = 1;
97     BLASFUNC(dgemm)(&trans1, &trans2, &n, &n, &n, &oned, a, &n, b, &n, &zerod, c, &n);
98
99     fork_pid = fork();
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);
106
107         // Nested fork to check that the pthread_atfork protection can work
108         // recursively
109         fork_pid_nested = fork();
110         if (fork_pid_nested == -1) {
111             CTEST_ERR("Failed to fork process.");
112             exit(1);
113         } else if (fork_pid_nested == 0) {
114             check_dgemm(a, b, d, c, n);
115             exit(0);
116         } else {
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));
122             exit(0);
123         }
124     } else {
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));
131     }
132 #endif
133 }