Wednesday, January 22, 2025
HomeProgrammingOpenMP | Hello World program

OpenMP | Hello World program

Here is a simple “Hello World” program using OpenMP in C:

#include <stdio.h>
#include <omp.h>

int main() {
    #pragma omp parallel
    {
        printf("Hello, World! from thread %d\n", omp_get_thread_num());
    }
    return 0;
}
Explanation:
  1. #include <omp.h>: This header file is required to use OpenMP functions and directives.
  2. #pragma omp parallel: This directive tells the compiler to parallelize the following block of code. Multiple threads will execute the block concurrently.
  3. omp_get_thread_num(): This function returns the thread number (ID) of the current thread.
See also  How do I clone a specific Git branch?
Compilation:

To compile this program, use an OpenMP-compatible compiler like gcc with the -fopenmp flag:

gcc -fopenmp hello_world.c -o hello_world
Execution:

When you run the program:

./hello_world

It will print “Hello, World!” from multiple threads, with each thread displaying its thread ID.

See also  Merging or concatenating two dictionaries in Python
Example Output:
Hello, World! from thread 0
Hello, World! from thread 1
Hello, World! from thread 2
Hello, World! from thread 3

The output order may vary depending on how the threads are scheduled by the system.

RELATED ARTICLES

C Function Pointers

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

C Function Pointers

List of Asian Countries

Recent Comments

0
Would love your thoughts, please comment.x
()
x