You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_fork.c 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*****************************************************************************
  2. Copyright (c) 2011-2014, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. **********************************************************************************/
  28. #ifndef OS_WINDOWS
  29. #include "common_utest.h"
  30. #include <sys/wait.h>
  31. #include <cblas.h>
  32. void* xmalloc(size_t n)
  33. {
  34. void* tmp;
  35. tmp = malloc(n);
  36. if (tmp == NULL) {
  37. fprintf(stderr, "You are about to die\n");
  38. exit(1);
  39. } else {
  40. return tmp;
  41. }
  42. }
  43. void check_dgemm(double *a, double *b, double *result, double *expected, int n)
  44. {
  45. int i;
  46. cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n,
  47. 1.0, a, n, b, n, 0.0, result, n);
  48. for(i = 0; i < n * n; ++i) {
  49. CU_ASSERT_DOUBLE_EQUAL(expected[i], result[i], CHECK_EPS);
  50. }
  51. }
  52. void test_fork_safety(void)
  53. {
  54. int n = 1000;
  55. int i;
  56. double *a, *b, *c, *d;
  57. size_t n_bytes;
  58. pid_t fork_pid;
  59. pid_t fork_pid_nested;
  60. n_bytes = sizeof(*a) * n * n;
  61. a = xmalloc(n_bytes);
  62. b = xmalloc(n_bytes);
  63. c = xmalloc(n_bytes);
  64. d = xmalloc(n_bytes);
  65. // Put ones in a and b
  66. for(i = 0; i < n * n; ++i) {
  67. a[i] = 1;
  68. b[i] = 1;
  69. }
  70. // Compute a DGEMM product in the parent process prior to forking to
  71. // ensure that the OpenBLAS thread pool is initialized.
  72. cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n,
  73. 1.0, a, n, b, n, 0.0, c, n);
  74. fork_pid = fork();
  75. if (fork_pid == -1) {
  76. CU_FAIL("Failed to fork process.");
  77. } else if (fork_pid == 0) {
  78. // Compute a DGEMM product in the child process to check that the
  79. // thread pool as been properly been reinitialized after the fork.
  80. check_dgemm(a, b, d, c, n);
  81. // Nested fork to check that the pthread_atfork protection can work
  82. // recursively
  83. fork_pid_nested = fork();
  84. if (fork_pid_nested == -1) {
  85. CU_FAIL("Failed to fork process.");
  86. exit(1);
  87. } else if (fork_pid_nested == 0) {
  88. check_dgemm(a, b, d, c, n);
  89. exit(0);
  90. } else {
  91. check_dgemm(a, b, d, c, n);
  92. int child_status = 0;
  93. pid_t wait_pid = wait(&child_status);
  94. CU_ASSERT(wait_pid == fork_pid_nested);
  95. CU_ASSERT(WEXITSTATUS (child_status) == 0);
  96. exit(0);
  97. }
  98. } else {
  99. check_dgemm(a, b, d, c, n);
  100. // Wait for the child to finish and check the exit code.
  101. int child_status = 0;
  102. pid_t wait_pid = wait(&child_status);
  103. CU_ASSERT(wait_pid == fork_pid);
  104. CU_ASSERT(WEXITSTATUS (child_status) == 0);
  105. }
  106. }
  107. #endif