#include <stdio.h>
#include <omp.h>
#if !defined(_WIN32)
#include <sys/resource.h>
#endif
#define STK 4800000
double foo(int n, int th)
{
double arr[n];
int i;
double res = 0.0;
for (i = 0; i < n; ++i) {
arr[i] = (double)i / (n + 2);
}
for (i = 0; i < n; ++i) {
res += arr[i] / n;
}
return res;
}
int main(int argc, char *argv[])
{
#if defined(_WIN32)
printf("stack propagation not implemented, skipping test...\n");
return 0;
#else
int status;
double val = 0.0;
int m = STK / 8;
struct rlimit rlim;
status = getrlimit(RLIMIT_STACK, &rlim);
if (sizeof(void *) > 4 &&
status == 0 && rlim.rlim_cur > STK) {
#pragma omp parallel reduction(+:val)
{
val += foo(m, omp_get_thread_num());
}
} else {
printf("too small stack size limit (needs about 8MB), skipping test...\n");
return 0;
}
if (val > 0.1) {
printf("passed\n");
return 0;
} else {
printf("failed, val = %f\n", val);
return 1;
}
#endif
}