Update dgemm_kernel_4x8_haswell.S
[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 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 void check_dgemm(double *a, double *b, double *result, double *expected, blasint n)
52 {
53     char trans1 = 'T';
54     char trans2 = 'N';
55     double zerod = 0, oned = 1;
56     int i;
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);
60     }
61 }
62
63 CTEST(fork, safety)
64 {
65     blasint n = 1000;
66     int i;
67
68     double *a, *b, *c, *d;
69     size_t n_bytes;
70
71     pid_t fork_pid;
72     pid_t fork_pid_nested;
73
74     n_bytes = sizeof(*a) * n * n;
75
76     a = xmalloc(n_bytes);
77     b = xmalloc(n_bytes);
78     c = xmalloc(n_bytes);
79     d = xmalloc(n_bytes);
80
81     // Put ones in a and b
82     for(i = 0; i < n * n; ++i) {
83         a[i] = 1;
84         b[i] = 1;
85     }
86
87     // Compute a DGEMM product in the parent process prior to forking to
88     // ensure that the OpenBLAS thread pool is initialized.
89     char trans1 = 'T';
90     char trans2 = 'N';
91     double zerod = 0, oned = 1;
92     BLASFUNC(dgemm)(&trans1, &trans2, &n, &n, &n, &oned, a, &n, b, &n, &zerod, c, &n);
93
94     fork_pid = fork();
95     if (fork_pid == -1) {
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);
101
102         // Nested fork to check that the pthread_atfork protection can work
103         // recursively
104         fork_pid_nested = fork();
105         if (fork_pid_nested == -1) {
106             CTEST_ERR("Failed to fork process.");
107             exit(1);
108         } else if (fork_pid_nested == 0) {
109             check_dgemm(a, b, d, c, n);
110             exit(0);
111         } else {
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));
117             exit(0);
118         }
119     } else {
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));
126     }
127 }