Exemplo n.º 1
0
//on linux Mordor::IOManager user epoll
TEST(IOManager, event) {
//   Log::root()->level(Log::TRACE);
//   Log::root()->addSink( LogSink::ptr(new StdoutLogSink));
   //IOManager(size_t threads = 1, bool useCaller = true, bool autoStart = true);
   IOManager iomanager;
   const size_t conn_num = 5;
   std::vector<boost::shared_ptr<Conn> > conns; 
   conns.reserve(conn_num);

   //register event
   for(size_t i = 0 ; i < conn_num; i++){
       boost::shared_ptr<Conn> conn(new Conn);
       iomanager.registerEvent(conn->getReadFd(), IOManager::READ,  boost::bind(&Conn::read, conn));
       conns.push_back(conn);
   }

   //write 
   for(size_t i = 0; i < conns.size(); i++) {
      MORDOR_LOG_DEBUG(Log::root()) <<  " write on for  "  << conns[i]->getReadFd();
      conns[i]->write(100);
   }

    iomanager.dispatch();
    iomanager.stop();

   for(size_t i = 0; i < conns.size(); i++) {
      ASSERT_EQ(boost::this_thread::get_id() , conns[i]->tid);
      ASSERT_EQ(100, conns[i]->read_count);
   }
}
Exemplo n.º 2
0
void PivotCommand :: OutputPivot( IOManager & io ) {

    CSVRow r;
    r.push_back( "" );    // corresponds to row header
    for ( auto col : mCols ) {
        r.push_back( col );
    }
    io.WriteRow( r );

    for( auto row : mRows ) {
        r.clear();
        r.push_back( row );
        for ( auto col : mCols ) {
            ColRow cr( col, row );
            SumCount sc = mColRowValMap[ cr ];
            if ( mAction == Action::Average ) {
                r.push_back( ALib::Str( sc.mSum / sc.mCount ) );
            }
            else {
                r.push_back( ALib::Str( sc.mSum ) );
            }
        }
        io.WriteRow( r );
    }


}
Exemplo n.º 3
0
void JoinCommand :: WriteJoinRows( IOManager & io, const CSVRow & row ) {

	string key = MakeKey( row, true );

	MapType::const_iterator pos = mRowMap.lower_bound( key );
	MapType::const_iterator last = mRowMap.upper_bound( key );

	if ( mOuterJoin && pos == last ) {
		io.WriteRow( row );
	}
	else if ( mInvert ) {
		if ( pos == last ) {
			io.WriteRow( row );
		}
		return;
	}
	else {
		while( pos != last ) {
			const CSVRow & jr = pos->second;
			CSVRow newrow = row;
			for ( unsigned int i = 0; i < jr.size(); i++ ) {
				newrow.push_back( jr[i] );
			}
			io.WriteRow( newrow );
			++pos;
		}
	}
}
Exemplo n.º 4
0
void AddVars( ALib::Expression & e, const IOManager & io, const CSVRow & row ) {
	e.ClearPosParams();
	e.AddVar( LINE_VAR, ALib::Str( io.CurrentLine() ));
	e.AddVar( FILE_VAR, ALib::Str( io.CurrentFileName()));
	e.AddVar( FIELD_VAR, ALib::Str( row.size()));
	for ( unsigned int j = 0; j < row.size(); j++ ) {
		e.AddPosParam( row.at( j ) );
	}
}
Exemplo n.º 5
0
MORDOR_UNITTEST(IOManager, laterTimer)
{
    int sequence = 0;
    IOManager manager;
    manager.registerTimer(100000, boost::bind(&singleTimer, boost::ref(sequence), 1));
    MORDOR_TEST_ASSERT_EQUAL(sequence, 0);
    manager.dispatch();
    ++sequence;
    MORDOR_TEST_ASSERT_EQUAL(sequence, 2);
}
Exemplo n.º 6
0
void SummaryCommand :: DoMedian( IOManager & io ) {

	CSVRow r;

	for ( unsigned int i = 0; i < mFields.size(); i++ ) {
		unsigned int col = mFields[i];
		NumSort ns( col );
		std::sort( mRows.begin(), mRows.end(), ns );
		unsigned int sz = mRows.size();
		double d;
		if ( sz % 2 ) {
			unsigned int idx = (sz / 2);
			d = ALib::ToReal( mRows.at(idx).at(col) );
		}
		else {
			unsigned int idx = (sz / 2) - 1;
			double d1 = ALib::ToReal( mRows.at(idx).at(col) );
			double d2 = ALib::ToReal( mRows.at(idx+1).at(col) );
			d = (d1 + d2) /2;
		}

		r.push_back( ALib::Str( d ) );
	}

	io.WriteRow( r );
}
Exemplo n.º 7
0
void SummaryCommand :: PrintSizes( IOManager & io, const SizeMap & sm ) {
	SizeMap::const_iterator it = sm.begin();
	while( it != sm.end() ) {
		io.Out() << it->first + 1 << ": "
				 << it->second.first << "," << it->second.second
				 << "\n";
		++it;
	}
}
Exemplo n.º 8
0
void SummaryCommand :: DoAvg( IOManager & io ) {
	vector <double> sums;
	SumCols( sums );
	CSVRow r;
	for ( unsigned int i = 0; i < sums.size(); i++ ) {
		r.push_back( ALib::Str( sums.at(i) / mRows.size() ) );
	}
	io.WriteRow( r );
}
Exemplo n.º 9
0
void ShuffleCommand :: Shuffle( IOManager & io ) {
    ALib::RandGen rg( mSeed );
    int nout = mCount;
    int last = mRows.size();
    for ( unsigned int i = 0; i < mRows.size() && nout-- > 0; i++ ) {
        int pos = rg.NextInt( 0, last );
        io.WriteRow( mRows[pos] );
        mRows[pos].swap( mRows[--last] );
    }
}
Exemplo n.º 10
0
void SummaryCommand :: DoFreq( IOManager & io ) {
	CalcFreqs();

	for ( unsigned int i = 0; i < mRows.size(); i++ ) {
		string key = MakeKey( mRows.at(i) );
		unsigned int n = mFreqMap.find( key )->second.mFreq;
		CSVRow r = mRows.at(i);
		r.insert( r.begin(), ALib::Str( n ) );
		io.WriteRow( r );
	}
}
Exemplo n.º 11
0
void  ReadXMLCommand :: WriteRow( const ALib::XMLElement * r,
										IOManager & io ) {
	CSVRow row;

	for ( unsigned int i = 0; i < r->ChildCount(); i++  ) {
		const ALib::XMLElement * ce = r->ChildElement( i );
		if ( ce && ce->Name() == "td" ) {
			row.push_back( TDToCSV( ce ) );
		}
	}

//	ALib::Dump( std::cout, row );
	io.WriteRow( row );
}
Exemplo n.º 12
0
void SummaryCommand :: DoMode( IOManager & io ) {

	unsigned int mode = CalcFreqs();

	for ( FreqMap::const_iterator it = mFreqMap.begin();
			it != mFreqMap.end(); ++it ) {

		if ( it->second.mFreq == mode ) {
			for ( unsigned int i = 0; i < it->second.mIndex.size(); i++ ) {
				CSVRow r = mRows.at( it->second.mIndex.at(i) );
				r.insert( r.begin(), ALib::Str( mode ) );
				io.WriteRow( r );
			}
		}
	}
}
Exemplo n.º 13
0
void ReadXMLCommand :: TableToCSV( const ALib::XMLElement * e,
										IOManager & io,
										unsigned int idx  ) {

	const ALib::XMLElement * table = FindTable( e );
	if ( table == 0 ) {
		CSVTHROW( "No XML table found in " <<  io.InFileName( idx ) );
	}

	for ( unsigned int i = 0; i < table->ChildCount(); i++ ) {
		const ALib::XMLElement * ce = table->ChildElement( i );
		if ( ce && ce->Name() == "tr" ) {
			WriteRow( ce, io );
		}
	}

}
Exemplo n.º 14
0
void SummaryCommand :: DoMinMax( IOManager & io ) {
	CSVRow r = mRows.at(0);

	for ( unsigned int i = 1; i < mRows.size(); i++ ) {
		if ( mType == Min && Cmp( mRows.at(i), r ) < 0 ) {
			r = mRows.at(i);
		}
		else if ( mType == Max && Cmp( mRows.at(i), r ) > 0 ) {
			r = mRows.at(i);
		}
	}

	for ( unsigned int i = 0; i < mRows.size(); i++ ) {
		if ( Cmp( r, mRows.at(i) ) == 0 ) {
			io.WriteRow( mRows.at(i) );
		}
	}
}
Exemplo n.º 15
0
 //
 //Saves the pair on the disk & memory
 //
 void privateStore(K _key, V _value){
     
     //convert key to char*
     int sizeOfKey = (int)log10(_key) + 2;
     char* keyString = new char[sizeOfKey];
     keyString[sizeOfKey - 1] = '\0';
     sprintf(keyString, "%ld", _key);
     
     
     //place in hashtable
     long index = Hash::stringHash(keyString) % 1000;
     LinkedList<Pair<K, V>>& row = hashTable[index];
     Pair<K,V> pair;
     pair.key = _key;
     pair.value = _value;
     row.addElement(pair);
     IOManager.writeToFile(_key , _value);
     
     delete [] keyString;
 }
Exemplo n.º 16
0
void FileSplitCommand :: WriteRow( IOManager & ioman, const CSVRow & row ) {

	string key = MakeKey( row );
	const string * p = mDict.GetPtr( key );
	string fname = (p == 0) ? NewFileName( key ) : * p;

	if ( p == 0 ) {
		mDict.Add( key, fname );
	}

	if ( fname != mCurrentFile ) {
		mOutFile.close();
		mOutFile.clear();	// close does not reset state
		mOutFile.open( fname.c_str(), std::ios::out | std::ios::app );   // append

		if ( ! mOutFile.is_open() ) {
			CSVTHROW( "Could not open file " << fname << " for output" );
		}
		mCurrentFile = fname;
	}

	mOutFile << ioman.CurrentInput() << "\n";
}
Exemplo n.º 17
0
void ValidateCommand :: Report( IOManager & io,
								const ValidationRule::Results & res,
								int errcount ) const {
	if ( res.size() == 0 ) {
		return;
	}
	else {
		if ( errcount == 0 ) {
			io.Out() << io.CurrentFileName() << " (" << io.CurrentLine () << "): ";
			io.Out() << io.CurrentInput() << "\n";
		}
		for ( unsigned int i = 0; i < res.size(); i++ ) {
			if ( res[i].Field() > 0 ) {
				io.Out() << "    field: " << res[i].Field() << " - ";
			}
			else {
				io.Out() << "    ";
			}
			io.Out() << res[i].Msg() << "\n";
		}
	}
}
Exemplo n.º 18
0
int main(int argc, const char *argv[])
{
    ConfigManager configManager;
    Domain domain(1);
    Mesh mesh(domain, 2);
    Field<double, 2> u, f;
    Field<double> fu;
    TimeManager timeManager;
    IOManager io;
    int outputFileIdx;
    TimeLevelIndex<2> oldIdx, newIdx, halfIdx;

    double dt, dx;
    string outputPattern = "beam_warming.%3s.nc";

    if (argc != 2) {
        REPORT_ERROR("Configure file is needed!");
    }

    // Read configuration from file.
    configManager.parse(argv[1]);
    dt = configManager.getValue("beam_warming", "dt", 1);
    dx = configManager.getValue("beam_warming", "dx", 0.01);
    outputPattern = configManager.getValue("beam_warming", "output_pattern", outputPattern);

    // Set the one dimensional space axis.
    domain.setAxis(0, "x", "x axis", "m",
                   0, geomtk::BndType::PERIODIC,
                   1, geomtk::BndType::PERIODIC);

    // Set the discrete mesh on the domain.
    mesh.init(domain.axisSpan(0)/dx);

    // Set the time manager.
    Time startTime(0*geomtk::TimeUnit::SECONDS);
    Time endTime(200*geomtk::TimeUnit::SECONDS);
    timeManager.init(startTime, endTime, dt);

    // Set up velocity and density fields.
    u.create("u", "m s-1", "velocity component along x axis", mesh, X_FACE, 1, true);
    f.create("f", "kg m-1", "tracer density", mesh, CENTER, 1);
    fu.create("fu", "kg s-1", "tracer mass flux", mesh, X_FACE, 1);

    // Set the initial conditions.
    newIdx = oldIdx+1;
    for (int i = mesh.is(HALF); i <= mesh.ie(HALF); ++i) {
        u(oldIdx, i) = 0.005;
        u(newIdx, i) = 0.005;
    }
    u.applyBndCond(oldIdx);
    u.applyBndCond(newIdx, true);
    for (int i = mesh.is(FULL); i <= mesh.ie(FULL); ++i) {
        const SpaceCoord &x = mesh.gridCoord(CENTER, i);
        if (x(0) >= 0.05 && x(0) <= 0.1) {
            f(oldIdx, i) = 1.0;
        } else {
            f(oldIdx, i) = 0.0;
        }
    }
    f.applyBndCond(oldIdx);

    // Set up IO manager.
    io.init(timeManager);
    outputFileIdx = io.registerOutputFile(mesh, outputPattern, geomtk::TimeStepUnit::STEP, 1);
    io.registerField(outputFileIdx, "double", FULL_DIMENSION, {&f});
    io.output<double, 2>(outputFileIdx, oldIdx, {&f});

    // Run the main loop.
    double C = dt/dx;
    while (!timeManager.isFinished()) {
        newIdx = oldIdx+1; halfIdx = oldIdx+0.5;
        for (int i = mesh.is(HALF); i <= mesh.ie(HALF); ++i) {
            fu(i) = 0.5*C*(      u(halfIdx, i)    *(3*f(oldIdx, i)-f(oldIdx, i-1))-
                           C*pow(u(halfIdx, i), 2)*(  f(oldIdx, i)-f(oldIdx, i-1)));
        }
        fu.applyBndCond();
        for (int i = mesh.is(FULL); i <= mesh.ie(FULL); ++i) {
            f(newIdx, i) = f(oldIdx, i)-(fu(i)-fu(i-1));
        }
        f.applyBndCond(newIdx);
        timeManager.advance(); oldIdx.shift();
        io.output<double, 2>(outputFileIdx, oldIdx, {&f});
    }

    return 0;
}