Skip to content

Commit

Permalink
[ProgressBar] Remove old SG_PROGRESS code.
Browse files Browse the repository at this point in the history
Refactor also one bare io->progress usage.
  • Loading branch information
geektoni committed Jun 6, 2017
1 parent 0566192 commit c106236
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 269 deletions.
22 changes: 19 additions & 3 deletions src/shogun/features/hashed/HashedWDFeaturesTransposed.cpp
Expand Up @@ -15,6 +15,8 @@

#ifdef HAVE_PTHREAD
#include <pthread.h>
#include <shogun/base/progress.h>

#endif

using namespace shogun;
Expand All @@ -31,6 +33,7 @@ struct HASHEDWD_THREAD_PARAM
float64_t* vec;
float64_t bias;
bool progress;
PRange<int32_t>* progress_bar;
uint32_t* index;
};
#endif // DOXYGEN_SHOULD_SKIP_THIS
Expand Down Expand Up @@ -227,6 +230,7 @@ void CHashedWDFeaturesTransposed::dense_dot_range(float64_t* output, int32_t sta
if (num_threads < 2)
{
HASHEDWD_THREAD_PARAM params;
auto pb = progress(range(start, stop), *this->io);
params.hf=this;
params.sub_index=NULL;
params.output=output;
Expand All @@ -236,14 +240,17 @@ void CHashedWDFeaturesTransposed::dense_dot_range(float64_t* output, int32_t sta
params.vec=vec;
params.bias=b;
params.progress=false; //true;
params.progress_bar=&pb
params.index=index;
dense_dot_range_helper((void*) &params);
pb.complete();
}
#ifdef HAVE_PTHREAD
else
{
pthread_t* threads = SG_MALLOC(pthread_t, num_threads-1);
HASHEDWD_THREAD_PARAM* params = SG_MALLOC(HASHEDWD_THREAD_PARAM, num_threads);
auto pb = progress(range(start, stop), *this->io);
int32_t step= num_vectors/num_threads;

int32_t t;
Expand All @@ -259,6 +266,7 @@ void CHashedWDFeaturesTransposed::dense_dot_range(float64_t* output, int32_t sta
params[t].vec=vec;
params[t].bias=b;
params[t].progress = false;
params[t].progress_bar = &pb;
params[t].index=index;
pthread_create(&threads[t], NULL,
CHashedWDFeaturesTransposed::dense_dot_range_helper, (void*)&params[t]);
Expand All @@ -273,6 +281,7 @@ void CHashedWDFeaturesTransposed::dense_dot_range(float64_t* output, int32_t sta
params[t].vec=vec;
params[t].bias=b;
params[t].progress = false; //true;
params[t].progress_bar = &pb;
params[t].index=index;
CHashedWDFeaturesTransposed::dense_dot_range_helper((void*) &params[t]);

Expand Down Expand Up @@ -314,6 +323,7 @@ void CHashedWDFeaturesTransposed::dense_dot_range_subset(int32_t* sub_index, int
if (num_threads < 2)
{
HASHEDWD_THREAD_PARAM params;
auto pb = progress(range(num), *this->io);
params.hf=this;
params.sub_index=sub_index;
params.output=output;
Expand All @@ -323,16 +333,18 @@ void CHashedWDFeaturesTransposed::dense_dot_range_subset(int32_t* sub_index, int
params.vec=vec;
params.bias=b;
params.progress=false; //true;
params.progress_bar = &pb;
params.index=index;
dense_dot_range_helper((void*) &params);
pb.complete();
}
#ifdef HAVE_PTHREAD
else
{
pthread_t* threads = SG_MALLOC(pthread_t, num_threads-1);
HASHEDWD_THREAD_PARAM* params = SG_MALLOC(HASHEDWD_THREAD_PARAM, num_threads);
int32_t step= num/num_threads;

auto pb = progress(range(num), *this->io);
int32_t t;

for (t=0; t<num_threads-1; t++)
Expand All @@ -346,6 +358,7 @@ void CHashedWDFeaturesTransposed::dense_dot_range_subset(int32_t* sub_index, int
params[t].vec=vec;
params[t].bias=b;
params[t].progress = false;
params[t].progress_bar = &pb;
params[t].index=index;
pthread_create(&threads[t], NULL,
CHashedWDFeaturesTransposed::dense_dot_range_helper, (void*)&params[t]);
Expand All @@ -360,12 +373,14 @@ void CHashedWDFeaturesTransposed::dense_dot_range_subset(int32_t* sub_index, int
params[t].vec=vec;
params[t].bias=b;
params[t].progress = false; //true;
params[t].progress_bar = &pb;
params[t].index=index;
CHashedWDFeaturesTransposed::dense_dot_range_helper((void*) &params[t]);

for (t=0; t<num_threads-1; t++)
pthread_join(threads[t], NULL);

pb.complete();
SG_FREE(params);
SG_FREE(threads);
SG_FREE(index);
Expand All @@ -390,6 +405,7 @@ void* CHashedWDFeaturesTransposed::dense_dot_range_helper(void* p)
float64_t* vec=par->vec;
float64_t bias=par->bias;
bool progress=par->progress;
auto pb = par->progress_bar;
uint32_t* index=par->index;
int32_t string_length=hf->string_length;
int32_t degree=hf->degree;
Expand Down Expand Up @@ -440,7 +456,7 @@ void* CHashedWDFeaturesTransposed::dense_dot_range_helper(void* p)
offs+=partial_w_dim*degree;

if (progress)
hf->io->progress(i, 0,string_length, i);
pb->print_progress();
}

for (int32_t j=start; j<stop; j++)
Expand Down Expand Up @@ -491,7 +507,7 @@ void* CHashedWDFeaturesTransposed::dense_dot_range_helper(void* p)
offs+=partial_w_dim*degree;

if (progress)
hf->io->progress(i, 0,string_length, i);
pb->print_progress();
}

for (int32_t j=start; j<stop; j++)
Expand Down
129 changes: 2 additions & 127 deletions src/shogun/io/SGIO.cpp
Expand Up @@ -53,16 +53,14 @@ char SGIO::file_buffer[FBUFSIZE];
char SGIO::directory_name[FBUFSIZE];

SGIO::SGIO()
: target(stdout), last_progress_time(0), progress_start_time(0),
last_progress(0), show_progress(false), location_info(MSG_NONE),
: target(stdout), show_progress(false), location_info(MSG_NONE),
syntax_highlight(true), loglevel(MSG_WARN)
{
m_refcount = new RefCount();
}

SGIO::SGIO(const SGIO& orig)
: target(orig.get_target()), last_progress_time(0),
progress_start_time(0), last_progress(0),
: target(orig.get_target()),
show_progress(orig.get_show_progress()),
location_info(orig.get_location_info()),
syntax_highlight(orig.get_syntax_highlight()),
Expand Down Expand Up @@ -152,129 +150,6 @@ void SGIO::buffered_message(EMessageType prio, const char *fmt, ... ) const
}
}

void SGIO::progress(
float64_t current_val, float64_t min_val, float64_t max_val,
int32_t decimals, const char* prefix)
{
if (!show_progress)
return;

float64_t runtime = CTime::get_curtime();

char str[1000];
float64_t v=-1, estimate=0, total_estimate=0 ;

if (max_val-min_val>0.0)
v=100*(current_val-min_val+1)/(max_val-min_val+1);
else
return;

v=CMath::clamp(v,1e-5,100.0);

if (decimals < 1)
decimals = 1;

if (last_progress==0)
{
last_progress_time = runtime;
progress_start_time = runtime;
last_progress = v;
}
else
{
last_progress = v-1e-6;
if ((v!=100.0) && (runtime - last_progress_time<0.5))
{

// This is made to display correctly the percentage
// if the algorithm execution is too fast
if (current_val >= max_val-1)
{
v = 100;
last_progress=v-1e-6;
snprintf(str, sizeof(str), "%%s %%%d.%df%%%% %%1.1f seconds remaining %%1.1f seconds total ",decimals+3, decimals);
message(MSG_MESSAGEONLY, "", "", -1, str, prefix, v, estimate, total_estimate);
message(MSG_MESSAGEONLY, "", "", -1, "\n");
}
return;
}

last_progress_time = runtime;
estimate = (1-v/100)*(last_progress_time-progress_start_time)/(v/100);
total_estimate = (last_progress_time-progress_start_time)/(v/100);
}

if (estimate>120)
{
snprintf(str, sizeof(str), "%%s %%%d.%df%%%% %%1.1f minutes remaining %%1.1f minutes total \r",decimals+3, decimals);
message(MSG_MESSAGEONLY, "", "", -1, str, prefix, v, estimate/60, total_estimate/60);
}
else
{
snprintf(str, sizeof(str), "%%s %%%d.%df%%%% %%1.1f seconds remaining %%1.1f seconds total \r",decimals+3, decimals);
message(MSG_MESSAGEONLY, "", "", -1, str, prefix, v, estimate, total_estimate);
}

// Print a new line if the execution is completed
// to prevent bad display
if (current_val >= max_val-1)
{
message(MSG_MESSAGEONLY, "", "", -1, "\n");
}

fflush(target);
}

void SGIO::absolute_progress(
float64_t current_val, float64_t val, float64_t min_val, float64_t max_val,
int32_t decimals, const char* prefix)
{
if (!show_progress)
return;

float64_t runtime = CTime::get_curtime();

char str[1000];
float64_t v=-1, estimate=0, total_estimate=0 ;

if (max_val-min_val>0)
v=100*(val-min_val+1)/(max_val-min_val+1);

if (decimals < 1)
decimals = 1;

if (last_progress>v)
{
last_progress_time = runtime;
progress_start_time = runtime;
last_progress = v;
}
else
{
v=CMath::clamp(v,1e-5,100.0);
last_progress = v-1e-6;

if ((v!=100.0) && (runtime - last_progress_time<100))
return;

last_progress_time = runtime;
estimate = (1-v/100)*(last_progress_time-progress_start_time)/(v/100);
total_estimate = (last_progress_time-progress_start_time)/(v/100);
}

if (estimate>120)
{
snprintf(str, sizeof(str), "%%s %%%d.%df %%1.1f minutes remaining %%1.1f minutes total \r",decimals+3, decimals);
message(MSG_MESSAGEONLY, "", "", -1, str, prefix, current_val, estimate/60, total_estimate/60);
}
else
{
snprintf(str, sizeof(str), "%%s %%%d.%df %%1.1f seconds remaining %%1.1f seconds total \r",decimals+3, decimals);
message(MSG_MESSAGEONLY, "", "", -1, str, prefix, current_val, estimate, total_estimate);
}

fflush(target);
}

void SGIO::done()
{
Expand Down
65 changes: 0 additions & 65 deletions src/shogun/io/SGIO.h
Expand Up @@ -139,21 +139,6 @@ __FILE__ ":" func ": Unstable method! Please report if it seems to " \
#define SG_GPL_ONLY { io->gpl_only(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
#define SG_DEPRECATED { io->deprecated(__PRETTY_FUNCTION__, __FILE__, __LINE__); }

#define SG_PROGRESS(...) { \
if (SG_UNLIKELY(io->get_show_progress())) \
io->progress(__VA_ARGS__); \
}

#define SG_OBJ_PROGRESS(o, ...) { \
if (SG_UNLIKELY(o->io->get_show_progress()))\
o->io->progress(__VA_ARGS__); \
}

#define SG_ABS_PROGRESS(...) { \
if (SG_UNLIKELY(io->get_show_progress())) \
io->absolute_progress(__VA_ARGS__); \
}

#define SG_DONE() { \
if (SG_UNLIKELY(io->get_show_progress())) \
io->done(); \
Expand All @@ -179,17 +164,6 @@ __FILE__ ":" func ": Unstable method! Please report if it seems to " \
#define SG_SERROR(...) { sg_io->message(MSG_ERROR,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
#define SG_SPRINT(...) { sg_io->message(MSG_MESSAGEONLY,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }


#define SG_SPROGRESS(...) { \
if (SG_UNLIKELY(sg_io->get_show_progress())) \
sg_io->progress(__VA_ARGS__); \
}

#define SG_SABS_PROGRESS(...) { \
if (SG_UNLIKELY(sg_io->get_show_progress())) \
sg_io->absolute_progress(__VA_ARGS__); \
}

#define SG_SDONE() { \
if (SG_UNLIKELY(sg_io->get_show_progress())) \
sg_io->done(); \
Expand Down Expand Up @@ -290,12 +264,6 @@ class SGIO
return location_info;
}

/** @return last progress as a percentage */
inline float64_t get_last_progress() const
{
return last_progress;
}

/** get syntax highlight
*
* @return if syntax highlighting is enabled
Expand All @@ -319,33 +287,6 @@ class SGIO
void message(EMessageType prio, const char* function, const char* file,
int32_t line, const char *fmt, ... ) const;

/** print progress bar
*
* @param current_val current value
* @param min_val minimum value
* @param max_val maximum value
* @param decimals decimals
* @param prefix message prefix
*/
void progress(
float64_t current_val,
float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
const char* prefix="PROGRESS:\t");

/** print absolute progress bar
*
* @param current_val current value
* @param val value
* @param min_val minimum value
* @param max_val maximum value
* @param decimals decimals
* @param prefix message prefix
*/
void absolute_progress(
float64_t current_val, float64_t val,
float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
const char* prefix="PROGRESS:\t");

/** print 'done' with priority INFO,
* but only if progress bar is enabled
*
Expand Down Expand Up @@ -575,12 +516,6 @@ class SGIO
protected:
/** target file */
FILE* target;
/** last progress time */
float64_t last_progress_time;
/** progress start time */
float64_t progress_start_time;
/** last progress */
float64_t last_progress;
/** if progress bar shall be shown */
bool show_progress;
/** if each print function should append filename and linenumber of
Expand Down

0 comments on commit c106236

Please sign in to comment.