blob: 3f6f5fa9295adfaa6313369b3734ff8cff639f08 [file] [log] [blame]
bartbbd3dcf2008-10-11 18:04:52 +00001/* Simple OpenMP test program that calls printf() from a parallel section. */
2
bart5fb36bb2009-07-26 09:04:42 +00003#include <assert.h> // assert()
bartbbd3dcf2008-10-11 18:04:52 +00004#include <omp.h>
5#include <stdio.h>
bart5fb36bb2009-07-26 09:04:42 +00006#include <stdlib.h> // atoi()
bartbbd3dcf2008-10-11 18:04:52 +00007#include <unistd.h> // getopt()
8
9static void usage(const char* const exe)
10{
bart5fb36bb2009-07-26 09:04:42 +000011 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);
bartbbd3dcf2008-10-11 18:04:52 +000018}
19
20int main(int argc, char** argv)
21{
22 int i;
23 int optchar;
24 int silent = 0;
25 int tid;
bart5fb36bb2009-07-26 09:04:42 +000026 int num_iterations = 2;
27 int num_threads = 2;
bartbbd3dcf2008-10-11 18:04:52 +000028
bart5fb36bb2009-07-26 09:04:42 +000029 while ((optchar = getopt(argc, argv, "hi:qt:")) != EOF)
bartbbd3dcf2008-10-11 18:04:52 +000030 {
31 switch (optchar)
32 {
33 case 'h': usage(argv[0]); return 1;
bart5fb36bb2009-07-26 09:04:42 +000034 case 'i': num_iterations = atoi(optarg); break;
bartbbd3dcf2008-10-11 18:04:52 +000035 case 'q': silent = 1; break;
bart5fb36bb2009-07-26 09:04:42 +000036 case 't': num_threads = atoi(optarg); break;
bartbbd3dcf2008-10-11 18:04:52 +000037 default:
38 return 1;
39 }
40 }
41
bart5fb36bb2009-07-26 09:04:42 +000042 /*
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++)
bartbbd3dcf2008-10-11 18:04:52 +000054 {
55 tid = omp_get_thread_num();
56 if (! silent)
57 {
bart5fb36bb2009-07-26 09:04:42 +000058 fprintf(stderr,
59 "iteration %d; thread number = %d; number of threads = %d\n",
60 i, tid, omp_get_num_threads());
bartbbd3dcf2008-10-11 18:04:52 +000061 }
62 else
63 {
bart5fb36bb2009-07-26 09:04:42 +000064 fprintf(stderr, "%s", "");
bartbbd3dcf2008-10-11 18:04:52 +000065 }
66 }
67
68 fprintf(stderr, "Finished.\n");
69
70 return 0;
71}