xref: /openbsd-src/gnu/gcc/libgomp/testsuite/libgomp.c/omp_hello.c (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert /******************************************************************************
2*404b540aSrobert * FILE: omp_hello.c
3*404b540aSrobert * DESCRIPTION:
4*404b540aSrobert *   OpenMP Example - Hello World - C/C++ Version
5*404b540aSrobert *   In this simple example, the master thread forks a parallel region.
6*404b540aSrobert *   All threads in the team obtain their unique thread number and print it.
7*404b540aSrobert *   The master thread only prints the total number of threads.  Two OpenMP
8*404b540aSrobert *   library routines are used to obtain the number of threads and each
9*404b540aSrobert *   thread's number.
10*404b540aSrobert * AUTHOR: Blaise Barney  5/99
11*404b540aSrobert * LAST REVISED: 04/06/05
12*404b540aSrobert ******************************************************************************/
13*404b540aSrobert #include <omp.h>
14*404b540aSrobert #include <stdio.h>
15*404b540aSrobert #include <stdlib.h>
16*404b540aSrobert 
main(int argc,char * argv[])17*404b540aSrobert int main (int argc, char *argv[]) {
18*404b540aSrobert 
19*404b540aSrobert int nthreads, tid;
20*404b540aSrobert 
21*404b540aSrobert /* Fork a team of threads giving them their own copies of variables */
22*404b540aSrobert #pragma omp parallel private(nthreads, tid)
23*404b540aSrobert   {
24*404b540aSrobert 
25*404b540aSrobert   /* Obtain thread number */
26*404b540aSrobert   tid = omp_get_thread_num();
27*404b540aSrobert   printf("Hello World from thread = %d\n", tid);
28*404b540aSrobert 
29*404b540aSrobert   /* Only master thread does this */
30*404b540aSrobert   if (tid == 0)
31*404b540aSrobert     {
32*404b540aSrobert     nthreads = omp_get_num_threads();
33*404b540aSrobert     printf("Number of threads = %d\n", nthreads);
34*404b540aSrobert     }
35*404b540aSrobert 
36*404b540aSrobert   }  /* All threads join master thread and disband */
37*404b540aSrobert 
38*404b540aSrobert   return 0;
39*404b540aSrobert }
40