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.2 kB

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