xref: /llvm-project/openmp/runtime/test/misc_bugs/stack-propagate.c (revision d47f5488cf02fa06259a0f8563f684e2d45165c9)
1 // RUN: %libomp-compile-and-run
2 
3 // https://bugs.llvm.org/show_bug.cgi?id=26540 requested
4 // stack size to be propagated from master to workers.
5 // Library implements propagation of not too big stack
6 // for Linux x86_64 platform (skipped Windows for now).
7 //
8 // The test checks that workers can use more than 4MB
9 // of stack (4MB - was historical default for
10 // stack size of worker thread in runtime library).
11 
12 #include <stdio.h>
13 #include <omp.h>
14 #if !defined(_WIN32)
15 #include <sys/resource.h> // getrlimit
16 #endif
17 
18 #define STK 4800000
19 
foo(int n,int th)20 double foo(int n, int th)
21 {
22   double arr[n];
23   int i;
24   double res = 0.0;
25   for (i = 0; i < n; ++i) {
26     arr[i] = (double)i / (n + 2);
27   }
28   for (i = 0; i < n; ++i) {
29     res += arr[i] / n;
30   }
31   return res;
32 }
33 
main(int argc,char * argv[])34 int main(int argc, char *argv[])
35 {
36 #if defined(_WIN32)
37   // don't test Windows
38   printf("stack propagation not implemented, skipping test...\n");
39   return 0;
40 #else
41   int status;
42   double val = 0.0;
43   int m = STK / 8; // > 4800000 bytes per thread
44   // read stack size of calling thread, save it as default
45   struct rlimit rlim;
46   status = getrlimit(RLIMIT_STACK, &rlim);
47   if (sizeof(void *) > 4 &&                 // do not test 32-bit systems,
48       status == 0 && rlim.rlim_cur > STK) { // or small initial stack size
49 #pragma omp parallel reduction(+:val)
50     {
51       val += foo(m, omp_get_thread_num());
52     }
53   } else {
54     printf("too small stack size limit (needs about 8MB), skipping test...\n");
55     return 0;
56   }
57   if (val > 0.1) {
58     printf("passed\n");
59     return 0;
60   } else {
61     printf("failed, val = %f\n", val);
62     return 1;
63   }
64 #endif // _WIN32
65 }
66