Skip to content

Commit 5fdd66c

Browse files
committed
keep code with atomic on class
1 parent e8aa415 commit 5fdd66c

File tree

3 files changed

+46
-38
lines changed

3 files changed

+46
-38
lines changed

.vscode/tasks.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"label": "C/C++: release g++.exe",
2929
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
3030
"args": [
31+
"-std=c++20",
3132
"-O2",
3233
"${file}",
3334
"-o",

random_walk_thread.cpp

+40-36
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Progress {
3131

3232
void do_random_walk(const int thread_id, const int size, const int dimension,
3333
const unsigned int max_try_before_giveup, const int MODULO, int& oWent_back_to_start,
34-
std::atomic<Progress>* progress)
34+
std::atomic<Progress*>* progress)
3535
{
3636
std::random_device rd; // obtain a random number from hardware
3737
std::mt19937 gen(rd()); // seed the generator
@@ -55,10 +55,11 @@ void do_random_walk(const int thread_id, const int size, const int dimension,
5555
int percent = (100 * i) / size;
5656
if (percent >= nextPrint)
5757
{
58-
//std::cout << "\rThread progress(" << thread_id << "): " << std::string(percent/5, '|') << percent << "%" ; std::cout.flush();
59-
Progress aProgress = progress->load();
60-
aProgress._percent=percent;
58+
//std::cout << "Thread progress(" << thread_id << "): " << std::string(percent/5, '|') << percent << "%" << std::endl;
59+
Progress* aProgress = progress->load();
60+
aProgress->_percent=percent;
6161
progress->store(aProgress);
62+
progress->notify_all();
6263
nextPrint += step;
6364
}
6465

@@ -67,19 +68,8 @@ void do_random_walk(const int thread_id, const int size, const int dimension,
6768
current_point[j]=0;
6869
}
6970

70-
//to print each walk progress
71-
double walk_step = 1.0;
72-
double walk_nextPrint = walk_step;
73-
bool has_printed = false;
7471
//random walk
7572
for(unsigned int j=0; j < max_try_before_giveup; ++j){
76-
77-
double walk_percent = 100*( (double) j / max_try_before_giveup);
78-
if (walk_percent >= walk_nextPrint)
79-
{
80-
//std::cout << "\rWalk(" << thread_id << "): " << std::string(walk_percent/5, '|') << walk_percent << "%"; std::cout.flush(); has_printed = true;
81-
walk_nextPrint += walk_step;
82-
}
8373

8474
//go +1 or -1 ?
8575
int aStep = 0;
@@ -107,43 +97,53 @@ void do_random_walk(const int thread_id, const int size, const int dimension,
10797

10898
if (went_back_to_start) {
10999
oWent_back_to_start++;
110-
Progress aProgress = progress->load();
100+
/*Progress aProgress = progress->load();
111101
aProgress._went_back_to_start++;
112-
progress->store(aProgress);
102+
progress->store(aProgress);*/
113103
break;
114104
}
115105
}
116-
if(has_printed){
117-
cout << endl;
118-
}
119-
//cout << endl;
120106
}
121107

122-
Progress aProgress = progress->load();
123-
aProgress._percent = 100;
108+
Progress* aProgress = progress->load();
109+
aProgress->_percent = 100;
124110
progress->store(aProgress);
111+
125112
}
126113

127-
void monitor_progress(std::vector<std::atomic<Progress>*>* progress, int size){
114+
void monitor_progress(std::vector<std::atomic<Progress*>*>* progress, int size){
128115
bool keep = true;
129116
float barWidth = 15.0;
130117
while (keep){
131-
chrono::milliseconds dura( 200 );
118+
chrono::milliseconds dura( 2000 );
132119
this_thread::sleep_for(dura); // Sleeps for a bit
133120
keep = false;
134121
cout << "\r";
122+
123+
/*int total_success = 0;
135124
for (int i = 0; i < progress->size(); i++) {
136-
int l_progress = (*progress)[i]->load()._percent;
137-
int success = (*progress)[i]->load()._went_back_to_start;
125+
total_success+=(*progress)[i]->load()->_went_back_to_start;
126+
}
127+
128+
cout << "[F=" << std::fixed << setprecision(1) << std::setw(4) << (100.0*(size-total_success))/size << "%]" ;*/
129+
130+
131+
132+
for (int i = 0; i < progress->size(); i++) {
133+
int l_progress = (*progress)[i]->load()->_percent;
138134
cout << " T#" << i << ":[";
139135
int pos = l_progress * (barWidth / 100);
140136
for (int w = 0; w < barWidth; ++w) {
141137
if (w < pos) std::cout << "=";
142138
else if (w == pos) std::cout << ">";
143139
else std::cout << " ";
144140
}
145-
std::cout << "]" << std::setfill(' ') << std::setw(3) << l_progress << "% " ;
146-
std::cout << "@" << std::fixed << setprecision(2) << success << "=" << (100.0*success)/size << "%" ;
141+
if (l_progress<100){
142+
std::cout << "|" << std::setfill(' ') << std::setw(2) << l_progress << "%" << "]" ;
143+
}else{
144+
std::cout << "|END" << "]" ;
145+
}
146+
147147
if(l_progress != 100){
148148
keep = true;
149149
}
@@ -154,12 +154,12 @@ void monitor_progress(std::vector<std::atomic<Progress>*>* progress, int size){
154154

155155
int main()
156156
{
157-
//int MAX_TRY_BEFORE_GIVEUP = std::numeric_limits<int>::max();
158-
unsigned int MAX_TRY_BEFORE_GIVEUP = std::numeric_limits<unsigned int>::max()/10;
157+
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
158+
unsigned int MAX_TRY_BEFORE_GIVEUP = std::numeric_limits<unsigned int>::max();
159159
int DIMENSION = 2;
160-
int NUMBER_OF_RDM_WALKS = 1000;
160+
int NUMBER_OF_RDM_WALKS = 100;
161161
int MODULO = 0;
162-
int THREAD_NUMBER = 7;
162+
int THREAD_NUMBER = 4;
163163
cout << "MAX_TRY_BEFORE_GIVEUP=" << MAX_TRY_BEFORE_GIVEUP << endl;
164164
cout << "DIMENSION=" << DIMENSION << endl;
165165
cout << "NUMBER_OF_RDM_WALKS=" << NUMBER_OF_RDM_WALKS << endl;
@@ -169,20 +169,21 @@ int main()
169169
cout << "REAL_NUMBER_OF_RDM_WALKS=" << REAL_NUMBER_OF_RDM_WALKS << endl;
170170
cout << "MODULO=" << MODULO << endl;
171171

172+
cout << "C++:" <<__cplusplus << endl;
172173

173174
std::vector<std::thread> threads(THREAD_NUMBER);
174175
std::vector<int> went_back_count(THREAD_NUMBER);
175176

176-
std::vector<std::atomic<Progress>*> cProgress;
177+
std::vector<std::atomic<Progress*>*> cProgress;
177178

178179
// spawn n threads:
179180
for (int i = 0; i < THREAD_NUMBER; i++) {
180181
went_back_count[i] = 0;
181-
cProgress.emplace_back(new std::atomic<Progress>());
182+
cProgress.emplace_back(new std::atomic<Progress*>(new Progress()));
182183
threads[i] = std::thread(do_random_walk, i, NUMBER_OF_RDM_WALKS/THREAD_NUMBER, DIMENSION, MAX_TRY_BEFORE_GIVEUP, MODULO, std::ref(went_back_count[i]), cProgress[i] );
183184
}
184185

185-
monitor_progress(&cProgress, NUMBER_OF_RDM_WALKS/THREAD_NUMBER);
186+
monitor_progress(&cProgress, REAL_NUMBER_OF_RDM_WALKS);
186187

187188
for (int i = 0; i < THREAD_NUMBER; i++) {
188189
threads[i].join();
@@ -196,5 +197,8 @@ int main()
196197
std::cout << endl << "went_back_to_start_count " << went_back_to_start_count << endl;
197198
std::cout << "failure_count " << failure_count << "="<< 100.0* failure_count / REAL_NUMBER_OF_RDM_WALKS << "%" << endl;
198199

200+
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
201+
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::seconds> (end - begin).count() << "[sec]" << std::endl;
202+
199203
return 0;
200204
}

random_walk_vector.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ void print_global_vector(const vector<Points>& aGlobal_vector ){
8383
int main()
8484
{
8585

86-
typedef std::chrono::high_resolution_clock myclock;
87-
myclock::time_point beginning = myclock::now();
86+
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
87+
8888

8989
//unsigned int MAX_TRY_BEFORE_GIVEUP = std::numeric_limits<unsigned int>::max();
9090
unsigned int MAX_TRY_BEFORE_GIVEUP = sqrt(std::numeric_limits<unsigned int>::max());
@@ -187,5 +187,8 @@ int main()
187187
std::cout << "failure_count " << failure_count << "="<< 100.0* failure_count / NUMBER_OF_RDM_WALKS << "%" << endl;
188188
std::cout << "max_steps_before_going_back " << max_steps_before_going_back << endl;
189189

190+
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
191+
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::seconds> (end - begin).count() << "[sec]" << std::endl;
192+
190193
return 0;
191194
}

0 commit comments

Comments
 (0)