Skip to content

Commit affc00e

Browse files
committed
Update random walk
1 parent 5fdd66c commit affc00e

4 files changed

+135
-32
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"forward_list": "cpp",
6464
"optional": "cpp",
6565
"sstream": "cpp",
66-
"numeric": "cpp"
66+
"numeric": "cpp",
67+
"deque": "cpp"
6768
}
6869
}

atomic_progress.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
#include <random>
5+
#include <limits>
6+
#include <thread>
7+
#include <numeric>
8+
#include <atomic>
9+
#include <iomanip>
10+
11+
12+
void do_work(int size, std::atomic<int>& progress){
13+
for(int i=0; i<=size;++i){
14+
std::chrono::milliseconds dura( 350 );
15+
std::this_thread::sleep_for(dura); // Sleeps for a bit
16+
progress = (100*i)/size;
17+
}
18+
progress = 100;
19+
}
20+
21+
22+
void monitor_progress(std::atomic<int>& progress0,
23+
std::atomic<int>& progress1,
24+
std::atomic<int>& progress2,
25+
std::atomic<int>& progress3)
26+
{
27+
bool isOver=false;
28+
while(not isOver){
29+
30+
isOver = (progress0+progress1+progress2+progress3== 400) ;
31+
std::cout << " T#0:" << progress0
32+
<< " T#1:" << progress1
33+
<< " T#2:" << progress2
34+
<< " T#3:" << progress3 << std::endl;
35+
36+
std::chrono::milliseconds dura( 200 );
37+
std::this_thread::sleep_for(dura); // Sleeps for a bit
38+
}
39+
}
40+
41+
int main()
42+
{
43+
44+
std::atomic<int> progress0, progress1, progress2, progress3;
45+
46+
std::thread thread0 = std::thread(do_work, 20, std::ref(progress0));
47+
std::thread thread1 = std::thread(do_work, 30, std::ref(progress1));
48+
std::thread thread2 = std::thread(do_work, 40, std::ref(progress2));
49+
std::thread thread3 = std::thread(do_work, 50, std::ref(progress3));
50+
51+
monitor_progress(progress0, progress1, progress2, progress3);
52+
thread0.join();
53+
thread1.join();
54+
thread2.join();
55+
thread3.join();
56+
57+
return 0;
58+
}

atomic_progress_vector.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
#include <thread>
3+
#include <atomic>
4+
#include <vector>
5+
#include <deque>
6+
7+
void do_work(int size, std::atomic<int> &progress)
8+
{
9+
for (int i = 0; i <= size; ++i)
10+
{
11+
std::chrono::milliseconds dura(350);
12+
std::this_thread::sleep_for(dura); // Sleeps for a bit
13+
progress = (100 * i) / size;
14+
}
15+
}
16+
17+
void monitor_progress(std::deque<std::atomic<int>> &v_progress)
18+
{
19+
bool isOver = false;
20+
while (not isOver)
21+
{
22+
isOver = true;
23+
for (int i = 0; i < v_progress.size(); ++i)
24+
{
25+
std::cout << "T#" << i << ":" << v_progress[i] << "% ";
26+
if (v_progress[i] != 100)
27+
{
28+
isOver = false;
29+
}
30+
}
31+
std::cout << std::endl;
32+
33+
std::chrono::milliseconds dura(200);
34+
std::this_thread::sleep_for(dura); // Sleeps for a bit
35+
}
36+
}
37+
38+
int main()
39+
{
40+
const int THREAD_NUMBER = 4;
41+
std::vector<std::thread> threads(THREAD_NUMBER);
42+
std::deque<std::atomic<int>> v_progress;
43+
// Make and launch THREAD_NUMBER threads
44+
for (int i = 0; i < THREAD_NUMBER; ++i)
45+
{
46+
v_progress.emplace_back(0);
47+
threads[i] = std::thread(do_work, 10 * (i + 1), std::ref(v_progress[i]));
48+
}
49+
// Monitor the threads
50+
monitor_progress(std::ref(v_progress));
51+
52+
for (int i = 0; i < THREAD_NUMBER; ++i)
53+
{
54+
threads[i].join();
55+
}
56+
return 0;
57+
}

random_walk_thread.cpp

+18-31
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <numeric>
99
#include <atomic>
1010
#include <iomanip>
11+
#include <deque>
1112

1213
using namespace std;
1314

@@ -31,7 +32,7 @@ class Progress {
3132

3233
void do_random_walk(const int thread_id, const int size, const int dimension,
3334
const unsigned int max_try_before_giveup, const int MODULO, int& oWent_back_to_start,
34-
std::atomic<Progress*>* progress)
35+
std::atomic<int>& progress)
3536
{
3637
std::random_device rd; // obtain a random number from hardware
3738
std::mt19937 gen(rd()); // seed the generator
@@ -56,10 +57,8 @@ void do_random_walk(const int thread_id, const int size, const int dimension,
5657
if (percent >= nextPrint)
5758
{
5859
//std::cout << "Thread progress(" << thread_id << "): " << std::string(percent/5, '|') << percent << "%" << std::endl;
59-
Progress* aProgress = progress->load();
60-
aProgress->_percent=percent;
61-
progress->store(aProgress);
62-
progress->notify_all();
60+
progress = percent;
61+
progress.notify_one();
6362
nextPrint += step;
6463
}
6564

@@ -97,40 +96,26 @@ void do_random_walk(const int thread_id, const int size, const int dimension,
9796

9897
if (went_back_to_start) {
9998
oWent_back_to_start++;
100-
/*Progress aProgress = progress->load();
101-
aProgress._went_back_to_start++;
102-
progress->store(aProgress);*/
10399
break;
104100
}
105101
}
106102
}
107103

108-
Progress* aProgress = progress->load();
109-
aProgress->_percent = 100;
110-
progress->store(aProgress);
104+
progress=100;
111105

112106
}
113107

114-
void monitor_progress(std::vector<std::atomic<Progress*>*>* progress, int size){
108+
void monitor_progress(std::deque<std::atomic<int>>& progress, int size){
115109
bool keep = true;
116110
float barWidth = 15.0;
117111
while (keep){
118-
chrono::milliseconds dura( 2000 );
112+
chrono::milliseconds dura( 100 );
119113
this_thread::sleep_for(dura); // Sleeps for a bit
120114
keep = false;
121-
cout << "\r";
115+
cout << "\r";
122116

123-
/*int total_success = 0;
124-
for (int i = 0; i < progress->size(); i++) {
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;
117+
for (int i = 0; i < progress.size(); i++) {
118+
int l_progress = progress[i];
134119
cout << " T#" << i << ":[";
135120
int pos = l_progress * (barWidth / 100);
136121
for (int w = 0; w < barWidth; ++w) {
@@ -155,11 +140,11 @@ void monitor_progress(std::vector<std::atomic<Progress*>*>* progress, int size){
155140
int main()
156141
{
157142
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();
143+
unsigned int MAX_TRY_BEFORE_GIVEUP = std::numeric_limits<unsigned int>::max() ;
159144
int DIMENSION = 2;
160145
int NUMBER_OF_RDM_WALKS = 100;
161146
int MODULO = 0;
162-
int THREAD_NUMBER = 4;
147+
int THREAD_NUMBER = 10;
163148
cout << "MAX_TRY_BEFORE_GIVEUP=" << MAX_TRY_BEFORE_GIVEUP << endl;
164149
cout << "DIMENSION=" << DIMENSION << endl;
165150
cout << "NUMBER_OF_RDM_WALKS=" << NUMBER_OF_RDM_WALKS << endl;
@@ -174,16 +159,18 @@ int main()
174159
std::vector<std::thread> threads(THREAD_NUMBER);
175160
std::vector<int> went_back_count(THREAD_NUMBER);
176161

177-
std::vector<std::atomic<Progress*>*> cProgress;
162+
std::deque<std::atomic<int>> cProgress;
178163

179164
// spawn n threads:
180165
for (int i = 0; i < THREAD_NUMBER; i++) {
181166
went_back_count[i] = 0;
182-
cProgress.emplace_back(new std::atomic<Progress*>(new Progress()));
183-
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] );
167+
cProgress.emplace_back(0);
168+
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]), std::ref(cProgress[i]) );
184169
}
185170

186-
monitor_progress(&cProgress, REAL_NUMBER_OF_RDM_WALKS);
171+
172+
173+
monitor_progress(std::ref(cProgress), REAL_NUMBER_OF_RDM_WALKS);
187174

188175
for (int i = 0; i < THREAD_NUMBER; i++) {
189176
threads[i].join();

0 commit comments

Comments
 (0)