You are on page 1of 10

Reg No: 17BCE0500

Name: Rahul Mangal


Lab Slot: L25+L26

Exercise 3 ( OPENMP –III)

SCENARIO – I
Matrix multiplication

Write a simple OpenMP program for Matrix Multiplication. It is helpful to


understand how threads make use of the cores to enable course-grain parallelism.

Note that different threads will write different parts of the result in the array �, so
we dont’t get any problems during the parallel execution. Note that accesses to
matrices � ��� � are read-only and do not introduce any problems either.

Algorithm:

1. Start
2. Define dimensions of matrices and number of OpenMP threads.
3. Assign storage and values for matrices values using OpenMP parallel region.
4. Perform the matrix product in an OpenMP parallel region for loop with a sum
reduction.
5. Explicitly sets number of threads where each thread keeps track of its partial
sum.
6. Stop.

Source Code:

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

#define NRA 3
#define NCA 3
#define NCB 3

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


Reg No: 17BCE0500
Name: Rahul Mangal
Lab Slot: L25+L26

{
int tid, nthreads, i, j, k, chunk;
double a[NRA][NCA],
b[NCA][NCB],
c[NRA][NCB];

chunk = 10;

#pragma omp parallel shared(a,b,c,nthreads,chunk) private(tid,i,j,k)


{
tid = omp_get_thread_num();
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Starting matrix multiple example with %d threads\n",nthreads);
printf("Initializing matrices...\n");
}

#pragma omp for schedule (static, chunk)


for (i=0; i<NRA; i++)
for (j=0; j<NCA; j++)
a[i][j]= i+j;
#pragma omp for schedule (static, chunk)
for (i=0; i<NCA; i++)
for (j=0; j<NCB; j++)
b[i][j]= i*j;
#pragma omp for schedule (static, chunk)
for (i=0; i<NRA; i++)
for (j=0; j<NCB; j++)
c[i][j]= 0;

printf("Thread %d starting matrix multiply...\n",tid);


#pragma omp for schedule (static, chunk)
for (i=0; i<NRA; i++)
{
printf("Thread=%d did row=%d\n",tid,i);
for(j=0; j<NCB; j++)
for (k=0; k<NCA; k++)
c[i][j] += a[i][k] * b[k][j];
}
Reg No: 17BCE0500
Name: Rahul Mangal
Lab Slot: L25+L26

printf("******************************************************\n");
printf("Result Matrix:\n");
for (i=0; i<NRA; i++)
{
for (j=0; j<NCB; j++)
printf("%6.2f ", c[i][j]);
printf("\n");
}
printf("******************************************************\n");
printf ("Done.\n");

Output Screenshot:

Result:

17bce0500@sjt418scs008:~/Desktop$ gcc -o matrix_multiplication -fopenmp


matrix_multiplication.c
Reg No: 17BCE0500
Name: Rahul Mangal
Lab Slot: L25+L26

17bce0500@sjt418scs008:~/Desktop$ ./matrix_multiplication
Starting matrix multiple example with 4 threads
Initializing matrices...
Thread 0 starting matrix multiply...
Thread=0 did row=0
Thread=0 did row=1
Thread=0 did row=2
Thread 3 starting matrix multiply...
Thread 2 starting matrix multiply...
Thread 1 starting matrix multiply...
******************************************************
Result Matrix:
0.00 5.00 10.00
0.00 8.00 16.00
0.00 11.00 22.00
******************************************************
Done.

SCENARIO – II
Calculating pi by evaluating the integral 4/(1 + x2)

Write a OpenMP program to calculate the value of PI by evaluating the integral 4/


(1 + x2). You can use three pragma block directives to handle the critical section ,
the sum and the product display.

Algorithm:

1. Start
2. Define global variable and initialise it to value of PI
Reg No: 17BCE0500
Name: Rahul Mangal
Lab Slot: L25+L26

3. Scan the number of intervals and initialise sum=0 and h=1/no. Of intervals
4. In parallel section, for i=1 to no. Of intervals, calculate x and sumthread as in
the given code.
5. Calculate partial sum = sumthread*h and add the partial sum to sum
6. Print Sum as value of PI and subtract the new value from defined value to get
the error.
7. End

Source Code:

#include<stdio.h>
#include<omp.h>
#define PI 3.1415926538837211
main()
{
int Noofintervals, i;
Float sum, x, totalsum, h, partialsum, sumthread;
printf("Enter number of intervals\n");
scanf("%d", &Noofintervals);
if (Noofintervals <= 0) {
printf("Number of intervals should be positive integer\n");
exit(1);
}
sum = 0.0;
h = 1.0 / Noofintervals;
#pragma omp parallel for private(x) shared(sumthread)
Reg No: 17BCE0500
Name: Rahul Mangal
Lab Slot: L25+L26

for (i = 1; i < Noofintervals + 1; i = i + 1) {


x = h * (i - 0.5);
#pragma omp critical
sumthread = sumthread + 4.0 / (1 + x * x);
}
partialsum = sumthread * h;
#pragma omp critical
sum = sum + partialsum;
printf("The value of PI is \t%f \nerror is \t%1.16f\n", sum, fabs(sum - PI));
}

Output Screenshot:

Result:
17bce0500@sjt418scs008:~$ cd /home/likewise-
open/VITUNIVERSITY/17bce0500/Desktop
Reg No: 17BCE0500
Name: Rahul Mangal
Lab Slot: L25+L26

17bce0500@sjt418scs008:~/Desktop$ gcc -o PI -fopenmp PI.c


PI.c: In function ‘main’:
PI.c:37:3: warning: incompatible implicit declaration of built-in function ‘exit’
[enabled by default]
exit(1);
^
PI.c:63:64: warning: incompatible implicit declaration of built-in function ‘fabs’
[enabled by default]
printf("The value of PI is \t%f \nerror is \t%1.16f\n", sum, fabs(sum - PI));

17bce0500@sjt418scs008:~/Desktop$ ./PI
Enter number of intervals
5
The value of PI is 3.144926
error is 0.0033331788646920
17bce0500@sjt418scs008:~/Desktop$

SCENARIO – III

Print environment information (Number of Processes, Number of Threads etc.)

Write a simple OpenMP program that uses some OpenMP API functions to extract
information about the environment. It should be helpful to understand the language
/ compiler features of OpenMP runtime library.
Reg No: 17BCE0500
Name: Rahul Mangal
Lab Slot: L25+L26

To examine the above scenario, the functions such as omp_get_num_procs(),


omp_set_num_threads(), omp_get_num_threads(), omp_in_parallel(),
omp_get_dynamic() and omp_get_nested() are listed and the explanation is given
below to explore the concept practically.
omp_set_num_threads() takes an integer argument and requests that the
Operating System provide that number of threads in subsequent parallel regions.
omp_get_num_threads() (integer function) returns the actual number of threads
in the current team of threads.
omp_get_thread_num() (integer function) returns the ID of a thread, where the ID
rangesfrom 0 to the number of threads minus 1. The thread with the ID of 0 is the
master thread.
omp_get_num_procs() returns the number of processors that are available when
the function is called.
omp_get_dynamic() returns a value that indicates if the number of threads
available in subsequent parallel region can be adjusted by the run time.
omp_get_nested( ) returns a value that indicates if nested parallelism is enabled.

Algorithm:
1. Start
2. Initialize tid with omp_get_thread_num() function.
3. Using the above predefined functions, print the values of the inbuilt functions if
tid==0.
4. End

Source Code:

#include <stdio.h>
#include <stdlib.h>
Reg No: 17BCE0500
Name: Rahul Mangal
Lab Slot: L25+L26

#include <omp.h>
int main (int argc, char *argv[])
{
int nthreads, tid, procs, maxt, inpar, dynamic, nested;
#pragma omp parallel private(nthreads, tid)
{
tid = omp_get_thread_num();
if (tid == 0) {
printf("Thread %d getting environment info...\n", tid);
procs = omp_get_num_procs();
nthreads = omp_get_num_threads();
maxt = omp_get_max_threads();
inpar = omp_in_parallel();
dynamic = omp_get_dynamic();
nested = omp_get_nested();
printf("Number of processors = %d\n", procs);
printf("Number of threads = %d\n", nthreads);
printf("Max threads = %d\n", maxt);
printf("In parallel? = %d\n", inpar);
printf("Dynamic threads enabled? = %d\n", dynamic);
printf("Nested parallelism supported? = %d\n", nested);
}
}
}
Reg No: 17BCE0500
Name: Rahul Mangal
Lab Slot: L25+L26

Output Screenshot:

Result:
17bce0500@sjt418scs008:~$ cd /home/likewise-
open/VITUNIVERSITY/17bce0500/Desktop
17bce0500@sjt418scs008:~/Desktop$ gcc -o Information -fopenmp Information.c
17bce0500@sjt418scs008:~/Desktop$ ./Information
Thread 0 getting environment info...
Number of processors = 4
Number of threads = 4
Max threads = 4
In parallel? = 1
Dynamic threads enabled? = 0
Nested parallelism supported? = 0
17bce0500@sjt418scs008:~/Desktop$

You might also like