52 lines · c
1// RUN: %libomp-compile-and-run2 3#include <stdio.h>4#include <omp.h>5 6int test_omp_parallel_num_threads() {7 int num_failed;8 int threads;9 int nthreads;10 int max_threads = 0;11 12 num_failed = 0;13#pragma omp task14 {}15 16/* first we check how many threads are available */17#pragma omp parallel18 {19#pragma omp task20 {}21#pragma omp master22 max_threads = omp_get_num_threads();23 }24 25 /* we increase the number of threads from one to maximum:*/26 for (threads = 1; threads <= max_threads; threads++) {27 nthreads = 0;28#pragma omp parallel reduction(+ : num_failed) num_threads(threads)29 {30#pragma omp task31 {}32 num_failed = num_failed + !(threads == omp_get_num_threads());33#pragma omp atomic34 nthreads += 1;35 }36 num_failed = num_failed + !(nthreads == threads);37 }38 return (!num_failed);39}40 41int main() {42 int i;43 int num_failed = 0;44 45 for (i = 0; i < 100; i++) {46 if (!test_omp_parallel_num_threads()) {47 num_failed++;48 }49 }50 return num_failed;51}52