Lab # 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Lab # 1 (Installing Linux, configuring OpenMP, compiling,

and running C/C++ progams using OpenMP)

Objectives:
1- Know how to install unix operating systems in your computer
2- Know how to install gcc and g++ compilers
3- Know how to configure OpenMP
4- Know how to compile C and C++ program codes using OpenMP

Steps to install Linux on Windows with WSL:


1- Open command (CMD) prompt.
2- write the following command:
wsl --install -d Ubuntu

Install gcc and g++ compilers:


write the following command in Ubutue terminal:
sudo apt-get install gcc g++

Configuring OpenMP:
We can check whether the OpenMP features are configured into our
compiler or not, using the command:

If OpenMP is not featured in the compiler, we can configure it use using the
command:
Setting the number of threads:
In OpenMP, before running the code, we can initialize the number of
threads to be executed using the following command. Here, we set the
number of threads to be getting executed to be 8 threads.

Running First C program code in OpenMP


// OpenMP header
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

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


{
int nthreads, tid;

// Begin of parallel region


#pragma omp parallel private(nthreads, tid)
{
// Getting thread number
tid = omp_get_thread_num();
printf("Welcome to GFG from thread = %d\n",tid);

if (tid == 0) {

// Only master thread does this


nthreads = omp_get_num_threads();
printf("Number of threads = %d\n",nthreads);
}
}
}
This program will print a message which will be executed by various
threads.

Compile command:
gcc -o program.exe -fopenmp program1.c

*** If “stdio.h: No such file or directory” error appears write the following
command in Ubuntu terminal:

sudo apt install libc6-dev

Execute command:
./program.exe

Captured UNIX terminal output snapshot:

 The <stdio.h> header file will provide us with print functionality.


 The <omp.h> header file will provide openmp functionality.
 To know the number of available cores in your computer use one of
the following Linux commands:

nproc

or

cat /proc/cpuinfo

or

lscpu
 In case of asking to use –fix-broken or –fix-missing during the
installation of any package use the following command to try to fix it:

sudo apt update && sudo apt upgrade -y

 omp_get_num_threads: it is a routine that returns the number of


threads in the current OpenMP region. If this function is called
outside a parallel region, only the master thread is present hence
the value returned is 1.
 omp_get_thread_num: it is a routine that returns the thread
number of the calling thread, within the team that is executing the
parallel region to which the routine region binds. The thread
number is an integer between 0 and one less than the value
returned by omp_get_num_threads, inclusive. The thread number
of the master thread of the team is 0. The routine returns 0 if it is
called from the sequential part of a program.
 omp_set_num_threads: it is a routine that specifies the number of
threads used by default in subsequent parallel sections, if those do
not specify a num_threads clause. The argument of
omp_set_num_threads shall be a positive integer.
Exercise 1: Name the C program code in the below table as “program1.c”
and set the amount of threads we want OpenMP to run on to 8 threads.
Then, answer the following questions:

1) What is the UNIX command that can be used to compile this C program
code?

2) What is the UNIX command to execute it?

3) Put the snapshot for the output.


//compute the sum of two arrays in parallel

#include <stdio.h>

#include <omp.h>

int main() {

const int N=100;

int a[N];

//initialize

for (int i=0; i < N; i++)

a[i] = i;

//compute sum

int local_sum, sum;

#pragma omp parallel private(local_sum) shared(sum)

local_sum =0;

//the array is distributde statically between threads

#pragma omp for schedule(static,1)

for (int i=0; i< N; i++) {

local_sum += a[i];
}

//each thread calculated its local_sum. ALl threads have to add to

//the global sum. It is critical that this operation is atomic.

#pragma omp critical

sum += local_sum;

printf("sum=%d should be %d\n", sum, N*(N-1)/2);

Exercise 2: Name the C program code in the below table as “program2.c”.


Then, answer the following questions:

1) What is the UNIX command that can be used to compile this C program
code?

2) What is the UNIX command to execute it?

3) Put the snapshot for the output.


#include <stdio.h>

#include <stdlib.h>

#include <omp.h>

/**

* @brief Illustrates how to get the number of threads.

* @details This code prints the number of threads at two specific locations:

* 1) Outside an OpenMP parallel region

* 2) Inside an OpenMP parallel region

**/

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

// Tell OpenMP to use 8 threads in parallel regions


omp_set_num_threads(8);

// 1) Outside the OpenMP parallel region

printf("Outside the OpenMP parallel region, we are %d threads.\n",


omp_get_num_threads());

// Create the OpenMP parallel region, which will contain 8 threads

#pragma omp parallel

omp_set_num_threads(8);

// 2) Inside the OpenMP parallel region

printf("Inside the OpenMP parallel region, we are %d threads.\n",


omp_get_num_threads());

return EXIT_SUCCESS;}

Exercise 3: Name the C program code in the below table as “program3.c”


and set the amount of threads we want OpenMP to run on to 8 threads.
Then, answer the following questions:

1) What is the UNIX command that can be used to compile this C program
code?

2) What is the UNIX command to execute it?

3) Put the snapshot for the output.


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

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


int thread_id;

#pragma omp parallel private(thread_id)


{
thread_id = omp_get_thread_num();
printf("Hello from process: %d\n", thread_id );
}

return 0;
}
Exercise 4: Name the C program code in the below table as “program4.c”.
Then, answer the following questions:

1) What is the UNIX command that can be used to compile this C program
code?

2) What is the UNIX command to execute it?

3) Put the snapshot for the output.


#include <omp.h>

#include <stdio.h>

#include <stdlib.h>

#define MAX_THREADS 8

static long steps = 1000000000;

double step;

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

int i,j;

double x;

double pi, sum = 0.0;

double start, delta;

step = 1.0/(double) steps;

// Compute parallel compute times for 1-MAX_THREADS

for (j=1; j<= MAX_THREADS; j++) {

printf(" running on %d threads: ", j);

// This is the beginning of a single PI computation

omp_set_num_threads(j);

sum = 0.0;

double start = omp_get_wtime();


#pragma omp parallel for reduction(+:sum) private(x)

for (i=0; i < steps; i++) {

x = (i+0.5)*step;

sum += 4.0 / (1.0+x*x);

// Out of the parallel region, finialize computation

pi = step * sum;

delta = omp_get_wtime() - start;

printf("PI = %.16g computed in %.4g seconds\n", pi, delta);

}
Exercise 5: Name the C program code in the below table as “program5.c”.
Then, answer the following questions:

1) What is the UNIX command that can be used to compile this C program
code?

2) What is the UNIX command to execute it?

3) Put the snapshot for the output.


#include <omp.h>

#include <stdio.h>

#include <stdlib.h>

#define MAX_THREADS 8

const unsigned long int size = 100000000;

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

int j;

unsigned long int sum;

double start, delta;

// Compute parallel compute times for 1-MAX_THREADS

for (j=1; j<= MAX_THREADS; j++) {

printf(" running on %d threads: ", j);

omp_set_num_threads(j);

sum=0;

start=0;

double start = omp_get_wtime();

#pragma omp parallel for reduction(+:sum)

for (int i=0; i < size; i++) {

sum +=i;
}

// Out of the parallel region, finialize computation

delta = omp_get_wtime() - start;

printf("sum is %lu %.4g seconds\n", sum, delta);

}}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy