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_post_fork.c 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*****************************************************************************
  2. Copyright (c) 2011-2020, 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. #include <sys/types.h>
  29. #include <sys/wait.h>
  30. #include <errno.h>
  31. #include <cblas.h>
  32. #ifdef USE_OPENMP
  33. #include <omp.h>
  34. #endif
  35. #include "openblas_utest.h"
  36. static void* xmalloc(size_t n)
  37. {
  38. void* tmp;
  39. tmp = malloc(n);
  40. if (tmp == NULL) {
  41. fprintf(stderr, "Failed to allocate memory for the test payload.\n");
  42. exit(1);
  43. } else {
  44. return tmp;
  45. }
  46. }
  47. #ifdef BUILD_DOUBLE
  48. static void check_dgemm(double *a, double *b, double *result, double *expected, blasint n)
  49. {
  50. char trans1 = 'T';
  51. char trans2 = 'N';
  52. double zerod = 0, oned = 1;
  53. int i;
  54. BLASFUNC(dgemm)(&trans1, &trans2, &n, &n, &n, &oned, a, &n, b, &n, &zerod, result, &n);
  55. for(i = 0; i < n * n; ++i) {
  56. ASSERT_DBL_NEAR_TOL(expected[i], result[i], DOUBLE_EPS);
  57. }
  58. }
  59. #endif
  60. CTEST(fork, safety_after_fork_in_parent)
  61. {
  62. #ifdef __UCLIBC__
  63. #if !defined __UCLIBC_HAS_STUBS__ && !defined __ARCH_USE_MMU__
  64. exit(0);
  65. #endif
  66. #endif
  67. #ifndef BUILD_DOUBLE
  68. exit(0);
  69. #else
  70. blasint n = 100;
  71. int i, nthreads_omp;
  72. double *a, *b, *c, *d;
  73. size_t n_bytes;
  74. pid_t fork_pid;
  75. n_bytes = sizeof(*a) * n * n;
  76. a = xmalloc(n_bytes);
  77. b = xmalloc(n_bytes);
  78. c = xmalloc(n_bytes);
  79. d = xmalloc(n_bytes);
  80. // Put ones in a, b and n in c (result)
  81. for(i = 0; i < n * n; ++i) {
  82. a[i] = 1;
  83. b[i] = 1;
  84. c[i] = 1 * n;
  85. }
  86. // Test that OpenBLAS works after a fork.
  87. // This situation routinely happens with Pythons numpy where a
  88. // `sys.platform` calls `uname` in a forked process.
  89. // So we simulate this situation here.
  90. // There was an issue where a different number of OpenBLAS and OpenMP
  91. // threads triggered a memory leak. So run this multiple times
  92. // with different number of threads set.
  93. #ifdef USE_OPENMP
  94. nthreads_omp = omp_get_max_threads();
  95. // Run with half the max OMP threads, the max threads and twice that
  96. for(i = (nthreads_omp + 1) / 2; i <= nthreads_omp * 2; i *= 2) {
  97. omp_set_num_threads(i);
  98. #endif
  99. fork_pid = fork();
  100. if (fork_pid == -1) {
  101. perror("fork");
  102. CTEST_ERR("Failed to fork subprocesses in a loop.");
  103. #ifdef USE_OPENMP
  104. CTEST_ERR("Number of OpenMP threads was %d in this attempt.",i);
  105. #endif
  106. } else if (fork_pid == 0) {
  107. // Just pretend to do something, e.g. call `uname`, then exit
  108. exit(0);
  109. } else {
  110. // Wait for the child to finish and check the exit code.
  111. int child_status = 0;
  112. pid_t wait_pid = wait(&child_status);
  113. ASSERT_EQUAL(wait_pid, fork_pid);
  114. ASSERT_EQUAL(0, WEXITSTATUS (child_status));
  115. // Now OpenBLAS has to work
  116. check_dgemm(a, b, d, c, n);
  117. }
  118. #ifdef USE_OPENMP
  119. }
  120. #endif
  121. #endif
  122. }