-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmult_parallel.c
164 lines (119 loc) · 3.82 KB
/
mmult_parallel.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#define NROW 1024
#define NCOL NROW
#define NUM_THREADS 8
#define TEST_RESULTS
//Matrix A
int matrixA [NROW][NCOL];
//Matrix B
int matrixB [NROW][NCOL];
//Matrix C
int matrixC [NROW][NCOL];
//Temp array
int tempMatrix [NROW][NCOL];
//Output Array C
int outputMatrix [NROW][NCOL];
struct timeval startTime;
struct timeval finishTime;
double timeIntervalLength;
void verifyMatrixSum();
// mutex
pthread_mutex_t mutex_p = PTHREAD_MUTEX_INITIALIZER;
// Pthread_matrix_mult
void *Pthread_matrix_mult(void *threadid){ // only needs to do one matrix, can call again for other?
long tid;
int temp_val;
tid = (long) threadid;
int i, j, k;
//pthread_mutex_lock(&mutex_p);
int my_work = NROW/NUM_THREADS; // might want to lock this as it is accessing a global
// amount of work is equal to Nrow because we go through each of the positions in the
// row and multiplies and sum up each multiply
//pthread_mutex_unlock(&mutex_p);
int my_first_row = tid * my_work;
int my_last_row = ((tid +1) * my_work);
// maybe lock bc accessing global matrices
for(i = my_first_row; i < my_last_row; i++){ // this will be matrix A row and then temp Matrix's row
for(j = 0; j < NCOL; j++){ // this will be Matrix B column
for(k = 0; k < NROW; k++){
//pthread_mutex_lock(&mutex_p);
tempMatrix[i][j] += matrixA[i][k] * matrixB[k][j];
//pthread_mutex_unlock(&mutex_p);
}
}
for(j = 0; j < NCOL; j++){ // this will be matrix C column
for(k = 0; k < NROW; k++){
//pthread_mutex_lock(&mutex_p);
outputMatrix[i][j] += tempMatrix[i][k] * matrixC[k][j];
//pthread_mutex_unlock(&mutex_p);
}
}
}
}
int main(int argc, char* argv[])
{
int i,j,k;
// Matrix initialization. Just filling with arbitrary numbers.
for(i=0;i<NROW;i++)
{
for(j=0;j<NCOL;j++)
{
matrixA[i][j]= (i + j)/128;
matrixB[i][j]= (j + j)/128;
matrixC[i][j]= (i + j)/128;
tempMatrix[i][j] = 0;
outputMatrix[i][j]= 0;
}
}
//Get the start time
gettimeofday(&startTime, NULL); /* START TIME */
// DECLARE THREADS
pthread_t threads[NUM_THREADS]; // Handles for our threads
int rc;
long t;
void *status;
// CREATE MULTIPLE THREADS
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, Pthread_matrix_mult, (void *)t); //pthread_create is always inside the loop
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
// JOIN THREADS
for(t=0; t<NUM_THREADS; t++) {
rc = pthread_join(threads[t], &status);
}
//Get the end time
gettimeofday(&finishTime, NULL); /* END TIME */
//Calculate the interval length
timeIntervalLength = (double)(finishTime.tv_sec-startTime.tv_sec) * 1000000
+ (double)(finishTime.tv_usec-startTime.tv_usec);
timeIntervalLength=timeIntervalLength/1000;
#ifdef TEST_RESULTS
//[Verifying the matrix summation]
verifyMatrixSum();
#endif
//Print the interval length
printf("Interval length: %g msec.\n", timeIntervalLength);
return 0;
}
// Helper function to verify if the sum from parallel and serial versions match
void verifyMatrixSum() {
int i, j;
double totalSum;
totalSum=0;
//
for(i=0;i<NROW;i++){
for(j=0;j<NCOL;j++)
{
totalSum+=(double)outputMatrix[i][j];
}
}
printf("\nTotal Sum = %g\n",totalSum);
}