text:
/*
    Copyright (c) 2014, Philipp Krähenbühl
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
        * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
        * Neither the name of the Stanford University nor the
        names of its contributors may be used to endorse or promote products
        derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY Philipp Krähenbühl ''AS IS'' AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL Philipp Krähenbühl BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "color.h"
#include "util/util.h"
#include "util/sse_defs.h"
#include <vector>
#include <Eigen/Core>
using namespace Eigen;
#include <emmintrin.h>

template<typename F>
std::vector<float> initTable( F f, int N, float s ) {
	std::vector<float> r( N );
	for( int i=0; i<N; i++ )
		r[i] = f(s*i);
	return r;
}
static const std::vector<float> ltable = initTable( [](float y){ return (y > ((6.0/29)*(6.0/29)*(6.0/29)) ? 116*pow((double)y,1.0/3.0)-16 : y*((29.0/3)*(29.0/3)*(29.0/3)))/270.; }, 1025, 1.0 / 1024. );
static const std::vector<float> stable = initTable( [](float v){ return (v < 0.4045f ? v / 12.92f : pow( (v+0.055f)/1.055f, 2.4f ) ); }, 1025, 1.0 / 1024. );

inline float mapTable( float v, const float * t, int N ) {
	if( v < 0 )  v = 0;
	if( v > N-1 ) v = N-1;
	return t[(int)v];
}
template<typename T>
T min( T a, T b ) {
	return std::min(a,b);
}
template<> __m128 min<__m128>( __m128 a, __m128 b ) {
	return _mm_min_ps(a,b);
}
template<typename T>
T max( T a, T b ) {
	return std::max(a,b);
}
template<> __m128 max<__m128>( __m128 a, __m128 b ) {
	return _mm_max_ps(a,b);
}
template<typename T>
static T c( float a ) {
	return a;
}
template<> __m128 c<__m128>( float a ) {
	return _mm_set1_ps( a );
}
inline __m128 mapTable( __m128 v, const float * t, int N ) {
	float * vv = (float*)&v;
	return _mm_set_ps( mapTable( vv[3], t, N ), mapTable( vv[2], t, N ), mapTable( vv[1], t, N ), mapTable( vv[0], t, N ) );
}
template<typename T>
void rgb2xyz( T & x, T & y, T & z, const T & r, const T & g, const T & b ) {
	x = c<T>(0.430574f)*r + c<T>(0.341550f)*g + c<T>(0.178325f)*b;
	y = c<T>(0.222015f)*r + c<T>(0.706655f)*g + c<T>(0.071330f)*b;
	z = c<T>(0.020183f)*r + c<T>(0.129553f)*g + c<T>(0.939180f)*b;
}
template<typename T>
T gcorr( const T & v ) {
	return mapTable( c<T>(1024)*v, stable.data(), stable.size() );
}
template<typename T>
void srgb2xyz( T & x, T & y, T & z, T r, T g, T b ) {
	r = gcorr( r );
	g = gcorr( g );
	b = gcorr( b );
	x = c<T>(0.4124564f)*r + c<T>(0.3575761f)*g + c<T>(0.1804375f)*b;
	y = c<T>(0.2126729f)*r + c<T>(0.7151522f)*g + c<T>(0.0721750f)*b;
	z = c<T>(0.0193339f)*r + c<T>(0.1191920f)*g + c<T>(0.9503041f)*b;
}
template<typename T> void xyz2luv( T& l, T& u, T& v, T x, T y, T z ) {
	const T un = c<T>(0.197833f), vn = c<T>(0.468331f);
	l = mapTable( c<T>(1024.f)*y, ltable.data(), ltable.size() );
	z = c<T>(1.f) / (x + c<T>(15.f)*y + c<T>(3.f)*z + c<T>(1e-35f) );
	u = l * (c<T>(13.f)*(c<T>(4.f)*x*z - un)) + c<T>(88.f/270.f);
	v = l * (c<T>(13.f)*(c<T>(9.f)*y*z - vn)) + c<T>(134.f/270.f);
}
template<typename T> void xyz2lab( T& l, T& a, T& b, T x, T y, T z ) {
	x *= c<T>(1.f/0.950456f);
	z *= c<T>(1.f/1.088754f);
	x = mapTable( c<T>(1024)*x, ltable.data(), ltable.size() );
	y = mapTable( c<T>(1024)*y, ltable.data(), ltable.size() );
	z = mapTable( c<T>(1024)*z, ltable.data(), ltable.size() );
	l = y;
	a = c<T>(500.f/116.f)*(x-y);
	b = c<T>(200.f/116.f)*(y-z);
}
enum ColorType{
	LUV,
	LAB
};
template<bool SRGB,ColorType type,typename T>
static void convertRGB( T& c1, T& c2, T& c3, const T & R, const T & G, const T & B ) {
	T x,y,z;
	if (SRGB)
		srgb2xyz( x,y,z, R,G,B );
	else
		rgb2xyz( x,y,z, R,G,B );
	if (type==LUV)
		xyz2luv( c1,c2,c3, x,y,z );
	else
		xyz2lab( c1,c2,c3, x,y,z );
}
template<bool SRGB,ColorType type>
static void convertRGB( Image & luv, const Image & rgb ) {
	if( rgb.C()!= 3 )
		throw std::invalid_argument( "RGB image required!" );
	const int W = rgb.W(), H = rgb.H();
	luv.create( W, H, 3 );
	int i;
	for( i=0; i+3<W*H; i+=4 ) {
		__m128 r = _mm_set_ps( rgb[3*i+0], rgb[3*(i+1)+0], rgb[3*(i+2)+0], rgb[3*(i+3)+0] );
		__m128 g = _mm_set_ps( rgb[3*i+1], rgb[3*(i+1)+1], rgb[3*(i+2)+1], rgb[3*(i+3)+1] );
		__m128 b = _mm_set_ps( rgb[3*i+2], rgb[3*(i+1)+2], rgb[3*(i+2)+2], rgb[3*(i+3)+2] );
		__m128 l,u,v;
		convertRGB<SRGB,type>( l, u, v, r, g, b );
		float * ll = (float*)&l, * uu = (float*)&u, * vv = (float*)&v;
		for( int k=0; k<4; k++ ){
			luv[3*(i+k)+0] = ll[k];
			luv[3*(i+k)+1] = uu[k];
			luv[3*(i+k)+2] = vv[k];
		}
	}
	for( i=0; i<W*H; i++ )
		convertRGB<SRGB,type>( luv[3*i+0],luv[3*i+1],luv[3*i+2], rgb[3*i+0],rgb[3*i+1],rgb[3*i+2] );
}
void rgb2luv( Image & luv, const Image & rgb ) {
	convertRGB<false,LUV>( luv, rgb );
}
void srgb2luv( Image & luv, const Image & rgb ) {
	convertRGB<true,LUV>( luv, rgb );
}
void rgb2lab( Image & lab, const Image & rgb ) {
	convertRGB<false,LAB>( lab, rgb );
}
void srgb2lab( Image & lab, const Image & rgb ) {
	convertRGB<true,LAB>( lab, rgb );
}
void rgb2hsv( Image & hsv, const Image & rgb ) {
	if( rgb.C()!= 3 )
		throw std::invalid_argument( "RGB image required!" );
	const int W = rgb.W(), H = rgb.H();
	hsv.create( W, H, 3 );
	const int N = W*H;
	for( int i=0; i<N; i++ ) {
		float s,v;
		hsv[3*i+2] = v = std::max(rgb[3*i+0],std::max(rgb[3*i+1],rgb[3*i+2]));
		hsv[3*i+1] = s = v-std::min(rgb[3*i+0],std::min(rgb[3*i+1],rgb[3*i+2]));
		if( v == rgb[3*i+0] )
			hsv[3*i+0] = (rgb[3*i+1] - rgb[3*i+2]) / (6.*s+1e-10);
		else if( v == rgb[3*i+1] )
			hsv[3*i+0] = (2+rgb[3*i+2] - rgb[3*i+0]) / (6.*s+1e-10);
		else if( v == rgb[3*i+2] )
			hsv[3*i+0] = (4+rgb[3*i+0] - rgb[3*i+1]) / (6.*s+1e-10);
		if( hsv[3*i+0] < 0 )
			hsv[3*i+0] += 1;
	}
}

ext: cpp

max_stars_repo_name: PingCheng-Wei/SD-MaskRCNN

max_issues_repo_name: PingCheng-Wei/SD-MaskRCNN

max_forks_repo_name: PingCheng-Wei/SD-MaskRCNN


text:
/**
 * This file cannot be built without CombBLAS and Elemental both being present.
 * The accompanying CMakeLists.txt checks for both libraries to be available.
 */
#include <iostream>
#include <functional>
#include <cstring>
#include <vector>
#include <utility>
#include <ext/hash_map>

#include <elemental.hpp>
#include <CombBLAS.h>
#include <boost/mpi.hpp>
#include <skylark.hpp>

#include "utilities.hpp"
#include "parser.hpp"

/*******************************************/
namespace bmpi =  boost::mpi;
namespace skys =  skylark::sketch;
namespace skyb =  skylark::base;
namespace skyalg = skylark::algorithms;
namespace skynla = skylark::nla;
namespace skyutil = skylark::utility;
/*******************************************/

/* These were declared as extern in utilities.hpp --- defining it here */
int int_params [NUM_INT_PARAMETERS];
char* chr_params[NUM_CHR_PARAMETERS];

/** Typedef DistMatrix and Matrix */
typedef std::vector<int> IntContainer;
typedef std::vector<double> DblContainer;
typedef elem::Matrix<double> DenseMatrixType;
typedef elem::DistMatrix<double> DenseDistMatrixType;

typedef SpDCCols<int, double> SparseMatrixType;
typedef SpParMat<int, double, SparseMatrixType> SparseDistMatrixType;
typedef FullyDistVec<int, double> SparseVectorType;
typedef FullyDistMultiVec<int, double> SparseMultiVectorType;

typedef skyutil::uniform_matrix_t<DenseDistMatrixType> uni_dense_dist_mat_t;
typedef skyutil::uniform_matrix_t<SparseDistMatrixType> uni_sparse_dist_mat_t;
typedef skyutil::uniform_matrix_t<SparseMultiVectorType>
                                                  uni_sparse_dist_multi_vec_t;

typedef skyutil::empty_matrix_t<DenseDistMatrixType> empty_dense_dist_mat_t;
typedef skyutil::empty_matrix_t<SparseDistMatrixType> empty_sparse_dist_mat_t;
typedef skyutil::empty_matrix_t<SparseMultiVectorType>
                                                  empty_sparse_dist_multi_vec_t;

typedef skyutil::print_t<SparseDistMatrixType> sparse_dist_mat_printer_t;
typedef skyutil::print_t<SparseMultiVectorType> sparse_dist_multi_vec_printer_t;
typedef skyutil::print_t<DenseDistMatrixType> dense_dist_mat_printer_t;

int main (int argc, char** argv) {

  /* Initilize MPI manually to make sure that threading is supported */
  int provided;
  MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);

  /* Create a global communicator */
  bmpi::communicator world;

  /* MPI sends argc and argv everywhere --- parse everywhere */
  parse_parameters (argc,argv);

  /* Initialize skylark */
  skyb::context_t context (int_params[RAND_SEED_INDEX]);

  /** Only left sketch is supported for now */
  if (SKETCH_LEFT != int_params[SKETCH_DIRECTION_INDEX]) {
    std::cout << "We don't support reading --- yet --" << std::endl;
    goto END;
  }

  /** Only randomization is supported for now */
  if (0==int_params[USE_RANDOM_INDEX]) {
    /** TODO: Read the entries! */
    std::cout << "We don't support reading --- yet --" << std::endl;
    goto END;
  }

  if (0==strcmp("elem",chr_params[MATRIX_TYPE_INDEX])) {
    /** Run the test for elemental matrices */

    /* Initialize elemental */
    elem::Initialize (argc, argv);
    MPI_Comm mpi_world(world);
    elem::Grid grid (mpi_world);

    DenseDistMatrixType A=uni_dense_dist_mat_t::generate
            (int_params[M_INDEX], int_params[N_INDEX], grid, context);
    DenseDistMatrixType B=uni_dense_dist_mat_t::generate
            (int_params[M_INDEX], int_params[N_RHS_INDEX], grid, context);
    DenseDistMatrixType X(int_params[N_INDEX], int_params[N_RHS_INDEX], grid);

    /** Depending on which sketch is requested, do the sketching */
    DenseMatrixType sketch_A(int_params[S_INDEX], int_params[N_INDEX]);
    DenseMatrixType sketch_B(int_params[S_INDEX], int_params[N_RHS_INDEX]);
    DenseMatrixType sketch_X(int_params[N_INDEX], int_params[N_RHS_INDEX]);

    /***********************************************************************/
    /* Hack to make sure that we can sketch matrices with MC,MR distributn */
    /***********************************************************************/
    typedef elem::DistMatrix<double, elem::VC, elem::STAR> CheatDistMatrixType;
    CheatDistMatrixType A_cheat(A), B_cheat(B);
    /***********************************************************************/
    if (0==strcmp("JLT", chr_params[TRANSFORM_INDEX]) ) {
      skys::JLT_t<CheatDistMatrixType, DenseMatrixType>
        JLT (int_params[M_INDEX], int_params[S_INDEX], context);
      JLT.apply (A_cheat, sketch_A, skys::columnwise_tag());
      JLT.apply (B_cheat, sketch_B, skys::columnwise_tag());
    } else if (0==strcmp("FJLT", chr_params[TRANSFORM_INDEX]) ) {
      skys::FJLT_t<CheatDistMatrixType, DenseMatrixType>
        FJLT (int_params[M_INDEX], int_params[S_INDEX], context);
        FJLT.apply (A_cheat, sketch_A, skys::columnwise_tag());
        FJLT.apply (B_cheat, sketch_B, skys::columnwise_tag());
    } else if (0==strcmp("CWT", chr_params[TRANSFORM_INDEX]) ) {
      skys::CWT_t<CheatDistMatrixType, DenseMatrixType>
        Sparse (int_params[M_INDEX], int_params[S_INDEX], context);
      Sparse.apply (A_cheat, sketch_A, skys::columnwise_tag());
      Sparse.apply (B_cheat, sketch_B, skys::columnwise_tag());
    } else {
      std::cout << "We only have JLT/FJLT/CWT sketching" << std::endl;
    }
    A_cheat.Empty();
    B_cheat.Empty();

    /** Set up the iterative solver parameters */
    skynla::iter_params_t params(1e-14,  /* tolerance */
                                 world.rank()==0, /* only root prints */
                                 100, /* number of iterations */
                                 int_params[DEBUG_INDEX]-1); /* debugging */

    /** Solve the exact problem */
    DblContainer exact_norms(int_params[N_RHS_INDEX], 0.0);
    skyalg::regression_problem_t<skyalg::l2_tag, DenseDistMatrixType>
            exact_problem(int_params[M_INDEX], int_params[N_INDEX], A);
    skyalg::exact_regressor_t
        <skyalg::l2_tag,
         DenseDistMatrixType,
         DenseDistMatrixType,
         skyalg::iterative_l2_solver_tag<skyalg::lsqr_tag> >
         exact_regr(exact_problem);
    exact_regr.solve(B, X, params);
    skynla::iter_solver_op_t<DenseDistMatrixType, DenseDistMatrixType>::
                      residual_norms(A, B, X, exact_norms);

    if (1<int_params[DEBUG_INDEX] &&
        (int_params[M_INDEX]*int_params[N_INDEX])<100 &&
        world.rank() == 0) {
      elem::Display(A, "A");
      elem::Display(B, "B");
      elem::Display(X, "X");
    }

    /** Solve the sketched problem---change to using the sketched regressor */
    DblContainer sketched_norms(int_params[N_RHS_INDEX], 0.0);
    skyalg::regression_problem_t<skyalg::l2_tag, DenseMatrixType>
        sketched_problem(int_params[S_INDEX], int_params[N_INDEX], sketch_A);
    skyalg::exact_regressor_t
        <skyalg::l2_tag,
         DenseMatrixType,
         DenseMatrixType,
         skyalg::iterative_l2_solver_tag<skyalg::lsqr_tag> >
         sketched_regr(sketched_problem);
    sketched_regr.solve(sketch_B, sketch_X, params);
    DenseDistMatrixType sketch_X_global(X);
    skynla::iter_solver_op_t<DenseDistMatrixType, DenseDistMatrixType>::
                    make_dist_from_local (sketch_X, sketch_X_global);
    skynla::iter_solver_op_t<DenseDistMatrixType, DenseDistMatrixType>::
                      residual_norms(A, B, sketch_X_global, sketched_norms);

    if (1<int_params[DEBUG_INDEX] &&
        (int_params[M_INDEX]*int_params[N_INDEX])<100 &&
        world.rank() == 0) {
      dense_dist_mat_printer_t::apply (A, "A", true, 2);
      dense_dist_mat_printer_t::apply (B, "B", true, 2);
      dense_dist_mat_printer_t::apply (X, "X", true, 2);
    }

    /** Check the quality of the solution */
    if (0==world.rank()) {
      for (int i=0; i<int_params[N_RHS_INDEX]; ++i)
        printf ("For RHS (%d), exact norm = %lf, sketched norm=%lf\n",
                              i, exact_norms[i], sketched_norms[i]);
    }

    /** Finalize Elemental */
    elem::Finalize();

  } else {
    /** Run the test for CombBLAS matrices */

    /* Create matrices A and B */
    const int NNZ = int_params[M_INDEX]*int_params[N_INDEX]*0.5;
    SparseDistMatrixType A=uni_sparse_dist_mat_t::generate
            (int_params[M_INDEX], int_params[N_INDEX], NNZ, context);
    SparseMultiVectorType B=uni_sparse_dist_multi_vec_t::generate
            (int_params[M_INDEX], int_params[N_RHS_INDEX], context);
    SparseMultiVectorType X (int_params[N_INDEX], int_params[N_RHS_INDEX]);

    /** Depending on which sketch is requested, do the sketching */
    SparseDistMatrixType sketch_A=empty_sparse_dist_mat_t::generate
            (int_params[S_INDEX], int_params[N_INDEX]);
    SparseMultiVectorType sketch_B=empty_sparse_dist_multi_vec_t::generate
            (int_params[S_INDEX], int_params[N_RHS_INDEX]);
    SparseMultiVectorType sketch_X=empty_sparse_dist_multi_vec_t::generate
            (int_params[N_INDEX], int_params[N_RHS_INDEX]);
    if (0==strcmp("CWT", chr_params[TRANSFORM_INDEX]) ) {
      skys::CWT_t<SparseDistMatrixType, SparseDistMatrixType>
        CWT_mat (int_params[M_INDEX], int_params[S_INDEX], context);
      CWT_mat.apply (A, sketch_A, skys::columnwise_tag());
      skys::CWT_t<SparseMultiVectorType, SparseMultiVectorType>
        CWT_vec (CWT_mat);
      CWT_vec.apply (B, sketch_B, skys::columnwise_tag());
    } else {
      std::cout << "We only have CWT for sparse sketching" << std::endl;
    }

    /** Set up the iterative solver parameters */
    skynla::iter_params_t params(1e-14,  /* tolerance */
                                 world.rank()==0, /* only root prints */
                                 100, /* number of iterations */
                                 int_params[DEBUG_INDEX]-1); /* debugging */

    /** Solve the exact problem */
    DblContainer exact_norms(int_params[N_RHS_INDEX], 0.0);
    skyalg::regression_problem_t<skyalg::l2_tag, SparseDistMatrixType>
            exact_problem(int_params[M_INDEX], int_params[N_INDEX], A);
    skyalg::exact_regressor_t
        <skyalg::l2_tag,
         SparseDistMatrixType,
         SparseMultiVectorType,
         skyalg::iterative_l2_solver_tag<skyalg::lsqr_tag> >
         exact_regr(exact_problem);
    exact_regr.solve(B, X, params);
    skynla::iter_solver_op_t<SparseDistMatrixType, SparseMultiVectorType>::
                      residual_norms(A, B, X, exact_norms);

    /** Solve the sketched problem */
    DblContainer sketched_norms(int_params[N_RHS_INDEX], 0.0);
    skyalg::regression_problem_t<skyalg::l2_tag, SparseDistMatrixType>
         sketched_problem(int_params[S_INDEX], int_params[N_INDEX], sketch_A);
    skyalg::exact_regressor_t
        <skyalg::l2_tag,
         SparseDistMatrixType,
         SparseMultiVectorType,
         skyalg::iterative_l2_solver_tag<skyalg::lsqr_tag> >
         sketched_regr(sketched_problem);
    sketched_regr.solve(sketch_B, sketch_X, params);
    skynla::iter_solver_op_t<SparseDistMatrixType, SparseMultiVectorType>::
              residual_norms(A, B, sketch_X, sketched_norms);

    if (1<int_params[DEBUG_INDEX] &&
        (int_params[M_INDEX]*int_params[N_INDEX])<100 &&
        world.rank() == 0) {
      sparse_dist_mat_printer_t::apply (A, "A", true, 2);
      sparse_dist_multi_vec_printer_t::apply (B, "B", true, 2);
      sparse_dist_multi_vec_printer_t::apply (X, "X", true, 2);
    }

    /** Check the quality of the solution */
    if (0==world.rank()) {
      for (int i=0; i<int_params[N_RHS_INDEX]; ++i)
        printf ("For RHS (%d), exact norm = %lf, sketched norm=%lf\n",
                              i, exact_norms[i], sketched_norms[i]);
    }
  }

END:
  return 0;
}

max_issues_repo_name: cjiyer/libskylark

max_forks_repo_name: cjiyer/libskylark


text:
#ifndef FDP_WORK_QUEUE_HPP
#define FDP_WORK_QUEUE_HPP

#include <opencv2/opencv.hpp>
#include <boost/log/trivial.hpp>

#include <vector>
#include <memory>
#include <thread>
#include <future>
#include <chrono>
#include <condition_variable>
#include <functional>

#include "pipeline.hpp"

namespace nntu::img {

    template<size_t batch_size>
    class work_queue {
        using image_handler_t = std::function<void(cv::Mat&&)>;

        struct daemon_meta {
            std::condition_variable cv{};
            std::mutex mutex{};
            std::atomic<bool> condition{true};
        };

    public:
        explicit work_queue(size_t pool_size)
        {
            work_queue::run_flusher();
        }

        work_queue(work_queue& other) = delete;

        auto operator=(work_queue& other) -> work_queue& = delete;

        auto attach_to(pipeline<batch_size>& value)
        {
            attached_pipeline_ = &value;
        }

        void submit(cv::Mat&& img, image_handler_t handler)
        {
            work_queue::wait();
            std::lock_guard lock(submission_mutex_);

            image_pool_.push_back(std::move(img));
            image_handlers_.push_back(std::move(handler));

            if (image_pool_.size()<batch_size) return;

            work_queue::force_processing();
        }

        void wait()
        {
            if (last_processing_.valid()) last_processing_.wait();
        }

        virtual ~work_queue()
        {
            flushing_meta_.condition = false;
            flushing_meta_.cv.notify_all();

            if (last_processing_.valid()) last_processing_.wait();
            if (flusher_process_.valid()) flusher_process_.wait();
        }

    protected:
        void run_flusher()
        {
            flusher_process_ = std::async(std::launch::async, [this]() {
                while (flushing_meta_.condition) {
                    using namespace std::chrono_literals;

                    work_queue::wait();

                    if (!last_processing_.valid()) { work_queue::force_processing(); }

                    std::unique_lock<std::mutex> lock(flushing_meta_.mutex);
                    flushing_meta_.cv.wait_for(lock, 10000ns);
                }
            }).share();
        }

        void force_processing()
        {
            if (attached_pipeline_==nullptr) return;
            if (image_pool_.empty()) return;

            last_processing_ = std::async(std::launch::async, [&]() -> void {
                auto begin = std::begin(image_pool_);
                auto end = image_pool_.size()>batch_size ? begin+batch_size : std::end(image_pool_);

                attached_pipeline_->process(begin, end);

                for (size_t i = 0; i<std::distance(begin, end); i++) {
                    image_handlers_[i](std::move(image_pool_[i]));
                }

                image_pool_.clear();
                image_handlers_.clear();

                image_pool_.reserve(batch_size);
                image_handlers_.reserve(batch_size);
            }).share();
        }

    private:
        std::vector<cv::Mat> image_pool_{};
        std::vector<image_handler_t> image_handlers_{};
        daemon_meta flushing_meta_{};
        std::shared_future<void> last_processing_{};
        std::shared_future<void> flusher_process_{};
        std::mutex submission_mutex_{};

        pipeline<batch_size>* attached_pipeline_ = nullptr;
    };
}

#endif //FDP_WORK_QUEUE_HPP

ext: hpp


text:
#ifndef JAMSCRIPT_NOTIFIER_HH
#define JAMSCRIPT_NOTIFIER_HH
#include <mutex>
#include <thread>
#include <atomic>
#include "concurrency/spinlock.hpp"
#include "concurrency/condition_variable.hpp"

#include <boost/intrusive/list.hpp>

namespace jamc
{
    class ConditionVariable;
    class Notifier
    {
    public:

        void Join();
        void Notify();

    private:

        bool isFinished = false;
        SpinMutex m;
        ConditionVariable cv;

    };

    struct NotifierIdKeyType
    {
        typedef uintptr_t type;
        const type operator()(const Notifier &v) const { return reinterpret_cast<uintptr_t>(&v); }
    };

} // namespace jamc
#endif


text:
// Copyright 2018 Victor Smirnov
//
// 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.

#pragma once

#include <memoria/reactor/application.hpp>
#include <memoria/core/strings/string.hpp>

#include <memoria/core/exceptions/exceptions.hpp>

#include <boost/winapi/environment.hpp>

namespace memoria {
namespace reactor {

class EnvironmentImpl {

public:
	EnvironmentImpl(const char* const* envp) {}

	Optional<U8String> get(const U8String& name)
	{
		UWString wname = name.to_uwstring();

		DWORD bufsize = boost::winapi::GetEnvironmentVariableW(wname.data(), nullptr, 0);

		if (bufsize > 0)
		{
			if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
				return Optional<U8String>();
			}
			else {
				auto buf = allocate_system_zeroed<wchar_t>(bufsize * sizeof(wchar_t));

				DWORD bufsize2 = boost::winapi::GetEnvironmentVariableW(wname.data(), buf.get(), bufsize);

				if (bufsize2 <= bufsize)
				{
					return UWString(buf.get(), bufsize2).to_u8();
				}
				else {
                    MMA_THROW(SystemException()) << format_ex("Incorrect estimation of env variable's '{}' value size {}:{}", name, bufsize, bufsize2);
				}
			}
		}
		else {
            MMA_THROW(SystemException()) << format_ex("Can't obtain value of environment variable {}", name);
		}
	}

	void set(const U8String& name, const U8String& value)
	{
		if (!boost::winapi::SetEnvironmentVariableW(name.to_uwstring().data(), value.to_uwstring().data()))
		{
            MMA_THROW(SystemException()) << format_ex("Can't set value for environment variable: {}={}", name, value);
		}
	}

	EnvironmentMap entries()
	{
		EnvironmentMap map;

		for (auto& entry: entries_list())
		{
			size_t idx = entry.find_first_of("=");
			if (idx != U8String::NPOS)
			{
				auto key = entry.substring(0, idx);
				auto value = entry.substring(idx + 1);
				map[key] = value;
			}
		}

		return map;
	}

	EnvironmentList entries_list()
	{
		auto env_list_ptr = MakeScopedDtr(boost::winapi::GetEnvironmentStringsW(), [](auto* list_ptr) {
			if (list_ptr != nullptr) {
				boost::winapi::FreeEnvironmentStringsW(list_ptr);
			}
		});

		if (env_list_ptr)
		{
			EnvironmentList list;

			size_t start{};
			size_t idx{};
			bool end_entry{true};

			while (true)
			{
				if (*(env_list_ptr.get() + idx) == 0)
				{
					if (!end_entry)
					{
						list.emplace_back(UWString(env_list_ptr.get() + start, idx - start).to_u16());
						start = idx + 1;
						end_entry = true;
					}
					else {
						break;
					}
				}
				else {
					end_entry = false;
				}

				idx++;
			}

			return list;
		}
		else {
			MMA_THROW(SystemException()) << WhatCInfo("Can't obtain environment variables");
		}
	}
};

}}

max_stars_repo_name: victor-smirnov/memoria

max_issues_repo_name: victor-smirnov/memoria

max_forks_repo_name: victor-smirnov/memoria


text:
#include "utils.h"
#include "ps/ps.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <thread>
#include <numeric>
#include <boost/program_options.hpp>
#include <limits>
#include <sstream>
#include <string>
#include <iostream>
#include <unistd.h>
#include <bitset>
#include <Eigen/Dense>
#include <Eigen/SparseCore>

using namespace ps;
using namespace std;

typedef double ValT;
typedef DefaultColoServerHandle<ValT> HandleT;
typedef ColoKVServer<ValT, HandleT> ServerT;
typedef ColoKVWorker<ValT, HandleT> WorkerT;

uint num_workers = -1;

// Config
size_t num_iterations = 0;
size_t num_threads = 0;
Key num_keys = 0;
size_t num_values_per_key = 0;

void RunWorker(int customer_id, ServerT* server=nullptr) {
  Start(customer_id);
  std::unordered_map<std::string, util::Stopwatch> sw {};
  WorkerT kv(0, customer_id, *server);

  int worker_id = ps::MyRank()*num_threads+customer_id-1; // a unique id for this worker thread

  std::vector<Key> keys (1);
  std::vector<ValT> values (num_values_per_key);
  std::iota(values.begin(), values.end(), 1);
  std::vector<ValT> values_pull (num_values_per_key);

  for (size_t x=0; x != num_iterations; ++x) {
    keys[0] = x;

    // push update
    kv.Wait(kv.Push(keys, values));

    // print current value
    kv.Wait(kv.Pull(keys, &values_pull));
    std::stringstream s;
    s << "Key " << x << " in worker " << worker_id << ": " << values_pull << "\n";
    std::cout << s.str();
  }

  kv.Finalize();
  Finalize(customer_id, false);
}

int process_program_options(const int argc, const char *const argv[]) {
  namespace po = boost::program_options;
  po::options_description desc("Allowed options");
  desc.add_options()
    ("help,h", "produce help message")
    ("num_keys,k", po::value<Key>(&num_keys)->default_value(10), "number of parameters")
    ("num_threads,t", po::value<size_t>(&num_threads)->default_value(2), "number of worker threads to run (per process)")
    ("num_iterations,i", po::value<size_t>(&num_iterations)->default_value(4), "number of iterations to run")
    ("num_values_per_key,v", po::value<size_t>(&num_values_per_key)->default_value(3), "number of values per key")
    ;

  po::variables_map vm;
  po::store(po::parse_command_line(argc, argv, desc), vm);
  po::notify(vm);

  if (vm.count("help")) {
    cout << desc << "\n";
    return 1;
  }

  // check for required options
  // if (!vm.count("important_option2") || !vm.count("important_option2")) {
  //   cout << "Either important option 1 or important option 2 was not specified. Usage:\n\n";
  //   cout << desc << "\n";
  //   return 1;
  // }

  return 0;
}

int main(int argc, char *argv[]) {
  // Read cmd arguments
  int po_error = process_program_options(argc, argv);
  if(po_error) return 1;
  Postoffice::Get()->enable_dynamic_allocation(num_keys, num_threads);

  std::string role = std::string(getenv("DMLC_ROLE"));
  std::cout << "simple. Starting " << role << ": running " << num_iterations << " iterations on " << num_keys << " keys (" << num_values_per_key << " length) in " << num_threads << " threads\n";

  if (role.compare("scheduler") == 0) {
    Start(0);
    Finalize(0, true);
  } else if (role.compare("server") == 0) { // worker+server

    // Start the server system
    int server_customer_id = 0; // server gets customer_id=0, workers 1..n
    Start(server_customer_id);
    HandleT handle (num_keys, num_values_per_key); // the handle specifies how the server handles incoming Push() and Pull() calls
    auto server = new ServerT(server_customer_id, handle);
    RegisterExitCallback([server](){ delete server; });

    num_workers = ps::NumServers() * num_threads;

    // run worker(s)
    std::vector<std::thread> workers {};
    for (size_t i=0; i!=num_threads; ++i)
      workers.push_back(std::thread(RunWorker, i+1, server));

    // wait for the workers to finish
    for (auto & w : workers)
      w.join();

    // stop the server
    Finalize(server_customer_id, true);
  }
}

ext: cc


text:
#ifndef TWITTERLIB_OBJECTS_MEDIA_HPP
#define TWITTERLIB_OBJECTS_MEDIA_HPP
#include <cstdint>
#include <optional>
#include <string>

#include <boost/property_tree/ptree_fwd.hpp>

#include <twitterlib/objects/indices.hpp>
#include <twitterlib/objects/sizes.hpp>

namespace twitter {

struct Media {
    std::string display_url;
    std::string expanded_url;
    std::int64_t id;
    Indices indices;
    std::string media_url;
    std::string media_url_https;
    Sizes sizes;
    std::optional<std::int64_t> source_status_id;
    std::string type;
    std::string url;
};

/// Generates a string display of all data in \p media.
[[nodiscard]] auto to_string(Media const& media) -> std::string;

/// Create a Media object from a property tree.
[[nodiscard]] auto parse_media(boost::property_tree::ptree const& tree)
    -> Media;

}  // namespace twitter
#endif  // TWITTERLIB_OBJECTS_MEDIA_HPP


text:
#pragma once

#include <memory>
#include <boost/asio.hpp>
#include "debug.hpp"
#include "request.hpp"
#include "response.hpp"
#include "http_router.hpp"
#include "noncopyable.hpp"
#include "request_parser.hpp"
#include "request_handler.hpp"

namespace httpc {
    class Connection
        : public std::enable_shared_from_this<Connection>, Noncopyable {
    public:
        // static int conn_num;
        Connection(
            boost::asio::io_context& io_context,
            /*ConnectionManager& manager,*/
            HttpRouter&              router
            /*boost::asio::ip::tcp::socket&& socket*/
        )
            : http_router_(router)
            , socket_(io_context)
            , keep_alive_timer_(io_context)
            // , no(++conn_num)
        {
           // debug().dg("connection ctor").lf();
            //std::cout << "no." << this->no << " connection start" << std::endl;
        }

        // ~Connection() {
        //     debug().dg("connection dector").lf();
        // }

        void Start(/*std::function<void(std::shared_ptr<Connection>)> complete()*/) {
            // this->socket_.set_option(boost::asio::socket_base::keep_alive(true));
            //this->complete_callback_ = complete;
            DoRead();
        }

        boost::asio::ip::tcp::socket& Socket() {
            return this->socket_;
        }

        void Close() {
            this->socket_.close();
        }

        bool IsSocketOpen() const {
            return this->socket_.is_open();
        }

    private:
        //ConnectionManager&              manager_;
        HttpRouter&                     http_router_;
        boost::asio::ip::tcp::socket    socket_;
        std::array<char, 512>           buffer_;
        RequestParser                   parser_;
        Request                         request_;
        Response                        response_;
        RequestHandler                  handler_;
        // std::string                     write_buffer_;
        bool                            keep_alive_ {false};
        // bool                            hand_shake_ {false};
        // std::function<void(std::shared_ptr<Connection>)>
        //                                 complete_callback_;
        boost::asio::steady_timer       keep_alive_timer_;
        // int no;

        void ShutDownSocket_() {
            boost::system::error_code ignored_ec;
            this->socket_.shutdown(
                boost::asio::ip::tcp::socket::shutdown_both,
                ignored_ec
            );
        }

        void ActiveKeepAliveTimer() {
            //debug().dg("set timer").lf();
            // this->keep_alive_ = true;
            this->keep_alive_timer_.expires_after(std::chrono::seconds{60});
            this->keep_alive_timer_.async_wait([this, self = this->shared_from_this()]
            (boost::system::error_code const& ec) {
                if (ec) {
                    //debug().dg("timer handler: ").dg(ec.message()).lf();
                    return;
                }
                debug().dg("timer handler get closed").lf();
                //std::cout << "no." << this->no << " timer close" << std::endl;
                self->Socket().close();
            });
        }

        void HandleReadBody(boost::system::error_code const& ec, std::size_t bytes_transferred) {
            debug().dg("HandleReadBody() member function").lf();

            // debug().dg(this->request_).lf();

            // judge if user would like to receive the next file
            // if (this->request_.RecvNextFileOrNot()) {
            this->parser_.ParseBody(
                this->request_,
                this->buffer_.data(),
                this->buffer_.data() + bytes_transferred
            );
            this->parser_.ParseFormData(
                this->request_,
                this->buffer_.data(),
                this->buffer_.data() + bytes_transferred
            );
            debug().dg("parse post body complete").lf();
            // debug().dg(this->request_).lf();
            auto itr = this->response_.FindHeader("Content-Length");
            if (itr != this->response_.HeaderCEnd()) {
                debug().dg("Response Content-Length => ").dg(itr->value).lf();
            }
            // complete the parsement
            this->http_router_.Route(
                this->request_.method,
                this->request_.uri.GetUri(),
                this->request_,
                this->response_
            );
            // }

            // this->CheckKeepAlive_();

            // this->request_.uri.Update();
                // debug().dg(this->request_.uri.GetUri()).lf();

            //debug().dg(this->response_).lf();
            this->handler_.Respond(
                this->request_,
                this->response_
            );
            this->DoWrite();
        }

        void ReadBody() {
            debug().dg("enter the ReadBody() member function").lf();

            this->socket_.async_read_some(
                boost::asio::buffer(this->buffer_),
                [this, self = this->shared_from_this()]
                (boost::system::error_code const& ec, std::size_t bytes_transferred) {
                    debug()
                        .dg("Read Body ec.message => ")
                        .dg(ec.message())
                        .lf()
                        .dg("size => ")
                        .dg(bytes_transferred)
                        .lf();

                    // if no error occurs
                    if (!ec) {
                        debug().dg("try to invoke HandleReadBody() memeber function").lf();
                        HandleReadBody(ec, bytes_transferred);
                    } else {
                        // debug().dg("read eof do write").lf();
                        // this->CheckKeepAlive_();
                        debug().dg("Invoke route").lf();

                            // debug().dg(this->request_.uri.GetUri()).lf();
                        this->http_router_.Route(
                            this->request_.method,
                            this->request_.uri.GetUri(),
                            this->request_,
                            this->response_
                        );
                        debug().dg("Make Respond").lf();
                        //debug().dg(this->response_).lf();
                        this->handler_.Respond(
                            this->request_,
                            this->response_
                        );
                        debug().dg("Do Write").lf();
                        this->DoWrite();
                    }
                }
            );
        }

        void ReadTillEof() {
            debug().dg("read till eof").lf();
            this->socket_.async_read_some(
                boost::asio::buffer(this->buffer_),
                [this, self = this->shared_from_this()]
                (boost::system::error_code const& ec, std::size_t bytes_transferred) {
                    debug().dg("cnm").lf();
                    if (ec) {

                    } else {

                    }
                }
            );
        }

        // parse the http headers
        void HandleRead_(
            boost::system::error_code const& ec, std::size_t bytes_transferred
        ) {
            auto parse_res =
                this->parser_.Parse(
                    this->request_, this->buffer_.data(),
                    this->buffer_.data() + bytes_transferred
                );
            auto res_enum = std::get<0>(parse_res);
            // if a request if parsed completely
            if (res_enum == RequestParser::kGood) {
                // all the headers is read
                // then check the alive state
                this->CheckKeepAlive_();
                // update the uri required
                this->request_.uri.Update();
                auto itr = this->request_.FindHeader("Content-Length");
                if (itr == this->request_.HeaderCEnd()) {
                    // if not found
                    // find the route
                    this->http_router_.Route(
                        this->request_.method,
                        this->request_.uri.GetUri(),
                        this->request_,
                        this->response_
                    );
                    this->handler_.Respond(
                        this->request_,
                        this->response_
                    );
                    this->DoWrite();
                } else {
                    // call once the user bussiness
                    // pass the judgement of whether receive the next file
                    // to user.
                    this->http_router_.Route(
                        this->request_.method,
                        this->request_.uri.GetUri(),
                        this->request_,
                        this->response_
                    );

                    auto itr2 = this->request_.FindHeader("Content-Type");
                    if (itr2 != this->request_.HeaderCEnd()
                        && itr2->value.find_first_of("multipart/formdata") != std::string::npos) {

                        if (this->request_.RecvNextFileOrNot()) {
                            debug().dg("here try to read post body").lf();
                            // debug().dg(this->socket_.available()).lf();
                            this->ReadBody();
                            return;
                        }
                    }
                    this->handler_.Respond(
                        this->request_,
                        this->response_
                    );
                    this->DoWrite();

                }
                // debug().dg("invoke this->readbody()").lf();
                //this->ResetKeepAliveTimer();
            } else if (res_enum == RequestParser::kIndeterminate) {
                //this->ResetKeepAliveTimer();
                this->DoRead();
            } else {   // make a bad request.
                this->response_.SetDefault(HTTPStatusCodeEnum::kBadRequest);
                this->DoWrite();
            }
        }

        void DoRead() {
            // set up timer for each call
            this->ActiveKeepAliveTimer();

            // try to read from buffer
            socket_.async_read_some(
                boost::asio::buffer(this->buffer_),
                [this, self = this->shared_from_this()]
                (boost::system::error_code const& ec, std::size_t bytes_transferred) {
                    // if no error occurs
                    if (!ec) {
                        // handle read.
                        this->HandleRead_(ec, bytes_transferred);
                    } else {
                        if (ec == boost::asio::error::eof) {
                            // this->hand_shake_ = false;
                            debug().dg("shut down send").lf();
                            boost::system::error_code ignore_ec;
                            this->Socket().shutdown(boost::asio::ip::tcp::socket::shutdown_send, ignore_ec);
                        }
                    }
                }
            );
        }

        void CheckKeepAlive_() {
            if (this->request_.http_major_version == 1) {
                bool conn_flag = false;
                auto itr = this->request_.FindHeader("Connection");
                if (itr != this->request_.HeaderCEnd()) {
                    if (itr->value == "close") {
                        this->keep_alive_ = false;
                    } else if (itr->value == "keep-alive") {
                        this->keep_alive_ = true;
                    }
                } else {
                    if (this->request_.http_minor_version == 1) {
                        this->keep_alive_ = true;
                    } else if (this->request_.http_minor_version == 0) {
                        this->keep_alive_ = false;
                    }
                }
            }
            if (this->keep_alive_) {
                this->response_.AddHeader(Header{"Connection", "keep-alive"});
            } else {
                this->response_.AddHeader(Header{"Connection", "close"});
            }

        }

        void HandleWrite_(boost::system::error_code ec, std::size_t bytes_transferred) {
            if (this->keep_alive_) {
                this->parser_.Reset();
                this->request_.Reset();
                this->response_.Reset();
                //std::cout << "keep-alive" << std::endl;
                this->DoRead();
            }
            else {
                this->keep_alive_timer_.cancel();
                this->ShutDownSocket_();
                this->Close();
            }

        }

        void DoWrite()  {
            // auto self{shared_from_this()};
            // std::cout << "write" << std::endl;
            // this->write_buffer_ = response_.ToBuffers();
// #ifdef HTTPC_ENABLE_GZIP
//             this->response_.WriteGzipBuffer(this->write_buffer_);
//             // this->response_.WriteBuffer(this->write_buffer_);
// #endif
            // std::vector<boost::asio::const_buffer> buffers = this->response_.ToBuffers();
            // for(auto x : buffers) {
            //     std::cout << static_cast<const char*>(x.data()) << std::endl;
            // }
            // std::cout << response_ << std::endl;
            // this->ResetKeepAliveTimer();
            // debug().dg(this->response_).lf();
            // this->ActiveKeepAliveTimer();
            boost::asio::async_write(
                socket_,
                this->response_.ToBuffers(),
                [this, self = this->shared_from_this()]
                (boost::system::error_code ec, std::size_t bytes_transferred) {
                    //std::cout << ec.message() << std::endl;
                    //std::cout << bytes_transferred << std::endl;
                    this->HandleWrite_(ec, bytes_transferred);
                }
            );
        }

    };

    // int Connection::conn_num = 0;
}


text:
// Copyright (c) 2019 - present Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <complex>
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>

#include <hip/hip_runtime.h>
#include <hip/hip_vector_types.h>
#include <rocfft.h>

#include "examplekernels.h"
#include "exampleutils.h"

int main(int argc, char* argv[])
{
    std::cout << "rocfft double-precision real/complex transform\n" << std::endl;

    // Length of transform:
    std::vector<size_t> length = {8};

    // Gpu device id:
    int deviceId = 0;

    // Command-line options:
    // clang-format off
    po::options_description desc("rocfft sample command line options");
    desc.add_options()("help,h", "produces this help message")
        ("device", po::value<int>(&deviceId)->default_value(0), "Select a specific device id")
        ("outofplace,o", "Perform an out-of-place transform")
        ("inverse,i", "Perform an inverse transform")
        ("length", po::value<std::vector<size_t>>(&length)->multitoken(),
         "Lengths of the transform separated by spaces");
    // clang-format on
    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);
    if(vm.count("help"))
    {
        std::cout << desc << std::endl;
        return 0;
    }

    // Placeness for the transform
    const rocfft_result_placement place
        = vm.count("outofplace") ? rocfft_placement_notinplace : rocfft_placement_inplace;
    const bool inplace = place == rocfft_placement_inplace;

    // Direction of transform
    const rocfft_transform_type direction = vm.count("inverse")
                                                ? rocfft_transform_type_real_inverse
                                                : rocfft_transform_type_real_forward;
    const bool forward = direction == rocfft_transform_type_real_forward;

    // Set up the strides and buffer size for the real values:
    std::vector<size_t> rstride = {1};
    for(int i = 1; i < length.size(); ++i)
    {
        // In-place transforms need space for two extra real values in the contiguous
        // direction.
        auto val = (length[i - 1] + ((inplace && i == 1) ? 2 : 0)) * rstride[i - 1];
        rstride.push_back(val);
    }
    // NB: not tight, but hey
    const size_t        real_size = length[length.size() - 1] * rstride[rstride.size() - 1];
    std::vector<double> rdata(real_size); // host storage

    // The complex data length is half + 1 of the real data length in the contiguous
    // dimensions.  Since rocFFT is column-major, this is the first index.
    std::vector<size_t> clength = length;
    clength[0]                  = clength[0] / 2 + 1;
    std::vector<size_t> cstride = {1};
    for(int i = 1; i < clength.size(); ++i)
    {
        cstride.push_back(clength[i - 1] * cstride[i - 1]);
    }
    const size_t complex_size = clength[clength.size() - 1] * cstride[cstride.size() - 1];
    std::vector<std::complex<double>> cdata(complex_size); // host storage

    // Based on the direction, we set the input and output parameters appropriately.
    const size_t isize  = forward ? real_size : complex_size;
    const size_t ibytes = isize * (forward ? sizeof(double) : sizeof(std::complex<double>));
    const std::vector<size_t> ilength = forward ? length : clength;
    const std::vector<size_t> istride = forward ? rstride : cstride;

    const size_t osize  = forward ? complex_size : real_size;
    const size_t obytes = osize * (forward ? sizeof(std::complex<double>) : sizeof(double));
    const std::vector<size_t> olength = forward ? clength : length;
    const std::vector<size_t> ostride = forward ? cstride : rstride;

    // Print information about the transform:
    std::cout << "direction: ";
    if(forward)
        std::cout << "forward\n";
    else
        std::cout << "inverse\n";
    std::cout << "length:";
    for(const auto i : length)
        std::cout << " " << i;
    std::cout << "\n";
    if(inplace)
        std::cout << "in-place transform\n";
    else
        std::cout << "out-of-place transform\n";
    std::cout << "deviceID: " << deviceId << "\n";
    std::cout << "input length:";
    for(auto i : ilength)
        std::cout << " " << i;
    std::cout << "\n";
    std::cout << "input buffer stride:";
    for(auto i : istride)
        std::cout << " " << i;
    std::cout << "\n";
    std::cout << "input buffer size: " << ibytes << "\n";

    std::cout << "output length:";
    for(auto i : olength)
        std::cout << " " << i;
    std::cout << "\n";
    std::cout << "output buffer stride:";
    for(auto i : ostride)
        std::cout << " " << i;
    std::cout << "\n";
    std::cout << "output buffer size: " << obytes << "\n";
    std::cout << std::endl;

    // Set the device:
    hipSetDevice(deviceId);

    // Create HIP device object and initialize data
    // Kernels are provided in examplekernels.h
    hipError_t hip_status = hipSuccess;
    void*      gpu_in     = NULL;
    hip_status            = hipMalloc(&gpu_in, ibytes);
    if(forward)
    {
        initreal(length, istride, gpu_in);
    }
    else
    {
        inithermitiancomplex(length, ilength, istride, gpu_in);
    }
    hipDeviceSynchronize();
    hip_status = hipGetLastError();
    assert(hip_status == hipSuccess);

    // Print the output:
    std::cout << "input:\n";
    if(forward)
    {
        hipMemcpy(rdata.data(), gpu_in, ibytes, hipMemcpyDeviceToHost);
        printbuffer(rdata, ilength, istride, 1, isize);
    }
    else
    {
        hipMemcpy(cdata.data(), gpu_in, ibytes, hipMemcpyDeviceToHost);
        printbuffer(cdata, ilength, istride, 1, isize);

        // Check that the buffer is Hermitian symmetric:
        check_symmetry(cdata, length, istride, 1, isize);
    }

    // rocfft_status can be used to capture API status info
    rocfft_status rc = rocfft_status_success;

    // Create the a descrition struct to set data layout:
    rocfft_plan_description gpu_description = NULL;
    rc                                      = rocfft_plan_description_create(&gpu_description);
    assert(rc == rocfft_status_success);
    rc = rocfft_plan_description_set_data_layout(
        gpu_description,
        // input data format:
        forward ? rocfft_array_type_real : rocfft_array_type_hermitian_interleaved,
        // output data format:
        forward ? rocfft_array_type_hermitian_interleaved : rocfft_array_type_real,
        NULL,
        NULL,
        istride.size(), // input stride length
        istride.data(), // input stride data
        0, // input batch distance
        ostride.size(), // output stride length
        ostride.data(), // output stride data
        0); // ouptut batch distance
    // We can also pass "NULL" instead of a description; rocFFT will use reasonable
    // default parameters.  If the data isn't contiguous, we need to set strides, etc,
    // using the description.

    // Create the FFT plan:
    rocfft_plan gpu_plan = NULL;
    rc                   = rocfft_plan_create(&gpu_plan,
                            place,
                            direction,
                            rocfft_precision_double,
                            length.size(), // Dimension
                            length.data(), // lengths
                            1, // Number of transforms
                            gpu_description); // Description
    assert(rc == rocfft_status_success);

    // Get the execution info for the fft plan (in particular, work memory requirements):
    rocfft_execution_info planinfo = NULL;
    rc                             = rocfft_execution_info_create(&planinfo);
    assert(rc == rocfft_status_success);
    size_t workbuffersize = 0;
    rc                    = rocfft_plan_get_work_buffer_size(gpu_plan, &workbuffersize);
    assert(rc == rocfft_status_success);

    // If the transform requires work memory, allocate a work buffer:
    void* wbuffer = NULL;
    if(workbuffersize > 0)
    {
        hip_status = hipMalloc(&wbuffer, workbuffersize);
        assert(hip_status == hipSuccess);
        rc = rocfft_execution_info_set_work_buffer(planinfo, wbuffer, workbuffersize);
        assert(rc == rocfft_status_success);
    }

    // If the transform is out-of-place, allocate the output buffer as well:
    void* gpu_out = inplace ? gpu_in : NULL;
    if(!inplace)
    {
        hip_status = hipMalloc(&gpu_out, obytes);
        assert(hip_status == hipSuccess);
    }

    // Execute the GPU transform:
    rc = rocfft_execute(gpu_plan, // plan
                        (void**)&gpu_in, // in_buffer
                        (void**)&gpu_out, // out_buffer
                        planinfo); // execution info

    // Get the output from the device and print to cout:
    std::cout << "output:\n";
    if(forward)
    {
        hipMemcpy(cdata.data(), gpu_out, obytes, hipMemcpyDeviceToHost);
        printbuffer(cdata, olength, ostride, 1, osize);
    }
    else
    {
        hipMemcpy(rdata.data(), gpu_out, obytes, hipMemcpyDeviceToHost);
        printbuffer(rdata, olength, ostride, 1, osize);
    }

    // Clean up: free GPU memory:
    hipFree(gpu_in);
    if(!inplace)
    {
        hipFree(gpu_out);
    }
    if(wbuffer != NULL)
    {
        hipFree(wbuffer);
    }

    // Clean up: destroy plans:
    rocfft_execution_info_destroy(planinfo);
    rocfft_plan_description_destroy(gpu_description);
    rocfft_plan_destroy(gpu_plan);

    return 0;
}

max_stars_repo_name: malcolmroberts/rocFFT

max_issues_repo_name: malcolmroberts/rocFFT

max_forks_repo_name: malcolmroberts/rocFFT


text:
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
//
// File contains modifications by: The Gulden developers
// All modifications:
// Copyright (c) 2016 The Gulden developers
// Authored by: Malcolm MacLeod (mmacleod@webmail.co.za)
// Distributed under the GULDEN software license, see the accompanying
// file COPYING

#include "ismine.h"

#include "key.h"
#include "keystore.h"
#include "script/script.h"
#include "script/standard.h"
#include "script/sign.h"
#include "wallet/wallet.h"

#include <boost/foreach.hpp>

using namespace std;

typedef vector<unsigned char> valtype;

unsigned int HaveKeys(const vector<valtype>& pubkeys, const CKeyStore& keystore)
{
    unsigned int nResult = 0;
    BOOST_FOREACH (const valtype& pubkey, pubkeys) {
        CKeyID keyID = CPubKey(pubkey).GetID();
        if (keystore.HaveKey(keyID))
            ++nResult;
    }
    return nResult;
}

isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest)
{
    CScript script = GetScriptForDestination(dest);
    return IsMine(keystore, script);
}

isminetype RemoveAddressFromKeypoolIfIsMine(CWallet& wallet, const CTxDestination& dest, uint64_t time)
{
    LOCK(wallet.cs_wallet);

    isminetype ret = isminetype::ISMINE_NO;
    for (const auto& accountItem : wallet.mapAccounts) {
        for (auto keyChain : { KEYCHAIN_EXTERNAL, KEYCHAIN_CHANGE }) {
            isminetype temp = (keyChain == KEYCHAIN_EXTERNAL ? RemoveAddressFromKeypoolIfIsMine(wallet, accountItem.second->externalKeyStore, dest, time) : RemoveAddressFromKeypoolIfIsMine(wallet, accountItem.second->internalKeyStore, dest, time));
            if (temp > ret)
                ret = temp;
        }
    }
    return ret;
}

isminetype RemoveAddressFromKeypoolIfIsMine(CWallet& wallet, const CKeyStore& keystore, const CTxDestination& dest, uint64_t time)
{
    CScript script = GetScriptForDestination(dest);
    return RemoveAddressFromKeypoolIfIsMine(wallet, keystore, script, time);
}

isminetype IsMine(const CKeyStore& keystore, const CTxOut& txout)
{
    return IsMine(keystore, txout.scriptPubKey);
}

isminetype RemoveAddressFromKeypoolIfIsMine(CWallet& wallet, const CKeyStore& keystore, const CTxOut& txout, uint64_t time)
{
    return RemoveAddressFromKeypoolIfIsMine(wallet, keystore, txout.scriptPubKey, time);
}

isminetype RemoveAddressFromKeypoolIfIsMine(CWallet& wallet, const CTxOut& txout, uint64_t time)
{
    LOCK(wallet.cs_wallet);

    isminetype ret = isminetype::ISMINE_NO;
    for (const auto& accountItem : wallet.mapAccounts) {
        for (auto keyChain : { KEYCHAIN_EXTERNAL, KEYCHAIN_CHANGE }) {
            isminetype temp = (keyChain == KEYCHAIN_EXTERNAL ? RemoveAddressFromKeypoolIfIsMine(wallet, accountItem.second->externalKeyStore, txout, time) : RemoveAddressFromKeypoolIfIsMine(wallet, accountItem.second->internalKeyStore, txout, time));
            if (temp > ret)
                ret = temp;
        }
    }
    return ret;
}

isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey)
{
    vector<valtype> vSolutions;
    txnouttype whichType;
    if (!Solver(scriptPubKey, whichType, vSolutions)) {
        if (keystore.HaveWatchOnly(scriptPubKey))
            return ISMINE_WATCH_UNSOLVABLE;
        return ISMINE_NO;
    }

    CKeyID keyID;
    switch (whichType) {
    case TX_NONSTANDARD:
    case TX_NULL_DATA:
        break;
    case TX_PUBKEY:
        keyID = CPubKey(vSolutions[0]).GetID();
        if (keystore.HaveKey(keyID))
            return ISMINE_SPENDABLE;
        break;
    case TX_PUBKEYHASH:
    case TX_WITNESS_V0_KEYHASH:
        keyID = CKeyID(uint160(vSolutions[0]));
        if (keystore.HaveKey(keyID))
            return ISMINE_SPENDABLE;
        break;
    case TX_SCRIPTHASH: {
        CScriptID scriptID = CScriptID(uint160(vSolutions[0]));
        CScript subscript;
        if (keystore.GetCScript(scriptID, subscript)) {
            isminetype ret = IsMine(keystore, subscript);
            if (ret == ISMINE_SPENDABLE)
                return ret;
        }
        break;
    }
    case TX_WITNESS_V0_SCRIPTHASH: {
        uint160 hash;
        CRIPEMD160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(hash.begin());
        CScriptID scriptID = CScriptID(hash);
        CScript subscript;
        if (keystore.GetCScript(scriptID, subscript)) {
            isminetype ret = IsMine(keystore, subscript);
            if (ret == ISMINE_SPENDABLE)
                return ret;
        }
        break;
    }

    case TX_MULTISIG: {

        vector<valtype> keys(vSolutions.begin() + 1, vSolutions.begin() + vSolutions.size() - 1);
        if (HaveKeys(keys, keystore) == keys.size())
            return ISMINE_SPENDABLE;
        break;
    }
    }

    if (keystore.HaveWatchOnly(scriptPubKey)) {

        SignatureData sigs;
        return ProduceSignature(DummySignatureCreator(&keystore), scriptPubKey, sigs) ? ISMINE_WATCH_SOLVABLE : ISMINE_WATCH_UNSOLVABLE;
    }
    return ISMINE_NO;
}

isminetype RemoveAddressFromKeypoolIfIsMine(CWallet& wallet, const CScript& scriptPubKey, uint64_t time)
{
    LOCK(wallet.cs_wallet);

    isminetype ret = isminetype::ISMINE_NO;
    for (const auto& accountItem : wallet.mapAccounts) {
        for (auto keyChain : { KEYCHAIN_EXTERNAL, KEYCHAIN_CHANGE }) {
            isminetype temp = (keyChain == KEYCHAIN_EXTERNAL ? RemoveAddressFromKeypoolIfIsMine(wallet, accountItem.second->externalKeyStore, scriptPubKey, time) : RemoveAddressFromKeypoolIfIsMine(wallet, accountItem.second->internalKeyStore, scriptPubKey, time));
            if (temp > ret)
                ret = temp;
        }
    }
    return ret;
}

isminetype RemoveAddressFromKeypoolIfIsMine(CWallet& wallet, const CKeyStore& keystore, const CScript& scriptPubKey, uint64_t time)
{
    vector<valtype> vSolutions;
    txnouttype whichType;
    if (!Solver(scriptPubKey, whichType, vSolutions)) {
        if (keystore.HaveWatchOnly(scriptPubKey))
            return ISMINE_WATCH_UNSOLVABLE;
        return ISMINE_NO;
    }

    CKeyID keyID;
    switch (whichType) {
    case TX_NONSTANDARD:
    case TX_NULL_DATA:
        break;
    case TX_PUBKEY:
        keyID = CPubKey(vSolutions[0]).GetID();
        if (keystore.HaveKey(keyID)) {
            wallet.MarkKeyUsed(keyID, time);
            return ISMINE_SPENDABLE;
        }
        break;
    case TX_PUBKEYHASH:
    case TX_WITNESS_V0_KEYHASH:
        keyID = CKeyID(uint160(vSolutions[0]));
        if (keystore.HaveKey(keyID)) {
            wallet.MarkKeyUsed(keyID, time);
            return ISMINE_SPENDABLE;
        }
        break;
    case TX_SCRIPTHASH: {
        CScriptID scriptID = CScriptID(uint160(vSolutions[0]));
        CScript subscript;
        if (keystore.GetCScript(scriptID, subscript)) {

            isminetype ret = IsMine(keystore, subscript);
            if (ret == ISMINE_SPENDABLE)
                return ret;
        }
        break;
    }
    case TX_WITNESS_V0_SCRIPTHASH: {
        uint160 hash;
        CRIPEMD160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(hash.begin());
        CScriptID scriptID = CScriptID(hash);
        CScript subscript;

        if (keystore.GetCScript(scriptID, subscript)) {
            isminetype ret = IsMine(keystore, subscript);
            if (ret == ISMINE_SPENDABLE)
                return ret;
        }
        break;
    }

    case TX_MULTISIG: {

        vector<valtype> keys(vSolutions.begin() + 1, vSolutions.begin() + vSolutions.size() - 1);
        if (HaveKeys(keys, keystore) == keys.size())
            return ISMINE_SPENDABLE;
        break;
    }
    }

    if (keystore.HaveWatchOnly(scriptPubKey)) {

        SignatureData sigs;
        return ProduceSignature(DummySignatureCreator(&keystore), scriptPubKey, sigs) ? ISMINE_WATCH_SOLVABLE : ISMINE_WATCH_UNSOLVABLE;
    }
    return ISMINE_NO;
}

max_stars_repo_name: guldenchain/gulden-unofficial

max_issues_repo_name: guldenchain/gulden-unofficial

max_forks_repo_name: guldenchain/gulden-unofficial


text:
// Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
//
// 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/.

#include <config.h>

#include <exceptions/exceptions.h>

#include <dns/exceptions.h>
#include <dns/rdata.h>
#include <dns/master_lexer.h>
#include <dns/rdata/generic/detail/char_string.h>
#include <util/buffer.h>

#include <boost/lexical_cast.hpp>

#include <cassert>
#include <cctype>
#include <cstring>
#include <vector>

#include <stdint.h>

namespace isc {
namespace dns {
namespace rdata {
namespace generic {
namespace detail {

namespace {
// Convert a DDD form to the corresponding integer
int
decimalToNumber(const char* s, const char* s_end) {
    if (s_end - s < 3) {
        isc_throw(InvalidRdataText, "Escaped digits too short");
    }

    const std::string num_str(s, s + 3);
    try {
        const int i = boost::lexical_cast<int>(num_str);
        if (i > 255) {
            isc_throw(InvalidRdataText, "Escaped digits too large: "
                      << num_str);
        }
        return (i);
    } catch (const boost::bad_lexical_cast&) {
        isc_throw(InvalidRdataText,
                  "Invalid form for escaped digits: " << num_str);
    }
}
}

void
stringToCharString(const MasterToken::StringRegion& str_region,
                   CharString& result)
{
    // make a space for the 1-byte length field; filled in at the end
    result.push_back(0);

    bool escape = false;
    const char* s = str_region.beg;
    const char* const s_end = str_region.beg + str_region.len;

    for (size_t n = str_region.len; n != 0; --n, ++s) {
        int c = (*s & 0xff);
        if (escape && std::isdigit(c) != 0) {
            c = decimalToNumber(s, s_end);
            assert(n >= 3);
            n -= 2;
            s += 2;
        } else if (!escape && c == '\\') {
            escape = true;
            continue;
        }
        escape = false;
        result.push_back(c);
    }
    if (escape) {               // terminated by non-escaped '\'
        isc_throw(InvalidRdataText, "character-string ends with '\\'");
    }
    if (result.size() > MAX_CHARSTRING_LEN + 1) { // '+ 1' due to the len field
        isc_throw(CharStringTooLong, "character-string is too long: " <<
                  (result.size() - 1) << "(+1) characters");
    }
    result[0] = result.size() - 1;
}

void
stringToCharStringData(const MasterToken::StringRegion& str_region,
                       CharStringData& result)
{
    bool escape = false;
    const char* s = str_region.beg;
    const char* const s_end = str_region.beg + str_region.len;

    for (size_t n = str_region.len; n != 0; --n, ++s) {
        int c = (*s & 0xff);
        if (escape && std::isdigit(c) != 0) {
            c = decimalToNumber(s, s_end);
            // decimalToNumber() already throws if (s_end - s) is less
            // than 3, so the following assertion is unnecessary. But we
            // assert it anyway. 'n' is an unsigned type (size_t) and
            // can underflow.
            assert(n >= 3);
            // 'n' and 's' are also updated by 1 in the for statement's
            // expression, so we update them by 2 instead of 3 here.
            n -= 2;
            s += 2;
        } else if (!escape && c == '\\') {
            escape = true;
            continue;
        }
        escape = false;
        result.push_back(c);
    }
    if (escape) {               // terminated by non-escaped '\'
        isc_throw(InvalidRdataText, "character-string ends with '\\'");
    }
}

std::string
charStringToString(const CharString& char_string) {
    std::string s;
    for (CharString::const_iterator it = char_string.begin() + 1;
         it != char_string.end(); ++it) {
        const uint8_t ch = *it;
        if ((ch < 0x20) || (ch >= 0x7f)) {
            // convert to escaped \xxx (decimal) format
            s.push_back('\\');
            s.push_back('0' + ((ch / 100) % 10));
            s.push_back('0' + ((ch / 10) % 10));
            s.push_back('0' + (ch % 10));
            continue;
        }
        if ((ch == '"') || (ch == ';') || (ch == '\\')) {
            s.push_back('\\');
        }
        s.push_back(ch);
    }

    return (s);
}

std::string
charStringDataToString(const CharStringData& char_string) {
    std::string s;
    for (CharString::const_iterator it = char_string.begin();
         it != char_string.end(); ++it) {
        const uint8_t ch = *it;
        if ((ch < 0x20) || (ch >= 0x7f)) {
            // convert to escaped \xxx (decimal) format
            s.push_back('\\');
            s.push_back('0' + ((ch / 100) % 10));
            s.push_back('0' + ((ch / 10) % 10));
            s.push_back('0' + (ch % 10));
            continue;
        }
        if ((ch == '"') || (ch == ';') || (ch == '\\')) {
            s.push_back('\\');
        }
        s.push_back(ch);
    }

    return (s);
}

int compareCharStrings(const detail::CharString& self,
                       const detail::CharString& other) {
    if (self.size() == 0 && other.size() == 0) {
        return (0);
    }
    if (self.size() == 0) {
        return (-1);
    }
    if (other.size() == 0) {
        return (1);
    }
    const size_t self_len = self[0];
    const size_t other_len = other[0];
    const size_t cmp_len = std::min(self_len, other_len);
    if (cmp_len == 0) {
        if (self_len < other_len) {
            return (-1);
        } else if (self_len > other_len) {
            return (1);
        } else {
            return (0);
        }
    }
    const int cmp = std::memcmp(&self[1], &other[1], cmp_len);
    if (cmp < 0) {
        return (-1);
    } else if (cmp > 0) {
        return (1);
    } else if (self_len < other_len) {
        return (-1);
    } else if (self_len > other_len) {
        return (1);
    } else {
        return (0);
    }
}

int compareCharStringDatas(const detail::CharStringData& self,
                           const detail::CharStringData& other) {
    if (self.size() == 0 && other.size() == 0) {
        return (0);
    }
    if (self.size() == 0) {
        return (-1);
    }
    if (other.size() == 0) {
        return (1);
    }
    const size_t self_len = self.size();
    const size_t other_len = other.size();
    const size_t cmp_len = std::min(self_len, other_len);
    const int cmp = std::memcmp(&self[0], &other[0], cmp_len);
    if (cmp < 0) {
        return (-1);
    } else if (cmp > 0) {
        return (1);
    } else if (self_len < other_len) {
        return (-1);
    } else if (self_len > other_len) {
        return (1);
    } else {
        return (0);
    }
}

size_t
bufferToCharString(isc::util::InputBuffer& buffer, size_t rdata_len,
                   CharString& target) {
    if (rdata_len < 1 || buffer.getLength() - buffer.getPosition() < 1) {
        isc_throw(isc::dns::DNSMessageFORMERR,
                  "insufficient data to read character-string length");
    }
    const uint8_t len = buffer.readUint8();
    if (rdata_len < len + 1) {
        isc_throw(isc::dns::DNSMessageFORMERR,
                  "character string length is too large: " <<
                  static_cast<int>(len));
    }
    if (buffer.getLength() - buffer.getPosition() < len) {
        isc_throw(isc::dns::DNSMessageFORMERR,
                  "not enough data in buffer to read character-string of len"
                  << static_cast<int>(len));
    }

    target.resize(len + 1);
    target[0] = len;
    buffer.readData(&target[0] + 1, len);

    return (len + 1);
}

} // end of detail
} // end of generic
} // end of rdata
} // end of dns
} // end of isc

max_stars_repo_name: svenauhagen/kea

max_issues_repo_name: jxiaobin/kea

max_forks_repo_name: jxiaobin/kea


text:
/// ---------------------------------------------------------------------------
/// @section LICENSE
///
/// Copyright (c) 2016 Georgia Tech Research Institute (GTRI)
///               All Rights Reserved
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
/// DEALINGS IN THE SOFTWARE.
/// ---------------------------------------------------------------------------
/// @file filename.ext
/// @author Kevin DeMarco <kevin.demarco@gtri.gatech.edu>
/// @author Eric Squires <eric.squires@gtri.gatech.edu>
/// @version 1.0
/// ---------------------------------------------------------------------------
/// @brief A brief description.
///
/// @section DESCRIPTION
/// A long description.
/// ---------------------------------------------------------------------------
#include "SimpleAircraft.h"
#include <scrimmage/common/Utilities.h>
#include <scrimmage/common/Random.h>
#include <scrimmage/parse/ParseUtils.h>
#include <scrimmage/plugin_manager/RegisterPlugin.h>
#include <scrimmage/math/Angles.h>
#include <boost/algorithm/clamp.hpp>
#include <scrimmage/entity/Entity.h>

using boost::algorithm::clamp;

REGISTER_PLUGIN(scrimmage::MotionModel, SimpleAircraft, SimpleAircraft_plugin)

namespace sc = scrimmage;
namespace pl = std::placeholders;

enum ModelParams
{
    X = 0,
    Y,
    Z,
    ROLL,
    PITCH,
    YAW,
    SPEED,
    MODEL_NUM_ITEMS
};

enum ControlParams
{
    THRUST = 0,
    TURN_RATE,
    PITCH_RATE,
    CONTROL_NUM_ITEMS
};

SimpleAircraft::SimpleAircraft()
{
    x_.resize(MODEL_NUM_ITEMS);
}

std::tuple<int,int,int> SimpleAircraft::version()
{
    return std::tuple<int,int,int>(0,0,1);
}

bool SimpleAircraft::init(std::map<std::string, std::string> &info,
                          std::map<std::string, std::string> &params)
{
    Eigen::Vector3d &pos = state_->pos();
    sc::Quaternion &quat = state_->quat();

    x_[X] = pos(0);
    x_[Y] = pos(1);
    x_[Z] = pos(2);
    x_[ROLL] = quat.roll();
    x_[PITCH] = quat.pitch();
    x_[YAW] = quat.yaw();
    x_[SPEED] = state_->vel().norm();

    length_ = sc::get("turning_radius", params, 50.0);

    min_velocity_ = sc::get("min_velocity", params, 15.0);
    max_velocity_ = sc::get("max_velocity", params, 40.0);
    max_roll_ = sc::Angles::deg2rad(sc::get("max_roll", params, 30.0));
    max_pitch_ = sc::Angles::deg2rad(sc::get("max_pitch", params, 30.0));
    noise_stdev_ = sc::get("noise_stdev", params, 0.01);

    state_->pos() << x_[X], x_[Y], x_[Z];
    state_->quat().set(-x_[ROLL], x_[PITCH], x_[YAW]);
    state_->vel() << x_[SPEED] * cos(x_[5]), x_[SPEED] * sin(x_[5]), 0;

    return true;
}

bool SimpleAircraft::step(double time, double dt)
{
    // Need to saturate state variables before model runs
    x_[ROLL] = clamp(x_[ROLL], -max_roll_, max_roll_);
    x_[PITCH] = clamp(x_[PITCH], -max_pitch_, max_pitch_);
    x_[SPEED] = clamp(x_[SPEED], min_velocity_, max_velocity_);

    if (u_ == nullptr) {
        std::shared_ptr<Controller> ctrl =
            std::dynamic_pointer_cast<Controller>(parent_.lock()->controllers().back());
        if (ctrl) {
            u_ = ctrl->u();
        }
    }

    if (u_ == nullptr) {
        return false;
    }

    ode_step(dt);

    state_->pos() << x_[X], x_[Y], x_[Z];
    state_->quat().set(-x_[ROLL], x_[PITCH], x_[YAW]);
    state_->vel() << x_[SPEED] * cos(x_[YAW]), x_[SPEED] * sin(x_[YAW]), 0;

    return true;
}

void SimpleAircraft::model(const vector_t &x , vector_t &dxdt , double t)
{
    /// 0 : x-position
    /// 1 : y-position
    /// 2 : z-position
    /// 3 : roll
    /// 4 : pitch
    /// 5 : yaw
    /// 6 : speed
    double thrust = (*u_)(THRUST);
    double roll_rate = (*u_)(TURN_RATE);
    double pitch_rate = (*u_)(PITCH_RATE);

    // Saturate control inputs
    thrust = clamp(thrust, -100.0, 100.0);
    roll_rate = clamp(roll_rate, -1, 1.0);
    pitch_rate = clamp(pitch_rate, -1.0, 1.0);

    double xy_speed = x[SPEED] * cos(x[PITCH]);
    sc::RandomPtr randomptr = parent_.lock()->random();
    dxdt[X] = xy_speed*cos(x[YAW])+randomptr->rng_normal()*noise_stdev_;
    dxdt[Y] = xy_speed*sin(x[YAW])+randomptr->rng_normal()*noise_stdev_;
    dxdt[Z] = -sin(x[PITCH])*x[SPEED]+randomptr->rng_normal()*noise_stdev_;
    dxdt[ROLL] = roll_rate/2+randomptr->rng_normal()*noise_stdev_;
    dxdt[PITCH] = pitch_rate/2+randomptr->rng_normal()*noise_stdev_;
    dxdt[YAW] = x[SPEED]/length_*tan(x[ROLL])+randomptr->rng_normal()*noise_stdev_;

    dxdt[SPEED] = thrust/5+randomptr->rng_normal()*noise_stdev_;
}

void SimpleAircraft::teleport(sc::StatePtr &state)
{
    x_[X] = state->pos()[0];
    x_[Y] = state->pos()[1];
    x_[Z] = state->pos()[2];
    x_[ROLL] = state->quat().roll();
    x_[PITCH] = state->quat().pitch();
    x_[YAW] = state->quat().yaw();
    x_[SPEED] = state->vel()[0];
}


text:
//---------------------------------------------------------------------------//
// Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//---------------------------------------------------------------------------//

#define BOOST_TEST_MODULE bls_api_test

#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/data/monomorphic.hpp>

#include <filcrypto.h>

BOOST_AUTO_TEST_SUITE(bls_api_test_suite)

BOOST_AUTO_TEST_CASE(key_verification) {
      let private_key = (*fil_private_key_generate()).private_key.inner;
      let public_key =
      (*fil_private_key_public_key(&private_key[0])).public_key.inner;
      let message = b "hello world";
      let digest = (*fil_hash(&message[0], message.size())).digest.inner;
      let signature =
      (*fil_private_key_sign(&private_key[0], &message[0], message.size()))
      .signature.inner;
      let verified = fil_verify(&signature[0], &digest[0], digest.size(),
      &public_key[0], public_key.size(), );

      BOOST_CHECK_EQUAL(1, verified);

      let flattened_messages = message;
      let message_sizes = [message.size()];
      let verified = fil_hash_verify(
      signature.as_ptr(), flattened_messages.as_ptr(),
      flattened_messages.size(), message_sizes.as_ptr(), message_sizes.size(),
      public_key.as_ptr(), public_key.size(), );

      BOOST_CHECK_EQUAL(1, verified);

      let different_message = b "bye world";
      let different_digest =
      (*fil_hash(&different_message[0], different_message.size()))
      .digest.inner;
      let not_verified = fil_verify(&signature[0], &different_digest[0],
      different_digest.size(), &public_key[0],
      public_key.size(), );

      BOOST_CHECK_EQUAL(0, not_verified);

      // garbage verification
      let different_digest = vec ![ 0, 1, 2, 3, 4 ];
      let not_verified = fil_verify(&signature[0], &different_digest[0],
      different_digest.size(), &public_key[0],
      public_key.size(), );

      BOOST_CHECK_EQUAL(0, not_verified);
}

BOOST_AUTO_TEST_CASE(private_key_with_seed) {
      let seed = fil_32ByteArray {
        inner:
        [5u8; 32]
      };
      let private_key =
      (*fil_private_key_generate_with_seed(seed)).private_key.inner;
      BOOST_CHECK_EQUAL(
      [
      115, 245, 77,  209, 4,   57,  40,  107, 10,  153, 141,
      16,  153, 172, 85,  197, 125, 163, 35,  217, 108, 241,
      64,  235, 231, 220, 131, 1,   77,  253, 176, 19
      ],
      private_key);
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: filecoin-github/filecoin-ffi

max_issues_repo_name: filecoin-github/filecoin-ffi

max_forks_repo_name: filecoin-github/filecoin-ffi


text: #include <boost/geometry/multi/geometries/register/multi_point.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
/*
   Copyright 2012, Telum Slavonski Brod, Croatia.

   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.

   This file is part of QFramework project, and can be used only as part of project.
   Should be used for peace, not war :)
*/

#include "localdataselector.h"
#include "../scriptmanager/scriptsession.h"
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/lexical_cast.hpp>
#include "httpserver.h"
#include "../threadmanager/threadmanager.h"

namespace qserver
{

  /**
  * Map for storing users
  */

  LocalDataSelector::LocalDataSelector()
  {
  }

  LocalDataSelector::~LocalDataSelector()
  {

  }

  bool LocalDataSelector::add(const std::string& user)
  {
    if (!find(user))
    {
      boost::mutex::scoped_lock lck(users_mutex);

      // insert user into map
      HttpUser userdata;
      userdata.mUsername = user;
      mUsers.insert( UsersPair( user, userdata) );
      return true;
    }
    return false;
  }

  bool LocalDataSelector::find(const std::string& user)
  {
    boost::mutex::scoped_lock lck(users_mutex);
    UsersMap::iterator iter = mUsers.find(user);
    if (iter != mUsers.end() )
    {
      return true;
    }
    return false;
  }

  bool LocalDataSelector::remove(const std::string& user)
  {
    std::string server;
    std::string room;
    bool toremove = false;

    {
      boost::mutex::scoped_lock lck(users_mutex);
      UsersMap::iterator iter = mUsers.find(user);

      if (iter != mUsers.end() )
      {
        if (iter->second.mRoomID.length() )
        {
          // remove user from server
          server = iter->second.mServerID;
          room = iter->second.mRoomID;
          toremove = true;

          if (iter->second.mConnection)
          {
            iter->second.mConnection = NULL;
          }
        }
        mUsers.erase( user );
      }
    }

    if (toremove)
    {
      /*
      ScriptSession* session = threadManager.mScriptManager.getScript(boost::lexical_cast<int>(room));
      if (session)
      {
        session->removeClient(user.c_str());
      }*/

      threadManager.putUserDisConnected( user, boost::lexical_cast<int>(room));

    }
    return toremove;
  }

  bool LocalDataSelector::setServer(const std::string& user, const std::string& server)
  {
    std::string oldserver;
    std::string room;
    bool toremove = false;
    bool res = false;
    {
      boost::mutex::scoped_lock lck(users_mutex);
      UsersMap::iterator iter = mUsers.find(user);

      if (iter != mUsers.end() )
      {
        if (iter->second.mServerID.length() && iter->second.mRoomID.length() )
        {
          if (server == iter->second.mServerID) // allready logged on server
          {
            return false;
          }else
          {
            // send leave room to server
            oldserver = iter->second.mServerID;
            room = iter->second.mRoomID;
            toremove = true;
          }
        }
      }
      iter->second.mServerID = server;
      res = true;
    }

    if (toremove)
    {
      //HttpServer::connections().removeUser(user, server , room);
    }

    return res;
  }

  bool LocalDataSelector::put(const std::string& user , const std::string& data)
  {
    std::string server;
    std::string room;
    {
      boost::mutex::scoped_lock lck(users_mutex);
      UsersMap::iterator iter = mUsers.find(user);
      if (iter != mUsers.end() )
      {
        // route data to server
        server = iter->second.mServerID;
        room = iter->second.mRoomID;
      }
    }

	if (server.length() && room.length() )
    {
      /*
      ScriptSession* session = threadManager.mScriptManager.getScript(boost::lexical_cast<int>(room));
      if (session)
      {
        session->onClientData(user.c_str() , data.c_str() );
      }*/
      threadManager.putUserData( user, data, boost::lexical_cast<int>(room));

      return true;
    }
    return false;
  }

  bool LocalDataSelector::setRoom(const std::string& user, const std::string& server, const std::string& room)
  {
    boost::mutex::scoped_lock lck(users_mutex);

    UsersMap::iterator iter = mUsers.find(user);

    if (iter != mUsers.end() )
    {
      if (iter->second.mServerID.length() )
      {
        if (iter->second.mRoomID.length() )
        {
          if (iter->second.mServerID == server && iter->second.mRoomID == room)
          {
            //HttpServer::connections().reconnectUser2Room(user, iter->second.mServerID, iter->second.mRoomID);
          }else
          {
            // send leave room to server
            //HttpServer::connections().removeUser(user, iter->second.mServerID, iter->second.mRoomID);
          }
        }
      }

      iter->second.mServerID = server;
      iter->second.mRoomID = room;
      /// add user
      //HttpServer::connections().addUser2Room(user, iter->second.mServerID, iter->second.mRoomID);
      /*
      ScriptSession* session = threadManager.mScriptManager.getScript(boost::lexical_cast<int>(room));
      if (session)
      {
        session->fireClientJoined(user);
      }
      */
      //threadManager.putUserConnected( user, boost::lexical_cast<int>(room));
      return true;
    }

    return false;
  }

  bool LocalDataSelector::leaveRoom(const std::string& user)
  {
    boost::mutex::scoped_lock lck(users_mutex);

    UsersMap::iterator iter = mUsers.find(user);
    if (iter != mUsers.end() )
    {
      if (iter->second.mServerID.length() && iter->second.mRoomID.length() )
      {
        if (iter->second.mConnection)
        {
          //iter->second.mConnection->stop();
          //iter->second.mConnection = NULL;
        }
        //HttpServer::connections().removeUser(user, iter->second.mServerID, iter->second.mRoomID);
        /*
        ScriptSession* session = threadManager.mScriptManager.getScript(boost::lexical_cast<int>(iter->second.mRoomID));
        if (session)
        {
          session->removeClient(user.c_str());
		      iter->second.mRoomID = "";
        }*/
        threadManager.putUserDisConnected( user, boost::lexical_cast<int>(iter->second.mRoomID));
        iter->second.mRoomID = "";
        return true;
      }
    }

    return false;
  }

  void LocalDataSelector::setUserConnection( const std::string& userid, const std::string& room,  HttpConnection* connection)
  {
    boost::mutex::scoped_lock lck(users_mutex);
    UsersMap::iterator iter = mUsers.find(userid);
    if (iter != mUsers.end() )
    {
      // store data to user buffers
      iter->second.mConnection = connection;
      connection->setUser(userid);
    }
    /*
	  ScriptSession* session = threadManager.mScriptManager.getScript(boost::lexical_cast<int>(room));
	  if (session)
	  {
		  session->addClient(userid,connection );
		  connection->setUser(userid);
	  }
    */
    threadManager.putUserConnected( userid, connection->getId(), boost::lexical_cast<int>(room));

  }

  HttpConnection* LocalDataSelector::getUserConnection( const std::string& userid)
  {
    boost::mutex::scoped_lock lck(users_mutex);
    UsersMap::iterator iter = mUsers.find(userid);
    if (iter != mUsers.end() )
    {
      return iter->second.mConnection;
    }
    return NULL;
  }

} // namespace qserver

max_stars_repo_name: qframework/QFramework

max_issues_repo_name: qframework/QFramework

max_forks_repo_name: qframework/QFramework


text:
// main.cpp
// Used to write exploratory/behavioural tests for CSVParser

#include "CSVParser.hpp"
#include "LeastSquaresFit.hpp"
#include <iostream>
#include <memory> // shared pointers
#include <unistd.h> // usleep
#include <boost/program_options.hpp>
#include <chrono>

void usage() {
	std::cout << "Example usage:\n"
		<< "CSVParser_tester --filename census_2000_all_places_sample_dimensions.csv --header SE_T001_001 --header_row_index 1 --first_data_row_index 2\n" << std::endl;
}

int main( int argc, char **argv ) {
	// program arg variables
	std::string 	filename;
	std::string 	header;
	index_type header_row_index;
	index_type  first_data_row_index;

	// ********* start of boost::program_options code ****************
	namespace po = boost::program_options;
	po::options_description desc("Options");
	desc.add_options()
		("help", "Print help messages")
		("filename", po::value<std::string>(&filename)->required(),
			"CSV filename including file extension")
		("header", po::value<std::string>(&header)->required(),
			"unique header of column")
		("header_row_index", po::value<index_type>(&header_row_index)->required(),
			"index of row header is located on")
		("first_data_row_index", po::value<index_type>(&first_data_row_index)->required(),
			"index of first row of actual data");

	po::variables_map vm;
	try{
		po::store(po::parse_command_line(argc, argv, desc), vm);
		if(argc == 1) {
			usage();
			std::cout << desc << std::endl;
			return 0;
		}

		if (vm.count("help") )
		{
			std::cout << "Progressive Analytics Application" << std::endl
					  << desc << std::endl;
			return 0;
		}

      po::notify(vm); // throws on error, so do after help in case
                      // there are any problems
    }
    catch(po::error& e)
    {
      std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
      std::cerr << desc << std::endl;
      return 1;
    }
	// ********* end of boost::program_options code ****************

	constexpr unsigned char DELIM {','};
	std::shared_ptr<CSVParser> t1;
	try {
		 t1 = std::make_shared<CSVParser>(makeCSVParser(filename, DELIM));
	} catch(const std::runtime_error &e) {
		std::cout << e.what() << std::endl;
		return 2;
	}
	ColumnData<int_data_type> results;

	// Set up values to perform a one-hit analysis
	{
		index_type num_segments {1};
		std::vector<ColumnData<int_data_type>> result_cds;
		try{
			result_cds = t1->makeSegments<int_data_type>(
				header, header_row_index, first_data_row_index, num_segments);
		}catch (std::runtime_error &e) {
			std::cout << e.what() << std::endl;
			return 3;
		}
		t1->runAnalysisSegment(result_cds.at(0));

		std::cout << "result of one-hit analysis: " << result_cds.at(0).data_summary_actual << '\n' << std::endl;
	}

	// Set up values to perform a progressive analysis
	// Note: This only creates ColumnData segments, it
	// does not perform any calculations
	{
		index_type num_segments {5};
		std::vector<ColumnData<int_data_type>> result_cds;
		try{
			result_cds = t1->makeSegments<int_data_type>(
				header, header_row_index, first_data_row_index, num_segments);
		}catch (std::runtime_error &e) {
			std::cout << e.what() << std::endl;
			return 3;
		}

		std::cout << "******************************************" << std::endl;
		std::cout << "printing contents of progressive analysis:" << std::endl;
		std::cout << "******************************************" << std::endl;

		int_data_type sum{};
		for(auto &cur_res : result_cds) {
			t1->runAnalysisSegment(cur_res);
			std::cout << cur_res << '\n' << std::endl;
			sum += cur_res.data_summary_actual;
		}
		std::cout << "Final sum: " << sum << std::endl;
	}

	// Performing a progressive analysis using LeastSquaresFit class
	std::string X_header {"SE_T003_001"};
	std::string Y_header {"SE_T013_001"};

	auto Xs = t1->makeSegments<float_data_type>(X_header, header_row_index, first_data_row_index, 5);
	auto Ys = t1->makeSegments<float_data_type>(Y_header, header_row_index, first_data_row_index, 5);
	std::cout << "Xs size: " << Xs.size() << std::endl;
	std::cout << "Ys size: " << Ys.size() << std::endl;
	std::cout << "X_bar: " << calcAvg(Xs) << std::endl;
	std::cout << "Y_bar: " << calcAvg(Ys) << std::endl;
	std::cout << "\n" << std::endl;

	using vec_cols_float = std::vector<ColumnData<float_data_type>>;
	LeastSquaresFit<vec_cols_float, vec_cols_float> lsf =
		makeLeastSquaresFit<vec_cols_float, vec_cols_float>(Xs, Ys);

	// Display results/projections after each segment is processed
	std::cout << lsf << "\n\n" << std::endl;
	constexpr int_data_type usecs {2000000}; // microseconds
	std::cout << "Displaying projections during progressive analysis:" << std::endl;
	while(lsf.calcNextProjection()) {
		auto start = std::chrono::system_clock::now();
		std::cout << '\r' << "a: " << lsf.getProja() << " b: " << lsf.getProjb();
		auto end = std::chrono::system_clock::now();
		auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);

		std::cout << " elapsed: " << elapsed.count() << " ns" << std::flush;
		usleep(usecs);
	}
	std::cout << "\nProgram closing" << std::endl;
	return 0;
}


text:
// -*-C++-*-
/*
  (c) Copyright 2007, Hewlett-Packard Development Company, LP

  See the file named COPYING for license details
*/

/** @file

    SRT-specfic statistic calculation over a DS converted CSV file,
    for purpose of the DataSeries paper.  This is suitably
    optimized. According to valgrind, we're spending about 40% of the
    instructions in strtod, 7% in strtol, 14% in fgets, 17% in the
    loop finding the ,'s.  Could probably optimize a little more by
    doing our own fgets.  Could use mmap if we were reading from an
    uncompressed file and were very careful about scanning through the
    file to handle files bigger than namespace.  Might be able to use
    mmap with compressed files with the same problem of carefulness.
*/

#include <string>
#include <inttypes.h>

#include <boost/format.hpp>

#include <Lintel/Stats.hpp>
#include <Lintel/HashMap.hpp>
#include <Lintel/AssertBoost.hpp>

using namespace std;

struct HashMap_hashintfast {
    unsigned operator()(const int _a) const {
        return _a;
    }
};

struct csvline {
    vector<char *> fields;
    vector<double> asdouble;
    vector<int> asint;
    vector<int> converted_flags;

    csvline() {
        fields.resize(42);
        asdouble.resize(42);
        asint.resize(42);
        converted_flags.resize(42);
    }

    void set(char *line) {
        unsigned i = 0;
        char * startbit = line;
        while (1) {
            while (*line != ',' && *line != '\0') {
                ++line;
            }
            INVARIANT(i < 42, "bad");
            fields[i] = startbit;
            converted_flags[i] = 0;
            ++i;
            if (*line == '\0') {
                break;
            }
            *line = '\0';
            ++line;
            startbit = line;
        }
        INVARIANT(i==42, boost::format("bad %d '%s'") % i % fields[0]);
    }

    double doubleVal(int field) {
        if ((converted_flags[field] & doubleFlag) == 0) {
            asdouble[field] = atof(fields[field]);
            converted_flags[field] |= doubleFlag;
        }
        return asdouble[field];
    }

    int intVal(int field) {
        if ((converted_flags[field] & intFlag) == 0) {
            asint[field] = atoi(fields[field]);
            converted_flags[field] |= intFlag;
        }
        return asint[field];
    }

    static const int doubleFlag = 2;
    static const int intFlag = 4;
};

class LatencyGroupByInt {
  public:
    LatencyGroupByInt(int _start_field, int _end_field, int _groupby_field)
            : start_field(_start_field),
              end_field(_end_field),
              groupby_field(_groupby_field)
    {
    }

    typedef HashMap<int32_t, Stats *, HashMap_hashintfast> mytableT;

    virtual ~LatencyGroupByInt() { }

    virtual void processRow(csvline &line) {
        Stats *stat = mystats[line.intVal(groupby_field)];
        if (stat == NULL) {
            stat = new Stats();
            mystats[line.intVal(groupby_field)] = stat;
        }
        stat->add(line.doubleVal(end_field) - line.doubleVal(start_field));
    }

    virtual void printResult() {
        cout << boost::format("#%d, count(*), mean(#%d - #%d), stddev, min, max")
                % groupby_field % end_field % start_field << endl;
        for (mytableT::iterator i = mystats.begin();
            i != mystats.end(); ++i) {
            cout << boost::format("%d, %d, %.6g, %.6g, %.6g, %.6g")
                    % i->first % i->second->count() % i->second->mean() % i->second->stddev()
                    % i->second->min() % i->second->max()
                 << endl;
        }
    }

    mytableT mystats;
    int start_field, end_field, groupby_field;
};

int
main(int argc, char *argv[])
{
    static const int enter_driver = 0;
    static const int leave_driver = 1;
    static const int return_to_driver = 2;
    static const int device_number = 5;
    static const int logical_volume_number = 10;
    static const int bytes = 3;

    vector<LatencyGroupByInt *> mods;
    while (1) {
        int opt = getopt(argc, argv, "123456789");
        if (opt == -1) break;
        switch(opt)
        {
            case '1':
                mods.push_back(new LatencyGroupByInt(enter_driver,leave_driver,device_number));
                break;
            case '2':
                mods.push_back(new LatencyGroupByInt(enter_driver,leave_driver,logical_volume_number));
                break;
            case '3':
                mods.push_back(new LatencyGroupByInt(enter_driver,leave_driver,bytes));
                break;

            case '4':
                mods.push_back(new LatencyGroupByInt(enter_driver,return_to_driver,device_number));
                break;
            case '5':
                mods.push_back(new LatencyGroupByInt(enter_driver,return_to_driver,logical_volume_number));
                break;
            case '6':
                mods.push_back(new LatencyGroupByInt(enter_driver,return_to_driver,bytes));
                break;

            case '7':
                mods.push_back(new LatencyGroupByInt(leave_driver,return_to_driver,device_number));
                break;
            case '8':
                mods.push_back(new LatencyGroupByInt(leave_driver,return_to_driver,logical_volume_number));
                break;
            case '9':
                mods.push_back(new LatencyGroupByInt(leave_driver,return_to_driver,bytes));
                break;
            default: INVARIANT(false, "bad");
        }
    }

    csvline line;

    // using cin.getline() is a performance disaster, running through
    // hourly-0000x.csv takes 15 seconds, mostly in getc() which is
    // apparently how getline() gets the line.  fgets() drops the time
    // to about 2 seconds.

    while (!feof(stdin)) {
        char buf[8192];
        buf[0] = '\0';
        fgets(buf, 8192, stdin);
        if (feof(stdin))
            break;
        INVARIANT(buf[0] != '\0', "bad");
        line.set(buf);

        for (unsigned i=0;i<mods.size();++i) {
            mods[i]->processRow(line);
        }
    }

    for (unsigned i=0;i<mods.size();++i) {
        mods[i]->printResult();
    }

    return 0;
}

max_stars_repo_name: sbu-fsl/DataSeries

max_issues_repo_name: yoursunny/DataSeries

max_forks_repo_name: sbu-fsl/DataSeries