Parallel programming in C/C++. In this assignment you will use the Pthreads libr
ID: 3786460 • Letter: P
Question
Parallel programming in C/C++.
In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this algorithm.
Use the matrix mulltiplication algorithm.
Write a program that contains three functions:
(1) A function that has an integer as a parameter and returns a pointer to square array of integers (i.e. both dimensions should be equal). The function should allocate storage from the heap and randomly fill that dynamically allocated array.
(2) A function that uses the algorithm described above to compute the matrix square of an array. It should be passed a pointer to an array of integers and an integer defining the dimensions of the array. It should return a pointer to the array containing the product.
(3) A function that uses pthreads to compute the matrix square of an array. This function should have the same parameters and return values of the previous function
The main() function in your program needs to use these functions to compute the square of matrices with 100, 500, 1000, 5000, and 10000 integers
Assume that the values used for the size of these square arrays will always be even.
My suggestion is to think about dividing each array into smaller and smaller pieces until you reach some reasonably small size. At that point multiply the matrices using the iterative algorithm.
Explanation / Answer
c program
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <process.h>
void main()
{
int a[10][10],i,j,m,n;
clrscr();
printf(" Enter array length row by coloum : ");
scanf("%d,%d",&n,&m);
if((n > 10) || (m > 10))
{
printf(" Input array is more than declared ");
sleep(3);
exit(1);
}
printf(" Enter elements row-wise... ");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
printf(" Elements entered by you are (in form of matrix) : ");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf(" %d",a[i][j]);
printf(" ");
}
getch();
}
/* 10. TO CALCULATE SUM OF TWO MATRIX. */
#include<stdio.h> #include<conio.h> #include<process.h> #include<dos.h>
void main()
{
int a[10][10],b[10][10],s[10][10];
int i,j,r1,c1,r2,c2;
printf(" Enter no. of elements for MATRIX:A Row by Column: ");
scanf("%d,%d",&r1,&c1);
printf(" Enter no. of elements for MATRIX:B Row by Column: ");
scanf("%d,%d",&r2,&c2);
if((r1 > 10) || (c1 > 10) || (r2>10) || (c2>10))
{
printf(" ENTERED NO. MORE THAN DECLARED");
sleep(1); exit(1);
}
if((r1 != r2) || (c1 != c2))
{
printf(" ADDITION OF MATRIX IS NOT FEASIBLE"); sleep(1); exit(1);
}
printf(" Enter elements for MATRIX-A by ROW WISE: ");
for(i=0; i<r1; i++) {
for(j=0; j<c1; j++)
{
scanf(" %d",&a[i][j]);
}
}
printf(" Enter elements for MATRIX-B by ROW WISEE: ");
for(i=0; i<r1; i++) {
for(j=0; j<c1; j++)
{
scanf(" %d",&b[i][j]);
}
}
printf(" Sum of Two Matrix is as... ");
for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
{
s[i][j] = a[i][j] + b[i][j];
}
}
for(i=0; i<r1; i++) {
for(j=0; j<c1; j++)
{
printf(" %d",s[i][j]);
}
}
getch();
}
/* 11. TO CALCULATE PRODUCT OF TWO MATRIX. */
#include <stdio.h> #include <conio.h> #include <dos.h> #include <process.h>
void main()
{
int a[10][10],b[10][10],c[10][10]; int i,j,r1,c1,r2,c2,k;
clrscr();
printf(" Enter length of Marix-A Row by Coloum: "); scanf("%d,%d",&r1,&c1);
printf(" Enter length of Marix-B Row by Coloum: "); scanf("%d,%d",&r2,&c2);
if ((r1 > 10) || (c1 > 10) || (r2>10) || (c2>10))
{
printf(" Input array is more than declared "); sleep(2); exit(1);
}
if (c1 != r2)
{
printf(" Matrix Multiplication is not feasible ");
sleep(2);
exit(1);
}
printf(" Enter elements of Matrix-A Row-wise... ");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++) scanf("%d",&a[i][j]);
printf(" Enter elements of Matrix-B Row-wise... ");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++) scanf("%d",&b[i][j]);
printf(" Product of Two Matrix is as... ");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j] = 0;
for(k=0;k<r2;k++)
c[i][j] += (a[i][k]*b[k][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%d ",c[i][j]);
printf(" ");
}
getch();
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.