-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pthreads_Matrix_Multiplication.cpp
137 lines (113 loc) · 3.33 KB
/
Pthreads_Matrix_Multiplication.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>
#include <cstdint>
int size, num_threads;
double **A, **B, **C;
using namespace std;
double matrix_creation(int rows, int columns, double **matrix) {
srand((unsigned)time(0));
for (int i = 0; i < rows; i ++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = rand()%10;
//cout << "(" << i << "," << j << ")" << matrix[i][j] << endl;
}
}
return matrix[rows][columns];
}
/**
* Thread routine.
* Each thread works on a portion of the 'matrix1'.
* The start and end of the portion depend on the 'arg' which
* is the ID assigned to threads sequentially.
*/
void * worker( void* arg )
{
int i, j, k, portion_size, row_start, row_end;
double sum = 0;
//cout << "inside worker" << endl;
int tid = *(int*)arg; // get the thread ID assigned sequentially.
portion_size = size / num_threads;
//cout << "size - " <<size << endl;
//cout << "thread id is : " << tid;
row_start = tid * portion_size;
//cout << "row_start - " << row_start << endl;
row_end = (tid+1) * portion_size;
//cout << "row_end - " << row_end << endl;
for (i = row_start-portion_size; i < row_end; ++i) { // hold row index of 'matrix1'
for (j = 0; j < size; ++j) { // hold column index of 'matrix2'
sum = 0; // hold value of a cell
/* one pass to sum the multiplications of corresponding cells
in the row vector and column vector. */
for (k = 0; k < size; ++k) {
sum += A[ i ][ k ] * B[ k ][ j ];
// cout << "in K loop " << i << " , " << k << " , " << j;
}
C[i][j] = sum;
// cout<<"matrix values : " << endl;
// cout << C[i][j]<<endl;
}
}
}
int main(int argc, char *argv[]) {
int K,L,M;
int i;
double sum = 0;
struct timeval tstart, tend;
double exectime;
K = atoi(argv[1]);
L = atoi(argv[2]);
M = atoi(argv[3]);
num_threads = atoi(argv[4]);
pthread_t threads[num_threads];
// initializing matrix
A = new double*[L];
B = new double*[M];
C = new double*[M];
// pointer initialization.
for(int i = 0; i<= L; i++) {
A[i] = new double[K];
}
for(int i = 0; i <= M; i++) {
B[i] = new double[L];
}
for(int i = 0; i<= M; i++) {
C[i] = new double[K];
}
C[K][M] = {0.0};
matrix_creation(K,L,A);
matrix_creation(L,M,B);
size = K;
if ( size % num_threads != 0 ) {
cout << "error";
return -1;
}
//threads = (pthread_t *) malloc( num_threads * sizeof(pthread_t) );
//gettimeofday( &tstart, NULL );
int *tid, x;
tid = &x;
for ( i = 0; i < num_threads; ++i ) {
//tid = &i;
*tid = i;
//cout << "finally calling output"<<endl;
pthread_create( &threads[i], NULL, worker, (void *)tid );
}
for ( i = 0; i < num_threads; ++i ) {
pthread_join( threads[i], NULL );
}
gettimeofday( &tend, NULL );
// for (int i = 0; i < K; i++) {
// for (int j = 0; j < K; j++) {
// cout << "( " << i << ","<<j <<" ) "<< C[i][j];
// }
// }
//exectime = (tend.tv_sec - tstart.tv_sec) * 1000.0; // sec to ms
//exectime += (tend.tv_usec - tstart.tv_usec) / 1000.0; // us to ms
//printf( "Number of MPI ranks: 0\tNumber of threads: %d\tExecution time:%.3lf sec\n",
//num_threads, exectime/1000.0);
return 0;
}