1 /* Simple OpenMP test program that calls printf() from a parallel section. */
3 #include <assert.h> // assert()
6 #include <stdlib.h> // atoi()
7 #include <unistd.h> // getopt()
9 static void usage(const char* const exe
)
12 "Usage: %s [-h] [-i <n>] [-q] [-t<n>]\n"
13 "-h: display this information.\n"
14 "-i <n>: number of loop iterations.\n"
15 "-q: quiet mode -- do not print computed error.\n"
16 "-t <n>: number of OMP threads.\n",
20 int main(int argc
, char** argv
)
26 int num_iterations
= 2;
29 while ((optchar
= getopt(argc
, argv
, "hi:qt:")) != EOF
)
33 case 'h': usage(argv
[0]); return 1;
34 case 'i': num_iterations
= atoi(optarg
); break;
35 case 'q': silent
= 1; break;
36 case 't': num_threads
= atoi(optarg
); break;
43 * Not the most user-friendly way of error checking, but still better than
46 assert(num_iterations
> 0);
47 assert(num_threads
> 0);
49 omp_set_num_threads(num_threads
);
52 #pragma omp parallel for private(tid)
53 for (i
= 0; i
< num_iterations
; i
++)
55 tid
= omp_get_thread_num();
59 "iteration %d; thread number = %d; number of threads = %d\n",
60 i
, tid
, omp_get_num_threads());
64 fprintf(stderr
, "%s", "");
68 fprintf(stderr
, "Finished.\n");