Assignment 4: Name: Harshil Shah Reg No: 201070033 Subject: Parallel Computing
Assignment 4: Name: Harshil Shah Reg No: 201070033 Subject: Parallel Computing
Assignment 4: Name: Harshil Shah Reg No: 201070033 Subject: Parallel Computing
ASSIGNMENT 4
3. Matrix multiplication
It creates as many threads which are processing cores in the system. Thus, for a dual-
core system, two threads are created, for a quad-core system, four are created; and so
forth. Then all the threads simultaneously execute the parallel region. When each
thread exits the parallel region, it is terminated. OpenMP provides several additional
directives for running code regions in parallel, including parallelizing loops.
In addition to providing directives for parallelization, OpenMP allows developers to
choose among several levels of parallelism. E.g., they can set the number of threads
manually. It also allows developers to identify whether data are shared between
threads or are private to a thread. OpenMP is available on several open-source and
commercial compilers for Linux, Windows, and Mac OS X systems
We must include the omp header file in the beginning of the program.
OUTPUT :
1) Fork Join
// OpenMP program to print Hello World
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main()
tid = omp_get_thread_num();
tid);
if(tid == 0){
nthreads = omp_get_num_threads();
}
}
// Ending of parallel region
2) Producer Consumer Problem
#include<stdio.h>
#include<omp.h>
int main()
{
if(omp_get_thread_num()==0)
{
printf("Master thread with Thread ID:%d\n", omp_get_thread_num());
printf("Since it is the producer thread It is adding some data to be consumed by other consumer threads\n");
i+=10; x=1;
}
else
{
while(x==0)
printf("Waiting for buffer to be filled. Thread ID:
%d\n",omp_get_thread_num());
#pragma critical
{
if(i>0){
printf("Data is consumed by Consumer with Thread ID: %d\n",omp_get_thread_num());
}
else {
i-=5;
}
}
}
3) Matrix multiplication
#include<stdio.h>
#include<omp.h>
#include<stdlib.h>
int main(){
int i,j,k,m,n,p;
printf("Enter the number of rows in Matrix 1:");
scanf("%d",&m);
int *matrixA[m];
printf("Enter the number of columns in Matrix 1:");
scanf("%d",&n);
for(i=0;i<m;i++){
matrixA[i] = (int *)malloc(n*sizeof(int));
}
printf("<--Now Input the values for matrix 1 row-wise-->\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&matrixA[i][j]);
}
}
}
printf("<--Now Input the values for matrix 2 row-wise-->\n");
for(i=0;i<n;i++){
for(j=0;j<p;j++){
scanf("%d",&matrixB[i][j]);
}
}
int matrixC[m][p];
#pragma omp parallel private(i,j,k) shared(matrixA,matrixB,matrixC)
{
matrixC[i][j]=(matrixC[i][j])+((matrixA[i][k])*(matrixB[k][j]));
}
}
}
}
printf("The output after Matrix Multiplication is: \n"); for(i=0;i<m;i++){
for(j=0;j<p;j++)
printf("%d \t",matrixC[i][j]);
printf("\n");
}
return 0;
int i;
for (i = 2; i < number; i++) {
if (number % i == 0 && i != number) return 0;
}
return 1;
}
int main()
{
int noOfThreads,valueN,indexCount=0,arrayVal[10000],tempValue;
printf("Enter the Number of threads: ");
scanf("%d",&noOfThreads);
indexCount++;
}
}
printf("Number of prime numbers between 2 and %d: %d\n",valueN,indexCount);
return 0;
}
#include<omp.h>
#include<stdlib.h>
int main()
int
for(iIterator=0;iIterator<numberOfElements;iIterator++){
scanf("%d",&arrayInput[iIterator]);
for(iIterator=0;iIterator<numberOfElements;iIterator++){
currentMax){
currentMax = arrayInput[iIterator];
%d\n",currentMax); return 0;
}
6) Pi calculation
#include<stdio.h>
#include<omp.h>
#include<stdlib.h>
int main(){
int num_steps=10000,i;
double aux,pi,step = 1.0/(double) num_steps,x=0.0,sum = 0.0;
#pragma omp parallel private(i,x,aux) shared(sum) {
#pragma omp for schedule(static)
for (i=0; i<num_steps; i=i+1){
x=(i+0.5)*step;
aux=4.0/(1.0+x*x);
#pragma omp critical sum = sum + aux;
}
}
pi=step*sum;
printf("The Value of PI is %lf\n",pi);
return 0;