omp_hello.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. int main (int argc, char *argv[]) {
  17. int nthreads, tid;
  18. /* Fork a team of threads giving them their own copies of variables */
  19. #pragma omp parallel private(nthreads, tid)
  20. {
  21. /* Obtain thread number */
  22. tid = omp_get_thread_num();
  23. printf("Hello World from thread = %d\n", tid);
  24. /* Only master thread does this */
  25. if (tid == 0)
  26. {
  27. nthreads = omp_get_num_threads();
  28. printf("Number of threads = %d\n", nthreads);
  29. }
  30. } /* All threads join master thread and disband */
  31. return 0;
  32. }