Skip to content

Commit a5e0e95

Browse files
authored
Fixes to issue #360. Regression caused by #352 . (#361)
1 parent 58292d6 commit a5e0e95

File tree

7 files changed

+64
-45
lines changed

7 files changed

+64
-45
lines changed

basecode/header.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ class Neutral;
9999
#include "SharedFinfo.h"
100100
#include "FieldElementFinfo.h"
101101
#include "FieldElement.h"
102-
#include "../builtins/Streamer.h"
103102
#include "../shell/Neutral.h"
104103

105104

builtins/Streamer.cpp

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
/***
2-
* Filename: Streamer.cpp
3-
*
42
* Description: Stream table data.
53
*
6-
* Version: 0.0.1
7-
* Created: 2016-04-26
8-
9-
* Revision: none
10-
*
114
* Author: Dilawar Singh <dilawars@ncbs.res.in>
125
* Organization: NCBS Bangalore
13-
*
14-
* License: GNU GPL2
156
*/
167

178
#include <algorithm>
@@ -45,12 +36,18 @@ const Cinfo* Streamer::initCinfo()
4536
, &Streamer::getFormat
4637
);
4738

48-
static ReadOnlyValueFinfo< Streamer, size_t > numTables (
39+
static ReadOnlyValueFinfo<Streamer, size_t> numTables (
4940
"numTables"
5041
, "Number of Tables handled by Streamer "
5142
, &Streamer::getNumTables
5243
);
5344

45+
static ReadOnlyValueFinfo<Streamer, size_t> numWriteEvents(
46+
"numWriteEvents"
47+
, "Number of time streamer was called to write. (For debugging/performance reason only)"
48+
, &Streamer::getNumWriteEvents
49+
);
50+
5451
/*-----------------------------------------------------------------------------
5552
*
5653
*-----------------------------------------------------------------------------*/
@@ -96,7 +93,7 @@ const Cinfo* Streamer::initCinfo()
9693
*-----------------------------------------------------------------------------*/
9794
static Finfo* procShared[] =
9895
{
99-
&process , &reinit , &addTable, &addTables, &removeTable, &removeTables
96+
&process, &reinit, &addTable, &addTables, &removeTable, &removeTables
10097
};
10198

10299
static SharedFinfo proc(
@@ -107,7 +104,7 @@ const Cinfo* Streamer::initCinfo()
107104

108105
static Finfo * tableStreamFinfos[] =
109106
{
110-
&outfile, &format, &proc, &numTables
107+
&outfile, &format, &proc, &numTables, &numWriteEvents
111108
};
112109

113110
static string doc[] =
@@ -140,8 +137,9 @@ Streamer::Streamer()
140137
{
141138
// Not all compilers allow initialization during the declaration of class
142139
// methods.
143-
format_ = "npy";
144-
columns_.push_back( "time" ); /* First column is time. */
140+
format_ = "csv";
141+
numWriteEvents_ = 0;
142+
columns_.push_back("time"); /* First column is time. */
145143
tables_.resize(0);
146144
tableIds_.resize(0);
147145
tableTick_.resize(0);
@@ -238,7 +236,7 @@ void Streamer::reinit(const Eref& e, ProcPtr p)
238236
// write now.
239237
currTime_ = 0.0;
240238
zipWithTime( );
241-
StreamerBase::writeToOutFile( outfilePath_, format_, "w", data_, columns_);
239+
StreamerBase::writeToOutFile(outfilePath_, format_, "w", data_, columns_);
242240
data_.clear( );
243241
}
244242

@@ -261,15 +259,11 @@ void Streamer::cleanUp( )
261259
*/
262260
void Streamer::process(const Eref& e, ProcPtr p)
263261
{
264-
//LOG( moose::debug, "Writing to table" );
262+
// LOG( moose::debug, "Writing Streamer data to file." );
265263
zipWithTime( );
266-
267-
// Write only if there are more than 100 entry in first table.
268-
if( tables_[0]->getVecSize() > 100 )
269-
{
270-
StreamerBase::writeToOutFile( outfilePath_, format_, "a", data_, columns_ );
271-
data_.clear( );
272-
}
264+
StreamerBase::writeToOutFile( outfilePath_, format_, "a", data_, columns_ );
265+
data_.clear();
266+
numWriteEvents_ += 1;
273267
}
274268

275269

@@ -356,6 +350,19 @@ size_t Streamer::getNumTables( void ) const
356350
return tables_.size();
357351
}
358352

353+
/* --------------------------------------------------------------------------*/
354+
/**
355+
* @Synopsis Get number of write events in streamer. Useful for debugging and
356+
* performance measuerments.
357+
*
358+
* @Returns
359+
*/
360+
/* ----------------------------------------------------------------------------*/
361+
size_t Streamer::getNumWriteEvents( void ) const
362+
{
363+
return numWriteEvents_;
364+
}
365+
359366

360367
string Streamer::getOutFilepath( void ) const
361368
{

builtins/Streamer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Streamer : public StreamerBase
4949
void setFormat( string format );
5050

5151
size_t getNumTables( void ) const;
52+
size_t getNumWriteEvents( void ) const;
5253

5354
void addTable( Id table );
5455
void addTables( vector<Id> tables);
@@ -74,6 +75,9 @@ class Streamer : public StreamerBase
7475

7576
string outfilePath_;
7677
string format_;
78+
79+
size_t numWriteEvents_;
80+
7781
bool isOutfilePathSet_;
7882

7983
// dt_ and tick number of Table's clock

builtins/StreamerBase.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ void StreamerBase::writeToCSVFile( const string& filepath, const string& openmod
9191
}
9292

9393
// If writing in "w" mode, write the header first.
94-
if( openmode == "w" )
94+
if(openmode == "w")
9595
{
9696
string headerText = "";
9797
for( vector<string>::const_iterator it = columns.begin();
9898
it != columns.end(); it++ )
9999
headerText += ( *it + delimiter_ );
100100
headerText += eol;
101-
fprintf( fp, "%s", headerText.c_str() );
101+
fprintf(fp, "%s", headerText.c_str());
102102
}
103103

104104
string text = "";
@@ -111,8 +111,8 @@ void StreamerBase::writeToCSVFile( const string& filepath, const string& openmod
111111
// At the end of each row, we remove the delimiter_ and append newline_.
112112
*(text.end()-1) = eol;
113113
}
114-
fprintf( fp, "%s", text.c_str() );
115-
fclose( fp );
114+
fprintf(fp, "%s", text.c_str() );
115+
fclose(fp);
116116
}
117117

118118
/* write data to a numpy file */

shell/Shell.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "../msg/OneToAllMsg.h"
2323
#include "../msg/SparseMsg.h"
2424
#include "../builtins/SocketStreamer.h"
25+
#include "../builtins/Streamer.h"
2526

2627
#include "Shell.h"
2728
#include "Wildcard.h"

tests/python/test_streamer.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# -*- coding: utf-8 -*-
2-
"""test_streamer.py:
2+
from __future__ import print_function
33

4+
"""test_streamer.py:
45
Test script for Streamer class.
5-
66
"""
77

88
__author__ = "Dilawar Singh"
@@ -14,13 +14,17 @@
1414
__email__ = "dilawars@ncbs.res.in"
1515
__status__ = "Development"
1616

17-
import os
18-
import sys
19-
import time
2017
import moose
18+
import threading
2119
import numpy as np
22-
print(( '[INFO] Using moose form %s' % moose.__file__ ))
20+
import time
21+
import os
22+
import sys
23+
print('[INFO] Using moose form %s' % moose.__file__)
24+
25+
all_done_ = False
2326

27+
# Poll the file to see that we are really writing to it.
2428
def sanity_test( ):
2529
a = moose.Table( '/t1' )
2630
b = moose.Table( '/t1/t1' )
@@ -64,6 +68,7 @@ def sanity_test( ):
6468

6569
def test( ):
6670
compt = moose.CubeMesh( '/compt' )
71+
assert compt
6772
r = moose.Reac( '/compt/r' )
6873
a = moose.Pool( '/compt/a' )
6974
a.concInit = 1
@@ -77,6 +82,10 @@ def test( ):
7782
r.Kf = 0.1
7883
r.Kb = 0.01
7984

85+
outfile = 'streamer_test.csv'
86+
if os.path.exists(outfile):
87+
os.remove(outfile)
88+
8089
tabA = moose.Table2( '/compt/a/tab' )
8190
tabB = moose.Table2( '/compt/tabB' )
8291
tabC = moose.Table2( '/compt/tabB/tabC' )
@@ -88,33 +97,32 @@ def test( ):
8897

8998
# Now create a streamer and use it to write to a stream
9099
st = moose.Streamer( '/compt/streamer' )
91-
st.outfile = os.path.join( os.getcwd(), 'temp.npy' )
92-
print(("outfile set to: %s " % st.outfile ))
93-
assert st.outfile == os.path.join( os.getcwd(), 'temp.npy' ), st.outfile
100+
st.outfile = outfile
94101

102+
print("outfile set to: %s " % st.outfile )
95103
st.addTable( tabA )
96104
st.addTables( [ tabB, tabC ] )
97-
98105
assert st.numTables == 3
99106

100107
moose.reinit( )
101-
print( '[INFO] Running for 57 seconds' )
102-
moose.start( 57 )
108+
t = 100
109+
print( '[INFO] Running for %d seconds' % t )
110+
moose.start(t)
103111
outfile = st.outfile
104112
moose.quit() # Otherwise Streamer won't flush the rest of entries.
105113

114+
print('Moose is done. Waiting for monitor to shut down...')
115+
106116
# Now read the table and verify that we have written
107117
print( '[INFO] Reading file %s' % outfile )
108118
if 'csv' in outfile:
109119
data = np.loadtxt(outfile, skiprows=1 )
110120
else:
111121
data = np.load( outfile )
112122
# Total rows should be 58 (counting zero as well).
113-
# print(data)
123+
# print(data)
114124
# print( data.dtype )
115-
time = data['time']
116-
print( time )
117-
assert data.shape >= (58,), data.shape
125+
assert data.shape >= (101,), data.shape
118126
print( '[INFO] Test 2 passed' )
119127
return 0
120128

tests/python/test_table_streaming_support.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test( ):
5959
moose.reinit( )
6060
[ print_table( x) for x in [tabA, tabB, tabC] ]
6161
runtime = 1000
62-
print( 'Starting moose for %s' % runtime )
62+
print( 'Starting moose for %d secs' % runtime )
6363
moose.start( runtime, 1 )
6464
print( ' MOOSE is done' )
6565

0 commit comments

Comments
 (0)