Professional Documents
Culture Documents
Ex 3 (Openmp - Iii) : Int Alg - Matmul2d (Int M, Int N, Int P, Float A, Float B, Float C)
Ex 3 (Openmp - Iii) : Int Alg - Matmul2d (Int M, Int N, Int P, Float A, Float B, Float C)
Ex 3 (Openmp - Iii) : Int Alg - Matmul2d (Int M, Int N, Int P, Float A, Float B, Float C)
EX 3 ( OPENMP –III)
SCENARIO – I
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.
Code Snippet
int alg_matmul2D(int m, int n, int p, float** a, float** b, float** c)
{
int i,j,k;
#pragma omp parallel shared(a,b,c) private(i,j,k)
{
#pragma omp for
schedule(static) for (i=0; i<m;
i=i+1){ for (j=0; j<n;
j=j+1){
a[i][j]=0.;
for (k=0; k<p; k=k+1){
a[i][j]=(a[i][j])+((b[i][k])*(c[k][j]));
}
}
}
}
return 0;
}
ALGORITHM:
1. Parallelize the outermost loop which drives the accesses to the result matrix a in the first
dimension.
2. Work-sharing among the threads is such that different threads will calculate different
rows of the result matrix a
SOURCE CODE:
#include<stdio.h>
#include<omp.h>
int alg_matmul2D(int m, int n, int p, float **a, float **b, float **c)
{
int i,j,k;
Reg No : 19BCE2028
Name : Pratham Shah
Slot : L47+L48
#pragma omp parallel shared(a,b,c) private(i,j,k)
{
#pragma omp for schedule(static) for (i=0; i<m; i=i+1){ for (j=0; j<n; j=j+1){
a[i][j]=0.;
for (k=0; k<p; k=k+1){
a[i][j]=(a[i][j])+((b[i][k])*(c[k][j]));
}
}
}
}
return 0;
}
int main()
{
alg_matmul2D(3,4,5,1.3,4.3,3.1);
}
OUTPUT:
RESULTS:
SCENARIO – II
Write a OpenMP program to calculate the value of PI by evaluating the integral 4/(1 + x 2) .
You can use three pragma block directives to handle the critical section , the sum and the
product display.
ALGORITHM:
SOURCE CODE:
#include<stdio.h>
#include<omp.h>
#include<math.h>
Reg No : 19BCE2028
Name : Pratham Shah
Slot : L47+L48
#define PI 3.1415926538837211
/* Main Program */
int main()
{
int Noofintervals, i;
float sum, x, totalsum, h, partialsum, sumthread;
if (Noofintervals <= 0) {
printf("Number of intervals should be positive integer\n");
exit(1);
}
sum = 0.0;
h = 1.0 / Noofintervals;
/*
* OpenMP Parallel Directive With Private Shared Clauses And Critical
* Section
*/
OUTPUT :
Reg No : 19BCE2028
Name : Pratham Shah
Slot : L47+L48
RESULTS:
SCENARIO – III
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.
ALGORITHM:
SOURCE CODE:
#include <stdio.h>
#include <omp.h>
int main()
{
omp_set_num_threads(4);
printf("%d\n", omp_get_num_threads( ));
#pragma omp parallel
#pragma omp master
{
printf("%d\n", omp_get_num_threads( ));
}
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
// Create the OpenMP parallel region, containing the number of threads as defined by
OMP_NUM_THREADS
#pragma omp parallel
{
// Each thread prints its identifier
printf("Loop 1: We are %d threads, I am thread %d.\n", omp_get_num_threads(),
omp_get_thread_num());
#pragma single
{
current_num_threads = omp_get_num_threads();
}
}
omp_set_num_threads(current_num_threads+1);
return EXIT_SUCCESS;
}
Output:
#include <stdio.h>
#include <omp.h>
int main( )
{
printf("%d\n", omp_get_num_procs( ));
#pragma omp parallel
#pragma omp master
{
printf("%d\n", omp_get_num_procs( ));
}
Reg No : 19BCE2028
Name : Pratham Shah
Slot : L47+L48
}
Output:
#include <stdio.h>
#include <omp.h>
int main( )
{
omp_set_num_threads(4);
printf_s("%d\n", omp_in_parallel( ));
5. omp_get_dynamic():
#include <stdio.h>
#include <omp.h>
int main()
{
omp_set_dynamic(9);
omp_set_num_threads(4);
printf("%d\n", omp_get_dynamic( ));
#pragma omp parallel
#pragma omp master
{
printf("%d\n", omp_get_dynamic( ));
}
}
Output:
6. omp_get_nested():
#include <stdio.h>
#include <omp.h>
int main( )
Reg No : 19BCE2028
Name : Pratham Shah
Slot : L47+L48
{
omp_set_nested(1);
omp_set_num_threads(4);
printf("%d\n", omp_get_nested( ));
#pragma omp parallel
#pragma omp master
{
printf("%d\n", omp_get_nested( ));
}
}
Output:
EXECUTION:
RESULTS:
The different OpenMP API functions were successfully implemented and their uses were
observed.