-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_queueparallel.cpp
121 lines (99 loc) · 2.23 KB
/
simple_queueparallel.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
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <thread>
#include <mutex>
#define LL long long int
using namespace std;
std::queue < pair <LL,LL> > Q;
std::mutex flock;
#ifdef FIL
#include <fstream>
std::ifstream cin("inputMatrix3.txt");
std::ofstream cout("output4.txt");
#else
#include <iostream>
using std::cin;
using std::cout;
#endif
long long int mult(long long int* a,long long int *b,long long int n){
long long int sum = 0;
for (int i = 0; i < n; ++i)
{
sum+=a[i]*b[i];
}
return sum;
}
void thread_rcm(long long int** a,long long int **b, long long int **c,long long int n){
std::pair <LL,LL> p;
while(1){
flock.lock();
if(Q.empty()){
flock.unlock();
return;
}
p = Q.front();
Q.pop();
//printf("%lld %lld %d\n",p.first,p.second,Q.size());
flock.unlock();
c[p.first][p.second] = mult(a[p.first],b[p.second],n);
}
}
int main(int argc, char* argv[])
{
long long int n;
cin >> n;
long long int **a = new long long int *[n];
long long int **b = new long long int *[n];
long long int **c = new long long int *[n];
LL th;
cin >> th;
std::thread *compute = new thread[th];
// queue
for(int i = 0; i < n; i++){
a[i] = new long long int[n];
b[i] = new long long int[n];
c[i] = new long long int[n];
// compute[i] = new thread[n];
for(int j = 0; j < n; j++){
cin >> a[i][j];
Q.push({i,j});
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> b[j][i];
}
}
int count = 0;
for(int i = 0; i < th; i++){
compute[i] = thread(thread_rcm,a,b,c,n);
}
for(int i = 0; i < th; i++){
compute[i].join();
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << c[i][j] <<" ";
}
cout <<endl;
}
return 0;
}