17 #include <utility/io/mpistream.hh>
18 #include <utility/exit.hh>
19 #include <utility/file/file_sys_util.hh>
21 #include <basic/Tracer.hh>
24 #include <utility/vector1.hh>
35 using namespace utility::io::mpi_stream;
37 static basic::Tracer
tr(
"protocols.jd2.MpiFileBuffer");
47 if ( unfinished_blocks_[ slave ].
size() ) {
48 write_lines( unfinished_blocks_[ slave ] );
49 unfinished_blocks_[ slave ].clear();
54 for ( BufferMap::const_iterator it = unfinished_blocks_.begin(); it != unfinished_blocks_.end(); ++it ) {
55 if ( it->second.size() > 0 ) {
63 return unfinished_blocks_.size();
67 if ( unfinished_blocks_[ slave ].
size() == 0 ) {
68 tr.Trace <<
"EMPTY OPEN/CLOSE Operation from slave " << slave << std::endl;
70 tr.Debug <<
"close " << filename_ <<
" from slave " << slave << std::endl;
81 runtime_assert( channel == mpi_channel_ );
82 unfinished_blocks_[ slave ].push_back( line );
89 LineBuffer & buf( unfinished_blocks_[ slave ] );
90 for (LineBuffer::iterator iter = buf.begin(); iter != buf.end(); ++iter) {
91 length += iter->length();
98 std::cout <<
"START_BLOCK" << std::endl;
99 copy( buf.begin(), buf.end(), std::ostream_iterator< std::string>( std::cout ) );
100 std::cout <<
"END_BLOCK" << std::endl;
107 tr.Debug <<
"send blocking confirmation... " <<
filename() << std::endl;
108 MPI_Send( &status, 1, MPI_INT, slave, MPI_STREAM_TAG, MPI_COMM_WORLD );
109 tr.Debug <<
"blocked..." << std::endl;
112 MPI_Recv( &buf, 4, MPI_INT, slave, MPI_STREAM_TAG, MPI_COMM_WORLD, &stat );
113 tr.Debug <<
"release file? : received: " << buf[ 0 ] <<
" " << buf[ 1 ] <<
" " << buf[ 2 ] <<
" " << buf[ 3 ] << std::endl;
123 while ( status == MPI_FAIL && trials > 0 ) {
128 tr.Debug <<
"open file " << filename <<
" ... " << std::endl;
129 out_.open( filename.c_str() );
130 if (
out_.good() ) status = MPI_SUCCESS_NEW;
133 tr.Debug <<
"open file (append) " << filename <<
" ... " << std::endl;
135 out_.open( filename.c_str(), std::ios::app );
136 if (
out_.good() ) status = MPI_SUCCESS_APPEND;
140 tr.Debug <<
"open file " << filename <<
" ... " << std::endl;
141 out_.open( filename.c_str() );
142 if (
out_.good() ) status = MPI_SUCCESS_NEW;
144 if ( status == MPI_FAIL ) {
145 std::cerr <<
"failing to write file " << filename <<
" try again after 1 second of sleep ... " << std::endl;
157 tr.Debug <<
"close file " <<
filename() << std::endl;
162 static time_t
const all_start_time = clock();
164 static time_t last_time;
166 if ( buf.size()==1 )
tr.Debug << -1.0*(all_start_time-ntime)/CLOCKS_PER_SEC <<
" " << 1.0*( ntime-last_time )/CLOCKS_PER_SEC <<
" seconds: write block of " << buf.begin()->size() <<
" characters to file " <<
filename() << std::endl;
167 else tr.Debug << -1.0*(all_start_time-ntime)/CLOCKS_PER_SEC <<
" " <<1.0*( ntime-last_time )/CLOCKS_PER_SEC <<
" seconds: write " << buf.size() <<
" blocks of data to file " <<
filename() << std::endl;
170 copy( buf.begin(), buf.end(), std::ostream_iterator< std::string>(
out_ ) );
171 if (
tr.Trace.visible() ) {
172 copy( buf.begin(), buf.end(), std::ostream_iterator< std::string>(
tr.Trace ) );
173 tr.Trace << std::endl;
179 tr.Debug <<
"block file " <<
filename() << std::endl;
182 tr.Debug <<
"open file (append): " <<
filename() << std::endl;