예제 #1
0
파일: acceptor.hpp 프로젝트: viboes/hpx
 void irecv_header()
 {
     /*
     HPX_ASSERT(source_ != -1);
     HPX_ASSERT(source_ != util::mpi_environment::rank());
     */
     MPI_Irecv(
         header_.data(), // data pointer
         header_.data_size_,     // number of elements
         header_.type(), // MPI Datatype
         MPI_ANY_SOURCE,         // Source
         0,              // Tag
         communicator_,  // Communicator
         &request_);     // Request
 }
예제 #2
0
 bool readpacket() // watch performance, if bad, optimize with chunk reading
 {
     std::size_t offset = 0;
     if( !m_synchronized )
     {
         bool first = true;
         while( offset < 4 ) // watch performance, if bad, optimize with chunk reading
         {
             m_istream->read( &m_buf[offset], 1 );
             if( first && m_istream->eof() ) { return false; }
             first = false;
             if( m_istream->eof() || m_istream->bad() ) { COMMA_THROW( comma::exception, "failed to synchronize, bad stream" ); }
             unsigned char header_byte = reinterpret_cast<const unsigned char*>( m_header->data() )[offset];
             offset = header_byte == header::sentinel_value[offset] ? offset + 1 : 0;
         }
         m_synchronized = true; // quick and dirty: synchronize once, should be enough
     }
     m_istream->read( &m_buf[0] + offset, header::size - offset );
     if( m_istream->gcount() == 0 ) { return false; }
     if( m_istream->eof() || m_istream->bad() || m_istream->gcount() != static_cast< int >( header::size - offset ) ) { COMMA_THROW( comma::exception, "failed to read packet header" ); }
     if( !m_header->valid() )
     {
         m_synchronized = false;
         COMMA_THROW( comma::exception, "invalid header (stream from laser went out of sync)" );
     }
     const std::size_t size = m_header->payload_size();
     if( m_buf.size() < size + header::size )
     {
         m_buf.resize( size + header::size );
         m_header = reinterpret_cast< const header* >( &m_buf[0] );
         m_payload = &m_buf[0] + header::size;
     }
     m_istream->read( m_payload, size ); // todo: check payload size so that it does not go crazy?
     if( m_istream->eof() ) { COMMA_THROW( comma::exception, "failed to read payload of size " << m_header->payload_size() << ", end of file" ); }
     if( m_istream->bad() || m_istream->gcount() != int( size ) ) { COMMA_THROW( comma::exception, "failed to read payload of size " << m_header->payload_size() << ", bad stream" ); }
     if( m_header->type() == header::fault_type ) { m_fault = *( reinterpret_cast< fault* >( m_payload ) ); }
     return true;
 }
예제 #3
0
    /**
     * Saves the given matrix to the given stream. The header is modified
     * with appropriate data derived from the matrix structure.
     */
    bool saveMatrix(const std::string& filename, 
                    const matrix<T>& theChannel,
                    header& theHeader, 
                    const eCompressionType compr,
                    dataCodec* codec) {

      //open stream
      std::ofstream out;
      out.open(filename.c_str(),std::ios::out|std::ios::binary);

      //stream ok?
      if (!(out.good() && out.is_open())) {
        std::string str = "Could not open file " + filename + " for writing.";
        setStatusString(str.c_str());
        out.close();
        return false;
      }

      theHeader.encoding.contents=getTypeCode(T());
      theHeader.encoding.compression=compr;
      
      int tmpsize=sizeof(T)*theChannel.rows()*theChannel.columns();
      int encsize = codec->estimateEncodedSize(tmpsize);
      
      // create the temporary buffer
      dataCodec::buffer tmp(encsize);
      
      // encode the data
      codec->encode((const ubyte*)&theChannel.at(0,0),tmpsize,&tmp.at(0),encsize);
      
      theHeader.size = encsize;
      theHeader.rows = theChannel.rows();
      theHeader.columns = theChannel.columns();
    
      // write the header
      if (!theHeader.write(out)) {
        setStatusString("Could not write header.");
        out.close();
        return false;
      }

      // write the data
      out.write((const char*)(&tmp.at(0)),theHeader.size);
      out.close();
      
      return true;
    }
예제 #4
0
 typename command::response readresponse()
 {
     while( true )
     {
         if( !readpacket() ) { COMMA_THROW( comma::exception, "expected command response, got end of stream" ); }
         switch( m_header->type() )
         {
             case header::scan_type:
             case header::fault_type: // cannot throw yet, since need to get response first
                 break;
             case header::response_type:
             {
                 unsigned int id = reinterpret_cast< const commands::response_header* >( m_payload )->id() & 0x3fff;
                 if( int( id ) != *m_commandId ) { COMMA_THROW( comma::exception, "expected response to command 0x" << std::hex << *m_commandId << ", got 0x" << id << std::dec ); }
                 m_commandId.reset();
                 return *( reinterpret_cast< typename command::response* >( m_payload ) );
             }
             default:
                 COMMA_THROW( comma::exception, "expected command response, got packet of unknown type (0x" << std::hex << m_header->type() << std::dec );
         }
     }
 }
예제 #5
0
    /**
     * Loads a matrix from the given stream. The data are written
     * into theChannel, the meta-data are written into theHeader
     * These methods ignore all the parameters in this objects.
     * The only reason they are not static is that they modify
     * the status string.
     */
    bool loadMatrix(const std::string& filename, matrix<T>& theChannel,
                    header& theHeader) {
      theChannel.clear();
      
      //open stream
      std::ifstream in;
      in.open(filename.c_str(),std::ios::in|std::ios::binary);

      //open failed?
      if (!(in.good() && in.is_open())) {
        std::string str = "Could not open file " + filename;
        setStatusString(str.c_str());
        return false;
      }

      // read header
      if (!theHeader.read(in)) {
        setStatusString("Wrong header. Is this an LTI file?");
        return false;
      }

     
      //create the right codec for this file
      dataCodec* codec=0;

      switch (theHeader.encoding.compression) {
        case None: 
          codec=new identityCodec();
          break;
#if HAVE_LIBZ
        case Flate:
          codec=new flateCodec();
          break;
#endif
        case RunLength:
          codec=new runLengthCodec();
          break;
        default: 
          delete codec;
          setStatusString("Unknown codec of libz not installed");
          return false;
      }
      
      
      _lti_debug2("type: " << theHeader.type << std::endl);
      _lti_debug2("encoding.contents: " << theHeader.encoding.contents << std::endl);
      _lti_debug2("encoding.compression: " << theHeader.encoding.compression << std::endl);
      _lti_debug2("size: " << theHeader.size << std::endl);
      _lti_debug2("rows: " << theHeader.rows << std::endl);
      _lti_debug2("columns: " << theHeader.columns << std::endl);
      
      if (theHeader.encoding.compression == None && theHeader.size != 
          (sizeof(T)*theHeader.rows*theHeader.columns)) {
        setStatusString("Inconsistent header.  Wrong file size");
        delete codec;
        return false;
      }
      
      if (theHeader.encoding.contents != getTypeCode(T())) {
        bool flag=true;
        std::string str="Inconsistent file type. ";
        str+=theChannel.getTypeName();
        str+" expected but ";
        // should we really try to recover from type errors???
        // the following compiles, but might not get you what you expect
        switch(theHeader.encoding.contents) {
          case 'b':
            {
              str+=" lti::matrix<ubyte> found.";
              channel8 tmp;
              ioLTIworker<channel8::value_type> worker;
              if (!(flag=worker.loadBody(in,tmp,theHeader,codec))) {
                setStatusString(worker.getStatusString());
              }
              theChannel.castFrom(tmp);
              break;
            }
          case 'i':
            {
              str+=" lti::imatrix found.";
              imatrix tmp;
              ioLTIworker<imatrix::value_type> worker;
              if (!(flag=worker.loadBody(in,tmp,theHeader,codec))) {
                setStatusString(worker.getStatusString());
              }
              theChannel.castFrom(tmp);
              break;
            }
          case 'f':
            {
              str+=" lti::channel found.";
              flag=false;
              //           channel tmp;
              //           flag=loadBody(in,tmp);
              //           // this generates a warning and will probabily generate funny 
              //           // images or channels
              //           theChannel.castFrom(tmp);
              break;
            }
          case 'c':
            {
              // no chance: Forcing an image to something else is task-dependent
              str+=" lti::image found.";
              flag=false;
              //           image tmp;
              //           flag=loadBody(in,tmp);
              //           theChannel.castFrom(tmp);
              break;
            }
          default:
            str+=" unknown type found.";
            flag=false;
        }
        setStatusString(str.c_str());
        in.close();
        delete codec;
        return flag;
      } else {
        bool flag=loadBody(in,theChannel,theHeader,codec);
        in.close();
        delete codec;
        return flag;
      }
      
    }