1 /* Simple OpenMP test program that calls printf() from a parallel section. */
2 
3 #include <assert.h>  // assert()
4 #include <omp.h>
5 #include <stdio.h>
6 #include <stdlib.h>  // atoi()
7 #include <unistd.h>  // getopt()
8 
9 static void usage(const char* const exe)
10 {
11   fprintf(stderr,
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",
17           exe);
18 }
19 
20 int main(int argc, char** argv)
21 {
22   int i;
23   int optchar;
24   int silent = 0;
25   int tid;
26   int num_iterations = 2;
27   int num_threads = 2;
28 
29   while ((optchar = getopt(argc, argv, "hi:qt:")) != EOF)
30   {
31     switch (optchar)
32     {
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;
37     default:
38       return 1;
39     }
40   }
41 
42   /*
43    * Not the most user-friendly way of error checking, but still better than
44    * no error checking.
45    */
46   assert(num_iterations > 0);
47   assert(num_threads > 0);
48 
49   omp_set_num_threads(num_threads);
50   omp_set_dynamic(0);
51 
52 #pragma omp parallel for private(tid)
53   for (i = 0; i < num_iterations; i++)
54   {
55     tid = omp_get_thread_num();
56     if (! silent)
57     {
58       fprintf(stderr,
59               "iteration %d; thread number = %d; number of threads = %d\n",
60               i, tid, omp_get_num_threads());
61     }
62     else
63     {
64       fprintf(stderr, "%s", "");
65     }
66   }
67 
68   fprintf(stderr, "Finished.\n");
69 
70   return 0;
71 }
72