Below is the c program that I wrote that multiplies two matrixs and puts it in a
ID: 3590058 • Letter: B
Question
Below is the c program that I wrote that multiplies two matrixs and puts it in a new matrix. Can someone help me to impliment this program with OpenMP please?(Using static or dynamic threads, 8 threads would be fine) This should run on linux please.Thank you. This is the third time I am posting this question, because the one someone already answered but it didn't even compile, and doesn't work correctly. Please test the program once for me.Thank you.
#include
#include
#include
#include
#define A 800
#define B 800
#define C 800
int main()
{
//int row1 = 300, col1 = 300, row2 = 300, col2 = 300;
int i = 0, j = 0, k = 0;
int matxa[A][B], matxb[B][A], matxc[A][C];
clock_t t1,t2;
/*
printf("Enter the number of rows and columns for the first matrix: ");
printf("Number of rows: ");
scanf("%d", &row1);
printf("Number of columns: ");
scanf("%d", &col1);
srand(time(NULL));
printf("Enter the number of rows and columns for the second matrix: ");
printf("Number of rows: ");
scanf("%d", &row2);
printf("Number of columns: ");
scanf("%d", &col2);
if(row1 != col2 || col2!= row1)
{
printf("error!, the rows and column doesn't match");
return 0;
}
printf("First Array: ");
*/
srand(time(NULL));
t1 = clock();
for(i = 0; i
{
for(j = 0; j
{
matxa[i][j] = rand() % 100;
printf("%d ", matxa[i][j]);
}
printf(" ");
}
printf("Second Array: ");
for(i = 0; i
{
for(j = 0; j
{
matxb[i][j] = rand() % 10;
printf("%d ", matxb[i][j]);
}
printf(" ");
}
for(i = 0; i < A; i++)
{
for(j = 0; j < B; j++)
{
matxc[i][j] = 0;
}
}
for(i = 0; i < A; i++)
{
for(j = 0; j < B; j++)
{
for(k = 0; k < B; k++)
{
matxc[i][j] += matxa[i][k] * matxb[k][j];
}
}
}
printf("The multiplication of the given Matrixes is: ");
for(i = 0; i < A; i++)
{
for(j = 0; j < B; j++)
{
printf("%d ", matxc[i][j]);
}
printf(" ");
}
t2 = clock();
printf("Time passses: %f ",(t2-t1)/(float)(CLOCKS_PER_SEC));
return 0;
}
Explanation / Answer
Here is the code...
#include <omp.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
/* Marix size declaratins */
#define A 800
#define B 800
#define C 800
int main (int argc, char *argv[])
{
int pid, totalThreads, i, j, k, _chunk;
// matrix arrays declared
int matxa[A][B],
matxb[B][C],
matxc[A][C];
clock_t t1,t2;
srand(time(NULL));
t1 = clock();
_chunk = 10; /* chunk size */
/* parallel threads */
#pragma omp parallel shared(matxa,matxb,matxc,totalThreads,_chunk) private(pid,i,j,k)
{
pid = omp_get_thread_num();
if (pid == 0)
{
totalThreads = omp_get_num_threads();
printf("Started with %d threads ",totalThreads);
printf("matrix initialiazation started... ");
}
/* Initialize matrices */
#pragma omp for schedule (static, _chunk)
for (i=0; i<A; i++)
for (j=0; j<B; j++)
matxa[i][j]= rand() % 10;;
#pragma omp for schedule (static, _chunk)
for (i=0; i<B; i++)
for (j=0; j<C; j++)
matxb[i][j]= rand() % 10;;
#pragma omp for schedule (static, _chunk)
for (i=0; i<A; i++)
for (j=0; j<C; j++)
matxc[i][j]= 0;
/* multiplying with shared threads */
printf("Thread %d starting matrix multiply... ",pid);
#pragma omp for schedule (static, _chunk)
for (i=0; i<A; i++)
{
printf("thread -> %d performs row -> %d ",pid,i);
for(j=0; j<C; j++)
for (k=0; k<B; k++)
matxc[i][j] += matxa[i][k] * matxb[k][j];
}
}
/* result */
printf("The multiplication of the given Matrixes is: ");
for(i = 0; i < A; i++)
{
for(j = 0; j < B; j++)
{
printf("%d ", matxc[i][j]);
}
printf(" ");
}
t2 = clock();
printf("Time passses: %f ",(t2-t1)/(float)(CLOCKS_PER_SEC));
}
/*** ************* PLEASE ENDURE RUN WITH ****
gcc -multiplication.c -o obj -fopenmp
**/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.