std::vector<char>& abstract_broker::wr_buf(connection_handle hdl) {
  auto x = by_id(hdl);
  if (!x) {
    CAF_LOG_ERROR("tried to access wr_buf() of an unknown connection_handle");
    return dummy_wr_buf_;
  }
  return x->wr_buf();
}
continue_helper& continue_helper::continue_with(behavior::continuation_fun f) {
  auto ref_opt = m_self->sync_handler(m_mid);
  if (ref_opt) {
    behavior cpy = *ref_opt;
    *ref_opt = cpy.add_continuation(std::move(f));
  } else {
    CAF_LOG_ERROR("failed to add continuation");
  }
  return *this;
}
bool udp::write_datagram(size_t& result, native_socket fd, void* buf,
                         size_t buf_len, const ip_endpoint& ep) {
  CAF_LOG_TRACE(CAF_ARG(fd) << CAF_ARG(buf_len));
  socket_size_type len = static_cast<socket_size_type>(*ep.clength());
  auto sres = ::sendto(fd, reinterpret_cast<io::network::socket_send_ptr>(buf),
                       buf_len, 0, ep.caddress(), len);
  if (is_error(sres, true)) {
    CAF_LOG_ERROR("sendto returned" << CAF_ARG(sres));
    return false;
  }
  result = (sres > 0) ? static_cast<size_t>(sres) : 0;
  return true;
}
bool udp::read_datagram(size_t& result, native_socket fd, void* buf,
                        size_t buf_len, ip_endpoint& ep) {
  CAF_LOG_TRACE(CAF_ARG(fd));
  memset(ep.address(), 0, sizeof(sockaddr_storage));
  socket_size_type len = sizeof(sockaddr_storage);
  auto sres = ::recvfrom(fd, static_cast<io::network::socket_recv_ptr>(buf),
                         buf_len, 0, ep.address(), &len);
  if (is_error(sres, true)) {
    CAF_LOG_ERROR("recvfrom returned" << CAF_ARG(sres));
    return false;
  }
  if (sres == 0)
    CAF_LOG_INFO("Received empty datagram");
  else if (sres > static_cast<signed_size_type>(buf_len))
    CAF_LOG_WARNING("recvfrom cut of message, only received "
                    << CAF_ARG(buf_len) << " of " << CAF_ARG(sres) << " bytes");
  result = (sres > 0) ? static_cast<size_t>(sres) : 0;
  *ep.length() = static_cast<size_t>(len);
  return true;
}
Exemple #5
0
void basp_broker_state::learned_new_node(const node_id& nid) {
  CAF_LOG_TRACE(CAF_ARG(nid));
  if (spawn_servers.count(nid) > 0) {
    CAF_LOG_ERROR("learned_new_node called for known node " << CAF_ARG(nid));
    return;
  }
  auto tmp = system().spawn<hidden>([=](event_based_actor* tself) -> behavior {
    CAF_LOG_TRACE("");
    // terminate when receiving a down message
    tself->set_down_handler([=](down_msg& dm) {
      CAF_LOG_TRACE(CAF_ARG(dm));
      tself->quit(std::move(dm.reason));
    });
    // skip messages until we receive the initial ok_atom
    tself->set_default_handler(skip);
    return {
      [=](ok_atom, const std::string& /* key == "info" */,
          const strong_actor_ptr& config_serv, const std::string& /* name */) {
        CAF_LOG_TRACE(CAF_ARG(config_serv));
        // drop unexpected messages from this point on
        tself->set_default_handler(print_and_drop);
        if (!config_serv)
          return;
        tself->monitor(config_serv);
        tself->become(
          [=](spawn_atom, std::string& type, message& args)
          -> delegated<strong_actor_ptr, std::set<std::string>> {
            CAF_LOG_TRACE(CAF_ARG(type) << CAF_ARG(args));
            tself->delegate(actor_cast<actor>(std::move(config_serv)),
                                 get_atom::value, std::move(type),
                                 std::move(args));
            return {};
          }
        );
      },
      after(std::chrono::minutes(5)) >> [=] {
        CAF_LOG_INFO("no spawn server found:" << CAF_ARG(nid));
        tself->quit();
      }
    };
  });
void opencl_metainfo::initialize() {
  // get number of available platforms
  auto num_platforms = v1get<cl_uint>(CAF_CLF(clGetPlatformIDs));
  // get platform ids
  std::vector<cl_platform_id> platforms(num_platforms);
  v2callcl(CAF_CLF(clGetPlatformIDs), num_platforms, platforms.data());
  if (platforms.empty()) {
    throw std::runtime_error("no OpenCL platform found");
  }
  // support multiple platforms -> "for (auto platform : platforms)"?
  auto platform = platforms.front();
  // detect how many devices we got
  cl_uint num_devs = 0;
  cl_device_type dev_type = CL_DEVICE_TYPE_GPU;
  // try get some GPU devices and try falling back to CPU devices on error
  try {
    num_devs = v1get<cl_uint>(CAF_CLF(clGetDeviceIDs), platform, dev_type);
  }
  catch (std::runtime_error&) {
    dev_type = CL_DEVICE_TYPE_CPU;
    num_devs = v1get<cl_uint>(CAF_CLF(clGetDeviceIDs), platform, dev_type);
  }
  // get available devices
  std::vector<cl_device_id> ds(num_devs);
  v2callcl(CAF_CLF(clGetDeviceIDs), platform, dev_type, num_devs, ds.data());
  std::vector<device_ptr> devices(num_devs);
  // lift raw pointer as returned by OpenCL to C++ smart pointers
  auto lift = [](cl_device_id ptr) { return device_ptr{ptr, false}; };
  std::transform(ds.begin(), ds.end(), devices.begin(), lift);
  // create a context
  context_.reset(v2get(CAF_CLF(clCreateContext), nullptr, num_devs, ds.data(),
                        pfn_notify, nullptr),
                  false);
  for (auto& device : devices) {
    CAF_LOG_DEBUG("creating command queue for device(s)");
    command_queue_ptr cmd_queue;
    try {
      cmd_queue.reset(v2get(CAF_CLF(clCreateCommandQueue),
                            context_.get(), device.get(),
                            unsigned{CL_QUEUE_PROFILING_ENABLE}),
                      false);
    }
    catch (std::runtime_error&) {
      CAF_LOG_DEBUG("unable to create command queue for device");
    }
    if (cmd_queue) {
      auto max_wgs = v3get<size_t>(CAF_CLF(clGetDeviceInfo), device.get(),
                      unsigned{CL_DEVICE_MAX_WORK_GROUP_SIZE});
      auto max_wid = v3get<cl_uint>(CAF_CLF(clGetDeviceInfo), device.get(),
                      unsigned{CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS});
      dim_vec max_wi_per_dim(max_wid);
      v2callcl(CAF_CLF(clGetDeviceInfo), device.get(),
             unsigned{CL_DEVICE_MAX_WORK_ITEM_SIZES},
             sizeof(size_t) * max_wid,
             max_wi_per_dim.data());
      devices_.push_back(device_info{std::move(device), std::move(cmd_queue),
                                      max_wgs, max_wid, max_wi_per_dim});
    }
  }
  if (devices_.empty()) {
    std::string errstr = "could not create a command queue for any device";
    CAF_LOG_ERROR(errstr);
    throw std::runtime_error(std::move(errstr));
  }
}