-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.hpp
73 lines (55 loc) · 1.59 KB
/
add.hpp
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
#include <iostream>
#include <pthread.h>
#include <thread>
#include "matrix.hpp"
template <typename T>
void *add_t(void *arg)
{
threadData<T> *data;
data = (threadData<T> *) arg;
Matrix<T>& mat1 = *data->mat1;
Matrix<T>& mat2 = *data->mat2;
Matrix<T>& output = *data->output;
for (int i=data->start; i<data->end; i++)
{
int row = i/mat1.getWidth();
int col = i%mat1.getWidth();
output[row][col] = mat1[row][col] + mat2[row][col];
}
pthread_exit(NULL);
}
template <typename T>
void add(Matrix<T> &mat1, Matrix<T> &mat2, Matrix<T> &output)
{
int num_threads = std::thread::hardware_concurrency();
if(num_threads == 0)
{
num_threads = 4;
}
if((mat1.getWidth() != mat2.getWidth()) || (mat1.getHeight() != mat2.getHeight()))
{
std::cout << "Matrix sizes must be equal" << std::endl;
throw(1);
}
output.setWidth(mat2.getWidth());
output.setHeight(mat2.getHeight());
int num_operations = output.getWidth() * output.getHeight();
pthread_t thread[num_threads];
int rc;
threadData<T> data[num_threads];
for (int t=0; t<num_threads; t++)
{
data[t] = threadData<T>();
data[t].mat1 = &mat1;
data[t].mat2 = &mat2;
data[t].output = &output;
data[t].start = num_operations*t/num_threads;
data[t].end = num_operations*(t+1)/num_threads;
rc = pthread_create(&thread[t], NULL, add_t<T>, (void *) &data[t]);
if (rc)
std::cout << "Erro ao criar a thread" << std::endl;
}
for (int t=0; t<num_threads; t++){
pthread_join(thread[t], NULL);
}
}