-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathats_time.cpp
209 lines (203 loc) · 7.21 KB
/
ats_time.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
double** myImg; //input image
double* oldMeans; //means for each cluster in the previous iteration
double* newMeans; //means for each cluster after current iteration
atomic<bool>*** updating; //indicates the status of a data point whether it is being updated by some thread in the current iteration
atomic<bool>*** updated; //indicates the status of a data point whether it is already updated by some thread or not in the current iteration
double*** membershipMatix; //stores the membership values of each datapoint to each of the cluster
double*** spatialMatix;
ifstream fin("sample.txt");
int w,h,p,q,K,C;
bool truth = true;
bool fallacy = false;
double square(double x){
return x*x;
}
void sfcm(int x_start, int x_end, int y_start, int y_end){ //calculates membership matrix for specified region by also considering the spatial information
for(int i = x_start; i <= x_end; i++){ //find the membership matrix without considering the spatial information
for(int j = y_start; j <= y_end; j++){
for(int k = 0; k < C; k++){
if(updating[i][j][k].compare_exchange_strong(fallacy, truth)){
if(!updated[i][j][k]){
double temp = 0;
for(int l = 0; l < C; l++)
temp += square((myImg[i][j] - oldMeans[k])/(myImg[i][j] - oldMeans[l] + 1e-5));
membershipMatix[i][j][k] = 1.0/(temp + 1e-5);
updated[i][j][k].store(true, memory_order_seq_cst);
updating[i][j][k].store(false, memory_order_seq_cst);
}
else{
updating[i][j][k].store(false, memory_order_seq_cst);
}
}
}
}
}
for(int i = x_start; i <= x_end; i++){ //calculates the spatial matrix
for(int j = y_start; j <= y_end; j++){
for(int k = 0; k < C; k++){
double temp = 0;
for(int b1 = max(i-2,0); b1 < min(i+3, h); b1++){
for(int b2 = max(j-2, 0); b2 < min(j+3,w); b2++){
if(updated[b1][b2][k].load(memory_order_seq_cst))
temp += membershipMatix[b1][b2][k];
else if(updating[b1][b2][k].compare_exchange_strong(fallacy, truth)){
double temp = 0;
for(int l = 0; l < C; l++)
temp += square((myImg[b1][b2] - oldMeans[k])/(myImg[b1][b2] - oldMeans[l] + 1e-5));
membershipMatix[b1][b2][k] = 1.0/(temp + 1e-5);
updated[b1][b2][k].store(true, memory_order_seq_cst);
updating[b1][b2][k].store(false, memory_order_seq_cst);
}
else{
while(!updated[b1][b2][k]);
temp += membershipMatix[b1][b2][k];
}
}
}
spatialMatix[i][j][k] = temp;
}
}
}
for(int i = x_start; i <= x_end; i++){ // calculates the membership matrix with spatial information using previously calculated
for(int j = y_start; j <= y_end; j++){ // membership matrix and spatial matrix
double sum = 0;
for(int k = 0; k < C; k++)
sum += pow(membershipMatix[i][j][k],p) * pow(spatialMatix[i][j][k],q);
for(int k = 0; k < C; k++)
membershipMatix[i][j][k] = (pow(membershipMatix[i][j][k],p) * pow(spatialMatix[i][j][k],q))/(sum + 1e-5);
}
}
}
void update(int idx){ //calculates final means for all clusters
double num = 0;
double den = 0;
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
num += square(membershipMatix[i][j][idx]) * myImg[i][j];
den += square(membershipMatix[i][j][idx]);
}
}
newMeans[idx] = num/(den+1e-5);
}
bool checkConv(double eps){ //checks if the algorithm converged
bool temp = true;
for(int i = 0; i < C; i++){
if(abs(newMeans[i] - oldMeans[i]) > eps)
temp = false;
oldMeans[i] = newMeans[i];
newMeans[i] = 0;
}
return temp;
}
int main(){
double epsilon; // used to check convergence
srand(time(NULL));
cout << "Width and Height: ";
cin >> w >> h;
cout << "No. of clusters: ";
cin >> C;
cout << "No. of threads: ";
cin >> K;
cout << "Threshold for convergence: ";
cin >> epsilon;
cout << "p and q: ";
cin >> p >> q;
oldMeans = new double[C];
newMeans = new double[C];
myImg = new double*[h];
membershipMatix = new double**[h];
spatialMatix = new double**[h];
updating = new atomic<bool>**[h];
updated = new atomic<bool>**[h];
for(int i = 0; i < h; i++){ //initalizing myImg ,spatial, membership, updated and updating matrices
myImg[i] = new double[w];
string params;
getline(fin, params);
istringstream ss(params);
membershipMatix[i] = new double*[w];
spatialMatix[i] = new double*[w];
updating[i] = new atomic<bool>*[w];
updated[i] = new atomic<bool>*[w];
for(int j = 0; j < w; j++){
ss >> myImg[i][j];
membershipMatix[i][j] = new double[C];
spatialMatix[i][j] = new double[C];
updating[i][j] = new atomic<bool>[C];
updated[i][j] = new atomic<bool>[C];
for(int k = 0; k < C; k++){
membershipMatix[i][j][k] = 0;
spatialMatix[i][j][k] = 0;
updated[i][j][k].store(false, memory_order_seq_cst);
updating[i][j][k].store(false, memory_order_seq_cst);
}
}
}
int randrows[h] = {0}; // random row indices used for selecting distinct data points from myImg matrix
int randcols[w] = {0}; // random column indices used for selecting distinct data points from myImg matrix
iota(randrows,randrows+h,0); // initializing the randrows array with values 0,1,2..h-1
iota(randcols,randcols+w,0); // initializing the randcols array with values 0,1,2..w-1
random_shuffle(randrows,randrows+h); // randomizing row indices
random_shuffle(randcols,randcols+w); // randomizing column indices
cout << "Initial means:" << endl;
for(int i = 0; i < C; i++){
oldMeans[i] = myImg[randrows[i]][randcols[i]]; // selecting random C distinct data points from the myImg
cout << oldMeans[i] << endl;
newMeans[i] = 0;
}
auto tstart = high_resolution_clock::now(); // starting time
int iter = 0;
while(!checkConv(epsilon)){
thread tids[K];
int wstep = w/K; // dividing the matrix into disjoint regions(submatrices) for each thread
int hstep = h/K;
int wleft = w%K;
int hleft = h%K;
int xstart = 0;
int ystart = 0;
int xend = 0;
int yend = 0;
for(int k = 0; k < K; k++){ // each thread executes the sfcm function to calculate membership values
xend = xstart + hstep;
yend = ystart + wstep;
if(hleft <= 0){
xend--;
}
if(wleft <= 0){
yend--;
}
hleft--;
wleft--;
tids[k] = thread(sfcm, xstart, xend, ystart, yend);
xstart = xend+1;
ystart = yend+1;
}
for(auto& t : tids)
t.join();
for(int i=0;i<h;++i){ //make the updated and updating matrices false for next iteration
for(int j=0;j<w;++j){
for(int k=0;k<C;++k){
updated[i][j][k].store(false,memory_order_seq_cst);
updating[i][j][k].store(false,memory_order_seq_cst);
}
}
}
thread tid2[C];
for(int k = 0; k < C; k++) // allocating each thread to each cluster and calculating final membership matrix
tid2[k] = thread(update, k);
for(auto& t : tid2)
t.join();
iter++;
}
auto tend = high_resolution_clock::now(); // end timer
auto duration = duration_cast<microseconds>(tend-tstart); // time taken
cout<<"time: "<<duration.count()<<endl;
cout<<"iters: "<<iter<<endl;
cout << "New means:" << endl;
for(int i = 0; i < C; i++)
cout << oldMeans[i] << endl;
fin.close();
return 0;
}