-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cpp
148 lines (115 loc) · 3.64 KB
/
matrix.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
138
139
140
141
142
143
144
145
146
147
148
#include "matrix.hpp"
void matrix::print_matrix(){
for(int j=0; j<n; j++){
for(int i=0; i<m; i++){
printf("%f ", mtx[j*m + i]);
}
}
printf("\n");
}
void matrix::print_sumcols_matrix(){
for(int i=0; i<m; i++){
if(i>=0 && i<10){
printf("%f ", sum_cols_mtx[i]);
}
}
printf("\n");
}
//column major access
void matrix::sum_cols_matrix_v1(){
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
sum_cols_mtx[i] += mtx[j*m + i];
}
}
//print_sumcols_matrix();
}
//row major access
void matrix::sum_cols_matrix_v2(){
for(int j=0; j<n; j++){
for(int i=0; i<m; i++){
sum_cols_mtx[i] += mtx[j*m + i];
}
}
//print_sumcols_matrix();
}
//row major access
void matrix::sum_cols_matrix_openmp_v2(){
int i=0, j=0;
#pragma omp parallel num_threads(nb_threads)
{
float sum_cols_mtx_private[m] = {0};
#pragma omp for private(i) schedule(static)
for(j=0; j<n; j++){
for(i=0; i<m; i++){
sum_cols_mtx_private[i] += mtx[j*m + i];
}
}
#pragma omp critical
{
for(i=0; i<m; i++){
sum_cols_mtx[i] += sum_cols_mtx_private[i];
}
}
}
//print_sumcols_matrix();
}
void* sum_columns(void* thread_arg) {
struct thread_data *thread_d = (struct thread_data*) thread_arg;
int size_work = 0, rest_work, row_start = 0, row_end = 0;
size_work = thread_d->nb_rows/thread_d->nb_threads;
rest_work = thread_d->nb_rows%thread_d->nb_threads;
if((rest_work != 0) && (thread_d->id_thread == 0)){
size_work += rest_work;
}
row_start = thread_d->id_thread*size_work;
row_end = (thread_d->id_thread+1)*size_work;
if((rest_work != 0) && (thread_d->id_thread != 0)){
row_start += rest_work;
row_end += rest_work;
}
//printf("ID_THREAD %d, T_ID %d size_work %d row_start %d row_end %d\n",
// (int)pthread_self(), thread_d->id_thread, size_work, row_start, row_end);
for(int j=row_start; j<row_end; j++){
for(int i=0; i<thread_d->nb_columns; i++){
thread_d->sum_cols[i] += thread_d->data_matrix[j*thread_d->nb_columns + i];
//printf(" THREAD_ID %d ROW %d :sum_cols[COLS : %d] %f\n", thread_d->id_thread, j, i, thread_d->sum_cols[i]);
}
}
}
void matrix::sum_cols_matrix_pthreads_v2(){
pthread_t threads[nb_threads];
struct thread_data thr_data[nb_threads];
for(int j=0; j<nb_threads; j++){
float* sum_cols = NULL;
if((sum_cols = (float*)malloc(m*sizeof(float))) == NULL){
printf("error while allocating memory for sum_cols\n");
exit(1);
}
thr_data[j].id_thread = j;
thr_data[j].nb_columns = m;
thr_data[j].nb_rows = n;
thr_data[j].nb_threads = nb_threads;
thr_data[j].data_matrix = mtx;
thr_data[j].sum_cols = sum_cols;
if((pthread_create(&threads[j], NULL, sum_columns, &thr_data[j]) != 0)){
perror("Problem when creating a thread");
}
}
for(int j=0; j<nb_threads; j++){
pthread_join(threads[j], NULL);
//printf("End of thread ID : %d\n", (int)threads[j]);
}
for(int j=0; j<nb_threads; j++){
for(int k=0; k<m; k++){
sum_cols_mtx[k] += thr_data[j].sum_cols[k];
}
}
//print_sumcols_matrix();
for(int j=0; j<nb_threads; j++){
free(thr_data[j].sum_cols);
}
}
void matrix::memset_sum_cols_mtx(){
memset(sum_cols_mtx, 0, sizeof(float)*m);
}