Assignment 2 Notes
Matrix multiplication:
0
1
2
3
1
2
3
4
2
3
4
5
3
4
5
6
*
0
1
2
3
=
14
20
26
32
We Use:
(
a
0 0
a
0 1
0
0
a
1 0
a
1 1
0
0
a
2 0
a
2 1
0
0
a
3 0
a
3 1
0
0
+
0
0
a
0 2
a
0 3
0
0
a
1 2
a
2 3
0
0
a
2 2
a
2 3
0
0
a
3 2
a
3 3
)
*
b
0
b
1
b
2
b
3
=
x
0,0
x
1,0
x
2,0
x
3,0
+
x
0,1
x
1,1
x
2,1
x
3,1
=
c
0
c
1
c
2
c
3
The code below using two
pragma
s.
The Code:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define NRA 4
/* number of rows in matrix A */
#define NCA 4
/* number of columns in matrix A = rows of B*/
#define CHUNK 2
/*chunk size */
int
main
(
int
argc
,
char
*
argv
[])
{
int
tid
,
i
,
j
;
int
a
[
NRA
][
NCA
],
/* matrix A to be multiplied */
b
[
NCA
],
/* matrix B to be multiplied */
c
[
NRA
],
/* result matrix C */
x
[
NRA
][
NCA
/
CHUNK
];
/* Individual Thread results */
for
(
i
=
0
;
i
<
NRA
;
i
++)
{
for
(
j
=
0
;
j
<
NCA
;
j
++)
{
a
[
i
][
j
]=
i
+
j
;
printf
(
" a[%d,%d]=%i "
,
i
,
j
,
a
[
i
][
j
]);
}
printf
(
"\n"
);
}
for
(
i
=
0
;
i
<
NCA
;
i
++)
{
b
[
i
]=
i
;
printf
(
"b[%i]=%i "
,
i
,
b
[
i
]);
}
printf
(
"\nChecking\n"
);
/*Verify result*/
for
(
i
=
0
;
i
<
NRA
;
i
++)
{
c
[
i
]=
0
;
for
(
j
=
0
;
j
<
NCA
;
j
++)
{
c
[
i
]
=
c
[
i
]+
a
[
i
][
j
]*
b
[
j
];
}
printf
(
"c[%i]= %i "
,
i
,
c
[
i
]);
}
printf
(
"\n\nEntering Parallel Region\n"
);
#pragma omp parallel num_threads(NCA/CHUNK) shared(a,b,c,x) private(tid,i,j)
{
tid
=
omp_get_thread_num
();
printf
(
"Thread %d starting matrix multiplication\n"
,
tid
);
for
(
i
=
0
;
i
<
NRA
;
i
++)
{
x
[
i
][
tid
]=
0
;
for
(
j
=
tid
*
CHUNK
;
j
<(
tid
+
1
)*
CHUNK
;
j
++)
{
x
[
i
][
tid
]
=
x
[
i
][
tid
]+
a
[
i
][
j
]*
b
[
j
];
}
printf
(
"Thread %i x[%i][%i] = %i\n"
,
tid
,
i
,
tid
,
x
[
i
][
tid
]);
}
}
printf
(
"\nLeft Parallel Region\n"
);
for
(
i
=
0
;
i
<
NRA
;
i
++)
{
c
[
i
]=
0
;
for
(
j
=
0
;
j
<
CHUNK
;
j
++)
{
c
[
i
]=
c
[
i
]+
x
[
i
][
j
];
}
printf
(
"c[%i]=%i "
,
i
,
c
[
i
]);
}
printf
(
"\n"
);
return
0
;
}
The Run: