Question: Task 2.1 - Compile a hello world program The newer versions of the gcc compiler will compile OpenMP programs. Create a subdirectory in your work

Task 2.1 - Compile a hello world program

The newer versions of the gcc compiler will compile OpenMP programs. Create a subdirectory in your work space and call it OpenMP. In this directory create a text file calling it helloworld.c and in it place the following code:

#include <omp.h>

#include <stdio.h>

#include <stdlib.h>

int main (int argc, char *argv[]) {

int nthreads, tid;

/* Fork a team of threads giving them their own copies of variables */

#pragma omp parallel private(nthreads, tid)

{

tid = omp_get_thread_num(); // Obtain thread number

printf("Hello World from thread = %d ", tid);

if (tid == 0) { // Only master thread does this

nthreads = omp_get_num_threads();

printf("Number of threads = %d ", nthreads);

}

} /* All threads join master thread and disband */

}

This program has the basic parallel construct for defining a single parallel region for multiple threads. It also

has a private clause for defining a variable local to each thread. Compile the program with the following command:

gcc fopenmp o helloworld helloworld.c

Execute the compiled program, observe and document output in your report.

Alter the number of threads to 4 by altering the environment variable with the command:

export OMP_NUM_THREADS=4

Verify value of env var just set with:

echo $OMP_NUM_THREADS

Re-execute the program, observe and document output in your report.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!