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

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