@@ -31,7 +31,7 @@ class Progress {
31
31
32
32
void do_random_walk (const int thread_id, const int size, const int dimension,
33
33
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)
35
35
{
36
36
std::random_device rd; // obtain a random number from hardware
37
37
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,
55
55
int percent = (100 * i) / size;
56
56
if (percent >= nextPrint)
57
57
{
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;
61
61
progress->store (aProgress);
62
+ progress->notify_all ();
62
63
nextPrint += step;
63
64
}
64
65
@@ -67,19 +68,8 @@ void do_random_walk(const int thread_id, const int size, const int dimension,
67
68
current_point[j]=0 ;
68
69
}
69
70
70
- // to print each walk progress
71
- double walk_step = 1.0 ;
72
- double walk_nextPrint = walk_step;
73
- bool has_printed = false ;
74
71
// random walk
75
72
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
- }
83
73
84
74
// go +1 or -1 ?
85
75
int aStep = 0 ;
@@ -107,43 +97,53 @@ void do_random_walk(const int thread_id, const int size, const int dimension,
107
97
108
98
if (went_back_to_start) {
109
99
oWent_back_to_start++;
110
- Progress aProgress = progress->load ();
100
+ /* Progress aProgress = progress->load();
111
101
aProgress._went_back_to_start++;
112
- progress->store (aProgress);
102
+ progress->store(aProgress);*/
113
103
break ;
114
104
}
115
105
}
116
- if (has_printed){
117
- cout << endl;
118
- }
119
- // cout << endl;
120
106
}
121
107
122
- Progress aProgress = progress->load ();
123
- aProgress. _percent = 100 ;
108
+ Progress* aProgress = progress->load ();
109
+ aProgress-> _percent = 100 ;
124
110
progress->store (aProgress);
111
+
125
112
}
126
113
127
- void monitor_progress (std::vector<std::atomic<Progress>*>* progress, int size){
114
+ void monitor_progress (std::vector<std::atomic<Progress* >*>* progress, int size){
128
115
bool keep = true ;
129
116
float barWidth = 15.0 ;
130
117
while (keep){
131
- chrono::milliseconds dura ( 200 );
118
+ chrono::milliseconds dura ( 2000 );
132
119
this_thread::sleep_for (dura); // Sleeps for a bit
133
120
keep = false ;
134
121
cout << " \r " ;
122
+
123
+ /* int total_success = 0;
135
124
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 ;
138
134
cout << " T#" << i << " :[" ;
139
135
int pos = l_progress * (barWidth / 100 );
140
136
for (int w = 0 ; w < barWidth; ++w) {
141
137
if (w < pos) std::cout << " =" ;
142
138
else if (w == pos) std::cout << " >" ;
143
139
else std::cout << " " ;
144
140
}
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
+
147
147
if (l_progress != 100 ){
148
148
keep = true ;
149
149
}
@@ -154,12 +154,12 @@ void monitor_progress(std::vector<std::atomic<Progress>*>* progress, int size){
154
154
155
155
int main ()
156
156
{
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 ();
159
159
int DIMENSION = 2 ;
160
- int NUMBER_OF_RDM_WALKS = 1000 ;
160
+ int NUMBER_OF_RDM_WALKS = 100 ;
161
161
int MODULO = 0 ;
162
- int THREAD_NUMBER = 7 ;
162
+ int THREAD_NUMBER = 4 ;
163
163
cout << " MAX_TRY_BEFORE_GIVEUP=" << MAX_TRY_BEFORE_GIVEUP << endl;
164
164
cout << " DIMENSION=" << DIMENSION << endl;
165
165
cout << " NUMBER_OF_RDM_WALKS=" << NUMBER_OF_RDM_WALKS << endl;
@@ -169,20 +169,21 @@ int main()
169
169
cout << " REAL_NUMBER_OF_RDM_WALKS=" << REAL_NUMBER_OF_RDM_WALKS << endl;
170
170
cout << " MODULO=" << MODULO << endl;
171
171
172
+ cout << " C++:" <<__cplusplus << endl;
172
173
173
174
std::vector<std::thread> threads (THREAD_NUMBER);
174
175
std::vector<int > went_back_count (THREAD_NUMBER);
175
176
176
- std::vector<std::atomic<Progress>*> cProgress;
177
+ std::vector<std::atomic<Progress* >*> cProgress;
177
178
178
179
// spawn n threads:
179
180
for (int i = 0 ; i < THREAD_NUMBER; i++) {
180
181
went_back_count[i] = 0 ;
181
- cProgress.emplace_back (new std::atomic<Progress>( ));
182
+ cProgress.emplace_back (new std::atomic<Progress*>( new Progress () ));
182
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] );
183
184
}
184
185
185
- monitor_progress (&cProgress, NUMBER_OF_RDM_WALKS/THREAD_NUMBER );
186
+ monitor_progress (&cProgress, REAL_NUMBER_OF_RDM_WALKS );
186
187
187
188
for (int i = 0 ; i < THREAD_NUMBER; i++) {
188
189
threads[i].join ();
@@ -196,5 +197,8 @@ int main()
196
197
std::cout << endl << " went_back_to_start_count " << went_back_to_start_count << endl;
197
198
std::cout << " failure_count " << failure_count << " =" << 100.0 * failure_count / REAL_NUMBER_OF_RDM_WALKS << " %" << endl;
198
199
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
+
199
203
return 0 ;
200
204
}
0 commit comments