Professional Documents
Culture Documents
SCENARIO – I
Matrix multiplication
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 tid, nthreads, i, j, k, chunk;
double a[NRA][NCA],
b[NCA][NCB],
c[NRA][NCB];
chunk = 10;
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$ ./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)
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
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$ ./PI
Enter number of intervals
5
The value of PI is 3.144926
error is 0.0033331788646920
17bce0500@sjt418scs008:~/Desktop$
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.
Reg No: 17BCE0500
Name: Rahul Mangal
Lab Slot: L25+L26
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$