inline void algorithm_resample_u01_sequence(std::size_t N, std::size_t M,
    std::size_t L, int twid, const std::string &name)
{
    mckl::RNG rng;
    std::size_t num = 0;
    RealType error = 0;
    mckl::UniformIntDistribution<std::size_t> rsize(N / 2, N);
    U01SeqType u01seq;

    mckl::RNG rng1;
    mckl::RNG rng2;

    mckl::StopWatch watch1;
    mckl::StopWatch watch2;

    mckl::Vector<RealType> r1;
    mckl::Vector<RealType> r2;
    r1.reserve(N);
    r2.reserve(N);

    for (std::size_t i = 0; i != M; ++i) {
        std::size_t K = rsize(rng);
        num += K;
        r1.resize(K);
        r2.resize(K);

        watch1.start();
        mckl::U01Distribution<RealType> dist;
        mckl::rand(rng1, dist, std::min(L, K), r1.data());
        u01seq(K, r1.data(), r1.data());
        watch1.stop();

        watch2.start();
        u01seq(rng2, K, r2.data());
        watch2.stop();

        for (std::size_t j = 0; j != K; ++j)
            error = std::max(error, std::abs(r1[j] - r2[j]));
    }
    std::stringstream ss;
    ss << error / std::numeric_limits<RealType>::epsilon() << " eps";

    double c1 = 1.0 * watch1.cycles() / num;
    double c2 = 1.0 * watch2.cycles() / num;
    if (std::is_same<RealType, float>::value)
        std::cout << std::setw(twid) << std::left << "float";
    if (std::is_same<RealType, double>::value)
        std::cout << std::setw(twid) << std::left << "double";
    std::cout << std::setw(twid) << std::left << name;
    std::cout << std::setw(twid) << std::right << c1;
    std::cout << std::setw(twid) << std::right << c2;
    std::cout << std::setw(twid) << std::right << ss.str();
    std::cout << std::endl;
}
/* Uses a 2D gaussian to spread blocks on the surface
 * https://en.wikipedia.org/wiki/Gaussian_function#Two-dimensional_Gaussian_function
 */
void Simulation::add_blocks(int count, int size){

    float xc = -0.4; //skew gauss and location
    float yc = 0;
    float s = 0.5; //spread gauss and location

    typedef boost::mt19937 RNGType;
    RNGType rng( time(0) );
    /* s-1 is the location based on how spread it is, which wraps the gaussian bell over
     * the relevant parts making the gauss and the locations in the same range.
     * The reason we subtract 1 is to chop off the "skirts" of the gauss, prevent the creation
     * of miniscule boxes which does nothing but impact performance.
     */
    boost::uniform_real<> loc_range(-(s-0.1),s-0.1);
    boost::variate_generator<boost::mt19937&, boost::uniform_real<> > rlocation(rng,loc_range);
    boost::uniform_real<> size_range(0.002, (float) size/1000);
    boost::variate_generator<boost::mt19937&, boost::uniform_real<> > rsize(rng,size_range);

    for(int i = 0; i < count; ++i){
        //Gaussian gaussian amplitudes
        float a = rsize();

        float x = rlocation() + xc; //random + gaussian skew
        float y = rlocation() + yc;

        //2D gaussian
        float bsize = a*exp( -( (pow((x-xc), 2) / (2*pow(s, 2)) ) +
                    (pow((y-yc), 2) / (2*pow(s, 2)) ) ));


        /*tan(angle) is conversion from degrees to slope (relationship between rise and run)
         *Multiplying this with -x will find the correct height of the block based on the
         *rise. This means that you can place boxes along x and y in a slope, and the boxes will
         *follow the slope
         */
        ode::Object::ptr_t b
            (new ode::Box(*env, Eigen::Vector3d(x, y, bsize/2 + (tan(tilt)*-x)),
                          10, bsize*4, bsize*4, bsize)); //multiply by 4 to stretch out

        b->set_rotation(0.0f, -tilt, 0.0f);
        boxes.push_back(b);
        if(!headless){
            b->accept(*v);
        }
        b->fix();
        env->add_to_ground(*b);
    }
}
Exemple #3
0
void basicString::findLongestSpecificWord(const vector<string>& text,const string& notChar)
{
	auto iter = std::max_element(text.begin(), text.end(), [&](const string& lhs, const string& rhs) ->bool
	{
		string::size_type lsize(0), rsize(0);
		if (lhs.find_first_of(notChar) == string::npos)
			lsize = lhs.size();
		if (rhs.find_first_of(notChar) == string::npos)
			rsize = rhs.size();
		return lsize < rsize;
	});
	if (iter != text.end())
		std::cout << "Longest word without specific character is :" << *iter << std::endl;
	else
		std::cout << "input text is empty" << std::endl;
}
Exemple #4
0
inline void random_dirichlet(
    std::size_t N, std::size_t M, const RealType *alpha, bool scalar)
{
    using param_type =
        typename mckl::DirichletDistribution<RealType>::param_type;

    MCKLRNGType rng;
    MCKLRNGType rng1;
    MCKLRNGType rng2;
#if MCKL_HAS_MKL
    MKLRNGType rng_mkl;
#endif

    mckl::UniformIntDistribution<std::size_t> rsize(N / 2, N);
    mckl::DirichletDistribution<RealType> dist;
    if (scalar)
        dist.param(param_type(2, alpha[0]));
    else
        dist.param(param_type(2, alpha));

    bool pass = true;

    mckl::Matrix<RealType, mckl::RowMajor> r1;
    mckl::Matrix<RealType, mckl::RowMajor> r2;
#if MCKL_HAS_MKL
    mckl::Matrix<RealType, mckl::RowMajor> r3;
#endif
    for (std::size_t i = 0; i != M; ++i) {
        std::size_t K = rsize(rng);
        r1.resize(K, 2);
        r2.resize(K, 2);

        std::stringstream ss1;
        ss1.precision(20);
        ss1 << dist;
        for (std::size_t j = 0; j != K; ++j)
            dist(rng1, r1.data() + j * 2);
        ss1 >> dist;
        for (std::size_t j = 0; j != K; ++j)
            dist(rng2, r2.data() + j * 2);
        pass = pass && r1 == r2;

        std::stringstream ssb;
        ssb << dist;
        mckl::rand(rng1, dist, K, r1.data());
        ssb >> dist;
        mckl::rand(rng2, dist, K, r2.data());
        pass = pass && r1 == r2;
    }

    bool has_cycles = mckl::StopWatch::has_cycles();
    double c1 = has_cycles ? std::numeric_limits<double>::max() : 0.0;
    double c2 = has_cycles ? std::numeric_limits<double>::max() : 0.0;
#if MCKL_HAS_MKL
    double c3 = has_cycles ? std::numeric_limits<double>::max() : 0.0;
#endif
    for (std::size_t k = 0; k != 10; ++k) {
        std::size_t num = 0;
        mckl::StopWatch watch1;
        mckl::StopWatch watch2;
#if MCKL_HAS_MKL
        mckl::StopWatch watch3;
#endif
        for (std::size_t i = 0; i != M; ++i) {
            std::size_t K = rsize(rng);
            num += K * 2;
            r1.resize(K, 2);
            r2.resize(K, 2);
#if MCKL_HAS_MKL
            r3.resize(K, 2);
#endif

            watch1.start();
            RealType *p = r1.data();
            for (std::size_t j = 0; j != K; ++j, p += 2)
                dist(rng, p);
            watch1.stop();

            watch2.start();
            mckl::rand(rng, dist, K, r2.data());
            watch2.stop();

#if MCKL_HAS_MKL
            watch3.start();
            mckl::rand(rng_mkl, dist, K, r3.data());
            watch3.stop();
#endif

            pass = pass && r1 != r2;
#if MCKL_HAS_MKL
            pass = pass && r1 != r3;
#endif
        }
        if (has_cycles) {
            c1 = std::min(c1, 1.0 * watch1.cycles() / num);
            c2 = std::min(c2, 1.0 * watch2.cycles() / num);
#if MCKL_HAS_MKL
            c3 = std::min(c3, 1.0 * watch3.cycles() / num);
#endif
        } else {
            c1 = std::max(c1, num / watch1.seconds() * 1e-6);
            c2 = std::max(c2, num / watch2.seconds() * 1e-6);
#if MCKL_HAS_MKL
            c3 = std::max(c3, num / watch3.seconds() * 1e-6);
#endif
        }
    }

    std::stringstream ss;
    ss << "Dirichlet" << '<' << random_typename<RealType>() << ">(";
    const RealType *a = dist.alpha();
    ss << '{' << a[0] << ", " << a[1] << "})";

    std::cout << std::setw(40) << std::left << ss.str();
    std::cout << std::setw(12) << std::right << c1;
    std::cout << std::setw(12) << std::right << c2;
#if MCKL_HAS_MKL
    std::cout << std::setw(12) << std::right << c3;
#endif
    std::cout << std::setw(15) << std::right << random_pass(pass);
    std::cout << std::endl;

    std::ofstream txt("random_dirichlet.txt", std::ios_base::app);
    for (std::size_t i = 0; i != r1.nrow(); ++i) {
        txt << r1(i, 0) << '\t' << r1(i, 1) << '\t';
        txt << '"' << ss.str() << '"' << '\t' << "Single" << '\n';
    }
    for (std::size_t i = 0; i != r2.nrow(); ++i) {
        txt << r2(i, 0) << '\t' << r2(i, 1) << '\t';
        txt << '"' << ss.str() << '"' << '\t' << "Batch" << '\n';
    }
#if MCKL_HAS_MKL
    for (std::size_t i = 0; i != r3.nrow(); ++i) {
        txt << r3(i, 0) << '\t' << r3(i, 1) << '\t';
        txt << '"' << ss.str() << '"' << '\t' << "VSL" << '\n';
    }
#endif
    txt.close();
}
Exemple #5
0
void budget::display_table(std::vector<std::string> columns, std::vector<std::vector<std::string>> contents, std::size_t groups){
    budget_assert(groups > 0, "There must be at least 1 group");

    for(auto& row : contents){
        for(auto& cell : row){
            trim(cell);
        }
    }

    std::vector<std::size_t> widths;
    std::vector<std::size_t> header_widths;

    if(!contents.size()){
        for(auto& column : columns){
            widths.push_back(rsize(column));
        }
    } else {
        auto& first = contents[0];

        widths.assign(first.size(), 0);

        for(auto& row : contents){
            for(std::size_t i = 0; i < row.size(); ++i){
                widths[i] = std::max(widths[i], rsize(row[i]) + 1);
            }
        }
    }

    budget_assert(widths.size() == groups * columns.size(), "Widths incorrectly computed");

    for(std::size_t i = 0; i < columns.size(); ++i){
        auto& column = columns[i];

        std::size_t width = 0;
        for(std::size_t j = i * groups; j < (i + 1) * groups; ++j){
            width += widths[j];
        }

        width = std::max(width, rsize(column));
        header_widths.push_back(width + (i < columns.size() - 1 && rsize(column) >= width ? 1 : 0));

        //The last space is not underlined
        --width;

        std::cout << format_code(4, 0, 7) << column << (width > rsize(column) ? std::string(width - rsize(column), ' ') : "") << format_code(0, 0, 7);

        //The very last column has no trailing space

        if(i < columns.size() - 1){
            std::cout << " ";
        }
    }

    std::cout << std::endl;

    for(std::size_t i = 0; i < contents.size(); ++i){
        auto& row = contents[i];

        std::cout << format_code(0, 0, 7);

        for(std::size_t j = 0; j < row.size(); j += groups){
            std::size_t acc_width = 0;

            //First columns of the group
            for(std::size_t k = 0; k < groups - 1; ++k){
                auto column = j + k;

                std::string value = format(row[column]);

                acc_width += widths[column];
                std::cout << value;

                //The content of the column can change the style, it is
                //important to reapply it

                std::cout << format_code(0, 0, 7);

                std::cout << std::string(widths[column] - rsize(value), ' ');
            }

            //The last column of the group

            auto last_column = j + (groups - 1);
            auto width = widths[last_column];
            acc_width += width;

            //Pad with spaces to fit the header column width

            if(header_widths[j / groups] > acc_width){
                width += header_widths[j / groups] - acc_width;
            } else if(last_column == row.size() - 1){
                --width;
            }

            auto value = format(row[last_column]);
            std::cout << value;

            //The content of the column can change the style, it is
            //important to reapply it

            std::cout << format_code(0, 0, 7);

            std::cout << std::string(width - rsize(row[last_column]), ' ');
        }

        std::cout << std::endl;
    }
}
TEST(UnitTestMalloc, DISABLED_Performance)
{
    void *pointers[MAXP];
    int size[MAXP];

    int i = 0, r = 0, loop = 0, pass = 0, subpass = 0;
    size_t absmax=0, curmax=0;
    int realloc_mask = -1;
    size_t allocations = 0;
    size_t allocations_size = 0;
    size_t frees = 0;

    memset(pointers, 0, MAXP*sizeof(pointers[0]));

    double start_time = stk::cpu_time();
#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR
    free(malloc(1));

    ptrdiff_t start_mem = malloc_used();
    ptrdiff_t start_footprint = malloc_footprint();
#else
    ptrdiff_t start_mem = reinterpret_cast<ptrdiff_t>(sbrk(0));
#endif

#if defined SIERRA_PTMALLOC3_ALLOCATOR
    std::cout << "Modified ptmalloc3 allocator: ";
#elif defined SIERRA_PTMALLOC2_ALLOCATOR
    std::cout << "Modified ptmalloc2 allocator: ";
#else
    std::cout << "Default allocator: ";
#endif

#ifndef NDEBUG
    std::cout << "(debug)" << std::endl;
#endif

    std::cout << std::endl;

#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR
    std::cout << "Start used " << start_mem << std::endl;
    std::cout << "Start footprint " << start_footprint << std::endl;
#endif

    std::cout << std::endl
              << std::setw(14) << "elapsed" << "       "

#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR
              << std::setw(14) << "footprint" << "   "
              << std::setw(14) << "max_footprint" << "   "
#endif
              << std::setw(14) << "used " << "   "
              << std::setw(14) << "curmax" << " " << std::endl;

    for (loop=0; loop<NLOOP; loop++) {
        for (pass=0; pass<NPASS; pass++) {
            for (subpass=0; subpass<SUBP; subpass++) {
                for (i=0; i<MAXP; i++) {
                    int rno = rand();
                    if (rno & 8) {
                        if (pointers[i]) {
                            if (!(rno & realloc_mask)) {
                                r = rsize();
                                curmax -= size[i];
                                curmax += r;
                                pointers[i] = realloc(pointers[i], rsize());
                                size[i] = r;
                                if (absmax < curmax) absmax = curmax;
                            }
                            else {
                                curmax -= size[i];
                                free(pointers[i]);
                                pointers[i] = 0;
                                ++frees;
                            }
                        }
                        else {
                            r = rsize();
                            curmax += r;
                            pointers[i] = malloc(r);
                            size[i] = r;
                            ++allocations;
                            allocations_size += r;
                            if (absmax < curmax) absmax = curmax;
                        }
                    }
                }
            }
        }

        double end_time = stk::cpu_time();
#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR
        ptrdiff_t end_mem = malloc_used();
        ptrdiff_t end_footprint = malloc_footprint();
#else
        ptrdiff_t end_mem = reinterpret_cast<ptrdiff_t>(sbrk(0));
#endif

        double elapsed = end_time - start_time;
        std::cout << std::setw(14) << elapsed << " ticks "
#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR
                  << std::setw(14) << (end_footprint - start_footprint)/1024 << " K "
                  << std::setw(14) << malloc_max_footprint()/1024 << " K "
#endif
                  << std::setw(14) << (end_mem - start_mem)/1024 << " K "
                  << std::setw(14) << curmax/1024 << " K" << std::endl;
    }

    std::cout << allocations << " allocations of " << allocations_size/1024 << " K " << std::endl
              << frees << " frees" << std::endl;

    for (i=0; i<MAXP; i++)
        if (pointers[i])
            free(pointers[i]);
}
Exemple #7
0
inline RandomRNGPerf random_rng_p(std::size_t N, std::size_t M)
{
    using result_type = typename DistributionType::result_type;

    RNGType rng;
    DistributionType dist;
    mckl::UniformIntDistribution<std::size_t> rsize(N / 2, N);
    bool pass = true;

    RNGType rng1;
    RNGType rng2;

    mckl::Vector<result_type> r1(N);
    mckl::Vector<result_type> r2(N);

    bool has_cycles = mckl::StopWatch::has_cycles();

    double c1 = has_cycles ? std::numeric_limits<double>::max() : 0.0;
    double c2 = has_cycles ? std::numeric_limits<double>::max() : 0.0;
    for (std::size_t k = 0; k != 10; ++k) {
        std::size_t n = 0;
        mckl::StopWatch watch1;
        mckl::StopWatch watch2;
        for (std::size_t i = 0; i != M; ++i) {
            std::size_t K = rsize(rng);
            r1.resize(K);
            r2.resize(K);
            n += K;

            watch1.start();
            for (std::size_t j = 0; j != K; ++j) {
                r1[j] = mckl::rand(rng1, dist);
            }
            watch1.stop();

            watch2.start();
            mckl::rand(rng2, dist, K, r2.data());
            watch2.stop();

            pass = pass && (r1 == r2 || rng != rng);
        }
        if (std::is_integral<result_type>::value) {
            std::size_t bytes = sizeof(result_type) * n;
            if (has_cycles) {
                c1 = std::min(c1, 1.0 * watch1.cycles() / bytes);
                c2 = std::min(c2, 1.0 * watch2.cycles() / bytes);
            } else {
                c1 = std::max(c1, bytes / watch1.seconds() * 1e-9);
                c2 = std::max(c2, bytes / watch2.seconds() * 1e-9);
            }
        } else {
            if (has_cycles) {
                c1 = std::min(c1, 1.0 * watch1.cycles() / n);
                c2 = std::min(c2, 1.0 * watch2.cycles() / n);
            } else {
                c1 = std::max(c1, n / watch1.seconds() * 1e-6);
                c2 = std::max(c2, n / watch2.seconds() * 1e-6);
            }
        }
    }

    return {pass, c1, c2};
}
Exemple #8
0
inline bool random_rng_d(std::size_t N, std::size_t M)
{
    using result_type = typename RNGType::result_type;

    mckl::UniformIntDistribution<std::size_t> rsize(0, N);
    mckl::UniformBitsDistribution<std::uint16_t> ubits16;
    mckl::UniformBitsDistribution<std::uint32_t> ubits32;
    mckl::UniformBitsDistribution<std::uint64_t> ubits64;
    RNGType rng;
    bool pass = true;

    RNGType rng1;
    RNGType rng2;

    mckl::Vector<result_type> r1;
    mckl::Vector<result_type> r2;
    mckl::Vector<std::uint16_t> u16_1;
    mckl::Vector<std::uint16_t> u16_2;
    mckl::Vector<std::uint32_t> u32_1;
    mckl::Vector<std::uint32_t> u32_2;
    mckl::Vector<std::uint64_t> u64_1;
    mckl::Vector<std::uint64_t> u64_2;
    r1.reserve(N);
    r2.reserve(N);
    u16_1.reserve(N);
    u16_2.reserve(N);
    u32_1.reserve(N);
    u32_2.reserve(N);
    u64_1.reserve(N);
    u64_2.reserve(N);

    for (std::size_t i = 0; i != M; ++i) {
        std::size_t K = rsize(rng);
        r1.resize(K);
        r2.resize(K);
        u16_1.resize(K);
        u16_2.resize(K);
        u32_1.resize(K);
        u32_2.resize(K);
        u64_1.resize(K);
        u64_2.resize(K);

        for (std::size_t j = 0; j != K; ++j) {
            r1[j] = rng1();
        }
        mckl::rand(rng2, K, r2.data());
        pass = pass && (r1 == r2 || rng != rng);

        for (std::size_t j = 0; j != K; ++j) {
            u16_1[j] = mckl::rand(rng1, ubits16);
        }
        mckl::rand(rng2, ubits16, K, u16_2.data());
        pass = pass && (u16_1 == u16_2 || rng != rng);

        for (std::size_t j = 0; j != K; ++j) {
            u32_1[j] = mckl::rand(rng1, ubits32);
        }
        mckl::rand(rng2, ubits32, K, u32_2.data());
        pass = pass && (u32_1 == u32_2 || rng != rng);

        for (std::size_t j = 0; j != K; ++j) {
            u64_1[j] = mckl::rand(rng1, ubits64);
        }
        mckl::rand(rng2, ubits64, K, u64_2.data());
        pass = pass && (u64_1 == u64_2 || rng != rng);

        rng1.discard(static_cast<unsigned>(K));
        typename RNGType::result_type next = rng1();
        for (std::size_t j = 0; j != K; ++j) {
            rng2();
        }
        bool find = false;
        for (std::size_t j = 0; j != 2; ++j) {
            find = find || rng2() == next;
        }
        pass = pass && (find || rng != rng);

        std::stringstream ss;
        ss << rng;
        mckl::rand(rng, K, r1.data());
        ss >> rng;
        mckl::rand(rng, K, r2.data());
        pass = pass && (r1 == r2 || rng != rng);

        RNGType tmp(rng);
        mckl::rand(rng, K, r1.data());
        mckl::rand(tmp, K, r2.data());
        pass = pass && (r1 == r2 || rng != rng);

        tmp = rng;
        mckl::rand(rng, K, r1.data());
        mckl::rand(tmp, K, r2.data());
        pass = pass && (r1 == r2 || rng != rng);
    }

    return pass;
}