<commit_before>//  Copyright [2015] <kangic21@gmail.com>

#include "io_manager.h"
#include "event/event_dispatcher.h"

namespace eznetpp {
namespace sys {

io_manager::io_manager(int num_of_disp_threads, bool log_enable) {
  _num_of_disp_threads = num_of_disp_threads;
  eznetpp::util::logger::instance().set_enable_option(log_enable);
}

io_manager::~io_manager(void) {
}

/*
 * create epoll file descriptor and events
 */
int io_manager::init(int max_descs_cnt) {
  // create epoll fd and event structures
  _epoll_fd = epoll_create(max_descs_cnt);
  if (_epoll_fd == -1) {
    eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::error
                          , __FILE__, __FUNCTION__, __LINE__
                          , "epoll_create error(%d)"
                          , errno);

    return errno;
  }

  _events = new epoll_event[max_descs_cnt];
  if (_events == nullptr) {
    eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::error
                          , __FILE__, __FUNCTION__, __LINE__
                          , "failed to create events(%d)"
                          , errno);

    return errno;
  }

  eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::debug
                        , __FILE__, __FUNCTION__, __LINE__
                        , "created epoll_fd : %d"
                        , _epoll_fd);

  _max_descs_cnt = max_descs_cnt;

  return 0;

}

int io_manager::register_socket_event_handler(eznetpp::net::socket* sock
      , eznetpp::event::event_handler* handler) {
  // Find the socket class in the event_dispatcher's handlers map to check
  // to register already.
  if (!eznetpp::event::event_dispatcher::instance().register_socket_event_handler(sock, handler)) {
    return -1;
  }

  struct epoll_event ev;

  ev.events = EPOLLIN | EPOLLET; // | EPOLLONESHOT;

  ev.data.ptr = sock;

  return epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, sock->descriptor(), &ev);
}

int io_manager::deregister_socket_event_handler(eznetpp::net::socket* sock) {
  eznetpp::event::event_dispatcher::instance().deregister_socket_event_handler(sock);
  return epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, sock->descriptor(), NULL);
}

int io_manager::loop(void) {
  if (eznetpp::event::event_dispatcher::instance().init(_num_of_disp_threads) == -1) {
    return -1;
  }

  _loop_th = std::thread(&io_manager::epoll_loop, this);

  if (!_loop_th.joinable()) {
    eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::error
                           , __FILE__, __FUNCTION__, __LINE__
                           , "failed to create read thread(%d)"
                           , errno);
    return -1;
  }

  _loop_th.join();

  return 0;
}

void io_manager::epoll_loop(void) {
  eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::debug
                         , __FILE__, __FUNCTION__, __LINE__
                         , "start read_loop");

  while (1) {
    int changed_events = epoll_wait(_epoll_fd, _events, _max_descs_cnt, -1);
    eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::debug
                          , __FILE__, __FUNCTION__, __LINE__
                          , "changed_events : %d"
                          , changed_events);

    if (changed_events < 0) {
      eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::debug
                          , __FILE__, __FUNCTION__, __LINE__
                          , "epoll failed");

      // TODO : decide to the action
      break;
    }

    for (int i = 0; i < changed_events; ++i) {
      eznetpp::net::socket* sock = reinterpret_cast<eznetpp::net::socket*>(_events[i].data.ptr);

      eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::debug
                          , __FILE__, __FUNCTION__, __LINE__
                          , "changed descriptor : %d"
                          , sock->descriptor());

      if (_events[i].events & EPOLLIN) {
        sock->recv();
      } else if (_events[i].events & EPOLLHUP || _events[i].events & EPOLLERR) {
        // TODO : add log
        printf("epollhup or epollerr\n");
        sock->close();
      }
    }
  }
}

}  // namespace sys
}  // namespace eznetpp
<commit_msg>* change the casting statement   - reinteret_cast -> static_cast<commit_after>//  Copyright [2015] <kangic21@gmail.com>

#include "io_manager.h"
#include "event/event_dispatcher.h"

namespace eznetpp {
namespace sys {

io_manager::io_manager(int num_of_disp_threads, bool log_enable) {
  _num_of_disp_threads = num_of_disp_threads;
  eznetpp::util::logger::instance().set_enable_option(log_enable);
}

io_manager::~io_manager(void) {
}

/*
 * create epoll file descriptor and events
 */
int io_manager::init(int max_descs_cnt) {
  // create epoll fd and event structures
  _epoll_fd = epoll_create(max_descs_cnt);
  if (_epoll_fd == -1) {
    eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::error
                          , __FILE__, __FUNCTION__, __LINE__
                          , "epoll_create error(%d)"
                          , errno);

    return errno;
  }

  _events = new epoll_event[max_descs_cnt];
  if (_events == nullptr) {
    eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::error
                          , __FILE__, __FUNCTION__, __LINE__
                          , "failed to create events(%d)"
                          , errno);

    return errno;
  }

  eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::debug
                        , __FILE__, __FUNCTION__, __LINE__
                        , "created epoll_fd : %d"
                        , _epoll_fd);

  _max_descs_cnt = max_descs_cnt;

  return 0;

}

int io_manager::register_socket_event_handler(eznetpp::net::socket* sock
      , eznetpp::event::event_handler* handler) {
  // Find the socket class in the event_dispatcher's handlers map to check
  // to register already.
  if (!eznetpp::event::event_dispatcher::instance().register_socket_event_handler(sock, handler)) {
    return -1;
  }

  struct epoll_event ev;

  ev.events = EPOLLIN | EPOLLET; // | EPOLLONESHOT;

  ev.data.ptr = sock;

  return epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, sock->descriptor(), &ev);
}

int io_manager::deregister_socket_event_handler(eznetpp::net::socket* sock) {
  eznetpp::event::event_dispatcher::instance().deregister_socket_event_handler(sock);
  return epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, sock->descriptor(), NULL);
}

int io_manager::loop(void) {
  if (eznetpp::event::event_dispatcher::instance().init(_num_of_disp_threads) == -1) {
    return -1;
  }

  _loop_th = std::thread(&io_manager::epoll_loop, this);

  if (!_loop_th.joinable()) {
    eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::error
                           , __FILE__, __FUNCTION__, __LINE__
                           , "failed to create read thread(%d)"
                           , errno);
    return -1;
  }

  _loop_th.join();

  return 0;
}

void io_manager::epoll_loop(void) {
  eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::debug
                         , __FILE__, __FUNCTION__, __LINE__
                         , "start read_loop");

  while (1) {
    int changed_events = epoll_wait(_epoll_fd, _events, _max_descs_cnt, -1);
    eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::debug
                          , __FILE__, __FUNCTION__, __LINE__
                          , "changed_events : %d"
                          , changed_events);

    if (changed_events < 0) {
      eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::debug
                          , __FILE__, __FUNCTION__, __LINE__
                          , "epoll failed");

      // TODO : decide to the action
      break;
    }

    for (int i = 0; i < changed_events; ++i) {
      eznetpp::net::socket* sock = static_cast<eznetpp::net::socket*>(_events[i].data.ptr);

      eznetpp::util::logger::instance().log(eznetpp::util::logger::log_level::debug
                          , __FILE__, __FUNCTION__, __LINE__
                          , "changed descriptor : %d"
                          , sock->descriptor());

      if (_events[i].events & EPOLLIN) {
        sock->recv();
      } else if (_events[i].events & EPOLLHUP || _events[i].events & EPOLLERR) {
        // TODO : add log
        printf("epollhup or epollerr\n");
        sock->close();
      }
    }
  }
}

}  // namespace sys
}  // namespace eznetpp
<|endoftext|>


<commit_before><commit_msg>innodb: remove recv_sys_mem_free - unused<commit_after><|endoftext|>


<commit_before>// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#define EIGEN_USE_THREADS

#include "main.h"
#include <iostream>
#include <Eigen/CXX11/Tensor>

using Eigen::Tensor;

static void test_multithread_elementwise()
{
  Tensor<float, 3> in1(2,3,7);
  Tensor<float, 3> in2(2,3,7);
  Tensor<float, 3> out(2,3,7);

  in1.setRandom();
  in2.setRandom();

  Eigen::ThreadPoolDevice thread_pool_device(internal::random<int>(3, 11));
  out.device(thread_pool_device) = in1 + in2 * 3.14f;

  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 7; ++k) {
        VERIFY_IS_APPROX(out(i,j,k), in1(i,j,k) + in2(i,j,k) * 3.14f);
      }
    }
  }
}

static void test_multithread_compound_assignment()
{
  Tensor<float, 3> in1(2,3,7);
  Tensor<float, 3> in2(2,3,7);
  Tensor<float, 3> out(2,3,7);

  in1.setRandom();
  in2.setRandom();

  Eigen::ThreadPoolDevice thread_pool_device(internal::random<int>(3, 11));
  out.device(thread_pool_device) = in1;
  out.device(thread_pool_device) += in2 * 3.14f;

  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 7; ++k) {
        VERIFY_IS_APPROX(out(i,j,k), in1(i,j,k) + in2(i,j,k) * 3.14f);
      }
    }
  }
}

template<int DataLayout>
static void test_multithread_contraction()
{
  Tensor<float, 4, DataLayout> t_left(30, 50, 37, 31);
  Tensor<float, 5, DataLayout> t_right(37, 31, 70, 2, 10);
  Tensor<float, 5, DataLayout> t_result(30, 50, 70, 2, 10);

  t_left.setRandom();
  t_right.setRandom();

  // this contraction should be equivalent to a single matrix multiplication
  typedef Tensor<float, 1>::DimensionPair DimPair;
  Eigen::array<DimPair, 2> dims({{DimPair(2, 0), DimPair(3, 1)}});

  typedef Map<Matrix<float, Dynamic, Dynamic, DataLayout>> MapXf;
  MapXf m_left(t_left.data(), 1500, 1147);
  MapXf m_right(t_right.data(), 1147, 1400);
  Matrix<float, Dynamic, Dynamic, DataLayout> m_result(1500, 1400);

  Eigen::ThreadPoolDevice thread_pool_device(4);

  // compute results by separate methods
  t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
  m_result = m_left * m_right;

 for (ptrdiff_t i = 0; i < t_result.size(); i++) {
    VERIFY(&t_result.data()[i] != &m_result.data()[i]);
    if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
      std::cout << "mismatch detected: " << t_result.data()[i] << " vs " <<  m_result.data()[i] << std::endl;
      assert(false);
    }
  }
}

template<int DataLayout>
static void test_contraction_corner_cases()
{
  Tensor<float, 2, DataLayout> t_left(32, 500);
  Tensor<float, 2, DataLayout> t_right(32, 28*28);
  Tensor<float, 2, DataLayout> t_result(500, 28*28);

  t_left = (t_left.constant(-0.5f) + t_left.random()) * 2.0f;
  t_right = (t_right.constant(-0.6f) + t_right.random()) * 2.0f;
  t_result = t_result.constant(NAN);

  // this contraction should be equivalent to a single matrix multiplication
  typedef Tensor<float, 1>::DimensionPair DimPair;
  Eigen::array<DimPair, 1> dims{{DimPair(0, 0)}};

  typedef Map<Matrix<float, Dynamic, Dynamic, DataLayout>> MapXf;
  MapXf m_left(t_left.data(), 32, 500);
  MapXf m_right(t_right.data(), 32, 28*28);
  Matrix<float, Dynamic, Dynamic, DataLayout> m_result(500, 28*28);

  Eigen::ThreadPoolDevice thread_pool_device(12);

  // compute results by separate methods
  t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
  m_result = m_left.transpose() * m_right;

  for (ptrdiff_t i = 0; i < t_result.size(); i++) {
    assert(!isnan(t_result.data()[i]));
    if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
      std::cout << "mismatch detected at index " << i << " : " << t_result.data()[i] << " vs " <<  m_result.data()[i] << std::endl;
      assert(false);
    }
  }

  t_left.resize(32, 1);
  t_left = (t_left.constant(-0.5f) + t_left.random()) * 2.0f;
  t_result.resize (1, 28*28);
  t_result = t_result.constant(NAN);
  t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
  new(&m_left) MapXf(t_left.data(), 32, 1);
  m_result = m_left.transpose() * m_right;
  for (ptrdiff_t i = 0; i < t_result.size(); i++) {
    assert(!isnan(t_result.data()[i]));
    if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
      std::cout << "mismatch detected: " << t_result.data()[i] << " vs " <<  m_result.data()[i] << std::endl;
      assert(false);
    }
  }

  t_left.resize(32, 500);
  t_right.resize(32, 4);
  t_left = (t_left.constant(-0.5f) + t_left.random()) * 2.0f;
  t_right = (t_right.constant(-0.6f) + t_right.random()) * 2.0f;
  t_result.resize (500, 4);
  t_result = t_result.constant(NAN);
  t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
  new(&m_left) MapXf(t_left.data(), 32, 500);
  new(&m_right) MapXf(t_right.data(), 32, 4);
  m_result = m_left.transpose() * m_right;
  for (ptrdiff_t i = 0; i < t_result.size(); i++) {
    assert(!isnan(t_result.data()[i]));
    if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
      std::cout << "mismatch detected: " << t_result.data()[i] << " vs " <<  m_result.data()[i] << std::endl;
      assert(false);
    }
  }

  t_left.resize(32, 1);
  t_right.resize(32, 4);
  t_left = (t_left.constant(-0.5f) + t_left.random()) * 2.0f;
  t_right = (t_right.constant(-0.6f) + t_right.random()) * 2.0f;
  t_result.resize (1, 4);
  t_result = t_result.constant(NAN);
  t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
  new(&m_left) MapXf(t_left.data(), 32, 1);
  new(&m_right) MapXf(t_right.data(), 32, 4);
  m_result = m_left.transpose() * m_right;
  for (ptrdiff_t i = 0; i < t_result.size(); i++) {
    assert(!isnan(t_result.data()[i]));
    if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
      std::cout << "mismatch detected: " << t_result.data()[i] << " vs " <<  m_result.data()[i] << std::endl;
      assert(false);
    }
  }
}

template<int DataLayout>
static void test_multithread_contraction_agrees_with_singlethread() {
  int contract_size = internal::random<int>(1, 5000);

  Tensor<float, 3, DataLayout> left(internal::random<int>(1, 80),
                                    contract_size,
                                    internal::random<int>(1, 100));

  Tensor<float, 4, DataLayout> right(internal::random<int>(1, 25),
                                     internal::random<int>(1, 37),
                                     contract_size,
                                     internal::random<int>(1, 51));

  left.setRandom();
  right.setRandom();

  // add constants to shift values away from 0 for more precision
  left += left.constant(1.5f);
  right += right.constant(1.5f);

  typedef Tensor<float, 1>::DimensionPair DimPair;
  Eigen::array<DimPair, 1> dims({{DimPair(1, 2)}});

  Eigen::ThreadPoolDevice thread_pool_device(internal::random<int>(2, 11));

  Tensor<float, 5, DataLayout> st_result;
  st_result = left.contract(right, dims);

  Tensor<float, 5, DataLayout> tp_result(st_result.dimensions());
  tp_result.device(thread_pool_device) = left.contract(right, dims);

  VERIFY(dimensions_match(st_result.dimensions(), tp_result.dimensions()));
  for (ptrdiff_t i = 0; i < st_result.size(); i++) {
    // if both of the values are very small, then do nothing (because the test will fail
    // due to numerical precision issues when values are small)
    if (fabs(st_result.data()[i] - tp_result.data()[i]) >= 1e-4) {
      VERIFY_IS_APPROX(st_result.data()[i], tp_result.data()[i]);
    }
  }
}

static void test_memcpy() {

  for (int i = 0; i < 5; ++i) {
    const int num_threads = internal::random<int>(3, 11);
    Eigen::ThreadPoolDevice thread_pool_device(num_threads);

    const int size = internal::random<int>(13, 7632);
    Tensor<float, 1> t1(size);
    t1.setRandom();
    std::vector<float> result(size);
    thread_pool_device.memcpy(&result[0], t1.data(), size*sizeof(float));
    for (int i = 0; i < size; i++) {
      VERIFY_IS_EQUAL(t1(i), result[i]);
    }
  }
}

static void test_multithread_random()
{
  Eigen::ThreadPoolDevice device(2);
  Tensor<float, 1> t(1 << 20);
  t.device(device) = t.random<Eigen::internal::NormalRandomGenerator<float>>();
}

void test_cxx11_tensor_thread_pool()
{
  CALL_SUBTEST(test_multithread_elementwise());
  CALL_SUBTEST(test_multithread_compound_assignment());

  CALL_SUBTEST(test_multithread_contraction<ColMajor>());
  CALL_SUBTEST(test_multithread_contraction<RowMajor>());

  CALL_SUBTEST(test_multithread_contraction_agrees_with_singlethread<ColMajor>());
  CALL_SUBTEST(test_multithread_contraction_agrees_with_singlethread<RowMajor>());

  // Exercise various cases that have been problematic in the past.
  CALL_SUBTEST(test_contraction_corner_cases<ColMajor>());
  CALL_SUBTEST(test_contraction_corner_cases<RowMajor>());

  CALL_SUBTEST(test_memcpy());

  CALL_SUBTEST(test_multithread_random());
}
<commit_msg>Fix clang compilation<commit_after>// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#define EIGEN_USE_THREADS

#include "main.h"
#include <iostream>
#include <Eigen/CXX11/Tensor>

using Eigen::Tensor;
using std::isnan;

static void test_multithread_elementwise()
{
  Tensor<float, 3> in1(2,3,7);
  Tensor<float, 3> in2(2,3,7);
  Tensor<float, 3> out(2,3,7);

  in1.setRandom();
  in2.setRandom();

  Eigen::ThreadPoolDevice thread_pool_device(internal::random<int>(3, 11));
  out.device(thread_pool_device) = in1 + in2 * 3.14f;

  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 7; ++k) {
        VERIFY_IS_APPROX(out(i,j,k), in1(i,j,k) + in2(i,j,k) * 3.14f);
      }
    }
  }
}

static void test_multithread_compound_assignment()
{
  Tensor<float, 3> in1(2,3,7);
  Tensor<float, 3> in2(2,3,7);
  Tensor<float, 3> out(2,3,7);

  in1.setRandom();
  in2.setRandom();

  Eigen::ThreadPoolDevice thread_pool_device(internal::random<int>(3, 11));
  out.device(thread_pool_device) = in1;
  out.device(thread_pool_device) += in2 * 3.14f;

  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 7; ++k) {
        VERIFY_IS_APPROX(out(i,j,k), in1(i,j,k) + in2(i,j,k) * 3.14f);
      }
    }
  }
}

template<int DataLayout>
static void test_multithread_contraction()
{
  Tensor<float, 4, DataLayout> t_left(30, 50, 37, 31);
  Tensor<float, 5, DataLayout> t_right(37, 31, 70, 2, 10);
  Tensor<float, 5, DataLayout> t_result(30, 50, 70, 2, 10);

  t_left.setRandom();
  t_right.setRandom();

  // this contraction should be equivalent to a single matrix multiplication
  typedef Tensor<float, 1>::DimensionPair DimPair;
  Eigen::array<DimPair, 2> dims({{DimPair(2, 0), DimPair(3, 1)}});

  typedef Map<Matrix<float, Dynamic, Dynamic, DataLayout>> MapXf;
  MapXf m_left(t_left.data(), 1500, 1147);
  MapXf m_right(t_right.data(), 1147, 1400);
  Matrix<float, Dynamic, Dynamic, DataLayout> m_result(1500, 1400);

  Eigen::ThreadPoolDevice thread_pool_device(4);

  // compute results by separate methods
  t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
  m_result = m_left * m_right;

 for (ptrdiff_t i = 0; i < t_result.size(); i++) {
    VERIFY(&t_result.data()[i] != &m_result.data()[i]);
    if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
      std::cout << "mismatch detected: " << t_result.data()[i] << " vs " <<  m_result.data()[i] << std::endl;
      assert(false);
    }
  }
}

template<int DataLayout>
static void test_contraction_corner_cases()
{
  Tensor<float, 2, DataLayout> t_left(32, 500);
  Tensor<float, 2, DataLayout> t_right(32, 28*28);
  Tensor<float, 2, DataLayout> t_result(500, 28*28);

  t_left = (t_left.constant(-0.5f) + t_left.random()) * 2.0f;
  t_right = (t_right.constant(-0.6f) + t_right.random()) * 2.0f;
  t_result = t_result.constant(NAN);

  // this contraction should be equivalent to a single matrix multiplication
  typedef Tensor<float, 1>::DimensionPair DimPair;
  Eigen::array<DimPair, 1> dims{{DimPair(0, 0)}};

  typedef Map<Matrix<float, Dynamic, Dynamic, DataLayout>> MapXf;
  MapXf m_left(t_left.data(), 32, 500);
  MapXf m_right(t_right.data(), 32, 28*28);
  Matrix<float, Dynamic, Dynamic, DataLayout> m_result(500, 28*28);

  Eigen::ThreadPoolDevice thread_pool_device(12);

  // compute results by separate methods
  t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
  m_result = m_left.transpose() * m_right;

  for (ptrdiff_t i = 0; i < t_result.size(); i++) {
    assert(!isnan(t_result.data()[i]));
    if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
      std::cout << "mismatch detected at index " << i << " : " << t_result.data()[i] << " vs " <<  m_result.data()[i] << std::endl;
      assert(false);
    }
  }

  t_left.resize(32, 1);
  t_left = (t_left.constant(-0.5f) + t_left.random()) * 2.0f;
  t_result.resize (1, 28*28);
  t_result = t_result.constant(NAN);
  t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
  new(&m_left) MapXf(t_left.data(), 32, 1);
  m_result = m_left.transpose() * m_right;
  for (ptrdiff_t i = 0; i < t_result.size(); i++) {
    assert(!isnan(t_result.data()[i]));
    if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
      std::cout << "mismatch detected: " << t_result.data()[i] << " vs " <<  m_result.data()[i] << std::endl;
      assert(false);
    }
  }

  t_left.resize(32, 500);
  t_right.resize(32, 4);
  t_left = (t_left.constant(-0.5f) + t_left.random()) * 2.0f;
  t_right = (t_right.constant(-0.6f) + t_right.random()) * 2.0f;
  t_result.resize (500, 4);
  t_result = t_result.constant(NAN);
  t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
  new(&m_left) MapXf(t_left.data(), 32, 500);
  new(&m_right) MapXf(t_right.data(), 32, 4);
  m_result = m_left.transpose() * m_right;
  for (ptrdiff_t i = 0; i < t_result.size(); i++) {
    assert(!isnan(t_result.data()[i]));
    if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
      std::cout << "mismatch detected: " << t_result.data()[i] << " vs " <<  m_result.data()[i] << std::endl;
      assert(false);
    }
  }

  t_left.resize(32, 1);
  t_right.resize(32, 4);
  t_left = (t_left.constant(-0.5f) + t_left.random()) * 2.0f;
  t_right = (t_right.constant(-0.6f) + t_right.random()) * 2.0f;
  t_result.resize (1, 4);
  t_result = t_result.constant(NAN);
  t_result.device(thread_pool_device) = t_left.contract(t_right, dims);
  new(&m_left) MapXf(t_left.data(), 32, 1);
  new(&m_right) MapXf(t_right.data(), 32, 4);
  m_result = m_left.transpose() * m_right;
  for (ptrdiff_t i = 0; i < t_result.size(); i++) {
    assert(!isnan(t_result.data()[i]));
    if (fabs(t_result.data()[i] - m_result.data()[i]) >= 1e-4) {
      std::cout << "mismatch detected: " << t_result.data()[i] << " vs " <<  m_result.data()[i] << std::endl;
      assert(false);
    }
  }
}

template<int DataLayout>
static void test_multithread_contraction_agrees_with_singlethread() {
  int contract_size = internal::random<int>(1, 5000);

  Tensor<float, 3, DataLayout> left(internal::random<int>(1, 80),
                                    contract_size,
                                    internal::random<int>(1, 100));

  Tensor<float, 4, DataLayout> right(internal::random<int>(1, 25),
                                     internal::random<int>(1, 37),
                                     contract_size,
                                     internal::random<int>(1, 51));

  left.setRandom();
  right.setRandom();

  // add constants to shift values away from 0 for more precision
  left += left.constant(1.5f);
  right += right.constant(1.5f);

  typedef Tensor<float, 1>::DimensionPair DimPair;
  Eigen::array<DimPair, 1> dims({{DimPair(1, 2)}});

  Eigen::ThreadPoolDevice thread_pool_device(internal::random<int>(2, 11));

  Tensor<float, 5, DataLayout> st_result;
  st_result = left.contract(right, dims);

  Tensor<float, 5, DataLayout> tp_result(st_result.dimensions());
  tp_result.device(thread_pool_device) = left.contract(right, dims);

  VERIFY(dimensions_match(st_result.dimensions(), tp_result.dimensions()));
  for (ptrdiff_t i = 0; i < st_result.size(); i++) {
    // if both of the values are very small, then do nothing (because the test will fail
    // due to numerical precision issues when values are small)
    if (fabs(st_result.data()[i] - tp_result.data()[i]) >= 1e-4) {
      VERIFY_IS_APPROX(st_result.data()[i], tp_result.data()[i]);
    }
  }
}

static void test_memcpy() {

  for (int i = 0; i < 5; ++i) {
    const int num_threads = internal::random<int>(3, 11);
    Eigen::ThreadPoolDevice thread_pool_device(num_threads);

    const int size = internal::random<int>(13, 7632);
    Tensor<float, 1> t1(size);
    t1.setRandom();
    std::vector<float> result(size);
    thread_pool_device.memcpy(&result[0], t1.data(), size*sizeof(float));
    for (int i = 0; i < size; i++) {
      VERIFY_IS_EQUAL(t1(i), result[i]);
    }
  }
}

static void test_multithread_random()
{
  Eigen::ThreadPoolDevice device(2);
  Tensor<float, 1> t(1 << 20);
  t.device(device) = t.random<Eigen::internal::NormalRandomGenerator<float>>();
}

void test_cxx11_tensor_thread_pool()
{
  CALL_SUBTEST(test_multithread_elementwise());
  CALL_SUBTEST(test_multithread_compound_assignment());

  CALL_SUBTEST(test_multithread_contraction<ColMajor>());
  CALL_SUBTEST(test_multithread_contraction<RowMajor>());

  CALL_SUBTEST(test_multithread_contraction_agrees_with_singlethread<ColMajor>());
  CALL_SUBTEST(test_multithread_contraction_agrees_with_singlethread<RowMajor>());

  // Exercise various cases that have been problematic in the past.
  CALL_SUBTEST(test_contraction_corner_cases<ColMajor>());
  CALL_SUBTEST(test_contraction_corner_cases<RowMajor>());

  CALL_SUBTEST(test_memcpy());

  CALL_SUBTEST(test_multithread_random());
}
<|endoftext|>


<commit_before>#include <assert.h>

#include "treehub_server.h"

using std::string;

TreehubServer::TreehubServer() : using_oauth2(false) {
  auth_header_.data = const_cast<char*>(auth_header_contents_.c_str());
  auth_header_.next = &force_header_;
  force_header_contents_ = "x-ats-ostree-force: true";
  force_header_.data = const_cast<char*>(force_header_contents_.c_str());
  force_header_.next = NULL;
}

void TreehubServer::SetToken(const string& token) {
  assert(auth_header_.next == &force_header_);
  assert(force_header_.next == NULL);

  auth_header_contents_ = "Authorization: Bearer " + token;
  auth_header_.data = const_cast<char*>(auth_header_contents_.c_str());
  using_oauth2 = true;
}

// Note that this method creates a reference from curl_handle to this.  Keep
// this TreehubServer object alive until the curl request has been completed
void TreehubServer::InjectIntoCurl(const string& url_suffix,
                                   CURL* curl_handle) const {
  curl_easy_setopt(curl_handle, CURLOPT_URL, (root_url + url_suffix).c_str());

  curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, &auth_header_);
  if (!using_oauth2) {  // If we don't have a OAuth2 token fallback to legacy
                        // username/password
    curl_easy_setopt(curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_easy_setopt(curl_handle, CURLOPT_USERNAME, username.c_str());
    curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password.c_str());
  }
}
// vim: set tabstop=2 shiftwidth=2 expandtab:
<commit_msg>Added slash in case trailing slash is missing in credentials file<commit_after>#include <assert.h>

#include "treehub_server.h"

using std::string;

TreehubServer::TreehubServer() : using_oauth2(false) {
  auth_header_.data = const_cast<char*>(auth_header_contents_.c_str());
  auth_header_.next = &force_header_;
  force_header_contents_ = "x-ats-ostree-force: true";
  force_header_.data = const_cast<char*>(force_header_contents_.c_str());
  force_header_.next = NULL;
}

void TreehubServer::SetToken(const string& token) {
  assert(auth_header_.next == &force_header_);
  assert(force_header_.next == NULL);

  auth_header_contents_ = "Authorization: Bearer " + token;
  auth_header_.data = const_cast<char*>(auth_header_contents_.c_str());
  using_oauth2 = true;
}

// Note that this method creates a reference from curl_handle to this.  Keep
// this TreehubServer object alive until the curl request has been completed
void TreehubServer::InjectIntoCurl(const string& url_suffix,
                                   CURL* curl_handle) const {
  curl_easy_setopt(curl_handle, CURLOPT_URL, (root_url + "/" + url_suffix).c_str());

  curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, &auth_header_);
  if (!using_oauth2) {  // If we don't have a OAuth2 token fallback to legacy
                        // username/password
    curl_easy_setopt(curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_easy_setopt(curl_handle, CURLOPT_USERNAME, username.c_str());
    curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, password.c_str());
  }
}
// vim: set tabstop=2 shiftwidth=2 expandtab:
<|endoftext|>


<commit_before>//----------------------------------*-C++-*----------------------------------//
/*!
 * \file   tstCommIndexer.cpp
 * \author Stuart Slattery
 * \date   Wed May 25 12:36:14 2011
 * \brief  CommIndexer class unit tests.
 */
//---------------------------------------------------------------------------//

#include <iostream>
#include <vector>
#include <cmath>
#include <sstream>

#include <DTK_CommIndexer.hpp>

#include "Teuchos_UnitTestHarness.hpp"
#include "Teuchos_RCP.hpp"
#include "Teuchos_ArrayView.hpp"
#include "Teuchos_DefaultComm.hpp"
#include "Teuchos_CommHelpers.hpp"

//---------------------------------------------------------------------------//
// HELPER FUNCTIONS
//---------------------------------------------------------------------------//

// Get the default communicator.
template<class Ordinal>
Teuchos::RCP<const Teuchos::Comm<Ordinal> > getDefaultComm()
{
#ifdef HAVE_MPI
    return Teuchos::DefaultComm<Ordinal>::getComm();
#else
    return Teuchos::rcp(new Teuchos::SerialComm<Ordinal>() );
#endif
}

//---------------------------------------------------------------------------//
// TESTS
//---------------------------------------------------------------------------//

TEUCHOS_UNIT_TEST( CommIndexer, duplicate_test )
{
    using namespace DataTransferKit;

    typedef Teuchos::RCP<const Teuchos::Comm<int> > RCP_Comm;

    RCP_Comm global_comm = getDefaultComm<int>();
    RCP_Comm local_comm = global_comm->duplicate();

    CommIndexer indexer( global_comm, local_comm );

    TEST_ASSERT( (int) indexer.size() == local_comm->getSize() );
    TEST_ASSERT( indexer.l2g( local_comm->getRank() ) ==
		 global_comm->getRank() );
}

//---------------------------------------------------------------------------//
TEUCHOS_UNIT_TEST( CommIndexer, split_test )
{
    using namespace DataTransferKit;

    typedef Teuchos::RCP<const Teuchos::Comm<int> > RCP_Comm;

    RCP_Comm global_comm = getDefaultComm<int>();
    int rank = global_comm->getRank();
    RCP_Comm local_comm = global_comm->split( 0, rank );

    CommIndexer indexer( global_comm, local_comm );

    TEST_ASSERT( (int) indexer.size() == local_comm->getSize() );
    TEST_ASSERT( indexer.l2g( local_comm->getRank() ) ==
		 global_comm->getRank() );
}

//---------------------------------------------------------------------------//
TEUCHOS_UNIT_TEST( CommIndexer, inverse_split_test )
{
    using namespace DataTransferKit;

    typedef Teuchos::RCP<const Teuchos::Comm<int> > RCP_Comm;

    RCP_Comm global_comm = getDefaultComm<int>();
    int inverse_rank = global_comm->getSize() - global_comm->getRank() - 1;
    RCP_Comm local_comm = global_comm->split( 0, inverse_rank);

    CommIndexer indexer( global_comm, local_comm );

    TEST_ASSERT( (int) indexer.size() == local_comm->getSize() );
    TEST_ASSERT( indexer.l2g( local_comm->getRank() ) ==
		 global_comm->getRank() );
}

//---------------------------------------------------------------------------//
TEUCHOS_UNIT_TEST( CommIndexer, subcommunicator_test )
{
    using namespace DataTransferKit;

    typedef Teuchos::RCP<const Teuchos::Comm<int> > RCP_Comm;

    RCP_Comm global_comm = getDefaultComm<int>();
    std::vector<int> sub_ranks;
    for ( int n = 0; n < global_comm->getSize(); ++n )
    {
	if ( n % 2 == 0 )
	{
	    sub_ranks.push_back(n);
	}
    }
    RCP_Comm local_comm =
	global_comm->createSubcommunicator( sub_ranks() );

    CommIndexer indexer( global_comm, local_comm );

    if ( global_comm->getRank() % 2 == 0 )
    {
	TEST_ASSERT( Teuchos::nonnull(local_comm) );
	TEST_EQUALITY( (int) indexer.size(), local_comm->getSize() );
    	TEST_EQUALITY( indexer.l2g( local_comm->getRank() ),
		     global_comm->getRank() );
    }
    else
    {
	TEST_ASSERT( Teuchos::is_null(local_comm) );
    }
    TEST_EQUALITY( indexer.l2g(-32), -1 );
}

//---------------------------------------------------------------------------//
//                        end of tstCommIndexer.cpp
//---------------------------------------------------------------------------//
<commit_msg>Fixing std::vector error in CommIndexer test<commit_after>//----------------------------------*-C++-*----------------------------------//
/*!
 * \file   tstCommIndexer.cpp
 * \author Stuart Slattery
 * \date   Wed May 25 12:36:14 2011
 * \brief  CommIndexer class unit tests.
 */
//---------------------------------------------------------------------------//

#include <iostream>
#include <vector>
#include <cmath>
#include <sstream>

#include <DTK_CommIndexer.hpp>

#include "Teuchos_UnitTestHarness.hpp"
#include "Teuchos_RCP.hpp"
#include "Teuchos_Array.hpp"
#include "Teuchos_ArrayView.hpp"
#include "Teuchos_DefaultComm.hpp"
#include "Teuchos_CommHelpers.hpp"

//---------------------------------------------------------------------------//
// HELPER FUNCTIONS
//---------------------------------------------------------------------------//

// Get the default communicator.
template<class Ordinal>
Teuchos::RCP<const Teuchos::Comm<Ordinal> > getDefaultComm()
{
#ifdef HAVE_MPI
    return Teuchos::DefaultComm<Ordinal>::getComm();
#else
    return Teuchos::rcp(new Teuchos::SerialComm<Ordinal>() );
#endif
}

//---------------------------------------------------------------------------//
// TESTS
//---------------------------------------------------------------------------//

TEUCHOS_UNIT_TEST( CommIndexer, duplicate_test )
{
    using namespace DataTransferKit;

    typedef Teuchos::RCP<const Teuchos::Comm<int> > RCP_Comm;

    RCP_Comm global_comm = getDefaultComm<int>();
    RCP_Comm local_comm = global_comm->duplicate();

    CommIndexer indexer( global_comm, local_comm );

    TEST_ASSERT( (int) indexer.size() == local_comm->getSize() );
    TEST_ASSERT( indexer.l2g( local_comm->getRank() ) ==
		 global_comm->getRank() );
}

//---------------------------------------------------------------------------//
TEUCHOS_UNIT_TEST( CommIndexer, split_test )
{
    using namespace DataTransferKit;

    typedef Teuchos::RCP<const Teuchos::Comm<int> > RCP_Comm;

    RCP_Comm global_comm = getDefaultComm<int>();
    int rank = global_comm->getRank();
    RCP_Comm local_comm = global_comm->split( 0, rank );

    CommIndexer indexer( global_comm, local_comm );

    TEST_ASSERT( (int) indexer.size() == local_comm->getSize() );
    TEST_ASSERT( indexer.l2g( local_comm->getRank() ) ==
		 global_comm->getRank() );
}

//---------------------------------------------------------------------------//
TEUCHOS_UNIT_TEST( CommIndexer, inverse_split_test )
{
    using namespace DataTransferKit;

    typedef Teuchos::RCP<const Teuchos::Comm<int> > RCP_Comm;

    RCP_Comm global_comm = getDefaultComm<int>();
    int inverse_rank = global_comm->getSize() - global_comm->getRank() - 1;
    RCP_Comm local_comm = global_comm->split( 0, inverse_rank);

    CommIndexer indexer( global_comm, local_comm );

    TEST_ASSERT( (int) indexer.size() == local_comm->getSize() );
    TEST_ASSERT( indexer.l2g( local_comm->getRank() ) ==
		 global_comm->getRank() );
}

//---------------------------------------------------------------------------//
TEUCHOS_UNIT_TEST( CommIndexer, subcommunicator_test )
{
    using namespace DataTransferKit;

    typedef Teuchos::RCP<const Teuchos::Comm<int> > RCP_Comm;

    RCP_Comm global_comm = getDefaultComm<int>();
    Teuchos::Array<int> sub_ranks;
    for ( int n = 0; n < global_comm->getSize(); ++n )
    {
	if ( n % 2 == 0 )
	{
	    sub_ranks.push_back(n);
	}
    }
    RCP_Comm local_comm =
	global_comm->createSubcommunicator( sub_ranks() );

    CommIndexer indexer( global_comm, local_comm );

    if ( global_comm->getRank() % 2 == 0 )
    {
	TEST_ASSERT( Teuchos::nonnull(local_comm) );
	TEST_EQUALITY( (int) indexer.size(), local_comm->getSize() );
    	TEST_EQUALITY( indexer.l2g( local_comm->getRank() ),
		     global_comm->getRank() );
    }
    else
    {
	TEST_ASSERT( Teuchos::is_null(local_comm) );
    }
    TEST_EQUALITY( indexer.l2g(-32), -1 );
}

//---------------------------------------------------------------------------//
//                        end of tstCommIndexer.cpp
//---------------------------------------------------------------------------//
<|endoftext|>


<commit_before><commit_msg>Fix about box truncation (and truncation of probably every other box on glass frames).<commit_after><|endoftext|>


<commit_before>/*
 * Copyright (C) 2008-2009 Manjeet Dahiya
 *
 * Author:
 *	Manjeet Dahiya
 *
 * Source:
 *	http://code.google.com/p/qxmpp
 *
 * This file is a part of QXmpp library.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 */

#include "QXmppReconnectionManager.h"
#include "QXmppClient.h"
#include "QXmppLogger.h"
#include "QXmppUtils.h"

QXmppReconnectionManager::QXmppReconnectionManager(QXmppClient* client) :
        QObject(client),
        m_receivedConflict(false),
        m_reconnectionTries(0),
        m_timer(this),
        m_client(client)
{
    m_timer.setSingleShot(true);
    bool check = connect(&m_timer, SIGNAL(timeout()), SLOT(reconnect()));
    Q_ASSERT(check);
    Q_UNUSED(check);
}

void QXmppReconnectionManager::connected()
{
    m_receivedConflict = false;
    m_reconnectionTries = 0;
}

void QXmppReconnectionManager::error(QXmppClient::Error error)
{
    if(m_client && error == QXmppClient::XmppStreamError)
    {
        // if we receive a resource conflict, inhibit reconnection
        if(m_client->getXmppStreamError() == QXmppStanza::Error::Conflict)
            m_receivedConflict = true;
    }
    else if(m_client && error == QXmppClient::SocketError && !m_receivedConflict)
    {
        int time = getNextReconnectingInTime();

        // time is in sec
        m_timer.start(time*1000);
        emit reconnectingIn(time);
    }
    else if (m_client && error == QXmppClient::KeepAliveError)
    {
        // if we got a keepalive error, reconnect in one second
        m_timer.start(1000);
    }
}

int QXmppReconnectionManager::getNextReconnectingInTime()
{
    int reconnectingIn;
    if(m_reconnectionTries < 5)
        reconnectingIn = 10;
    else if(m_reconnectionTries < 10)
        reconnectingIn = 20;
    else if(m_reconnectionTries < 15)
        reconnectingIn = 40;
    else
        reconnectingIn = 60;

    return reconnectingIn;
}

void QXmppReconnectionManager::reconnect()
{
    if(m_client)
    {
        m_client->logger()->log(QXmppLogger::DebugMessage, "QXmppReconnectionManager::reconnect()");
        emit reconnectingNow();
        m_client->connectToServer(m_client->getConfiguration());
    }
}

void QXmppReconnectionManager::cancelReconnection()
{
    m_timer.stop();
    m_receivedConflict = false;
    m_reconnectionTries = 0;
}
<commit_msg>QXmppReconnectionManager should not alter the client's initial presence<commit_after>/*
 * Copyright (C) 2008-2009 Manjeet Dahiya
 *
 * Author:
 *	Manjeet Dahiya
 *
 * Source:
 *	http://code.google.com/p/qxmpp
 *
 * This file is a part of QXmpp library.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 */

#include "QXmppReconnectionManager.h"
#include "QXmppClient.h"
#include "QXmppLogger.h"
#include "QXmppUtils.h"

QXmppReconnectionManager::QXmppReconnectionManager(QXmppClient* client) :
        QObject(client),
        m_receivedConflict(false),
        m_reconnectionTries(0),
        m_timer(this),
        m_client(client)
{
    m_timer.setSingleShot(true);
    bool check = connect(&m_timer, SIGNAL(timeout()), SLOT(reconnect()));
    Q_ASSERT(check);
    Q_UNUSED(check);
}

void QXmppReconnectionManager::connected()
{
    m_receivedConflict = false;
    m_reconnectionTries = 0;
}

void QXmppReconnectionManager::error(QXmppClient::Error error)
{
    if(m_client && error == QXmppClient::XmppStreamError)
    {
        // if we receive a resource conflict, inhibit reconnection
        if(m_client->getXmppStreamError() == QXmppStanza::Error::Conflict)
            m_receivedConflict = true;
    }
    else if(m_client && error == QXmppClient::SocketError && !m_receivedConflict)
    {
        int time = getNextReconnectingInTime();

        // time is in sec
        m_timer.start(time*1000);
        emit reconnectingIn(time);
    }
    else if (m_client && error == QXmppClient::KeepAliveError)
    {
        // if we got a keepalive error, reconnect in one second
        m_timer.start(1000);
    }
}

int QXmppReconnectionManager::getNextReconnectingInTime()
{
    int reconnectingIn;
    if(m_reconnectionTries < 5)
        reconnectingIn = 10;
    else if(m_reconnectionTries < 10)
        reconnectingIn = 20;
    else if(m_reconnectionTries < 15)
        reconnectingIn = 40;
    else
        reconnectingIn = 60;

    return reconnectingIn;
}

void QXmppReconnectionManager::reconnect()
{
    if(m_client)
    {
        m_client->logger()->log(QXmppLogger::DebugMessage, "QXmppReconnectionManager::reconnect()");
        emit reconnectingNow();
        m_client->connectToServer(m_client->getConfiguration(), m_client->getClientPresence());
    }
}

void QXmppReconnectionManager::cancelReconnection()
{
    m_timer.stop();
    m_receivedConflict = false;
    m_reconnectionTries = 0;
}
<|endoftext|>


<commit_before>// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/perftimer.h"
#include "base/string_util.h"
#include "base/test_file_util.h"
#include "base/timer.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/block_files.h"
#include "net/disk_cache/disk_cache.h"
#include "net/disk_cache/disk_cache_test_util.h"
#include "net/disk_cache/hash.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

using base::Time;

extern int g_cache_tests_max_id;
extern volatile int g_cache_tests_received;
extern volatile bool g_cache_tests_error;

typedef PlatformTest DiskCacheTest;

namespace {

struct TestEntry {
  std::string key;
  int data_len;
};
typedef std::vector<TestEntry> TestEntries;

const int kMaxSize = 16 * 1024 - 1;

// Creates num_entries on the cache, and writes 200 bytes of metadata and up
// to kMaxSize of data to each entry.
int TimeWrite(int num_entries, disk_cache::Backend* cache,
              TestEntries* entries) {
  const int kSize1 = 200;
  scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize1);
  scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kMaxSize);

  CacheTestFillBuffer(buffer1->data(), kSize1, false);
  CacheTestFillBuffer(buffer2->data(), kMaxSize, false);

  CallbackTest callback(1);
  g_cache_tests_error = false;
  g_cache_tests_max_id = 1;
  g_cache_tests_received = 0;
  int expected = 0;

  MessageLoopHelper helper;

  PerfTimeLogger timer("Write disk cache entries");

  for (int i = 0; i < num_entries; i++) {
    TestEntry entry;
    entry.key = GenerateKey(true);
    entry.data_len = rand() % kMaxSize;
    entries->push_back(entry);

    disk_cache::Entry* cache_entry;
    if (!cache->CreateEntry(entry.key, &cache_entry))
      break;
    int ret = cache_entry->WriteData(0, 0, buffer1, kSize1, &callback, false);
    if (net::ERR_IO_PENDING == ret)
      expected++;
    else if (kSize1 != ret)
      break;

    ret = cache_entry->WriteData(1, 0, buffer2, entry.data_len, &callback,
                                 false);
    if (net::ERR_IO_PENDING == ret)
      expected++;
    else if (entry.data_len != ret)
      break;
    cache_entry->Close();
  }

  helper.WaitUntilCacheIoFinished(expected);
  timer.Done();

  return expected;
}

// Reads the data and metadata from each entry listed on |entries|.
int TimeRead(int num_entries, disk_cache::Backend* cache,
             const TestEntries& entries, bool cold) {
  const int kSize1 = 200;
  scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize1);
  scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kMaxSize);

  CacheTestFillBuffer(buffer1->data(), kSize1, false);
  CacheTestFillBuffer(buffer2->data(), kMaxSize, false);

  CallbackTest callback(1);
  g_cache_tests_error = false;
  g_cache_tests_max_id = 1;
  g_cache_tests_received = 0;
  int expected = 0;

  MessageLoopHelper helper;

  const char* message = cold ? "Read disk cache entries (cold)" :
                        "Read disk cache entries (warm)";
  PerfTimeLogger timer(message);

  for (int i = 0; i < num_entries; i++) {
    disk_cache::Entry* cache_entry;
    if (!cache->OpenEntry(entries[i].key, &cache_entry))
      break;
    int ret = cache_entry->ReadData(0, 0, buffer1, kSize1, &callback);
    if (net::ERR_IO_PENDING == ret)
      expected++;
    else if (kSize1 != ret)
      break;

    ret = cache_entry->ReadData(1, 0, buffer2, entries[i].data_len, &callback);
    if (net::ERR_IO_PENDING == ret)
      expected++;
    else if (entries[i].data_len != ret)
      break;
    cache_entry->Close();
  }

  helper.WaitUntilCacheIoFinished(expected);
  timer.Done();

  return expected;
}

int BlockSize() {
  // We can use form 1 to 4 blocks.
  return (rand() & 0x3) + 1;
}

}  // namespace

TEST_F(DiskCacheTest, Hash) {
  int seed = static_cast<int>(Time::Now().ToInternalValue());
  srand(seed);

  PerfTimeLogger timer("Hash disk cache keys");
  for (int i = 0; i < 300000; i++) {
    std::string key = GenerateKey(true);
    disk_cache::Hash(key);
  }
  timer.Done();
}

TEST_F(DiskCacheTest, CacheBackendPerformance) {
  MessageLoopForIO message_loop;

  ScopedTestCache test_cache;
  disk_cache::Backend* cache =
      disk_cache::CreateCacheBackend(test_cache.path_wstring(), false, 0,
                                     net::DISK_CACHE);
  ASSERT_TRUE(NULL != cache);

  int seed = static_cast<int>(Time::Now().ToInternalValue());
  srand(seed);

  TestEntries entries;
  int num_entries = 1000;

  int ret = TimeWrite(num_entries, cache, &entries);
  EXPECT_EQ(ret, g_cache_tests_received);

  MessageLoop::current()->RunAllPending();
  delete cache;

  ASSERT_TRUE(file_util::EvictFileFromSystemCache(
              test_cache.path().AppendASCII("index")));
  ASSERT_TRUE(file_util::EvictFileFromSystemCache(
              test_cache.path().AppendASCII("data_0")));
  ASSERT_TRUE(file_util::EvictFileFromSystemCache(
              test_cache.path().AppendASCII("data_1")));
  ASSERT_TRUE(file_util::EvictFileFromSystemCache(
              test_cache.path().AppendASCII("data_2")));
  ASSERT_TRUE(file_util::EvictFileFromSystemCache(
              test_cache.path().AppendASCII("data_3")));

  cache = disk_cache::CreateCacheBackend(test_cache.path_wstring(), false, 0,
                                         net::DISK_CACHE);
  ASSERT_TRUE(NULL != cache);

  ret = TimeRead(num_entries, cache, entries, true);
  EXPECT_EQ(ret, g_cache_tests_received);

  ret = TimeRead(num_entries, cache, entries, false);
  EXPECT_EQ(ret, g_cache_tests_received);

  MessageLoop::current()->RunAllPending();
  delete cache;
}

// Creating and deleting "entries" on a block-file is something quite frequent
// (after all, almost everything is stored on block files). The operation is
// almost free when the file is empty, but can be expensive if the file gets
// fragmented, or if we have multiple files. This test measures that scenario,
// by using multiple, highly fragmented files.
TEST_F(DiskCacheTest, BlockFilesPerformance) {
  MessageLoopForIO message_loop;

  ScopedTestCache test_cache;

  disk_cache::BlockFiles files(test_cache.path_wstring());
  ASSERT_TRUE(files.Init(true));

  int seed = static_cast<int>(Time::Now().ToInternalValue());
  srand(seed);

  const int kNumEntries = 60000;
  disk_cache::Addr* address = new disk_cache::Addr[kNumEntries];

  PerfTimeLogger timer1("Fill three block-files");

  // Fill up the 32-byte block file (use three files).
  for (int i = 0; i < kNumEntries; i++) {
    EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(),
                                  &address[i]));
  }

  timer1.Done();
  PerfTimeLogger timer2("Create and delete blocks");

  for (int i = 0; i < 200000; i++) {
    int entry = rand() * (kNumEntries / RAND_MAX + 1);
    if (entry >= kNumEntries)
      entry = 0;

    files.DeleteBlock(address[entry], false);
    EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(),
                                  &address[entry]));
  }

  timer2.Done();
  MessageLoop::current()->RunAllPending();
  delete address;
}
<commit_msg>s/delete/delete[]/ Review URL: http://codereview.chromium.org/67174<commit_after>// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/perftimer.h"
#include "base/string_util.h"
#include "base/test_file_util.h"
#include "base/timer.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/block_files.h"
#include "net/disk_cache/disk_cache.h"
#include "net/disk_cache/disk_cache_test_util.h"
#include "net/disk_cache/hash.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

using base::Time;

extern int g_cache_tests_max_id;
extern volatile int g_cache_tests_received;
extern volatile bool g_cache_tests_error;

typedef PlatformTest DiskCacheTest;

namespace {

struct TestEntry {
  std::string key;
  int data_len;
};
typedef std::vector<TestEntry> TestEntries;

const int kMaxSize = 16 * 1024 - 1;

// Creates num_entries on the cache, and writes 200 bytes of metadata and up
// to kMaxSize of data to each entry.
int TimeWrite(int num_entries, disk_cache::Backend* cache,
              TestEntries* entries) {
  const int kSize1 = 200;
  scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize1);
  scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kMaxSize);

  CacheTestFillBuffer(buffer1->data(), kSize1, false);
  CacheTestFillBuffer(buffer2->data(), kMaxSize, false);

  CallbackTest callback(1);
  g_cache_tests_error = false;
  g_cache_tests_max_id = 1;
  g_cache_tests_received = 0;
  int expected = 0;

  MessageLoopHelper helper;

  PerfTimeLogger timer("Write disk cache entries");

  for (int i = 0; i < num_entries; i++) {
    TestEntry entry;
    entry.key = GenerateKey(true);
    entry.data_len = rand() % kMaxSize;
    entries->push_back(entry);

    disk_cache::Entry* cache_entry;
    if (!cache->CreateEntry(entry.key, &cache_entry))
      break;
    int ret = cache_entry->WriteData(0, 0, buffer1, kSize1, &callback, false);
    if (net::ERR_IO_PENDING == ret)
      expected++;
    else if (kSize1 != ret)
      break;

    ret = cache_entry->WriteData(1, 0, buffer2, entry.data_len, &callback,
                                 false);
    if (net::ERR_IO_PENDING == ret)
      expected++;
    else if (entry.data_len != ret)
      break;
    cache_entry->Close();
  }

  helper.WaitUntilCacheIoFinished(expected);
  timer.Done();

  return expected;
}

// Reads the data and metadata from each entry listed on |entries|.
int TimeRead(int num_entries, disk_cache::Backend* cache,
             const TestEntries& entries, bool cold) {
  const int kSize1 = 200;
  scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize1);
  scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kMaxSize);

  CacheTestFillBuffer(buffer1->data(), kSize1, false);
  CacheTestFillBuffer(buffer2->data(), kMaxSize, false);

  CallbackTest callback(1);
  g_cache_tests_error = false;
  g_cache_tests_max_id = 1;
  g_cache_tests_received = 0;
  int expected = 0;

  MessageLoopHelper helper;

  const char* message = cold ? "Read disk cache entries (cold)" :
                        "Read disk cache entries (warm)";
  PerfTimeLogger timer(message);

  for (int i = 0; i < num_entries; i++) {
    disk_cache::Entry* cache_entry;
    if (!cache->OpenEntry(entries[i].key, &cache_entry))
      break;
    int ret = cache_entry->ReadData(0, 0, buffer1, kSize1, &callback);
    if (net::ERR_IO_PENDING == ret)
      expected++;
    else if (kSize1 != ret)
      break;

    ret = cache_entry->ReadData(1, 0, buffer2, entries[i].data_len, &callback);
    if (net::ERR_IO_PENDING == ret)
      expected++;
    else if (entries[i].data_len != ret)
      break;
    cache_entry->Close();
  }

  helper.WaitUntilCacheIoFinished(expected);
  timer.Done();

  return expected;
}

int BlockSize() {
  // We can use form 1 to 4 blocks.
  return (rand() & 0x3) + 1;
}

}  // namespace

TEST_F(DiskCacheTest, Hash) {
  int seed = static_cast<int>(Time::Now().ToInternalValue());
  srand(seed);

  PerfTimeLogger timer("Hash disk cache keys");
  for (int i = 0; i < 300000; i++) {
    std::string key = GenerateKey(true);
    disk_cache::Hash(key);
  }
  timer.Done();
}

TEST_F(DiskCacheTest, CacheBackendPerformance) {
  MessageLoopForIO message_loop;

  ScopedTestCache test_cache;
  disk_cache::Backend* cache =
      disk_cache::CreateCacheBackend(test_cache.path_wstring(), false, 0,
                                     net::DISK_CACHE);
  ASSERT_TRUE(NULL != cache);

  int seed = static_cast<int>(Time::Now().ToInternalValue());
  srand(seed);

  TestEntries entries;
  int num_entries = 1000;

  int ret = TimeWrite(num_entries, cache, &entries);
  EXPECT_EQ(ret, g_cache_tests_received);

  MessageLoop::current()->RunAllPending();
  delete cache;

  ASSERT_TRUE(file_util::EvictFileFromSystemCache(
              test_cache.path().AppendASCII("index")));
  ASSERT_TRUE(file_util::EvictFileFromSystemCache(
              test_cache.path().AppendASCII("data_0")));
  ASSERT_TRUE(file_util::EvictFileFromSystemCache(
              test_cache.path().AppendASCII("data_1")));
  ASSERT_TRUE(file_util::EvictFileFromSystemCache(
              test_cache.path().AppendASCII("data_2")));
  ASSERT_TRUE(file_util::EvictFileFromSystemCache(
              test_cache.path().AppendASCII("data_3")));

  cache = disk_cache::CreateCacheBackend(test_cache.path_wstring(), false, 0,
                                         net::DISK_CACHE);
  ASSERT_TRUE(NULL != cache);

  ret = TimeRead(num_entries, cache, entries, true);
  EXPECT_EQ(ret, g_cache_tests_received);

  ret = TimeRead(num_entries, cache, entries, false);
  EXPECT_EQ(ret, g_cache_tests_received);

  MessageLoop::current()->RunAllPending();
  delete cache;
}

// Creating and deleting "entries" on a block-file is something quite frequent
// (after all, almost everything is stored on block files). The operation is
// almost free when the file is empty, but can be expensive if the file gets
// fragmented, or if we have multiple files. This test measures that scenario,
// by using multiple, highly fragmented files.
TEST_F(DiskCacheTest, BlockFilesPerformance) {
  MessageLoopForIO message_loop;

  ScopedTestCache test_cache;

  disk_cache::BlockFiles files(test_cache.path_wstring());
  ASSERT_TRUE(files.Init(true));

  int seed = static_cast<int>(Time::Now().ToInternalValue());
  srand(seed);

  const int kNumEntries = 60000;
  disk_cache::Addr* address = new disk_cache::Addr[kNumEntries];

  PerfTimeLogger timer1("Fill three block-files");

  // Fill up the 32-byte block file (use three files).
  for (int i = 0; i < kNumEntries; i++) {
    EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(),
                                  &address[i]));
  }

  timer1.Done();
  PerfTimeLogger timer2("Create and delete blocks");

  for (int i = 0; i < 200000; i++) {
    int entry = rand() * (kNumEntries / RAND_MAX + 1);
    if (entry >= kNumEntries)
      entry = 0;

    files.DeleteBlock(address[entry], false);
    EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(),
                                  &address[entry]));
  }

  timer2.Done();
  MessageLoop::current()->RunAllPending();
  delete[] address;
}
<|endoftext|>


<commit_before>// The libMesh Finite Element Library.
// Copyright (C) 2002-2012 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner

// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

// C++ Includes -------------------------------------
#include <vector>

// Local Includes -----------------------------------
#include "libmesh/qoi_set.h"
#include "libmesh/system.h"

namespace libMesh
{

// ------------------------------------------------------------
// QoISet implementation

QoISet::QoISet(const System &sys) : _indices(sys.qoi.size(), true) {}

unsigned int QoISet::size (const System& sys) const
{
  unsigned int qoi_count = 0;
  for (unsigned int i=0; i != sys.qoi.size(); ++i)
    if (this->has_index(i))
      qoi_count++;
  return qoi_count;
}

void QoISet::add_indices(const std::vector<unsigned int> &indices)
{
  unsigned int size = 0;
  for (std::vector<unsigned int>::const_iterator i = indices.begin();
       i != indices.end(); ++i)
    size = std::max(size, *i + 1);

  _indices.resize(size);

  for (std::vector<unsigned int>::const_iterator i = indices.begin();
       i != indices.end(); ++i)
    _indices[*i] = true;
}

inline
void QoISet::remove_indices(const std::vector<unsigned int> &indices)
{
  for (std::vector<unsigned int>::const_iterator i = indices.begin();
       i != indices.end(); ++i)
    _indices[*i] = false;
}

} // namespace libMesh
<commit_msg>Changes in qoi_set.C for -Wshadow.<commit_after>// The libMesh Finite Element Library.
// Copyright (C) 2002-2012 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner

// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

// C++ Includes -------------------------------------
#include <vector>

// Local Includes -----------------------------------
#include "libmesh/qoi_set.h"
#include "libmesh/system.h"

namespace libMesh
{

// ------------------------------------------------------------
// QoISet implementation

QoISet::QoISet(const System &sys) : _indices(sys.qoi.size(), true) {}

unsigned int QoISet::size (const System& sys) const
{
  unsigned int qoi_count = 0;
  for (unsigned int i=0; i != sys.qoi.size(); ++i)
    if (this->has_index(i))
      qoi_count++;
  return qoi_count;
}

void QoISet::add_indices(const std::vector<unsigned int> &indices)
{
  unsigned int max_size = 0;
  for (std::vector<unsigned int>::const_iterator i = indices.begin();
       i != indices.end(); ++i)
    max_size = std::max(max_size, *i + 1);

  _indices.resize(max_size);

  for (std::vector<unsigned int>::const_iterator i = indices.begin();
       i != indices.end(); ++i)
    _indices[*i] = true;
}

inline
void QoISet::remove_indices(const std::vector<unsigned int> &indices)
{
  for (std::vector<unsigned int>::const_iterator i = indices.begin();
       i != indices.end(); ++i)
    _indices[*i] = false;
}

} // namespace libMesh
<|endoftext|>


<commit_before>/*
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "tango-plane-fitting/point_cloud_renderer.h"

#include <tango-gl/conversions.h>
#include <tango_support_api.h>

#include "tango-plane-fitting/plane_fitting.h"

namespace tango_plane_fitting {

namespace {

const std::string kPointCloudVertexShader =
    "precision mediump float;\n"
    "attribute vec4 vertex;\n"
    "uniform mat4 mvp;\n"
    "uniform vec4 plane1;\n"
    "uniform vec4 plane2;\n"
    "uniform vec4 plane3;\n"
    "uniform float plane_distance;\n"
    "uniform int plane_count;\n"
    "varying vec3 v_color;\n"
    ""
    "void main() {\n"
    "  gl_PointSize = 7.0;\n"
    "  gl_Position =  mvp*vertex;\n"
    "  "
    "  float d1 = dot(plane1, vertex);\n"
    "  float d2 = dot(plane2, vertex);\n"
    "  float d3 = dot(plane3, vertex);\n"
    "  if (plane_count > 0 && abs(d1) < (plane_distance * plane_distance)) {\n"
    "    v_color = vec3(0.0, 1.0, 0.0);\n"
    "  } else if (plane_count > 1 && abs(d2) < (plane_distance * "
    "plane_distance)) {\n"
    "    v_color = vec3(1.0, 1.0, 0.0);\n"
    "  } else if (plane_count > 2 && abs(d3) < (plane_distance * "
    "plane_distance)) {\n"
    "    v_color = vec3(1.0, 0.5, 0.0);\n"
    "  } else if (d1 < 0.0) {\n"
    "    v_color = vec3(1.0, 0.0, 0.0);\n"
    "  } else {\n"
    "    v_color = vec3(0.0, 0.0, 1.0);\n"
    "  }\n"
    "}\n";
const std::string kPointCloudFragmentShader =
    "precision mediump float;\n"
    "varying vec3 v_color;\n"
    "void main() {\n"
    "  gl_FragColor = vec4(v_color, 1.0);\n"
    "}\n";

}  // namespace

PointCloudRenderer::PointCloudRenderer()
    : plane_distance_(0.05f),
      debug_colors_(false),
      plane_count_(0),
      plane_model_({glm::vec4(0.0, 0.0, 1.0, 0.0),
                    glm::vec4(0.0, 0.0, 1.0, 0.0),
                    glm::vec4(0.0, 0.0, 1.0, 0.0)}) {
  shader_program_ = tango_gl::util::CreateProgram(
      kPointCloudVertexShader.c_str(), kPointCloudFragmentShader.c_str());

  glGenBuffers(1, &vertex_buffer_);

  mvp_handle_ = glGetUniformLocation(shader_program_, "mvp");
  vertices_handle_ = glGetAttribLocation(shader_program_, "vertex");
  plane_handle_[0] = glGetUniformLocation(shader_program_, "plane1");
  plane_handle_[1] = glGetUniformLocation(shader_program_, "plane2");
  plane_handle_[2] = glGetUniformLocation(shader_program_, "plane3");
  plane_distance_handle_ =
      glGetUniformLocation(shader_program_, "plane_distance");
  plane_count_handle_ = glGetUniformLocation(shader_program_, "plane_count");

  tango_gl::util::CheckGlError("PointCloudRenderer::Construction");
}

PointCloudRenderer::~PointCloudRenderer() {}

void PointCloudRenderer::DeleteGLResources() {
  glDeleteProgram(shader_program_);
  glDeleteBuffers(0, &vertex_buffer_);
}

void PointCloudRenderer::Render(const glm::mat4& projection_T_depth,
                                const glm::mat4& opengl_T_depth,
                                const TangoPointCloud* point_cloud) {
  if (!debug_colors_) {
    return;
  }

  glUseProgram(shader_program_);

  const size_t number_of_vertices = point_cloud->num_points;

  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
  glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4 * number_of_vertices,
               point_cloud->points[0], GL_STATIC_DRAW);

  const glm::mat4 depth_T_opengl = glm::inverse(opengl_T_depth);

  int plane_count = static_cast<int>(plane_count_);
  glUniform1i(plane_count_handle_, plane_count);

  // Transform plane into depth camera coordinates.
  glm::vec4 camera_plane;
  for (int i = 0; i < plane_count; ++i) {
    PlaneTransform(plane_model_[i], depth_T_opengl, &camera_plane);
    glUniform4fv(plane_handle_[i], 1, glm::value_ptr(camera_plane));
  }

  glUniformMatrix4fv(mvp_handle_, 1, GL_FALSE,
                     glm::value_ptr(projection_T_depth));

  // It looks better to have more points colored by the plane than the number
  // needed to be a good inlier support for fitting. Scale the distance here.
  constexpr float kDistanceScale = 5.0f;
  glUniform1f(plane_distance_handle_, kDistanceScale * plane_distance_);

  glEnableVertexAttribArray(vertices_handle_);
  glVertexAttribPointer(vertices_handle_, 4, GL_FLOAT, GL_FALSE, 0, nullptr);

  glDrawArrays(GL_POINTS, 0, number_of_vertices);

  glDisableVertexAttribArray(vertices_handle_);
  glBindBuffer(GL_ARRAY_BUFFER, 0);

  glUseProgram(0);
  tango_gl::util::CheckGlError("PointCloudRenderer::Render");
}

}  // namespace tango_plane_fitting
<commit_msg>release gankino<commit_after>/*
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "tango-plane-fitting/point_cloud_renderer.h"

#include <tango-gl/conversions.h>
#include <tango_support_api.h>

#include "tango-plane-fitting/plane_fitting.h"

namespace tango_plane_fitting {

namespace {

const std::string kPointCloudVertexShader =
    "precision mediump float;\n"
    "attribute vec4 vertex;\n"
    "uniform mat4 mvp;\n"
    "uniform vec4 plane1;\n"
    "uniform vec4 plane2;\n"
    "uniform vec4 plane3;\n"
    "uniform float plane_distance;\n"
    "uniform int plane_count;\n"
    "varying vec3 v_color;\n"
    ""
    "void main() {\n"
    "  gl_PointSize = 7.0;\n"
    "  gl_Position =  mvp*vertex;\n"
    "  "
    "  float d1 = dot(plane1, vertex);\n"
    "  float d2 = dot(plane2, vertex);\n"
    "  float d3 = dot(plane3, vertex);\n"
    "  if (plane_count > 0 && abs(d1) < (plane_distance * plane_distance)) {\n"
    "    v_color = vec3(0.0, 1.0, 0.0);\n"
    "  } else if (plane_count > 1 && abs(d2) < (plane_distance * "
    "plane_distance)) {\n"
    "    v_color = vec3(1.0, 1.0, 0.0);\n"
    "  } else if (plane_count > 2 && abs(d3) < (plane_distance * "
    "plane_distance)) {\n"
    "    v_color = vec3(1.0, 0.5, 0.0);\n"
    "  } else if (d1 < 0.0) {\n"
    "    v_color = vec3(1.0, 0.0, 0.0);\n"
    "  } else {\n"
    "    v_color = vec3(0.0, 0.0, 1.0);\n"
    "  }\n"
    "}\n";
const std::string kPointCloudFragmentShader =
    "precision mediump float;\n"
    "varying vec3 v_color;\n"
    "void main() {\n"
    "  gl_FragColor = vec4(v_color, 1.0);\n"
    "}\n";

}  // namespace

PointCloudRenderer::PointCloudRenderer()
    : plane_distance_(0.05f),
      debug_colors_(false),
      plane_count_(0),
      plane_model_{glm::vec4(0.0, 0.0, 1.0, 0.0),
                   glm::vec4(0.0, 0.0, 1.0, 0.0),
                   glm::vec4(0.0, 0.0, 1.0, 0.0)} {
  shader_program_ = tango_gl::util::CreateProgram(
      kPointCloudVertexShader.c_str(), kPointCloudFragmentShader.c_str());

  glGenBuffers(1, &vertex_buffer_);

  mvp_handle_ = glGetUniformLocation(shader_program_, "mvp");
  vertices_handle_ = glGetAttribLocation(shader_program_, "vertex");
  plane_handle_[0] = glGetUniformLocation(shader_program_, "plane1");
  plane_handle_[1] = glGetUniformLocation(shader_program_, "plane2");
  plane_handle_[2] = glGetUniformLocation(shader_program_, "plane3");
  plane_distance_handle_ =
      glGetUniformLocation(shader_program_, "plane_distance");
  plane_count_handle_ = glGetUniformLocation(shader_program_, "plane_count");

  tango_gl::util::CheckGlError("PointCloudRenderer::Construction");
}

PointCloudRenderer::~PointCloudRenderer() {}

void PointCloudRenderer::DeleteGLResources() {
  glDeleteProgram(shader_program_);
  glDeleteBuffers(0, &vertex_buffer_);
}

void PointCloudRenderer::Render(const glm::mat4& projection_T_depth,
                                const glm::mat4& opengl_T_depth,
                                const TangoPointCloud* point_cloud) {
  if (!debug_colors_) {
    return;
  }

  glUseProgram(shader_program_);

  const size_t number_of_vertices = point_cloud->num_points;

  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
  glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4 * number_of_vertices,
               point_cloud->points[0], GL_STATIC_DRAW);

  const glm::mat4 depth_T_opengl = glm::inverse(opengl_T_depth);

  int plane_count = static_cast<int>(plane_count_);
  glUniform1i(plane_count_handle_, plane_count);

  // Transform plane into depth camera coordinates.
  glm::vec4 camera_plane;
  for (int i = 0; i < plane_count; ++i) {
    PlaneTransform(plane_model_[i], depth_T_opengl, &camera_plane);
    glUniform4fv(plane_handle_[i], 1, glm::value_ptr(camera_plane));
  }

  glUniformMatrix4fv(mvp_handle_, 1, GL_FALSE,
                     glm::value_ptr(projection_T_depth));

  // It looks better to have more points colored by the plane than the number
  // needed to be a good inlier support for fitting. Scale the distance here.
  constexpr float kDistanceScale = 5.0f;
  glUniform1f(plane_distance_handle_, kDistanceScale * plane_distance_);

  glEnableVertexAttribArray(vertices_handle_);
  glVertexAttribPointer(vertices_handle_, 4, GL_FLOAT, GL_FALSE, 0, nullptr);

  glDrawArrays(GL_POINTS, 0, number_of_vertices);

  glDisableVertexAttribArray(vertices_handle_);
  glBindBuffer(GL_ARRAY_BUFFER, 0);

  glUseProgram(0);
  tango_gl::util::CheckGlError("PointCloudRenderer::Render");
}

}  // namespace tango_plane_fitting
<|endoftext|>


<commit_before><commit_msg>Remove unneeded #include<commit_after><|endoftext|>


<commit_before>#include "tcachefactory.h"
#include "tcachemongostore.h"
#include "tcacheredisstore.h"
#include "tcachesqlitestore.h"
#include "tcachememorystore.h"
#include "tsystemglobal.h"
#include <QDir>
#include <TAppSettings>

namespace {
QString SQLITE_CACHE_KEY;
QString MONGO_CACHE_KEY;
QString REDIS_CACHE_KEY;
QString MEMORY_CACHE_KEY;
}

QStringList TCacheFactory::keys()
{
    loadCacheKeys();

    QStringList ret;
    ret << SQLITE_CACHE_KEY
        << MONGO_CACHE_KEY
        << REDIS_CACHE_KEY
        << MEMORY_CACHE_KEY;
    return ret;
}

TCacheStore *TCacheFactory::create(const QString &key)
{
    TCacheStore *ptr = nullptr;
    loadCacheKeys();

    QString k = key.toLower();
    if (k == SQLITE_CACHE_KEY) {
        ptr = new TCacheSQLiteStore;
    } else if (k == MONGO_CACHE_KEY) {
        ptr = new TCacheMongoStore;
    } else if (k == REDIS_CACHE_KEY) {
        ptr = new TCacheRedisStore;
    } else if (k == MEMORY_CACHE_KEY) {
        ptr = new TCacheMemoryStore;
    } else {
        tSystemError("Not found cache store: %s", qUtf8Printable(key));
    }

    return ptr;
}

void TCacheFactory::destroy(const QString &key, TCacheStore *store)
{
    loadCacheKeys();

    QString k = key.toLower();
    if (k == SQLITE_CACHE_KEY) {
        delete store;
    } else if (k == MONGO_CACHE_KEY) {
        delete store;
    } else if (k == REDIS_CACHE_KEY) {
        delete store;
    } else if (k == MEMORY_CACHE_KEY) {
        delete store;
    } else {
        delete store;
    }
}

QMap<QString, QVariant> TCacheFactory::defaultSettings(const QString &key)
{
    QMap<QString, QVariant> settings;
    loadCacheKeys();

    QString k = key.toLower();
    if (k == SQLITE_CACHE_KEY) {
        settings = TCacheSQLiteStore().defaultSettings();
    } else if (k == MONGO_CACHE_KEY) {
        settings = TCacheMongoStore().defaultSettings();
    } else if (k == REDIS_CACHE_KEY) {
        settings = TCacheRedisStore().defaultSettings();
    } else if (k == MEMORY_CACHE_KEY) {
        settings = TCacheMemoryStore().defaultSettings();
    } else {
        // Invalid key
    }
    return settings;
}

TCacheStore::DbType TCacheFactory::dbType(const QString &key)
{
    TCacheStore::DbType type = TCacheStore::Invalid;
    loadCacheKeys();

    TCacheStore *store = create(key);
    if (store) {
        type = store->dbType();
        destroy(key, store);
    }
    return type;
}

bool TCacheFactory::loadCacheKeys()
{
    static bool done = []() {
        SQLITE_CACHE_KEY = TCacheSQLiteStore().key().toLower();
        MONGO_CACHE_KEY = TCacheMongoStore().key().toLower();
        REDIS_CACHE_KEY = TCacheRedisStore().key().toLower();
        MEMORY_CACHE_KEY = TCacheMemoryStore().key().toLower();
        return true;
    }();
    return done;
}
<commit_msg>bugfixes<commit_after>#include "tcachefactory.h"
#include "tcachemongostore.h"
#include "tcacheredisstore.h"
#include "tcachesqlitestore.h"
#include "tcachememorystore.h"
#include "tcachememcachedstore.h"
#include "tsystemglobal.h"
#include <QDir>
#include <TAppSettings>

namespace {
QString SQLITE_CACHE_KEY;
QString MONGO_CACHE_KEY;
QString REDIS_CACHE_KEY;
QString MEMCACHED_CACHE_KEY;
QString MEMORY_CACHE_KEY;
}

QStringList TCacheFactory::keys()
{
    loadCacheKeys();

    QStringList ret;
    ret << SQLITE_CACHE_KEY
        << MONGO_CACHE_KEY
        << REDIS_CACHE_KEY
        << MEMCACHED_CACHE_KEY
        << MEMORY_CACHE_KEY;
    return ret;
}

TCacheStore *TCacheFactory::create(const QString &key)
{
    loadCacheKeys();

    TCacheStore *ptr = nullptr;
    QString k = key.toLower();
    if (k == SQLITE_CACHE_KEY) {
        ptr = new TCacheSQLiteStore;
    } else if (k == MONGO_CACHE_KEY) {
        ptr = new TCacheMongoStore;
    } else if (k == REDIS_CACHE_KEY) {
        ptr = new TCacheRedisStore;
    } else if (k == MEMCACHED_CACHE_KEY) {
        ptr = new TCacheMemcachedStore;
    } else if (k == MEMORY_CACHE_KEY) {
        ptr = new TCacheMemoryStore;
    } else {
        tSystemError("Not found cache store: %s", qUtf8Printable(key));
    }
    return ptr;
}

void TCacheFactory::destroy(const QString &key, TCacheStore *store)
{
    loadCacheKeys();

    QString k = key.toLower();
    if (k == SQLITE_CACHE_KEY) {
        delete store;
    } else if (k == MONGO_CACHE_KEY) {
        delete store;
    } else if (k == REDIS_CACHE_KEY) {
        delete store;
    } else if (k == MEMCACHED_CACHE_KEY) {
        delete store;
    } else if (k == MEMORY_CACHE_KEY) {
        delete store;
    } else {
        delete store;
    }
}

QMap<QString, QVariant> TCacheFactory::defaultSettings(const QString &key)
{
    loadCacheKeys();

    QMap<QString, QVariant> settings;
    QString k = key.toLower();
    if (k == SQLITE_CACHE_KEY) {
        settings = TCacheSQLiteStore().defaultSettings();
    } else if (k == MONGO_CACHE_KEY) {
        settings = TCacheMongoStore().defaultSettings();
    } else if (k == REDIS_CACHE_KEY) {
        settings = TCacheRedisStore().defaultSettings();
    } else if (k == MEMCACHED_CACHE_KEY) {
        settings = TCacheMemcachedStore().defaultSettings();
    } else if (k == MEMORY_CACHE_KEY) {
        settings = TCacheMemoryStore().defaultSettings();
    } else {
        // Invalid key
    }
    return settings;
}

TCacheStore::DbType TCacheFactory::dbType(const QString &key)
{
    loadCacheKeys();

    TCacheStore::DbType type = TCacheStore::Invalid;
    TCacheStore *store = create(key);
    if (store) {
        type = store->dbType();
        destroy(key, store);
    }
    return type;
}

bool TCacheFactory::loadCacheKeys()
{
    static bool done = []() {
        SQLITE_CACHE_KEY = TCacheSQLiteStore().key().toLower();
        MONGO_CACHE_KEY = TCacheMongoStore().key().toLower();
        REDIS_CACHE_KEY = TCacheRedisStore().key().toLower();
        MEMCACHED_CACHE_KEY = TCacheMemcachedStore().key().toLower();
        MEMORY_CACHE_KEY = TCacheMemoryStore().key().toLower();
        return true;
    }();
    return done;
}
<|endoftext|>


<commit_before><commit_msg>INTEGRATION: CWS uaa02 (1.18.8); FILE MERGED 2003/04/11 17:00:13 mt 1.18.8.1: #108656# Moved accessibility from drafts to final<commit_after><|endoftext|>


<commit_before>//(c) 2016 by Authors
//This file is a part of ABruijn program.
//Released under the BSD license (see LICENSE file)

#include "parameters_estimator.h"
#include "../common/logger.h"

int ParametersEstimator::genomeSizeEstimate()
{
	/*
	int kmersNeeded = 0;
	for (auto& seqPair : _seqContainer.getIndex())
	{
		kmersNeeded += _seqContainer.seqLen(seqPair.first) / _coverage;
	}
	return kmersNeeded / 2;*/
	return _takenKmers;
}

void ParametersEstimator::estimateMinKmerCount(int upperCutoff)
{
	const int MIN_CUTOFF = 2;

	int kmersNeeded = 0;
	for (auto& seqPair : _seqContainer.getIndex())
	{
		kmersNeeded += _seqContainer.seqLen(seqPair.first) / 2 / _coverage;
	}
	Logger::get().debug() << "Genome size estimate: " << kmersNeeded;

	int takenKmers = 0;
	int cutoff = 0;
	int repetitiveKmers = 0;
	int prevDiff = 0;
	for (auto mapPair = _vertexIndex.getKmerHist().rbegin();
		 mapPair != _vertexIndex.getKmerHist().rend(); ++mapPair)
	{
		if (mapPair->first <= upperCutoff)
		{
			takenKmers += mapPair->second;
			if (takenKmers >= kmersNeeded)
			{
				if (abs(takenKmers - kmersNeeded) < prevDiff)
				{
					cutoff = mapPair->first;
				}
				else
				{
					cutoff = mapPair->first + 1;
					takenKmers -= mapPair->second;
				}
				break;
			}
			prevDiff = abs(takenKmers - kmersNeeded);
		}
		else
		{
			repetitiveKmers += mapPair->second;
		}
	}

	if (cutoff < 2)
	{
		Logger::get().warning() << "Unable to choose minimum kmer count cutoff."
					  " Check if the coverage parameter is correct. "
					  "Running with the default parameter t = " << MIN_CUTOFF;
		cutoff = MIN_CUTOFF;
	}

	Logger::get().debug() << "Filtered " << repetitiveKmers
						  << " repetitive kmers";
	Logger::get().debug() << "Estimated minimum kmer coverage: " << cutoff
						  << ", " << takenKmers << " unique kmers selected";

	_takenKmers = takenKmers;
	_minKmerCount = cutoff;
}
<commit_msg>slightly updated logging<commit_after>//(c) 2016 by Authors
//This file is a part of ABruijn program.
//Released under the BSD license (see LICENSE file)

#include "parameters_estimator.h"
#include "../common/logger.h"

int ParametersEstimator::genomeSizeEstimate()
{
	/*
	int kmersNeeded = 0;
	for (auto& seqPair : _seqContainer.getIndex())
	{
		kmersNeeded += _seqContainer.seqLen(seqPair.first) / _coverage;
	}
	return kmersNeeded / 2;*/
	return _takenKmers;
}

void ParametersEstimator::estimateMinKmerCount(int upperCutoff)
{
	const int MIN_CUTOFF = 2;

	int kmersNeeded = 0;
	for (auto& seqPair : _seqContainer.getIndex())
	{
		kmersNeeded += _seqContainer.seqLen(seqPair.first) / 2 / _coverage;
	}
	Logger::get().debug() << "Genome size estimate: " << kmersNeeded;

	int takenKmers = 0;
	int cutoff = 0;
	int repetitiveKmers = 0;
	int prevDiff = 0;
	for (auto mapPair = _vertexIndex.getKmerHist().rbegin();
		 mapPair != _vertexIndex.getKmerHist().rend(); ++mapPair)
	{
		if (mapPair->first <= upperCutoff)
		{
			takenKmers += mapPair->second;
			if (takenKmers >= kmersNeeded)
			{
				if (abs(takenKmers - kmersNeeded) < prevDiff)
				{
					cutoff = mapPair->first;
				}
				else
				{
					cutoff = mapPair->first + 1;
					takenKmers -= mapPair->second;
				}
				break;
			}
			prevDiff = abs(takenKmers - kmersNeeded);
		}
		else
		{
			repetitiveKmers += mapPair->second;
		}
	}

	if (cutoff < 2)
	{
		Logger::get().warning() << "Unable to separate erroneous k-mers "
					  "from solid k-mers. Possible reasons: \n"
					  "\t(1) Incorrect expected assembly size parameter \n"
					  "\t(2) Highly uneven coverage of the assembly \n"
					  "\t(3) Running with error-corrected reads "
					  "(please use raw reads instead)\n"
					  "\t(4) Assembling big genome with small k-mer size "
					  "(try to increase it manually).\n"
					  "\tAssembly will continue, but results might not be optimal";
		cutoff = MIN_CUTOFF;
	}

	Logger::get().debug() << "Filtered " << repetitiveKmers
						  << " repetitive kmers";
	Logger::get().debug() << "Estimated minimum kmer coverage: " << cutoff
						  << ", " << takenKmers << " unique kmers selected";

	_takenKmers = takenKmers;
	_minKmerCount = cutoff;
}
<|endoftext|>


<commit_before>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MIDDLEWARE_COOKIE_PARSER_HPP
#define MIDDLEWARE_COOKIE_PARSER_HPP

#include "../cookie/cookie.hpp"
#include "middleware.hpp"

namespace middleware {

/**
 * @brief A way to parse cookies: Reading cookies that the browser is sending to the server
 * @details
 *
 */

// hpp-declarations first and implement methods further down? Or in own cpp-file?

class CookieParser : public server::Middleware {
public:
  virtual void process(server::Request_ptr req, server::Response_ptr res, server::Next next) override;

  // Just name this method cookie?
  // Return bool or void?
  bool create_cookie(std::string& key, std::string& value);  // new Cookie(...) and add_header: Set-Cookie ?

  // Just name this method cookie?
  // Return bool or void?
  bool create_cookie(std::string& key, std::string& value, std::string& options);  // new Cookie(...) and add_header: Set-Cookie ?
// options: map eller enum

  // Return bool or void?
  bool clear_cookie(std::string& key); // remove Cookie from client

private:
  bool has_cookie(server::Request_ptr req) const noexcept;
};

inline void CookieParser::process(server::Request_ptr req, server::Response_ptr res, server::Next next) {
    if(not has_cookie(req)) {
      // No Cookie in header field: We want to create a cookie then??:
      // create cookie:
      // ...

      return (*next)();
    } else {
      // Found Cookie in header
      // Do something
    }
}

inline bool CookieParser::has_cookie(server::Request_ptr req) const noexcept {
  return req->has_header(http::header_fields::Request::Cookie);
}

} //< namespace middleware

#endif
<commit_msg>Removed unnecessary comment<commit_after>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MIDDLEWARE_COOKIE_PARSER_HPP
#define MIDDLEWARE_COOKIE_PARSER_HPP

#include "../cookie/cookie.hpp"
#include "middleware.hpp"

namespace middleware {

/**
 * @brief A way to parse cookies: Reading cookies that the browser is sending to the server
 * @details
 *
 */
class CookieParser : public server::Middleware {
public:
  virtual void process(server::Request_ptr req, server::Response_ptr res, server::Next next) override;

  // Just name this method cookie?
  // Return bool or void?
  bool create_cookie(std::string& key, std::string& value);  // new Cookie(...) and add_header: Set-Cookie ?

  // Just name this method cookie?
  // Return bool or void?
  bool create_cookie(std::string& key, std::string& value, std::string& options);  // new Cookie(...) and add_header: Set-Cookie ?
// options: map eller enum

  // Return bool or void?
  bool clear_cookie(std::string& key); // remove Cookie from client

private:
  bool has_cookie(server::Request_ptr req) const noexcept;
};

inline void CookieParser::process(server::Request_ptr req, server::Response_ptr res, server::Next next) {
    if(not has_cookie(req)) {
      // No Cookie in header field: We want to create a cookie then??:
      // create cookie:
      // ...

      return (*next)();
    } else {
      // Found Cookie in header
      // Do something
    }
}

inline bool CookieParser::has_cookie(server::Request_ptr req) const noexcept {
  return req->has_header(http::header_fields::Request::Cookie);
}

} //< namespace middleware

#endif
<|endoftext|>