void BasePrefetchingDataLayer<Ftype, Btype>::InternalThreadEntryN(size_t thread_id) {
#ifndef CPU_ONLY
  const bool use_gpu_transform = this->is_gpu_transform();
#endif
  static thread_local bool iter0 = this->phase_ == TRAIN;
  if (iter0 && this->net_inititialized_flag_ != nullptr) {
    this->net_inititialized_flag_->wait();
  } else {  // nothing to wait -> initialize and start pumping
    std::lock_guard<std::mutex> lock(mutex_in_);
    InitializePrefetch();
    start_reading();
    iter0 = false;
  }
  try {
    while (!must_stop(thread_id)) {
      const size_t qid = this->queue_id(thread_id);
#ifndef CPU_ONLY
      shared_ptr<Batch<Ftype>> batch = prefetches_free_[qid]->pop();

      CHECK_EQ((size_t) -1, batch->id());
      load_batch(batch.get(), thread_id, qid);
      if (Caffe::mode() == Caffe::GPU) {
        if (!use_gpu_transform) {
          batch->data_.async_gpu_push();
        }
        if (this->output_labels_) {
          batch->label_.async_gpu_push();
        }
        CUDA_CHECK(cudaStreamSynchronize(Caffe::th_stream_aux(Caffe::STREAM_ID_ASYNC_PUSH)));
      }

      prefetches_full_[qid]->push(batch);
#else
      shared_ptr<Batch<Ftype>> batch = prefetches_free_[qid]->pop();
      load_batch(batch.get(), thread_id, qid);
      prefetches_full_[qid]->push(batch);
#endif

      if (iter0) {
        if (this->net_iteration0_flag_ != nullptr) {
          this->net_iteration0_flag_->wait();
        }
        std::lock_guard<std::mutex> lock(mutex_out_);
        if (this->net_inititialized_flag_ != nullptr) {
          this->net_inititialized_flag_ = nullptr;  // no wait on the second round
          InitializePrefetch();
          start_reading();
        }
        if (this->auto_mode_) {
          break;
        }  // manual otherwise, thus keep rolling
        iter0 = false;
      }
    }
  } catch (boost::thread_interrupted&) {
  }
}
// read - return last value measured by sensor
bool AP_CollisionAvoidance_PulsedLightLRF::get_reading(uint16_t &reading_cm)
{
    uint8_t buff[2];

    // get pointer to i2c bus semaphore
    AP_HAL::Semaphore* i2c_sem = hal.i2c->get_semaphore();

    // exit immediately if we can't take the semaphore
    if (i2c_sem == NULL || !i2c_sem->take(1)) {
        return false;
    }

    // read the high and low byte distance registers
    if (hal.i2c->readRegisters(AP_COLLISIONAVOIDANCE_PULSEDLIGHTLRF_ADDR,
                               AP_COLLISIONAVOIDANCE_PULSEDLIGHTLRF_DISTHIGH_REG, 2, &buff[0]) != 0) {
        i2c_sem->give();
        return false;
    }

    // combine results into distance
    reading_cm = ((uint16_t)buff[0]) << 8 | buff[1];

    // return semaphore
    i2c_sem->give();

    // kick off another reading for next time
    // To-Do: replace this with continuous mode
    hal.scheduler->delay_microseconds(200);
    start_reading();

    return true;
}
Exemple #3
0
static void drain_until_closed(iocpdesc_t *iocpd) {
  size_t max_drain = 16 * 1024;
  char buf[512];
  read_result_t *result = iocpd->read_result;
  while (result->drain_count < max_drain) {
    int rv = recv(iocpd->socket, buf, 512, 0);
    if (rv > 0)
      result->drain_count += rv;
    else if (rv == 0) {
      iocpd->read_closed = true;
      return;
    } else if (WSAGetLastError() == WSAEWOULDBLOCK) {
      // wait a little longer
      start_reading(iocpd);
      return;
    }
    else
      break;
  }
  // Graceful close indication unlikely, force the issue
  if (iocpd->iocp->iocp_trace)
    if (result->drain_count >= max_drain)
      iocp_log("graceful close on reader abandoned (too many chars)\n");
    else
      iocp_log("graceful close on reader abandoned: %d\n", WSAGetLastError());
  iocpd->read_closed = true;
}
Exemple #4
0
static void complete_connect(connect_result_t *result, HRESULT status)
{
  iocpdesc_t *iocpd = result->base.iocpd;
  if (iocpd->closing) {
    pn_free(result);
    reap_check(iocpd);
    return;
  }

  if (status) {
    iocpdesc_fail(iocpd, status, "Connect failure");
    // Posix sets selectable events as follows:
    pni_events_update(iocpd, PN_READABLE | PN_EXPIRED);
  } else {
    release_sys_sendbuf(iocpd->socket);
    if (setsockopt(iocpd->socket, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT,  NULL, 0)) {
      iocpdesc_fail(iocpd, WSAGetLastError(), "Internal connect failure (update context)");
    } else {
      pni_events_update(iocpd, PN_WRITABLE);
      start_reading(iocpd);
    }
  }
  pn_free(result);
  return;
}
// read - return last value measured by sensor
bool AP_RangeFinder_PulsedLightLRF::get_reading(uint16_t &reading_cm)
{
    be16_t val;

    if (!_dev->get_semaphore()->take(1)) {
        return false;
    }

    // read the high and low byte distance registers
    bool ret = _dev->read_registers(AP_RANGEFINDER_PULSEDLIGHTLRF_DISTHIGH_REG,
                                    (uint8_t *) &val, sizeof(val));
    _dev->get_semaphore()->give();

    if (!ret) {
        return false;
    }

    // combine results into distance
    reading_cm = be16toh(val);

    // kick off another reading for next time
    // To-Do: replace this with continuous mode
    hal.scheduler->delay_microseconds(200);
    start_reading();

    return true;
}
// read - return last value measured by sensor
bool AP_RangeFinder_MaxsonarI2CXL::get_reading(uint16_t &reading_cm)
{
    uint8_t buff[2];

    // get pointer to i2c bus semaphore
    AP_HAL::Semaphore* i2c_sem = hal.i2c->get_semaphore();

    // exit immediately if we can't take the semaphore
    if (i2c_sem == NULL || !i2c_sem->take(1)) {
        return false;
    }

    // take range reading and read back results
    if (hal.i2c->read(AP_RANGE_FINDER_MAXSONARI2CXL_DEFAULT_ADDR, 2, buff) != 0) {
        i2c_sem->give();
        return false;
    }
    i2c_sem->give();

    // combine results into distance
    reading_cm = ((uint16_t)buff[0]) << 8 | buff[1];

    // trigger a new reading
    start_reading();

    return true;
}
Exemple #7
0
reader_t &
analyzer_t::begin()
{
    if (!start_reading())
        return *trace_end;
    return *trace_iter;
}
/* 
   detect if a Maxbotix rangefinder is connected. We'll detect by
   trying to take a reading on I2C. If we get a result the sensor is
   there.
*/
bool AP_RangeFinder_MaxsonarI2CXL::detect(RangeFinder &_ranger, uint8_t instance)
{
    if (!start_reading()) {
        return false;
    }
    // give time for the sensor to process the request
    hal.scheduler->delay(50);
    uint16_t reading_cm;
    return get_reading(reading_cm);
}
/*
   detect if a PulsedLight rangefinder is connected. We'll detect by
   trying to take a reading on I2C. If we get a result the sensor is
   there.
*/
bool AP_CollisionAvoidance_PulsedLightLRF::detect(CollisionAvoidance &_ranger, uint8_t instance)
{
    if (!start_reading()) {
        return false;
    }
    // give time for the sensor to process the request
    hal.scheduler->delay(50);
    uint16_t reading_cm;
    return get_reading(reading_cm);
}
void tof_data_read_thread::start_reading(const QString &fileName)
{
    QSettings settings;
    settings.beginGroup("tof_data_read_thread");
    //QString filename = settings.value("config_file").toString();
    QString bg_filename = settings.value("bg_file", "background.dat").toString();
    settings.endGroup();

    start_reading(fileName,bg_filename);
}
void tof_data_read_thread::start_reading(const QString &fileName, const QString &bg_fileName){
    //QString fileName = "massListforZabbix.txt";
    this->m_config = ucc_factory::create_ucc_from_file(fileName);
    //qDebug()<<m_config.getElements().size();
    //m_config.setFilename(fileName);
    if(m_config.getElements().size() >0){
        m_config.setFilename(fileName);
        m_config.setBg_filename(bg_fileName);
        start_reading(m_config);
    }
}
Exemple #12
0
void pni_iocpdesc_start(iocpdesc_t *iocpd)
{
  if (iocpd->bound) return;
  bind_to_completion_port(iocpd);
  if (is_listener(iocpd)) {
    begin_accept(iocpd->acceptor, NULL);
  }
  else {
    release_sys_sendbuf(iocpd->socket);
    pni_events_update(iocpd, PN_WRITABLE);
    start_reading(iocpd);
  }
}
Exemple #13
0
bool
analyzer_t::run()
{
    bool res = true;
    if (!start_reading())
        return false;

    for (; *trace_iter != *trace_end; ++(*trace_iter)) {
        for (int i = 0; i < num_tools; ++i) {
            memref_t memref = **trace_iter;
            res = tools[i]->process_memref(memref) && res;
        }
    }
    return res;
}
    void connect( const std::wstring &address )
    {
        HANDLE pipe = CreateFileW( address.c_str( ),
                                   GENERIC_WRITE | GENERIC_READ,
                                   0,
                                   NULL, OPEN_EXISTING,
                                   FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
                                   NULL);

        if(INVALID_HANDLE_VALUE == pipe) {
            throw vtrc::common::exception( GetLastError( ),
                                           rpc::errors::CATEGORY_SYSTEM );
        } else {
            get_socket( ).assign( pipe );
        }
        connection_setup( );
        start_reading( );
    }
Exemple #15
0
bool
analyzer_t::run()
{
    // XXX i#3286: Add a %-completed progress message by looking at the file sizes.
    if (!parallel) {
        if (!start_reading())
            return false;
        for (; *serial_trace_iter != *trace_end; ++(*serial_trace_iter)) {
            for (int i = 0; i < num_tools; ++i) {
                memref_t memref = **serial_trace_iter;
                // We short-circuit and exit on an error to avoid confusion over
                // the results and avoid wasted continued work.
                if (!tools[i]->process_memref(memref)) {
                    error_string = tools[i]->get_error_string();
                    return false;
                }
            }
        }
        return true;
    }
    if (worker_count <= 0) {
        error_string = "Invalid worker count: must be > 0";
        return false;
    }
    std::vector<std::thread> threads;
    VPRINT(this, 1, "Creating %d worker threads\n", worker_count);
    threads.reserve(worker_count);
    for (int i = 0; i < worker_count; ++i) {
        threads.emplace_back(
            std::thread(&analyzer_t::process_tasks, this, &worker_tasks[i]));
    }
    for (std::thread &thread : threads)
        thread.join();
    for (auto &tdata : thread_data) {
        if (!tdata.error.empty()) {
            error_string = tdata.error;
            return false;
        }
    }
    return true;
}
Exemple #16
0
static void accept_completed(BOOL resultOk, DWORD length,
   SocketState* socketState, WSAOVERLAPPED* ovl)
{
   SocketState* newSocketState;

   if (resultOk)
   {
      printf("* new connection created\n");

      // "updates the context" (whatever that is...)
	  // for inheritting info of local and remote addresses
      setsockopt(socketState->socket, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, 
         (char *)&listener, sizeof(listener));

      // associates new socket with completion port
      newSocketState = new_socket_state();
      newSocketState->socket = socketState->socket;
      if (CreateIoCompletionPort((HANDLE)newSocketState->socket, cpl_port,
         (ULONG_PTR)newSocketState, 0) != cpl_port)
      {
         int err = WSAGetLastError();
         printf("* error %d in CreateIoCompletionPort in line %d\n", err, __LINE__);
         exit(1);
      }

      // starts receiving from the new connection
      start_reading(newSocketState, new_overlapped());

      // starts waiting for another connection request
      start_accepting();
   }

   else // !resultOk
   {
      int err = GetLastError();
      printf("* error (%d,%d) in accept, cleaning up and retrying!!!", err,
         length);
      closesocket(socketState->socket);
      start_accepting();
   }
}
// read - return last value measured by sensor
bool AP_RangeFinder_MaxsonarI2CXL::get_reading(uint16_t &reading_cm)
{
    be16_t val;

    // exit immediately if we can't take the semaphore
    if (!_dev->get_semaphore()->take(1)) {
        return false;
    }

    // take range reading and read back results
    bool ret = _dev->transfer(nullptr, 0, (uint8_t *) &val, sizeof(val));
    _dev->get_semaphore()->give();

    if (ret) {
        // combine results into distance
        reading_cm = be16toh(val);

        // trigger a new reading
        start_reading();
    }

    return ret;
}
Exemple #18
0
static void write_completed(BOOL resultOk, DWORD length,
   SocketState* socketState, WSAOVERLAPPED* ovl)
{
   if (resultOk)
   {
      if (length > 0)
      {
         printf("* write operation completed\n");
         start_reading(socketState, ovl); // starts another read
      }
      else // length == 0 (strange!)
      {
         printf("* connection closed by client!\n");
         destroy_connection(socketState, ovl);
      }
   }
   else // !resultOk, assumes connection was reset
   {
      int err = GetLastError();
      printf("* error %d on send, assuming connection was reset!\n");
      destroy_connection(socketState, ovl);
   }
}
tof_data_read_thread::tof_data_read_thread(QObject *par) :
    QThread(par),
    mutex(),
    condition(),
    m_config(),
    restart(false),
    pause(false),
    abort(false),
    bg_meas(false)
{
    restart = false;
    abort = false;
    pause = false;

    QSettings settings;
    settings.beginGroup("tof_data_read_thread");
    QString filename = settings.value("config_file").toString();
    QString bg_filename = settings.value("bg_file", "background.dat").toString();
    settings.endGroup();

    if(!filename.isEmpty())
        start_reading(filename,bg_filename);

}
Exemple #20
0
// read - return last value measured by sensor
int AP_RangeFinder_LR4::read() {
    static uint8_t pos = 0;
    static char buf[5];
    static int val = 0;
    char tmp;
    
    // attempt to start reading if not reading
    if (!reading) {
        if (_port->available()) {
            tmp = _port->read();

            // receiving 'ok'
            if (tmp == 'o') {
                reading = true;
            }
        }
        else {
            reading = start_reading();
        }
    }

    // currently reading
    if (reading) {
        while (_port->available()) {
            tmp = _port->read();

            // if it is a digit
            if (tmp >= '0' && tmp <= '9') {
                buf[pos++] = tmp;
            }
            else if (tmp == '\r') {
                if (pos >= 5) {
                    // complete 5-digit integer received, quit receive loop
                    break;
                }
                else {
                    // received incomplete integer, reset position
                    pos = 0;
                }
            }
        }

        if (pos >= 5) {
            // convert result from string to integer, then divide by 10 for cm
            raw_value = atoi(buf) / 10;

            // ensure distance is within min and max
            val = constrain_float(raw_value, min_distance, max_distance);

            // apply mode (median?) filter?
            // val = _mode_filter->apply(val);
            
            // reading = !stop_reading();

            // reset position
            pos = 0;

            return val;
        }
    }

    // failed to get reading, return previous value
    return val;
}
void ImportBase::read_file( int process_header )
{
  //Assert the file name was set and the graph is not null
  assert( this->filename != "" && pgraph != 0 );
  start_reading( process_header );
}
void tof_data_read_thread::start_reading()
{
    if(m_config.getElements().size() >0){
        start_reading(m_config);
    }
}