Commit 73b7ca2b authored by Gio De Francesco's avatar Gio De Francesco
Browse files

Finished speed improvements by buffering line reads and added logging

parent b7374d6a
This diff is collapsed.
......@@ -186,7 +186,6 @@ void VolElement::add( int *n, int r )
MultiPoint::add( n );
if ( _n>1 ) {
int *newreg = new int[_n*_ptsPerObj];
/** GD: indexing out of range on the _region happens alot in this file, should be _n-1 I think. **/
memcpy( newreg, _region, _n*_ptsPerObj*sizeof(int) );
delete[] _region;
_region = newreg;
......
......@@ -3,7 +3,7 @@ HOSTMACHINE := $(shell uname)
FLTK_INC := $(shell fltk-config --use-gl --cxxflags)
FLTK_LD_FLAGS := $(shell fltk-config --use-gl --ldflags)
COMMON_LIBS = -lpng -lpthread -lm -lz
COMMON_INC = -I. -O0 -g -DOBJ_CLASS -pg -D_REENTRANT -MMD -DNOMINMAX
COMMON_INC = -I. -O3 -g -DOBJ_CLASS -D_REENTRANT -MMD -DNOMINMAX
ifeq ($(HOSTMACHINE),Darwin)
LIBS = -L/sw/lib $(FLTK_LD_FLAGS) $(COMMON_LIBS)
......
......@@ -9,6 +9,7 @@
#include <sstream>
#include <FL/filename.H>
#include "gzFileBuffer.h"
#include "logger.hpp"
struct Face {
int nsort[MAX_NUM_SURF_NODES]; //!< sorted nodes
......@@ -46,6 +47,7 @@ make_face( Face *f, int n, int* orig )
/** read in a line from a file, ignoring lines beginning with "#"
*
* \warn (GD) lines are limited to 1024 bytes.
* \warn not threadsafe
* \return pointer to a static buffer
*/
......@@ -81,7 +83,7 @@ int Model::new_region_label()
Model::Model():
_base1(false), _surface(NULL), _vertnrml(NULL),_cable(NULL),
_base1(false), _surface(NULL), _vertnrml(NULL),
_numReg(0), _region(NULL), _numVol(0), _vol(NULL), _cnnx(NULL)
{
for ( int i=0; i<maxobject; i++ ) {
......@@ -100,10 +102,7 @@ Model::Model():
const int bufsize=1024;
bool Model::read( const char* fnt, bool base1, bool no_elems )
{
ofstream logger("logger.txt");
clock_t timer = clock();
logger << "starting to monitor" << std::endl;
logger.flush();
LOG_TIMER_RESET;
_file = fnt;
char fn[bufsize];
......@@ -120,10 +119,7 @@ bool Model::read( const char* fnt, bool base1, bool no_elems )
}
allvis.resize(pt.num());
allvis.assign(pt.num(), true );
logger << "time1: " << (clock() - timer) / (double) CLOCKS_PER_SEC << std::endl; timer = clock();
logger.flush();
LOG_TIMER("pt.read()");
if ( !strcmp( ".gz", fn+strlen(fn)-3 ) ) // temporary
fn[strlen(fn)-3] = '\0';
......@@ -136,44 +132,29 @@ bool Model::read( const char* fnt, bool base1, bool no_elems )
_cnnx = new Connection( &pt );
_cnnx->read( fn );
logger << "time2: " << (clock() - timer) / (double) CLOCKS_PER_SEC << std::endl; timer = clock();
logger.flush();
LOG_TIMER("cnnx->read()");
_cable = new ContCable( &pt );
_cable->read( fn );
logger << "time3: " << (clock() - timer) / (double) CLOCKS_PER_SEC << std::endl; timer = clock();
logger.flush();
LOG_TIMER("cable->read()");
if( !no_elems ) read_elem_file( fn );
logger << "time4: " << (clock() - timer) / (double) CLOCKS_PER_SEC << std::endl; timer = clock();
logger.flush();
LOG_TIMER("read_elem_file()");
read_region_file( in, fn );
logger << "time5: " << (clock() - timer) / (double) CLOCKS_PER_SEC << std::endl; timer = clock();
logger.flush();
LOG_TIMER("read_region_file()");
determine_regions();
logger << "time6: " << (clock() - timer) / (double) CLOCKS_PER_SEC << std::endl; timer = clock();
logger.flush();
LOG_TIMER("determine_regions()");
add_surface_from_tri( fn );
logger << "time7: " << (clock() - timer) / (double) CLOCKS_PER_SEC << std::endl; timer = clock();
logger.flush();
LOG_TIMER("add_surface_from_tri()");
add_surface_from_surf( fn );
logger << "time8: " << (clock() - timer) / (double) CLOCKS_PER_SEC << std::endl; timer = clock();
logger.flush();
LOG_TIMER("add_surface_from_surf()");
add_surface_from_elem( fn );
logger << "time9: " << (clock() - timer) / (double) CLOCKS_PER_SEC << std::endl; timer = clock();
logger.flush();
LOG_TIMER("add_surface_from_elem()");
// find maximum dimension and bounding box
const GLfloat *p = pt.pt();
......@@ -183,10 +164,6 @@ bool Model::read( const char* fnt, bool base1, bool no_elems )
_vertnrml= new GLfloat[3*pt.num()];
logger << "time10: " << (clock() - timer) / (double) CLOCKS_PER_SEC << std::endl; timer = clock();
logger.flush();
return true;
}
......@@ -292,6 +269,7 @@ void Model::determine_regions()
}
}
/** add surface by selecting 2D elements from .elem file
*
* A surface will be created for each different region specified.
......@@ -315,28 +293,18 @@ int Model::add_surface_from_elem( const char *fn )
map<string,int> surfs; // number of elements in each surface
gzFileBuffer file(in);
while ( file.gets(buff,bufsize) != Z_NULL )
//while ( gzgets(in,buff,bufsize) != Z_NULL )
{
while ( file.gets(buff,bufsize) != Z_NULL ) {
char surfnum[10]="";
if ( !strncmp(buff,"Tr",2) )
{
if ( !strncmp(buff,"Tr",2) ) {
if ( sscanf(buff, "%*s %*d %*d %*d %s", surfnum )<1 )
strcpy(surfnum, "EMPTY");
}
else if ( !strncmp(buff,"Qd",2) )
{
strcpy( surfnum, "EMPTY" );
} else if ( !strncmp(buff,"Qd",2) ) {
if ( sscanf(buff, "%*s %*d %*d %*d %*d %s", &surfnum )<1 )
strcpy(surfnum, "EMPTY");
strcpy( surfnum, "EMPTY" );
}
else
continue;
if ( strlen(surfnum) )
surfs[surfnum]++;
}
if ( !surfs.size() ) return numSurf();
// allocate new surfaces
......@@ -357,7 +325,6 @@ int Model::add_surface_from_elem( const char *fn )
gzgets(in,buff,bufsize); //throw away first line
while( file2.gets(buff,bufsize) !=Z_NULL ) {
//while( gzgets(in,buff,bufsize) !=Z_NULL ) {
char etype[10],reg[10];
int idat[4];
if( !strncmp(buff,"Tr",2) ) {
......@@ -488,8 +455,8 @@ int Model::add_surface_from_surf( const char *fn )
int surfnum = 0;
char surflabel[bufsize];
while ( gzgets(in,buff,bufsize)!=Z_NULL && sscanf(buff, "%d", &nele )==1 ) {
gzFileBuffer file(in);
while ( file.gets(buff,bufsize)!=Z_NULL && sscanf(buff, "%d", &nele )==1 ) {
_surface.push_back( new Surfaces( &pt ) );
// use specified surface label if available
......@@ -501,7 +468,7 @@ int Model::add_surface_from_surf( const char *fn )
for ( int i=0; i<nele; i++ ) {
int nl[5];
char etype[12];
if ( gzgets(in,buff,bufsize) == Z_NULL ||
if ( file.gets(buff,bufsize) == Z_NULL ||
sscanf(buff, "%s %d %d %d", etype, nl, nl+1, nl+2, nl+3, nl+4 ) < 4 ) {
_surface.pop_back();
return surfnum;
......@@ -1045,31 +1012,28 @@ bool Model :: read_elem_file( const char *fname )
_vol = new VolElement*[_numVol];
gzFileBuffer file(in);
int nele = _numVol;
int ne = 0;
int surfe = 0;
for( int i=0; i<nele; i++ ) {
//if( gzgets(in, buf, bufsize)==Z_NULL || !strlen(buf) ) break;
if( file.gets(buf, bufsize)==Z_NULL || !strlen(buf) ) break;
int n[9];
if( tets )
//sscanf( buf, "%d %d %d %d %d", n, n+1, n+2, n+3, n+4 );
{
char * p = buf;
for (size_t i=0; i<5; i++)
n[i] = strtol(p, &p, 10);
}
else
//sscanf( buf, "%2s %d %d %d %d %d %d %d %d %d", eletype,
// n, n+1, n+2, n+3, n+4, n+5, n+6, n+7, n+8 );
{
eletype[0]=buf[0];
eletype[1]=buf[1];
char * p = buf+3;
for (size_t i=0; i<9; i++)
n[i] = strtol(p, &p, 10);
}
int n[9];
if( tets ) {
//sscanf( buf, "%d %d %d %d %d", n, n+1, n+2, n+3, n+4 );
char * p = buf;
for (size_t i=0; *p && i<5; i++)
n[i] = strtol(p, &p, 10);
}
else
{
//sscanf( buf, "%2s %d %d %d %d %d %d %d %d %d", eletype,
// n, n+1, n+2, n+3, n+4, n+5, n+6, n+7, n+8 );
eletype[0]=buf[0];
eletype[1]=buf[1];
char * p = buf+3;
for (size_t i=0; *p && i<9; i++)
n[i] = strtol(p, &p, 10);
}
if( !strcmp( eletype, "Tt" ) ) {
_vol[ne] = new Tetrahedral( &pt );
......
......@@ -142,7 +142,6 @@ bool Point :: read( const char *fname )
_pts[i+j] = _pts[i+j-3];
continue;
}
//if ( gzgets(in, buff, bufsize) == Z_NULL ) throw 2;
if ( file.gets(buff, bufsize) == Z_NULL ) throw 2;
if ( sscanf( buff, "%f %f %f", _pts+i, _pts+i+1, _pts+i+2 ) < 3 )
throw 3;
......
// generated by Fast Light User Interface Designer (fluid) version 1.0300
#include "Sequence.h"
void Sequence::cb_Start_i(Fl_Return_Button*, void*) {
mwtb->output_png( fname->value(), this );
}
void Sequence::cb_Start(Fl_Return_Button* o, void* v) {
((Sequence*)(o->parent()->user_data()))->cb_Start_i(o,v);
}
void Sequence::cb_browse_i(Fl_Button*, void*) {
const char* fn=fl_file_chooser("Choose output file name base", "*.png",fname->value() );
if( fn != NULL ) fname->value(fn);
}
void Sequence::cb_browse(Fl_Button* o, void* v) {
((Sequence*)(o->parent()->user_data()))->cb_browse_i(o,v);
}
Sequence::Sequence( TBmeshWin *tb ) {
{ window = new Fl_Double_Window(295, 190, "Write PNG Sequence");
window->user_data((void*)(this));
{ movieprog = new Fl_Progress(15, 10, 265, 35);
movieprog->selection_color((Fl_Color)91);
} // Fl_Progress* movieprog
{ Fl_Return_Button* o = new Fl_Return_Button(15, 145, 265, 35, "Start sequence");
o->callback((Fl_Callback*)cb_Start);
} // Fl_Return_Button* o
{ fname = new Fl_File_Input(15, 65, 215, 35, "output file base name:");
fname->align(Fl_Align(FL_ALIGN_TOP));
} // Fl_File_Input* fname
{ Fl_Button* o = new Fl_Button(240, 65, 40, 35, "browse");
o->labelsize(10);
o->callback((Fl_Callback*)cb_browse);
} // Fl_Button* o
{ lastFrameNum = new Fl_Value_Input(160, 110, 55, 25, "End Frame:");
lastFrameNum->step(1);
} // Fl_Value_Input* lastFrameNum
window->end();
} // Fl_Double_Window* window
mwtb = tb;
fname->value("image.png");
lastFrameNum->value( mwtb->numframes-1 );
lastFrameNum->maximum( mwtb->numframes-1 );
}
......@@ -636,9 +636,6 @@ int TBmeshWin::handle( int event )
return Fl_Gl_Tb_Window::handle(event);
}
#include <iostream>
#include <fstream>
#include <ctime>
/** read in the geometrical description
*
......
......@@ -47,31 +47,20 @@ void Triangle::draw( int p0, int p1, GLfloat *colour, Colourscale* cs,
for ( int i=3*p0; i<=3*p1; i+=3*stride ) {
if ( !_pt->vis(_node[i]) || !_pt->vis(_node[i+1]) || !_pt->vis(_node[i+2]) )
continue;
if ( lightson && ptnrml==NULL) glNormal3fv( nrml(i/3) );
if ( data!=NULL ) {
if ( dataopac->on() ) { // data opacity
for ( int j=0; j<3; j++ ) {
cs->colourize( data[_node[i+j]], dataopac->alpha(data[_node[i+j]]));
if ( lightson && ptnrml ) glNormal3fv( ptnrml+_node[i+j]*3 );
glVertex3fv( _pt->pt(_node[i+j]) );
}
} else { // no data opacity
for ( int j=0; j<3; j++ ) {
cs->colourize( data[_node[i+j]], colour[3] );
if ( lightson && ptnrml ) glNormal3fv( ptnrml+_node[i+j]*3 );
glVertex3fv( _pt->pt(_node[i+j]) );
}
}
} else { // no data
glColor4fv( colour );
for ( int j=0; j<3; j++ ) {
if ( lightson && ptnrml ) glNormal3fv( ptnrml+_node[i+j]*3 );
glVertex3fv( _pt->pt(_node[i+j]) );
}
}
for ( int j=0; j<3; j++ )
{
if (data)
cs->colourize( data[_node[i+j]], dataopac->on() ? dataopac->alpha(data[_node[i+j]]) : colour[3] );
else
glColor4fv( colour );
if ( lightson && ptnrml )
glNormal3fv( ptnrml+_node[i+j]*3 );
glVertex3fv( _pt->pt(_node[i+j]) );
}
}
glEnd();
}
......@@ -117,14 +106,12 @@ bool Triangle :: read( const char *fname )
gzFileBuffer file(in);
while ( file.gets(buff, bufsize) != Z_NULL ) {
//while ( gzgets(in, buff, bufsize) != Z_NULL ) {
sscanf( buff, "%d", &nele );
_n += nele;
_node = (int *)realloc( _node, _n*3*sizeof(int) );
_nrml = (GLfloat*)realloc( _nrml, _n*sizeof(GLfloat)*3 );
for ( int i=_n-nele; i<_n; i++ ) {
file.gets(buff, bufsize);
//gzgets(in, buff, bufsize);
sscanf( buff, "%d %d %d %*d", _node+3*i, _node+3*i+1, _node+3*i+2 );
}
}
......
// generated by Fast Light User Interface Designer (fluid) version 1.0300
#include "colourchoice.h"
#include "AuxGrid.h"
#include <string>
void colourChoice::cb_Done_i(Fl_Return_Button*, void*) {
col[0] = rgb_choice->r();
col[1] = rgb_choice->g();
col[2] = rgb_choice->b();
col[3] = opacity->value();
for( int r=0; r<numreg; r++ )
if( sc[r] )
tbmw->set_color( obj, r, col[0], col[1], col[2], col[3] );
tbmw->redraw();
window->~Fl_Window();
}
void colourChoice::cb_Done(Fl_Return_Button* o, void* v) {
((colourChoice*)(o->parent()->user_data()))->cb_Done_i(o,v);
}
void colourChoice::cb_apbut_i(Fl_Button*, void*) {
col[0] = rgb_choice->r();
col[1] = rgb_choice->g();
col[2] = rgb_choice->b();
col[3] = opacity->value();
for( int r=0; r<numreg; r++ )
if( sc[r] )
tbmw->set_color( obj, r, col[0], col[1], col[2], col[3] );
tbmw->redraw();
}
void colourChoice::cb_apbut(Fl_Button* o, void* v) {
((colourChoice*)(o->parent()->user_data()))->cb_apbut_i(o,v);
}
colourChoice::colourChoice( GLfloat *c, bool *b, int nb, Object_t o, TBmeshWin *t ) {
{ window = new Fl_Double_Window(225, 260, "Choose colour");
window->user_data((void*)(this));
{ rgb_choice = new Fl_Color_Chooser(0, 0, 225, 175);
rgb_choice->box(FL_NO_BOX);
rgb_choice->color(FL_BACKGROUND_COLOR);
rgb_choice->selection_color(FL_BACKGROUND_COLOR);
rgb_choice->labeltype(FL_NORMAL_LABEL);
rgb_choice->labelfont(0);
rgb_choice->labelsize(14);
rgb_choice->labelcolor(FL_FOREGROUND_COLOR);
rgb_choice->align(Fl_Align(FL_ALIGN_CENTER));
rgb_choice->when(FL_WHEN_RELEASE);
} // Fl_Color_Chooser* rgb_choice
{ opacity = new Fl_Slider(10, 190, 205, 25, "Opacity");
opacity->type(3);
opacity->selection_color(FL_FOREGROUND_COLOR);
opacity->step(0.01);
opacity->value(1);
opacity->align(Fl_Align(FL_ALIGN_TOP));
} // Fl_Slider* opacity
{ Fl_Return_Button* o = new Fl_Return_Button(125, 225, 90, 30, "Done");
o->callback((Fl_Callback*)cb_Done);
} // Fl_Return_Button* o
{ apbut = new Fl_Button(15, 225, 95, 30, "Apply");
apbut->callback((Fl_Callback*)cb_apbut);
} // Fl_Button* apbut
window->end();
} // Fl_Double_Window* window
col = c;
obj = o;
tbmw = t;
sc = b;
numreg = nb;
rgb_choice->rgb(col[0], col[1], col[2] );
opacity->value( col[3] );
}
colourChoice::~colourChoice() {
delete[] sc;
}
void AuxColourChoice::cb_Done1_i(Fl_Return_Button*, void*) {
col[0] = rgb_choice->r();
col[1] = rgb_choice->g();
col[2] = rgb_choice->b();
col[3] = opacity->value();
tbmw->auxGrid->color( obj, col );
tbmw->redraw();
window->~Fl_Window();
}
void AuxColourChoice::cb_Done1(Fl_Return_Button* o, void* v) {
((AuxColourChoice*)(o->parent()->user_data()))->cb_Done1_i(o,v);
}
void AuxColourChoice::cb_apbut1_i(Fl_Button*, void*) {
col[0] = rgb_choice->r();
col[1] = rgb_choice->g();
col[2] = rgb_choice->b();
col[3] = opacity->value();
tbmw->auxGrid->color( obj, col );
tbmw->redraw();
}
void AuxColourChoice::cb_apbut1(Fl_Button* o, void* v) {
((AuxColourChoice*)(o->parent()->user_data()))->cb_apbut1_i(o,v);
}
AuxColourChoice::AuxColourChoice( GLfloat *c, Object_t o, TBmeshWin *tb) {
{ window = new Fl_Double_Window(225, 260, "Choose colour");
window->user_data((void*)(this));
{ rgb_choice = new Fl_Color_Chooser(0, 0, 225, 175);
rgb_choice->box(FL_NO_BOX);
rgb_choice->color(FL_BACKGROUND_COLOR);
rgb_choice->selection_color(FL_BACKGROUND_COLOR);
rgb_choice->labeltype(FL_NORMAL_LABEL);
rgb_choice->labelfont(0);
rgb_choice->labelsize(14);
rgb_choice->labelcolor(FL_FOREGROUND_COLOR);
rgb_choice->align(Fl_Align(FL_ALIGN_CENTER));
rgb_choice->when(FL_WHEN_RELEASE);
} // Fl_Color_Chooser* rgb_choice
{ opacity = new Fl_Slider(10, 190, 205, 25, "Opacity");
opacity->type(3);
opacity->selection_color(FL_FOREGROUND_COLOR);
opacity->step(0.01);
opacity->value(1);
opacity->align(Fl_Align(FL_ALIGN_TOP));
} // Fl_Slider* opacity
{ Fl_Return_Button* o = new Fl_Return_Button(125, 225, 90, 30, "Done");
o->callback((Fl_Callback*)cb_Done1);
} // Fl_Return_Button* o
{ apbut = new Fl_Button(15, 225, 95, 30, "Apply");
apbut->callback((Fl_Callback*)cb_apbut1);
} // Fl_Button* apbut
window->end();
} // Fl_Double_Window* window
col = c;
obj = o;
tbmw = tb;
rgb_choice->rgb(col[0], col[1], col[2] );
opacity->value( col[3] );
string title=objnames[obj];
title += " color";
window->label( title.c_str() );
window->show();
}
#pragma once
#include <string>
#include <fstream>
//#define LOGGING_ENABLED
#ifdef LOGGING_ENABLED
#define LOG1(msg) \
({ \
Logger & log = Logger::GetInstance(); \
log << msg << std::endl; \
log.flush(); \
log.bump(); \
})
#define LOG2(msg1, msg2) \
({ \
Logger & log = Logger::GetInstance(); \
log << msg1 << msg2 << std::endl; \
log.flush(); \
log.bump(); \
})
#define LOG_TIMER(msg) \
({ \
Logger & log = Logger::GetInstance(); \
log << log.elapsed() << ": " << msg << std::endl; \
log.flush(); \
log.reset(); \
})
#define LOG_TIMER_RESET \
({ \
Logger & log = Logger::GetInstance(); \
log.reset(); \
})
#else
#define LOG1(msg) ;
#define LOG2(msg1, msg2) ;
#define LOG_TIMER(msg) ;
#define LOG_TIMER_RESET ;
#endif
class Logger : public ofstream
{
public:
static Logger & GetInstance()
{
static Logger * logger = 0;
if (!logger)
logger = new Logger();
return *logger;
}
private:
Logger()
: ofstream("logger.txt")
{
reset();
}
public:
~Logger()
{
}
void reset()
{
timer = clock();
}
void bump()
{
}
double elapsed()
{
double ret = (clock() - timer) / (double) CLOCKS_PER_SEC;
reset();
return ret;
}
private:
clock_t timer;
};
......@@ -465,7 +465,7 @@ Currently, the first 2 are ignored but must be present.