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