Browse Source

Removal of boost progress bar, replaced with simple RAII class

master
Trevor Irons 5 years ago
parent
commit
f4fb29b94b

+ 0
- 4
CMakeLists.txt View File

244
 			add_definitions(-DHAVE_BOOST_SPECIAL_FUNCTIONS)
244
 			add_definitions(-DHAVE_BOOST_SPECIAL_FUNCTIONS)
245
 		endif()
245
 		endif()
246
 	endif()
246
 	endif()
247
-	find_path( HAVE_BOOST_PROGRESS "boost/progress.hpp" ) 
248
-		if(HAVE_BOOST_PROGRESS)
249
-		add_definitions(-DHAVE_BOOST_PROGRESS)
250
-	endif()
251
 endif()
247
 endif()
252
 
248
 
253
 if (LEMMA_PYTHON3_BINDINGS)
249
 if (LEMMA_PYTHON3_BINDINGS)

+ 1
- 3
Modules/FDEM1D/include/EMEarth1D.h View File

33
 #include "QWEKey.h"
33
 #include "QWEKey.h"
34
 #include "CubicSplineInterpolator.h"
34
 #include "CubicSplineInterpolator.h"
35
 
35
 
36
-#ifdef HAVE_BOOST_PROGRESS
37
-#include "boost/progress.hpp"
38
-#endif
36
+#include "ProgressBar.h"
39
 
37
 
40
 namespace Lemma {
38
 namespace Lemma {
41
 
39
 

+ 14
- 26
Modules/FDEM1D/src/EMEarth1D.cpp View File

73
     EMEarth1D::EMEarth1D( const ctor_key& key ) : LemmaObject( key ),
73
     EMEarth1D::EMEarth1D( const ctor_key& key ) : LemmaObject( key ),
74
             Dipole(nullptr), Earth(nullptr), Receivers(nullptr), Antenna(nullptr),
74
             Dipole(nullptr), Earth(nullptr), Receivers(nullptr), Antenna(nullptr),
75
             FieldsToCalculate(BOTH), HankelType(ANDERSON801), icalcinner(0), icalc(0)
75
             FieldsToCalculate(BOTH), HankelType(ANDERSON801), icalcinner(0), icalc(0)
76
-        //#ifdef HAVE_BOOST_PROGRESS
77
-        //    , disp(0)
78
-        //#endif
79
         {
76
         {
80
     }
77
     }
81
 
78
 
191
 
188
 
192
     void EMEarth1D::CalculateWireAntennaFields(bool progressbar) {
189
     void EMEarth1D::CalculateWireAntennaFields(bool progressbar) {
193
 
190
 
194
-        #ifdef HAVE_BOOST_PROGRESS
195
-        boost::progress_display *disp;
196
-        #endif
191
+        ProgressBar *mdisp;
197
 
192
 
198
         if (Earth == nullptr) {
193
         if (Earth == nullptr) {
199
             throw NullEarth();
194
             throw NullEarth();
234
             if ( Antenna->IsHorizontallyPlanar() && ( HankelType == ANDERSON801 || HankelType == FHTKEY201  || HankelType==FHTKEY101 ||
229
             if ( Antenna->IsHorizontallyPlanar() && ( HankelType == ANDERSON801 || HankelType == FHTKEY201  || HankelType==FHTKEY101 ||
235
                                                       HankelType == FHTKEY51    || HankelType == FHTKONG61  || HankelType == FHTKONG121 ||
230
                                                       HankelType == FHTKEY51    || HankelType == FHTKONG61  || HankelType == FHTKONG121 ||
236
                                                       HankelType == FHTKONG241  || HankelType == IRONS )) {
231
                                                       HankelType == FHTKONG241  || HankelType == IRONS )) {
237
-                #ifdef HAVE_BOOST_PROGRESS
238
                 if (progressbar) {
232
                 if (progressbar) {
239
-                    disp = new boost::progress_display( Receivers->GetNumberOfPoints()*Antenna->GetNumberOfFrequencies() );
233
+                    mdisp = new ProgressBar( Receivers->GetNumberOfPoints()*Antenna->GetNumberOfFrequencies(), "Progress");
240
                 }
234
                 }
241
-                #endif
235
+
242
                 for (int ifreq=0; ifreq<Antenna->GetNumberOfFrequencies();++ifreq) {
236
                 for (int ifreq=0; ifreq<Antenna->GetNumberOfFrequencies();++ifreq) {
243
                     Real wavef = 2.*PI* Antenna->GetFrequency(ifreq);
237
                     Real wavef = 2.*PI* Antenna->GetFrequency(ifreq);
244
                     #ifdef LEMMAUSEOMP
238
                     #ifdef LEMMAUSEOMP
252
                     #endif
246
                     #endif
253
                     for (int irec=0; irec<Receivers->GetNumberOfPoints(); ++irec) {
247
                     for (int irec=0; irec<Receivers->GetNumberOfPoints(); ++irec) {
254
                         SolveLaggedTxRxPair(irec, Hankel.get(), wavef, ifreq, AntCopy.get());
248
                         SolveLaggedTxRxPair(irec, Hankel.get(), wavef, ifreq, AntCopy.get());
255
-                        #ifdef HAVE_BOOST_PROGRESS
256
-                        if (progressbar) ++(*disp);
257
-                        #endif
249
+                        if (progressbar) {
250
+                            ++(*mdisp);
251
+                        }
258
                     }
252
                     }
259
                     #pragma omp barrier
253
                     #pragma omp barrier
260
                     #ifdef LEMMAUSEOMP
254
                     #ifdef LEMMAUSEOMP
265
 
259
 
266
                 //std::cout << "freq parallel #1" << std::endl;
260
                 //std::cout << "freq parallel #1" << std::endl;
267
                 //** Progress display bar for long calculations */
261
                 //** Progress display bar for long calculations */
268
-                #ifdef HAVE_BOOST_PROGRESS
269
                 if (progressbar) {
262
                 if (progressbar) {
270
-                    disp = new boost::progress_display( Receivers->GetNumberOfPoints()*Antenna->GetNumberOfFrequencies() );
263
+                    mdisp = new ProgressBar( Receivers->GetNumberOfPoints()*Antenna->GetNumberOfFrequencies() );
271
                 }
264
                 }
272
-                #endif
265
+
273
                 // parallelise across receivers
266
                 // parallelise across receivers
274
                 #ifdef LEMMAUSEOMP
267
                 #ifdef LEMMAUSEOMP
275
                 #pragma omp parallel
268
                 #pragma omp parallel
336
                         //std::cout << "Normal Path\n";
329
                         //std::cout << "Normal Path\n";
337
                         //std::cout << Receivers->GetHfield(0, irec) << std::endl;
330
                         //std::cout << Receivers->GetHfield(0, irec) << std::endl;
338
                         //if (irec == 1) exit(0);
331
                         //if (irec == 1) exit(0);
339
-                        #ifdef HAVE_BOOST_PROGRESS
340
-                        if (progressbar) ++(*disp);
341
-                        #endif
332
+                        if (progressbar) {
333
+                            ++(*mdisp);
334
+                        }
342
                     } // receiver loop
335
                     } // receiver loop
343
                 } // OMP_PARALLEL BLOCK
336
                 } // OMP_PARALLEL BLOCK
344
             } else if (Antenna->GetNumberOfFrequencies() > 8) {
337
             } else if (Antenna->GetNumberOfFrequencies() > 8) {
390
                             } // frequency loop
383
                             } // frequency loop
391
                         } // OMP_PARALLEL BLOCK
384
                         } // OMP_PARALLEL BLOCK
392
                     } // mask loop
385
                     } // mask loop
393
-                    #ifdef HAVE_BOOST_PROGRESS
394
                     //if (Receivers->GetNumberOfPoints() > 100) {
386
                     //if (Receivers->GetNumberOfPoints() > 100) {
395
-                    //    ++ disp;
387
+                    //    ++ mdisp;
396
                     //}
388
                     //}
397
-                    #endif
398
                 } // receiver loop
389
                 } // receiver loop
399
                 //std::cout << "End freq parallel " << std::endl;
390
                 //std::cout << "End freq parallel " << std::endl;
400
             } // Frequency Parallel
391
             } // Frequency Parallel
457
                             } // frequency loop
448
                             } // frequency loop
458
                         } // OMP_PARALLEL BLOCK
449
                         } // OMP_PARALLEL BLOCK
459
                     } // mask loop
450
                     } // mask loop
460
-                    #ifdef HAVE_BOOST_PROGRESS
461
                     //if (Receivers->GetNumberOfPoints() > 100) {
451
                     //if (Receivers->GetNumberOfPoints() > 100) {
462
                     //    ++ disp;
452
                     //    ++ disp;
463
                     //}
453
                     //}
464
-                    #endif
465
                 } // receiver loop
454
                 } // receiver loop
466
            } // Polygonal parallel logic
455
            } // Polygonal parallel logic
467
         } else {
456
         } else {
479
             this->Dipole = nullptr;
468
             this->Dipole = nullptr;
480
         }
469
         }
481
 
470
 
482
-        #ifdef HAVE_BOOST_PROGRESS
483
         if (progressbar) {
471
         if (progressbar) {
484
-            delete disp;
472
+            delete mdisp;
485
         }
473
         }
486
-        #endif
474
+
487
     }
475
     }
488
 
476
 
489
     #ifdef KIHALEE_EM1D
477
     #ifdef KIHALEE_EM1D

+ 67
- 0
Modules/LemmaCore/include/ProgressBar.h View File

1
+#ifndef PROGRESS_BAR_H
2
+#define PROGRESS_BAR_H
3
+
4
+#include <string>
5
+
6
+/**
7
+ * RAII implementation of a progress bar.
8
+ * available from https://stackoverflow.com/questions/46717885/make-boostprogress-display-work-when-more-information-is-being-printed-to-the
9
+ */
10
+class ProgressBar
11
+{
12
+public:
13
+    /**
14
+     * Constructor.
15
+     * It takes two values: the expected number of iterations whose progress we
16
+     * want to monitor and an initial message to be displayed on top of the bar
17
+     * (which can be updated with updateLastPrintedMessage()).
18
+     */
19
+    ProgressBar(
20
+            uint32_t expectedIterations, const std::string& initialMessage="");
21
+
22
+    /**
23
+     * Destructor to guarantee RAII.
24
+     */
25
+    ~ProgressBar();
26
+
27
+    // Make the object non-copyable
28
+    ProgressBar(const ProgressBar& o) = delete;
29
+    ProgressBar& operator=(const ProgressBar& o) = delete;
30
+
31
+    /**
32
+     * Must be invoked when the progress bar is no longer needed to restore the
33
+     * position of the cursor to the end of the output.
34
+     * It is automatically invoked when the object is destroyed.
35
+     */
36
+    void endProgressBar();
37
+
38
+    /**
39
+     * Prints a new message under the last printed message, without overwriting
40
+     * it. This moves the progress bar down to be placed under the newly
41
+     * written message.
42
+     */
43
+    void printNewMessage(const std::string& message);
44
+
45
+    /**
46
+     * Prints a message while the progress bar is on the screen on top on the
47
+     * last printed message. Since the cursor is right at the beginning of the
48
+     * progress bar, it moves the cursor up by one line before printing, and
49
+     * then returns it to its original position.
50
+     */
51
+    void updateLastPrintedMessage(const std::string& message);
52
+
53
+    /**
54
+     * Overloaded prefix operator, used to indicate that the has been a new
55
+     * iteration.
56
+     */
57
+    void operator++();
58
+
59
+private:
60
+    unsigned int mTotalIterations;
61
+    unsigned int mNumberOfTicks;
62
+    bool mEnded;
63
+    size_t mLengthOfLastPrintedMessage;
64
+};
65
+
66
+#endif /* PROGRESS_BAR_H */
67
+

+ 1
- 0
Modules/LemmaCore/src/CMakeLists.txt View File

1
 set (SOURCE 
1
 set (SOURCE 
2
 	${SOURCE}
2
 	${SOURCE}
3
+	${CMAKE_CURRENT_SOURCE_DIR}/ProgressBar.cpp                # external to Lemma
3
 	${CMAKE_CURRENT_SOURCE_DIR}/helper.cpp
4
 	${CMAKE_CURRENT_SOURCE_DIR}/helper.cpp
4
 	${CMAKE_CURRENT_SOURCE_DIR}/LemmaObject.cpp
5
 	${CMAKE_CURRENT_SOURCE_DIR}/LemmaObject.cpp
5
 	${CMAKE_CURRENT_SOURCE_DIR}/ASCIIParser.cpp                # key 
6
 	${CMAKE_CURRENT_SOURCE_DIR}/ASCIIParser.cpp                # key 

+ 103
- 0
Modules/LemmaCore/src/ProgressBar.cpp View File

1
+#include "ProgressBar.h"
2
+#include <iostream>
3
+#include <iomanip>
4
+#include <sstream>
5
+
6
+#define LENGTH_OF_PROGRESS_BAR 55
7
+#define PERCENTAGE_BIN_SIZE (100.0/LENGTH_OF_PROGRESS_BAR)
8
+
9
+namespace
10
+{
11
+    std::string generateProgressBar(unsigned int percentage)
12
+    {
13
+        const int progress = static_cast<int>(percentage/PERCENTAGE_BIN_SIZE);
14
+        std::ostringstream ss;
15
+        ss << " " << std::setw(3) << std::right << percentage << "% ";
16
+        std::string bar("[" + std::string(LENGTH_OF_PROGRESS_BAR-2, ' ') + "]");
17
+
18
+        unsigned int numberOfSymbols = std::min(
19
+                std::max(0, progress - 1),
20
+                LENGTH_OF_PROGRESS_BAR - 2);
21
+
22
+        bar.replace(1, numberOfSymbols, std::string(numberOfSymbols, '|'));
23
+
24
+        ss << bar;
25
+        return ss.str();
26
+    }
27
+}
28
+
29
+ProgressBar::ProgressBar(
30
+            uint32_t expectedIterations, const std::string& initialMessage)
31
+    : mTotalIterations(expectedIterations),
32
+      mNumberOfTicks(0),
33
+      mEnded(false)
34
+{
35
+    std::cout << initialMessage << "\n";
36
+    mLengthOfLastPrintedMessage = initialMessage.size();
37
+    std::cout << generateProgressBar(0) << "\r" << std::flush;
38
+}
39
+
40
+ProgressBar::~ProgressBar()
41
+{
42
+    endProgressBar();
43
+}
44
+
45
+void ProgressBar::operator++()
46
+{
47
+    if (mEnded)
48
+    {
49
+        throw std::runtime_error(
50
+                "Attempted to use progress bar after having terminated it");
51
+    }
52
+
53
+    mNumberOfTicks = std::min(mTotalIterations, mNumberOfTicks+1);
54
+    const unsigned int percentage = static_cast<unsigned int>(
55
+            mNumberOfTicks*100.0/mTotalIterations);
56
+
57
+    std::cout << generateProgressBar(percentage) << "\r" << std::flush;
58
+}
59
+
60
+void ProgressBar::printNewMessage(const std::string& message)
61
+{
62
+    if (mEnded)
63
+    {
64
+        throw std::runtime_error(
65
+                "Attempted to use progress bar after having terminated it");
66
+    }
67
+
68
+    std::cout << "\r"
69
+        << std::left
70
+        << std::setw(LENGTH_OF_PROGRESS_BAR + 6)
71
+        << message << "\n";
72
+    mLengthOfLastPrintedMessage = message.size();
73
+    const unsigned int percentage = static_cast<unsigned int>(
74
+            mNumberOfTicks*100.0/mTotalIterations);
75
+
76
+    std::cout << generateProgressBar(percentage) << "\r" << std::flush;
77
+
78
+}
79
+
80
+void ProgressBar::updateLastPrintedMessage(const std::string& message)
81
+{
82
+    if (mEnded)
83
+    {
84
+        throw std::runtime_error(
85
+                "Attempted to use progress bar after having terminated it");
86
+    }
87
+
88
+    std::cout << "\r\033[F"
89
+        << std::left
90
+        << std::setw(mLengthOfLastPrintedMessage)
91
+        << message << "\n";
92
+    mLengthOfLastPrintedMessage = message.size();
93
+}
94
+
95
+void ProgressBar::endProgressBar()
96
+{
97
+    if (!mEnded)
98
+    {
99
+        std::cout << std::string(2, '\n');
100
+    }
101
+    mEnded = true;
102
+}
103
+

Loading…
Cancel
Save