text:
#include "reads.h"

#include <cmath>
#include <cstring>

#include <queue>
#include <algorithm>
#include <iostream>
#include <chrono>

#include <boost/format.hpp>

#include "sparsehash/dense_hash_map"

#include "../util/inBuf.h"
#include "../util/outBuf.h"
// #include "meminfo.h"

using google::dense_hash_map;
using namespace std;
using namespace std::chrono;

void reads::loadGraph(char *gName) {
    FILE *fg = fopen(gName, "r");
    if (fg == nullptr) {
        printf("No graph %s\n", gName);
        exit(0);
    }

    Timer tm;
    ef.resize(n);
    eb.resize(n);
    for (int x, y; fscanf(fg, "%d%d", &x, &y) != EOF;) {
        ef[x].push_back(y);
        eb[y].push_back(x);
    }
    rtime = tm.getTime();
    fclose(fg);
}

void reads::constructIndices() {
    vector<pair<int, int>> q;
    vector<int> pos;
    auto cc = int(RAND_MAX * sqrt(c));

    // r samples
    nt.resize(r);
    for (int i = 0; i < r; i++) { nt[i].resize(n); }
    for (int i = 0; i < r; i++) {
        q.resize(0);
        // used for coupling or merging, if pos[p] becomes current level position, a coupling occurs; otherwise not
        // pos[p] current level position of vertex p
        pos.assign(n, -1);

        // 1st: level 0 expansion without stopping
        for (int j = 0, parent; j < n; j++) {
            if (!eb[j].empty()) {
                parent = eb[j][rand() % eb[j].size()];
                // check if vertex parent first occurring for level j+1
                if (pos[parent] < 0) {
                    pos[parent] = q.size();
                    q.emplace_back(parent, -1);
                }
                nt[i][j] = pos[parent];
            } else {
                nt[i][j] = -1;
            }
        }

        // 2nd: further expansion of level [1, t)
        auto q_cur = 0;
        int q_end = q.size();
        for (int j = 0, p, rr; j < t - 1 && q_cur < q_end; j++) {
            for (; q_cur < q_end; q_cur++) {
                auto &in_neighbors = eb[q[q_cur].first];
                if (!in_neighbors.empty() && ((rr = rand()) < cc || j == 0)) {
                    p = in_neighbors[rr % in_neighbors.size()];
                    // check if vertex p first occurring for level j+1
                    if (pos[p] < q_end) {
                        pos[p] = q.size();
                        q.emplace_back(p, -1);
                    }
                    q[q_cur].second = pos[p];
                }
            }
            q_end = q.size();
        }

        // 3rd: link leaf nodes for later query puroose
        auto tmpCnt = 0;
        vector<int> sta, tmp(n);
        for (int j = 0, qid; j < n; j++)
            // check if vertex j has at least one in-neighbor
            if (nt[i][j] > -1) {
                for (sta.resize(1), sta[0] = nt[i][j]; q[*sta.rbegin()].second >= 0;) {
                    sta.push_back(q[*sta.rbegin()].second);
                }
                if ((qid = -q[*sta.rbegin()].second - 2) < 0) {
                    q[*sta.rbegin()].second = -tmpCnt - 2;
                    tmp[tmpCnt] = pos[tmpCnt] = j;
                    tmpCnt++;
                } else {
                    nt[i][tmp[qid]] = j;
                    tmp[qid] = j;
                }
                for (int k = sta.size() - 2; k >= 0; k--) {
                    q[sta[k]].second = q[*sta.rbegin()].second;
                }
            }
        for (int j = 0; j < tmpCnt; j++) {
            nt[i][tmp[j]] = pos[j];
        }
    }

    // randomly shuffle edges
    for (int i = 0; i < n; i++) {
        random_shuffle(ef[i].begin(), ef[i].end());
        random_shuffle(eb[i].begin(), eb[i].end());
    }
}

void reads::serializeForSingleSource(Timer &timer, char *iName) {
    tm.reset();
    outBuf buf(iName);
    // next node of all the leaves
    for (int i = 0; i < r; i++)
        for (int j = 0; j < n; j++)
            buf.insert(nt[i][j]);
    // adjacency list part1: original graph
    for (int i = 0; i < n; i++) {
        buf.insert(ef[i].size());
        for (int j : ef[i])
            buf.insert(j);
    }
    // adjacency list part2: reversed graph
    for (int i = 0; i < n; i++) {
        buf.insert(eb[i].size());
        for (int j : eb[i])
            buf.insert(j);
    }
    rtime += tm.getTime();
}

void reads::postProcessNextForSinglePair() {
    vector<bool> is_visited(n);

    for (auto &next_arr: nt) {
        // bfs to initialize next_arr
        for (auto i = 0; i < n; i++) { is_visited[i] = false; }
        for (auto i = 0; i < n; i++) {
            if (!is_visited[i] && next_arr[i] > -1) {
                // use the first element as the tree id
                auto tree_id = i;
                auto cur_leaf_idx = i;
                do {
                    is_visited[cur_leaf_idx] = true;

                    auto next_leaf_idx = next_arr[cur_leaf_idx];
                    next_arr[cur_leaf_idx] = tree_id;
                    cur_leaf_idx = next_leaf_idx;
                } while (cur_leaf_idx != tree_id);
            }
        }
    }
}

void reads::deserializeForSingleSource(char *iName) {
    inBuf buf(iName);
    ef.resize(n);
    eb.resize(n);
    nt.resize(r);
    for (int i = 0; i < r; i++) {
        nt[i].resize(n);
        for (int j = 0; j < n; j++)
            buf.nextInt(nt[i][j]);
    }
#ifdef SINGLE_PAIR
    postProcessNextForSinglePair();
#endif

    for (int i = 0, s; i < n; i++) {
        buf.nextInt(s);
        ef[i].resize(s);
        for (int j = 0; j < s; j++)
            buf.nextInt(ef[i][j]);
    }
    for (int i = 0, s; i < n; i++) {
        buf.nextInt(s);
        eb[i].resize(s);
        for (int j = 0; j < s; j++)
            buf.nextInt(eb[i][j]);
    }
    rtime = 0;
}

reads::reads(char *gName_, int n_, int r_, double c_, int t_) {
    sprintf(gName, "%s", gName_);
    n = n_, r = r_, c = c_, t = t_;
    t1 = t2 = qCnt = 0;

    char iName[125];
    sprintf(iName, "%s.reads.%d_%d_%lf_%d", gName, n, r, c, t);

#ifdef STORE_INDEX
    if (fopen(iName, "rb") != NULL) {
        // with ready indices: deserialization for index structures
        deserializeForSingleSource(iName);
        return;
    }
#endif

    // 1st: load graph
    loadGraph(gName);

    // 2nd: start constructing indices
    constructIndices();

    // 3rd: serialization for index structures
#ifdef STORE_INDEX
    serializeForSingleSource(tm, iName);
#endif
}

reads::reads(string gName_, int n_, int r_, double c_, int t_) {
    // init parameters
    n = n_, r = r_, c = c_, t = t_;
    t1 = t2 = qCnt = 0;

    // init graph path
    string input_graph_full_path =
            "/homes/ywangby/workspace/yche/git-repos/SimRank/LPMC-Profile/build/datasets/edge_list/" + gName_ + ".txt";
    memcpy(gName, input_graph_full_path.c_str(), sizeof(char) * input_graph_full_path.size());
    gName[input_graph_full_path.size()] = '\0';

    // init index path
    string iName = boost::str(boost::format(
            "/homes/ywangby/workspace/yche/git-repos/SimRank/LPMC-Profile/build/datasets/reads/%s_%d_%d_%lf_%d.bin") %
                              gName_ % n % r % c % t);

    if (fopen(iName.c_str(), "rb") != nullptr) {
        deserializeForSingleSource(const_cast<char *>(iName.c_str()));
    } else {
        loadGraph(gName);
        auto start = high_resolution_clock::now();
        constructIndices();
        auto end = high_resolution_clock::now();
        cout << "indexing time:" << duration_cast<microseconds>(end - start).count() / pow(10, 6) << " s\n";
        cout << iName << endl;
        serializeForSingleSource(tm, const_cast<char *>(iName.c_str()));
    }
}

reads::~reads() = default;

double reads::queryOne(int x, int y) {
    if (x == y) { return 1; }

    int match_count = 0;
    for (auto i = 0; i < r; i++) {
        // nt[i][x] means no in-neighbor for vertex x
        if (nt[i][x] == nt[i][y] && nt[i][x] != -1 && nt[i][y] != -1) {
            match_count++;
        };
    }
    return static_cast<double>(match_count) * c / r;
}

double reads::queryOne(int x, int y, double *ansVal) {
    if (x == y) { return 1; }
    queryAll(x, ansVal);
    return ansVal[y];
}

void reads::queryAll(int x, double *ansVal) {
    memset(ansVal, 0, sizeof(double) * n);
    if (eb[x].empty()) {
        ansVal[x] = 1;
        return;
    }

#ifdef SHIFT_ARRAY_HASH_TABLE
    if (qCnt >= 20 && t1 < t2 || qCnt < 20 && qCnt % 2 == 0) {
        if (qCnt < 20) tm.reset();
#endif
    dense_hash_map<int, double> sim1;
    sim1.set_empty_key(-1);

    auto *nID = new bool[n];
    memset(nID, 0, sizeof(bool) * n);

    double cc = (1 - c) * r, c1 = r / c;

    int p, q;
    for (auto i = eb[x].begin(); i != eb[x].end(); i++) {
        for (auto j = eb[p = *i].begin(); j != eb[p].end() && j - eb[p].begin() < 10; j++)
            for (auto k = ef[q = *j].begin(); k != ef[q].end() && k - ef[q].begin() < 10; k++)
                if (*k != p)
                    sim1[*k] += cc / eb[p].size() / eb[*k].size();
        sim1[*i] += c1;
    }

    for (int i = 0, cnt, q; i < r; i++) {
        for (int j : eb[x])
            nID[j] = true;

        for (auto j = eb[x].begin(); j != eb[x].end(); j++)
            if (nID[q = *j] && nt[i][q] != q && nt[i][q] != -1) {
                cnt = 0;

                for (int p = nt[i][q]; p != q; p = nt[i][p]) {
                    if (nID[p]) cnt++, nID[p] = false;
                    else sim1[p] += c;
                }

                if (cnt != 0) {
                    cc = c * cnt;
                    for (int p = nt[i][q]; p != q; p = nt[i][p])
                        sim1[p] += cc;
                    sim1[q] += cc;
                }
            }

    }
    cc = c * c / eb[x].size() / r;
    for (auto &it : sim1)
        for (auto jt = ef[p = it.first].begin(); jt != ef[p].end(); jt++)
            ansVal[*jt] += it.second * cc / eb[*jt].size();

    delete[] nID;

#ifdef SHIFT_ARRAY_HASH_TABLE
    if (qCnt++ < 20) t1 += tm.getTime();
} else {

    if (qCnt < 20) tm.reset();

    auto *sim1 = new double[n];
    memset(sim1, 0, sizeof(double) * n);

    auto *nID = new bool[n];
    memset(nID, 0, sizeof(bool) * n);

    double cc = (1 - c) * r, c1 = r / c;

    int p, q;
    for (auto i = eb[x].begin(); i != eb[x].end(); i++) {
        for (auto j = eb[p = *i].begin(); j != eb[p].end() && j - eb[p].begin() < 10; j++)
            for (auto k = ef[q = *j].begin(); k != ef[q].end() && k - ef[q].begin() < 10; k++)
                if (*k != p)
                    sim1[*k] += cc / eb[p].size() / eb[*k].size();
        sim1[*i] += c1;
    }

    for (int i = 0, cnt, q; i < r; i++) {
        for (int j : eb[x])
            nID[j] = true;

        for (auto j = eb[x].begin(); j != eb[x].end(); j++)
            if (nID[q = *j] && nt[i][q] != q && nt[i][q] != -1) {
                cnt = 0;

                for (int p = nt[i][q]; p != q; p = nt[i][p]) {
                    if (nID[p]) cnt++, nID[p] = false;
                    else sim1[p] += c;
                }

                if (cnt != 0) {
                    cc = c * cnt;
                    for (int p = nt[i][q]; p != q; p = nt[i][p])
                        sim1[p] += cc;
                    sim1[q] += cc;
                }
            }

    }

    for (int i = 0; i < n; i++)
        if (sim1[i] > 0)
            for (auto j = ef[i].begin(); j != ef[i].end(); j++)
                ansVal[*j] += sim1[i];

    cc = c * c / r / eb[x].size();
    for (int i = 0; i < n; i++)
        if (!eb[i].empty())
            ansVal[i] = ansVal[i] * cc / eb[i].size();

    delete[] nID;

    if (qCnt++ < 20) t2 += tm.getTime();
}
#endif
}

max_stars_repo_name: RapidsAtHKUST/SimRank

max_issues_repo_name: RapidsAtHKUST/SimRank

max_forks_repo_name: RapidsAtHKUST/SimRank


text:
#include "CartoCSSParser.h"
#include "Expression.h"
#include "Predicate.h"
#include "mapnikvt/CSSColorParser.h"
#include "mapnikvt/ParserUtils.h"
#include "mapnikvt/ValueConverter.h"

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/repository/include/qi_distinct.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_bind.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

namespace carto { namespace css {
    namespace cssparserimpl {
        template <typename Iterator>
        struct Skipper : boost::spirit::qi::grammar<Iterator> {
            Skipper() : Skipper::base_type(skip, "PL/0") {
                using namespace boost::spirit;

                skip = qi::iso8859_1::space | ("/*" >> *(qi::char_ - "*/") >> "*/") | ("//" >> *(qi::char_ - qi::char_("\n\r")));
            }

            boost::spirit::qi::rule<Iterator> skip;
        };

        template <typename Iterator>
        struct Grammar : boost::spirit::qi::grammar<Iterator, StyleSheet(), Skipper<Iterator>> {
            Grammar() : Grammar::base_type(stylesheet) {
                using namespace boost;
                using namespace boost::spirit;
                using qi::_val;
                using qi::_1;
                using qi::_2;
                using qi::_3;
                using qi::_4;
                using qi::_pass;

                unesc_char.add("\\a", '\a')("\\b", '\b')("\\f", '\f')("\\n", '\n')
                              ("\\r", '\r')("\\t", '\t')("\\v", '\v')("\\\\", '\\')
                              ("\\\'", '\'')("\\\"", '\"');

                nonascii_  = qi::char_("\xA0-\xFF");
                unescaped_ = qi::char_("_a-zA-Z0-9-#().,%");
                hex_       = *qi::char_("0-9a-fA-F");

                string =
                      ('\'' >> *(unesc_char | "\\x" >> octet_ | (qi::char_ - '\''))) > '\''
                    | ('\"' >> *(unesc_char | "\\x" >> octet_ | (qi::char_ - '\"'))) > '\"'
                    ;

                literal =
                      (qi::char_("/_a-zA-Z-") | nonascii_) > *(qi::char_("/_a-zA-Z0-9-") | nonascii_)
                    ;

                uri =
                      (qi::lit("url") >> '(' >> string) > ')'
                    | (qi::lit("url") >> '(' >> *(unesc_char | "\\x" >> octet_ | (qi::print - ')'))) > ')'
                    ;

                color =
                      (qi::lit("rgb")  >> '(' >> number > ',' > number > ',' > number > ')') [_val = phoenix::bind(&makeRGBAColor, _1, _2, _3, Value(1.0))]
                    | (qi::lit("rgba") >> '(' >> number > ',' > number > ',' > number > ',' > number > ')') [_val = phoenix::bind(&makeRGBAColor, _1, _2, _3, _4)]
                    | qi::no_skip[qi::lit('#') > (*qi::char_("0-9A-Fa-f"))]    [_pass = phoenix::bind(&getHEXColor, _val, _1)]
                    | qi::no_skip[*qi::char_("a-z")]                           [_pass = phoenix::bind(&getCSSColor, _val, _1)]
                    ;

                number =
                      (qi::real_parser<double, qi::real_policies<double>>() >> *qi::space >> '%') [_val = phoenix::construct<Value>(_1 / 100.0)]
                    | (qi::real_parser<double, qi::real_policies<double>>() >> *qi::space >> "px") [_val = phoenix::construct<Value>(_1)]
                    | qi::real_parser<double, qi::strict_real_policies<double>>() [_val = phoenix::construct<Value>(_1)]
                    | qi::long_long                                 [_val = phoenix::construct<Value>(_1)]
                    ;

                constant =
                      qi::lit("null")                               [_val = phoenix::construct<Value>()]
                    | qi::lit("false")                              [_val = phoenix::construct<Value>(false)]
                    | qi::lit("true")                               [_val = phoenix::construct<Value>(true)]
                    | number                                        [_val = _1]
                    | uri                                           [_val = phoenix::construct<Value>(_1)]
                    | color                                         [_val = phoenix::construct<Value>(_1)]
                    | string                                        [_val = phoenix::construct<Value>(_1)]
                    | literal                                       [_val = phoenix::construct<Value>(_1)]
                    ;

                blockid = qi::lexeme[+(qi::char_("_a-zA-Z0-9-") | nonascii_)];
                propid  = qi::lexeme[(qi::char_("/_a-zA-Z-") | nonascii_) > *(qi::char_("/_a-zA-Z0-9-") | nonascii_)];
                funcid  = qi::lexeme[(qi::char_("_a-zA-Z")   | nonascii_) > *(qi::char_("_a-zA-Z0-9")   | nonascii_)];
                varid   = qi::lexeme[+(qi::char_("_a-zA-Z0-9-") | nonascii_)];
                fieldid = qi::lexeme[+(qi::char_("_a-zA-Z0-9-") | nonascii_)];
                unescapedfieldid = qi::lexeme[+(qi::print - qi::char_("[]{}")) > -(qi::char_("[") > unescapedfieldid > qi::char_("]")) > -(qi::char_("{") > unescapedfieldid > qi::char_("}"))];

                expressionlist =
                      (expression >> (',' > (expression % ',')))    [_val = phoenix::bind(&makeListExpression, _1, _2)]
                    | expression                                    [_val = _1]
                    ;

                expression =
                    term0                                           [_val = _1]
                    >> -((qi::lit("?") > expression > ':' > expression) [_val = phoenix::bind(&makeConditionalExpression, _val, _1, _2)]
                        )
                    ;

                term0 =
                    term1                                           [_val = _1]
                    >> *( (qi::lit("&&") > term1)                   [_val = phoenix::bind(&makeBinaryExpression, BinaryExpression::Op::AND, _val, _1)]
                        | (qi::lit("||") > term1)                   [_val = phoenix::bind(&makeBinaryExpression, BinaryExpression::Op::OR,  _val, _1)]
                        )
                    ;

                term1 =
                    term2                                           [_val = _1]
                    >> *( (qi::lit("=~") > term2)                   [_val = phoenix::bind(&makeBinaryExpression, BinaryExpression::Op::MATCH, _val, _1)]
                        | (qi::lit("=")  > term2)                   [_val = phoenix::bind(&makeBinaryExpression, BinaryExpression::Op::EQ,  _val, _1)]
                        | (qi::lit("!=") > term2)                   [_val = phoenix::bind(&makeBinaryExpression, BinaryExpression::Op::NEQ, _val, _1)]
                        | (qi::lit("<=") > term2)                   [_val = phoenix::bind(&makeBinaryExpression, BinaryExpression::Op::LTE, _val, _1)]
                        | (qi::lit("<")  > term2)                   [_val = phoenix::bind(&makeBinaryExpression, BinaryExpression::Op::LT,  _val, _1)]
                        | (qi::lit(">=") > term2)                   [_val = phoenix::bind(&makeBinaryExpression, BinaryExpression::Op::GTE, _val, _1)]
                        | (qi::lit(">")  > term2)                   [_val = phoenix::bind(&makeBinaryExpression, BinaryExpression::Op::GT,  _val, _1)]
                        )
                    ;

                term2 =
                    term3                                           [_val = _1]
                    >> *( (qi::lit("+") > term3)                    [_val = phoenix::bind(&makeBinaryExpression, BinaryExpression::Op::ADD, _val, _1)]
                        | (qi::lit("-") > term3)                    [_val = phoenix::bind(&makeBinaryExpression, BinaryExpression::Op::SUB, _val, _1)]
                        )
                    ;

                term3 =
                    unary                                           [_val = _1]
                    >> *( (qi::lit("*") > unary)                    [_val = phoenix::bind(&makeBinaryExpression, BinaryExpression::Op::MUL, _val, _1)]
                        | (qi::lit("/") > unary)                    [_val = phoenix::bind(&makeBinaryExpression, BinaryExpression::Op::DIV, _val, _1)]
                        )
                    ;

                unary =
                      factor                                        [_val = _1]
                    | (qi::lit("-") > unary)                        [_val = phoenix::bind(&makeUnaryExpression, UnaryExpression::Op::NEG, _1)]
                    | (qi::lit("!") > unary)                        [_val = phoenix::bind(&makeUnaryExpression, UnaryExpression::Op::NOT, _1)]
                    ;

                factor =
                      ('@' > varid)                                 [_val = phoenix::construct<FieldOrVar>(false, _1)]
                    | ('[' > unescapedfieldid > ']')                [_val = phoenix::construct<FieldOrVar>(true, _1)]
                    | (funcid [_pass = phoenix::bind(&checkFunction, _1)] >> ('(' > (expression % ',') > ')')) [_val = phoenix::bind(&makeFunctionExpression, _1, _2)]
                    | ('(' > expressionlist > ')')                  [_val = _1]
                    | constant                                      [_val = phoenix::bind(&makeStringExpressionOrConstant, _1, _pass)]
                    ;

                op =
                      qi::lit("=~")                                 [_val = OpPredicate::Op::MATCH]
                    | qi::lit("=")                                  [_val = OpPredicate::Op::EQ]
                    | qi::lit("!=")                                 [_val = OpPredicate::Op::NEQ]
                    | qi::lit("<=")                                 [_val = OpPredicate::Op::LTE]
                    | qi::lit("<")                                  [_val = OpPredicate::Op::LT]
                    | qi::lit(">=")                                 [_val = OpPredicate::Op::GTE]
                    | qi::lit(">")                                  [_val = OpPredicate::Op::GT]
                    ;

                predicate =
                      qi::lit("Map")                                [_val = phoenix::construct<MapPredicate>()]
                    | (qi::lit('#') > blockid)                      [_val = phoenix::construct<LayerPredicate>(_1)]
                    | (qi::lit('.') > blockid)                      [_val = phoenix::construct<ClassPredicate>(_1)]
                    | (qi::lit("::") > blockid)                     [_val = phoenix::construct<AttachmentPredicate>(_1)]
                    | ((qi::lit('[') >> '@') > varid > op > constant > ']') [_val = phoenix::bind(&makeOpPredicate, _2, false, _1, _3)]
                    | (qi::lit('[') > (fieldid | string) > op > constant > ']') [_val = phoenix::bind(&makeOpPredicate, _2, true, _1, _3)]
                    | ((qi::lit("when") >> '(') > expression > ')') [_val = phoenix::bind(&makeWhenPredicate, _1)]
                    ;

                selector = (*predicate)                             [_val = phoenix::construct<Selector>(_1)];

                propdeclaration = (propid >> (':' > expressionlist)) [_val = phoenix::bind(&makePropertyDeclaration, phoenix::ref(_declarationOrder), _1, _2)];

                blockelement =
                      propdeclaration                               [_val = phoenix::construct<Block::Element>(_1)]
                    | ruleset                                       [_val = phoenix::construct<Block::Element>(_1)]
                    ;
                block = (*(blockelement > -qi::lit(';')))           [_val = phoenix::construct<Block>(_1)];

                ruleset = ((selector % ',') >> ('{' > block > '}')) [_val = phoenix::construct<RuleSet>(_1, _2)];

                vardeclaration = ('@' > varid > ':' > expressionlist) [_val = phoenix::construct<VariableDeclaration>(_1, _2)];

                stylesheetelement =
                      vardeclaration                                [_val = phoenix::construct<StyleSheet::Element>(_1)]
                    | ruleset                                       [_val = phoenix::construct<StyleSheet::Element>(_1)]
                    ;
                stylesheet = (*(stylesheetelement > -qi::lit(';'))) [_val = phoenix::construct<StyleSheet>(_1)];

                qi::on_error<qi::fail>(stylesheet, phoenix::ref(_errorPos) = _3 - _1);
            }

            std::string::size_type errorPos() const { return _errorPos; }

            boost::spirit::qi::int_parser<char, 16, 2, 2> octet_;
            boost::spirit::qi::symbols<char const, char const> unesc_char;
            boost::spirit::qi::rule<Iterator, char()> nonascii_, unescaped_;
            boost::spirit::qi::rule<Iterator, std::string()> hex_;
            boost::spirit::qi::rule<Iterator, std::string()> string, literal;
            boost::spirit::qi::rule<Iterator, std::string(), Skipper<Iterator> > uri;
            boost::spirit::qi::rule<Iterator, Color(), Skipper<Iterator> > color;
            boost::spirit::qi::rule<Iterator, Value(), Skipper<Iterator> > number, constant;
            boost::spirit::qi::rule<Iterator, std::string()> blockid, propid, funcid, varid, fieldid, unescapedfieldid;
            boost::spirit::qi::rule<Iterator, Expression(), Skipper<Iterator> > expressionlist, expression, term0, term1, term2, term3, unary, factor;
            boost::spirit::qi::rule<Iterator, OpPredicate::Op()> op;
            boost::spirit::qi::rule<Iterator, Predicate(), Skipper<Iterator> > predicate;
            boost::spirit::qi::rule<Iterator, Selector(), Skipper<Iterator> > selector;
            boost::spirit::qi::rule<Iterator, PropertyDeclaration(), Skipper<Iterator> > propdeclaration;
            boost::spirit::qi::rule<Iterator, Block::Element(), Skipper<Iterator> > blockelement;
            boost::spirit::qi::rule<Iterator, Block(), Skipper<Iterator> > block;
            boost::spirit::qi::rule<Iterator, RuleSet(), Skipper<Iterator> > ruleset;
            boost::spirit::qi::rule<Iterator, VariableDeclaration(), Skipper<Iterator> > vardeclaration;
            boost::spirit::qi::rule<Iterator, StyleSheet::Element(), Skipper<Iterator> > stylesheetelement;
            boost::spirit::qi::rule<Iterator, StyleSheet(), Skipper<Iterator> > stylesheet;

        private:
            static bool getHEXColor(Color& color, const std::vector<char>& code) {
                unsigned int value = 0;
                if (!mvt::parseCSSColor("#" + std::string(code.begin(), code.end()), value)) {
                    return false;
                }
                color = Color::fromValue(value);
                return true;
            }

            static bool getCSSColor(Color& color, const std::vector<char>& name) {
                unsigned int value = 0;
                if (!mvt::parseCSSColor(std::string(name.begin(), name.end()), value)) {
                    return false;
                }
                color = Color::fromValue(value);
                return true;
            }

            static Color makeRGBAColor(const Value& r, const Value& g, const Value& b, const Value& a) {
                auto getFloat = [](const Value& val) {
                    return std::holds_alternative<long long>(val) ? static_cast<float>(std::get<long long>(val)) : static_cast<float>(std::get<double>(val));
                };
                return Color::fromRGBA(getFloat(r) / 255.0f, getFloat(g) / 255.0f, getFloat(b) / 255.0f, getFloat(a));
            }

            static Expression makeStringExpressionOrConstant(const Value& val, bool& pass) {
                if (auto strVal = std::get_if<std::string>(&val)) {
                    try {
                        mvt::Expression mvtExpr = mvt::parseExpression(*strVal, true);
                        if (auto mvtVal = std::get_if<mvt::Value>(&mvtExpr)) {
                            return Value(mvt::ValueConverter<std::string>::convert(*mvtVal));
                        }
                        return std::make_shared<StringExpression>(*strVal);
                    }
                    catch (const std::exception&) {
                        pass = false;
                    }
                }
                return val;
            }

            static Expression makeListExpression(const Expression& initialExpr, const std::vector<Expression>& restExprs) {
                std::vector<Expression> exprs;
                exprs.push_back(initialExpr);
                exprs.insert(exprs.end(), restExprs.begin(), restExprs.end());
                return std::make_shared<ListExpression>(exprs);
            }

            static bool checkFunction(const std::string& func) {
                return func != "url" && func != "rgb" && func != "rgba"; // ignore special-built in constructors
            }

            static Expression makeFunctionExpression(const std::string& func, const std::vector<Expression>& exprs) {
                return std::make_shared<FunctionExpression>(func, exprs);
            }

            static Expression makeUnaryExpression(UnaryExpression::Op op, const Expression& expr) {
                return std::make_shared<UnaryExpression>(op, expr);
            }

            static Expression makeBinaryExpression(BinaryExpression::Op op, const Expression& expr1, const Expression& expr2) {
                return std::make_shared<BinaryExpression>(op, expr1, expr2);
            }

            static Expression makeConditionalExpression(const Expression& cond, const Expression& expr1, const Expression& expr2) {
                return std::make_shared<ConditionalExpression>(cond, expr1, expr2);
            }

            static Predicate makeOpPredicate(OpPredicate::Op op, bool field, const std::string& name, const Value& refValue) {
                return OpPredicate(op, FieldOrVar(field, name), refValue);
            }

            static Predicate makeWhenPredicate(const Expression& expr) {
                return WhenPredicate(expr);
            }

            static PropertyDeclaration makePropertyDeclaration(int& order, const std::string& field, const Expression& expr) {
                return PropertyDeclaration(field, expr, order++);
            }

            int _declarationOrder = 0;
            std::string::size_type _errorPos = std::string::npos;
        };
    }

    StyleSheet CartoCSSParser::parse(const std::string& cartoCSS) {
        std::string::const_iterator it = cartoCSS.begin();
        std::string::const_iterator end = cartoCSS.end();
        cssparserimpl::Grammar<std::string::const_iterator> grammar;
        cssparserimpl::Skipper<std::string::const_iterator> skipper;
        StyleSheet styleSheet;
        bool result = false;
        try {
            result = boost::spirit::qi::phrase_parse(it, end, grammar, skipper, styleSheet);
        }
        catch (const boost::spirit::qi::expectation_failure<std::string::const_iterator>& ex) {
            throw ParserError("Expectation error", resolvePosition(cartoCSS, ex.first - cartoCSS.begin()));
        }
        if (!result) {
            if (grammar.errorPos() != std::string::npos) {
                throw ParserError("Syntax error", resolvePosition(cartoCSS, grammar.errorPos()));
            }
            else {
                throw ParserError("Parsing error");
            }
        }
        else if (it != cartoCSS.end()) {
            throw ParserError("Failed to parse to the end", resolvePosition(cartoCSS, it - cartoCSS.begin()));
        }
        return styleSheet;
    }

    std::pair<int, int> CartoCSSParser::resolvePosition(const std::string& str, std::string::size_type pos) {
        std::pair<int, int> colLine(1, 1);
        for (std::string::size_type i = 0; i < pos; i++) {
            if (str[i] == '\n') {
                colLine.second++;
                colLine.first = 1;
            }
        }
        return colLine;
    }
} }

ext: cpp

max_stars_repo_name: CartoDB/mobile-carto-libs

max_issues_repo_name: CartoDB/mobile-carto-libs

max_forks_repo_name: CartoDB/mobile-carto-libs


text:
/**
 * @file winmain.cxx
 * @desc Main program.
 */
#include <windows.h>
#include <assert.h>
#include <boost/log/trivial.hpp>
#include <d3d9.h>
#include <d3dx9.h>
#include "d3d.hxx"
#include "game.hxx"

static Game *game = NULL;

/**
 * Initializes all core modules.
 */
bool Initialize(HWND hwnd)
{
	if (!D3DInit(hwnd))
		return false;
	game = new Game(d3dDev);
	return true;
}

/**
 * Uninitializes all core modules.
 */
void Uninitialize()
{
	if (game) {
		delete game;
		game = NULL;
	}
	D3DUninit();
}

/**
 * The main window function.
 */
LRESULT CALLBACK
WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) {
	case WM_KEYDOWN:
		if (!game || !game->handleKey(hwnd, wParam)) {
			if (wParam == VK_ESCAPE)
				PostMessage(hwnd, WM_CLOSE, 0, 0);
		}
		return 0;

	case WM_ACTIVATE:
		if (wParam == WA_ACTIVE || wParam == WA_CLICKACTIVE)
			active = true;
		else
			active = false;
		if (game)
			game->handleActivate(hwnd, active);
		return 0;

	case WM_DESTROY:
		Uninitialize();
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

/**
 * Main program for Windows.
 */
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	BOOST_LOG_TRIVIAL(trace) << "CapmanDX";

#ifdef _DEBUG
#ifdef _MSC_VER
	AllocConsole();
	freopen("CONOUT$", "w", stdout);
#endif
#endif

	// registers window class
	const TCHAR windowClassName[] = TEXT("capmandx");
	WNDCLASSEX wc = { 0 };
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WindowProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(0, IDI_APPLICATION);
	wc.hCursor = LoadCursor(0, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = windowClassName;
	wc.hIconSm = 0;
	RegisterClassEx(&wc);

	// creates window
	HWND hwnd = CreateWindowEx(0,
		windowClassName, TEXT("Capman"),
		WS_POPUP,
		0, 0, 1024, 768,
		HWND_DESKTOP, 0, hInstance, NULL);
	if (hwnd == 0) {
		return 0;
	}
	ShowWindow(hwnd, nCmdShow);

	// initializes all core modules
	if (!Initialize(hwnd)) {
		Uninitialize();
		return 0;
	}

	// main loop
	MSG msg;
	while (1) {
		while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		if (msg.message == WM_QUIT)
			break;
		if (active)
			game->render();
	}

	return (int)msg.wParam;
}

ext: cxx

max_stars_repo_name: taworn/capmandx

max_issues_repo_name: taworn/capmandx

max_forks_repo_name: taworn/capmandx


text:
// doMatching2D

// Program for 2D LIDAR alignment
// using the FRSM library. It uses pretty brute force
// settings.
//
// Options: - take 2 scans from user and give transformation between them (-a reference.csv, -b input.csv)
//          - loop over a dataset and give transformation between all combinations (write to file)

#include <icp-registration/icp_utils.h>

#include <zlib.h>
#include <lcm/lcm-cpp.hpp>

#include <lcmtypes/bot_core.hpp>
#include <boost/shared_ptr.hpp>
#include <lidar-odom/lidar-odometry.hpp>
#include <lcmtypes/bot_core.hpp>
#include <ConciseArgs>

#include <iterator>
#include <sstream>
#include <iostream>

void parseTransf(string& transform, float& x, float& y, float& theta)
{
  transform.erase(std::remove(transform.begin(), transform.end(), '['),
            transform.end());
  transform.erase(std::remove(transform.begin(), transform.end(), ']'),
            transform.end());
  std::replace( transform.begin(), transform.end(), ',', ' ');
  std::replace( transform.begin(), transform.end(), ';', ' ');

  float transValues[3] = {0};
  stringstream transStringStream(transform);
  for( int i = 0; i < 3; i++) {
    if(!(transStringStream >> transValues[i])) {
      cerr << "An error occured while trying to parse the initial "
         << "transformation." << endl
         << "No initial transformation will be used" << endl;
      return;
    }
  }

  x = transValues[0];
  y = transValues[1];
  theta = transValues[2];
}

struct CommandLineConfig
{
  std::string filenameA;
  std::string filenameB;
};

Eigen::Isometry3d getXYThetaAsIsometry3d(Eigen::Vector3d transl, Eigen::Vector3d rpy){
  Eigen::Isometry3d tf_out;
  tf_out.setIdentity();
  tf_out.translation()  << transl[0], transl[1], transl[2];

  double rpy_d[3] = { rpy[0], rpy[1], rpy[2] };
  double quat[4];
  bot_roll_pitch_yaw_to_quat(rpy_d, quat);
  Eigen::Quaterniond q(quat[0], quat[1],quat[2],quat[3]);
  tf_out.rotate(q);

  return tf_out;
}

class App{
  public:
    App(boost::shared_ptr<lcm::LCM> &lcm_, const CommandLineConfig& cl_cfg_, int indTransfFile);

    ~App(){
    }

    const char *homedir;

    int num_clouds; //number of clouds to combine

    string cloudA;
    string cloudB;

    string initialT;  // We store the transform used to initialize the
                      // pose of cloud B. The lidar odometry class outputs the complete
                      // transformation from the starting pose (without initialization)
                      // of cloud B to cloud A. Instead, we want to store just the remaining
                      // x,y,theta after initialization.
    ScanTransform initInput;
    ScanTransform initReference;

    void doWork(Eigen::MatrixXf &transf_matrix, int transf_index);
    void doWork();

  private:
    boost::shared_ptr<lcm::LCM> lcm_;
    const CommandLineConfig cl_cfg_;
    LidarOdom* lidarOdom_;
};

void readCSVFile(std::string filename, std::vector<float> &x, std::vector<float> &y){

    std::vector<std::vector<double> > values;
    std::ifstream fin(filename.c_str());
    for (std::string line; std::getline(fin, line); )
    {
        std::replace(line.begin(), line.end(), ',', ' ');
        std::istringstream in(line);
        values.push_back(
            std::vector<double>(std::istream_iterator<double>(in),
                                std::istream_iterator<double>()));
    }

   for (size_t i=0; i<values.size(); i++){
     x.push_back(values[i][0]);
     y.push_back(values[i][1]);
   }
}

App::App(boost::shared_ptr<lcm::LCM> &lcm_, const CommandLineConfig& cl_cfg_, int indTransfFile) :
       lcm_(lcm_), cl_cfg_(cl_cfg_){
  if ((homedir = getenv("HOME")) == NULL) {
    homedir = getpwuid(getuid())->pw_dir;
  }

  LidarOdomConfig lo_cfg = LidarOdomConfig();

  // Overwrite default config values with more brute force search:
  lo_cfg.matchingMode = FRSM_GRID_COORD;
  lo_cfg.initialSearchRangeXY = 1.7;
  lo_cfg.initialSearchRangeTheta = 1.7;
  lo_cfg.maxSearchRangeXY = 2.0;
  lo_cfg.maxSearchRangeTheta = 2.0;

  string fileinitname;
  fileinitname.clear();
  fileinitname.append(homedir);
  fileinitname.append("/logs/multisenselog__2015-11-16/initialization/initTransf.txt");

  initialT = readLineFromFile(fileinitname, indTransfFile);

  float tmpx, tmpy, tmptheta;
  parseTransf(initialT, tmpx, tmpy, tmptheta);
  initInput.x = tmpx;
  initInput.y = tmpy;
  initInput.theta = (tmptheta * M_PI)/180;

  cout << "initInput: " << tmpx << "," << tmpy << "," << tmptheta << endl;

  initReference.x = 0;
  initReference.y = 0;
  initReference.theta = (0 * M_PI)/180;

  num_clouds = 10;

  lidarOdom_ = new LidarOdom(lcm_, lo_cfg);
}

void App::doWork(Eigen::MatrixXf &transf_matrix, int transf_index){
  std::vector<float> xA;
  std::vector<float> yA;
  readCSVFile(cloudA, xA, yA);
  std::cout << xA.size() << " points in File A\n";
  lidarOdom_->doOdometry(xA, yA, xA.size(), 0, &initReference);

  std::vector<float> xB;
  std::vector<float> yB;
  readCSVFile(cloudB, xB, yB);
  std::cout << xB.size() << " points in File B\n";
  lidarOdom_->doOdometry(xB, yB, xB.size(), 1, &initInput);

  // 2. Determine the body position using the LIDAR motion estimate:
  /*
  Eigen::Isometry3d tf_01_fixed = lidarOdom_->getCurrentPose();
  Eigen::Isometry3d head_to_fixscan, fixscan_to_head;
  head_to_fixscan = getXYThetaAsIsometry3d(transl_head_to_fixed, rpy_head_to_fixed);
  fixscan_to_head = head_to_fixscan.inverse();
  Eigen::Isometry3d tf_01_head = fixscan_to_head * tf_01_fixed * head_to_fixscan;
  */
  Eigen::Isometry3d tf_01_head = lidarOdom_->getCurrentPose();

  Eigen::Quaterniond orientation(tf_01_head.rotation());
  Eigen::Vector3d rpy = orientation.matrix().eulerAngles(0,1,2);

  float xres,yres,thetares;
  xres = tf_01_head.translation().x();
  yres = tf_01_head.translation().y();
  thetares = rpy[2] * 180 / M_PI;

  transf_matrix(0, transf_index-1) = xres;
  transf_matrix(1, transf_index-1) = yres;
  transf_matrix(2, transf_index-1) = thetares;

  cout << "x,y,yaw (m,m,deg): "<< xres << ", " << yres << ", " << thetares << "\n";
}

int main(int argc, char **argv){
  boost::shared_ptr<lcm::LCM> lcm(new lcm::LCM);
  if(!lcm->good()){
    std::cerr <<"ERROR: lcm is not good()" <<std::endl;
  }

  CommandLineConfig cl_cfg;
  ConciseArgs parser(argc, argv, "simple-fusion");
  parser.add(cl_cfg.filenameA, "a", "filenameA", "FilenameA.csv");
  parser.add(cl_cfg.filenameB, "b", "filenameB", "FilenameB.csv");
  parser.parse();

  App* app = new App(lcm, cl_cfg, 0);

  if (cl_cfg.filenameA.empty())
  {
    int transf_index = 0;
    //-------------------------
    // To store...
    int cols = 0;
    for (int i = app->num_clouds-1; i > 0; i--)
    {
      cols = cols + i;
    }
    Eigen::MatrixXf truth_res = Eigen::MatrixXf::Zero(3, cols);
    //-------------------------
    for (int i = 0; i < app->num_clouds-1; i++)
    {
      for (int j = 1+i; j < app->num_clouds; j++)
      {
        // Set index to load inital transformations from file
        transf_index++;

        // Name reference cloud from file
        app->cloudA.clear();
        app->cloudA.append(app->homedir);
        app->cloudA.append("/logs/multisenselog__2015-11-16/planar_scans/scan_0");
        app->cloudA.append(to_string(i));
        app->cloudA.append(".csv");

        // Name input cloud from file
        app->cloudB.clear();
        app->cloudB.append(app->homedir);
        app->cloudB.append("/logs/multisenselog__2015-11-16/planar_scans/scan_0");
        app->cloudB.append(to_string(j));
        app->cloudB.append(".csv");

        app->doWork(truth_res, transf_index);

        // Uncomment for visualization of matching results one by one:
        //cout << "Press ENTER to continue..." << endl;
        //cin.get();

        app->~App();
        app = new App(lcm, cl_cfg, transf_index);
      }
    }
    cout << "Final matrix:" << endl << truth_res.block(0,0,3,6) << endl;

    string out_file;
    out_file.append(app->homedir);
    out_file.append("/logs/multisenselog__2015-11-16/results/truth_transf.txt");
    writeTransformToFile(truth_res, out_file, app->num_clouds);
  }
  else
  {
    app->~App();
    app = new App(lcm, cl_cfg, 0);

    app->cloudA = cl_cfg.filenameA;
    app->cloudB = cl_cfg.filenameB;

    Eigen::MatrixXf truth_res = Eigen::MatrixXf::Zero(3, 1);
    app->doWork(truth_res, 0);
  }
}

max_stars_repo_name: liangfok/oh-distro

max_issues_repo_name: liangfok/oh-distro

max_forks_repo_name: liangfok/oh-distro


text:
/***************************************************************************
 * Copyright 1998-2020 by authors (see AUTHORS.txt)                        *
 *                                                                         *
 *   This file is part of LuxCoreRender.                                   *
 *                                                                         *
 * Licensed under the Apache License, Version 2.0 (the "License");         *
 * you may not use this file except in compliance with the License.        *
 * You may obtain a copy of the License at                                 *
 *                                                                         *
 *     http://www.apache.org/licenses/LICENSE-2.0                          *
 *                                                                         *
 * Unless required by applicable law or agreed to in writing, software     *
 * distributed under the License is distributed on an "AS IS" BASIS,       *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*
 * See the License for the specific language governing permissions and     *
 * limitations under the License.                                          *
 ***************************************************************************/

#include <boost/format.hpp>
#include <boost/filesystem.hpp>

#include "slg/scene/scene.h"
#include "slg/imagemap/imagemapcache.h"
#include "slg/utils/filenameresolver.h"

using namespace std;
using namespace luxrays;
using namespace slg;

//------------------------------------------------------------------------------
// ImageMapResizeMipMapMemPolicy::ApplyResizePolicy()
//------------------------------------------------------------------------------

ImageMap *ImageMapResizeMipMapMemPolicy::ApplyResizePolicy(const std::string &fileName,
		const ImageMapConfig &imgCfg, bool &toApply) const {
	const string srcFileName = SLG_FileNameResolver.ResolveFile(fileName);

	// Check/Create and TX file for each image map
	const string dstFileName = srcFileName + ".tx";

	// Check if the TX file exist and it is up to date
	if (!boost::filesystem::exists(dstFileName) ||
			(boost::filesystem::last_write_time(srcFileName) > boost::filesystem::last_write_time(dstFileName))) {
		SDL_LOG("Creating TX image for file:  " << srcFileName);

		ImageMap::MakeTx(srcFileName, dstFileName);
	}

	pair<u_int, u_int> size = ImageMap::GetSize(dstFileName);
	const u_int width = size.first;
	const u_int height = size.second;

	if (Max(width, height) > minSize) {
		u_int newWidth, newHeight;
		if (width >= height) {
			newWidth = minSize;
			newHeight = Max<u_int>(minSize * (width / (float)height), 1u);
		} else {
			newWidth = Max<u_int>(minSize * (height / (float)width), 1u);
			newHeight = minSize;
		}

		SDL_LOG("Probe ImageMap: " << dstFileName << " [from " << width << "x" << height <<
				" to " << newWidth << "x" << newHeight <<"]");

		ImageMap *im = new ImageMap(dstFileName, imgCfg, newWidth, newHeight);
		// Set the ImageMap with  the original name
		im->SetName(fileName);

		im->SetUpInstrumentation(width, height, imgCfg);

		toApply = true;

		return im;
	} else {
		ImageMap *im = new ImageMap(fileName, imgCfg);

		toApply = false;

		return im;
	}
}

//------------------------------------------------------------------------------
// ImageMapResizeMinMemPolicy::Preprocess()
//------------------------------------------------------------------------------

void ImageMapResizeMipMapMemPolicy::Preprocess(ImageMapCache &imc, const Scene *scene,
		const bool useRTMode) const {
	if (useRTMode)
		return;

	SDL_LOG("Applying resize policy " << ImageMapResizePolicyType2String(GetType()) << "...");

	const double startTime = WallClockTime();

	// Build the list of image maps to check
	vector<u_int> imgMapsIndices;
	for (u_int i = 0; i < imc.resizePolicyToApply.size(); ++i) {
		if (imc.resizePolicyToApply[i]) {
			//SDL_LOG("Image map to process:  " << imc.maps[i]->GetName());
			imgMapsIndices.push_back(i);
		}
	}

	SDL_LOG("Image maps to process:  " << imgMapsIndices.size());

	// Enable instrumentation for image maps to check
	for (auto i : imgMapsIndices)
		imc.maps[i]->EnableInstrumentation();

	// Do a test render to establish the optimal image maps sizes
	CalcOptimalImageMapSizes(imc, scene, imgMapsIndices);

	size_t originalMemUsed = 0;
	size_t currentMemUsed = 0;
	for (auto i : imgMapsIndices) {
		const u_int originalWidth = imc.maps[i]->instrumentationInfo->originalWidth;
		const u_int originalHeigth = imc.maps[i]->instrumentationInfo->originalHeigth;

		u_int optimalWidth = scale * imc.maps[i]->instrumentationInfo->optimalWidth;
		u_int optimalHeigth = scale * imc.maps[i]->instrumentationInfo->optimalHeigth;

		u_int newWidth, newHeight;
		if ((optimalWidth == 0) || (optimalHeigth == 0)) {
			newWidth = imc.maps[i]->GetWidth();
			newHeight = imc.maps[i]->GetHeight();
		} else {
			if (originalWidth >= originalHeigth) {
				const float scaleFactor = optimalWidth / (float)originalWidth;

				if (scaleFactor > 1.f) {
					SDL_LOG("WARNING: image maps \"" << imc.maps[i]->GetName() << "\" too small !");
					newWidth = originalWidth;
					newHeight = originalHeigth;
				} else {
					newWidth = Max<u_int>(optimalWidth, minSize);
					newHeight = Max<u_int>(originalHeigth * (newWidth / (float)originalWidth), 1u);
				}
			} else {
				const float scaleFactor = optimalHeigth / (float)originalHeigth;

				if (scaleFactor > 1.f) {
					SDL_LOG("WARNING: image maps \"" << imc.maps[i]->GetName() << "\" too small !");
					newWidth = originalWidth;
					newHeight = originalHeigth;
				} else {
					newHeight = Max<u_int>(optimalHeigth, minSize);
					newWidth = Max<u_int>(originalWidth * (newHeight / (float)originalHeigth), 1u);
				}
			}
		}

		imc.resizePolicyToApply[i] = false;

		originalMemUsed += originalWidth * originalHeigth * imc.maps[i]->GetStorage()->GetMemoryPixelSize();

		// Reload the original image map with the best mip map level
		const string srcFileName = SLG_FileNameResolver.ResolveFile(imc.maps[i]->GetName());
		const string dstFileName = srcFileName + ".tx";
		imc.maps[i]->Reload(dstFileName, newWidth, newHeight);

		currentMemUsed += imc.maps[i]->GetStorage()->GetMemorySize();

		SDL_LOG("Image maps \"" << imc.maps[i]->GetName() << "\" scaled: " <<
				originalWidth << "x" << originalHeigth << " => " <<
				imc.maps[i]->GetWidth() << "x" << imc.maps[i]->GetHeight());
	}

	SDL_LOG("Memory required for original Image maps: " + ToMemString(originalMemUsed));
	SDL_LOG("Memory required for MIPMAPMEM Image maps: " + ToMemString(currentMemUsed));

	// Delete instrumentation for image maps checked
	for (auto i : imgMapsIndices)
		imc.maps[i]->DeleteInstrumentation();

	SDL_LOG("Applying resize policy " << ImageMapResizePolicyType2String(GetType()) << " time: " <<
			(boost::format("%.1f") % (WallClockTime() - startTime)) << "secs");
}

max_stars_repo_name: OmidGhotbi/LuxCore

max_issues_repo_name: OmidGhotbi/LuxCore

max_forks_repo_name: OmidGhotbi/LuxCore


text:
/// \file   test_type_marker.cpp
///
/// \brief
///
/// \authors    Maarten P. Scholl
/// \date       2019-09-17
/// \copyright  Copyright 2017-2019 The Institute for New Economic Thinking,
///             Oxford Martin School, University of Oxford
///
///             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.
///
///             You may obtain instructions to fulfill the attribution
///             requirements in CITATION.cff
///
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE type_marker

#include <boost/test/included/unit_test.hpp>

#include <esl/computation/type_code.hpp>
#include <esl/computation/type_marker.hpp>

BOOST_AUTO_TEST_SUITE(ESL)

    template<typename unique_type_, std::uint64_t type_code_>
    struct force_unique
    : public esl::type_code<type_code_>
    , public esl::type_marker<unique_type_>
    {

        friend esl::type_marker<unique_type_> marker(esl::type_code<type_code_>)
        {
            return esl::type_marker<unique_type_>();
        }
    };

    struct u0
    : public force_unique<u0, 0>
    {

    };

    struct u123
    : public force_unique<u123, 123>
    {

    };

    //struct u123_duplicate
    //: public force_unique<u123_duplicate, 123>    // definition should result
    //{                                             // in compile error
    //
    //};

    struct u123_wrong_mapping
    : public force_unique<u123, 456>
    {

    };

    BOOST_AUTO_TEST_CASE(type_marker_code)
    {
        // we must capture the value here first, as the comparison macro below
        // can not cope with the static const
        auto x0 = u0::code;
        BOOST_CHECK_EQUAL(x0, 0);
        u0 u0_;
        (void)u0_;

        constexpr auto x123 = u123::code;
        BOOST_CHECK_EQUAL( x123, 123);
        u123 u123_;
        (void)u123_;

        //u123_duplicate u123d_; // should not compile when instantiated
        //(void) u123d_;
        u123_wrong_mapping u123wm_; // should still compile
        (void)u123wm_;

    }

BOOST_AUTO_TEST_SUITE_END()  // ESL

max_stars_repo_name: rht/ESL


text:
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif // _WIN32
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <iostream>
#include <memory>
#include <string>
#include <ctime>

using namespace std;
namespace asio = boost::asio;

class Session : public std::enable_shared_from_this<Session>
{
public:
    explicit Session(asio::io_context& io_context, asio::ip::tcp::socket socket)
        : socket_(std::move(socket)),
        strand_(io_context.get_executor())
    {
    }

    void go()
    {
        auto self(shared_from_this());
        asio::spawn(strand_,
            [this, self](asio::yield_context yield)
        {
            try
            {
                auto re = socket_.remote_endpoint();
                cout << "address: " << re.address() << endl;
                cout << "port: " << re.port() << endl;

                time_t rawtime;
                time(&rawtime);
                struct tm* timeinfo = localtime(&rawtime);
                string now(asctime(timeinfo));

                socket_.async_write_some(boost::asio::buffer(now), yield);
            }
            catch (std::exception & e)
            {
                socket_.close();
            }
        });
    }

private:
    asio::ip::tcp::socket socket_;
    asio::strand<asio::io_context::executor_type> strand_;
};

int main(int argc, char* argv[])
{
    try
    {
        asio::io_context io_context;

        asio::spawn(io_context,
            [&](asio::yield_context yield)
        {
            asio::ip::tcp::acceptor acceptor(io_context,
                asio::ip::tcp::endpoint(asio::ip::tcp::v4(), 8888));

            while (true)
            {
                boost::system::error_code ec;
                asio::ip::tcp::socket socket(io_context);
                acceptor.async_accept(socket, yield[ec]);
                if (!ec)
                {
                    std::make_shared<Session>(io_context, std::move(socket))->go();
                }
            }
        });

        io_context.run();
    }
    catch (exception & e)
    {
        cerr << "Exception: " << e.what() << endl;
    }

    return 0;
}

max_stars_repo_name: gwankyun/AsioExample

max_issues_repo_name: gwankyun/AsioExample

max_forks_repo_name: gwankyun/AsioExample


text:
// Copyright 2018 Intel Corporation.

#include "plaidml/base/status.h"

#include <string>

#include <boost/exception_ptr.hpp>

#include "base/util/error.h"
#include "base/util/logging.h"
#include "plaidml/base/status_strings.h"

namespace {

// TODO: T451
#if TARGET_OS_IPHONE == 1
vai_status last_status = VAI_STATUS_OK;
std::string last_status_str;  // NOLINT
#else                         // TARGET_OS_IPHONE == 1
thread_local vai_status last_status = VAI_STATUS_OK;
thread_local std::string last_status_str;
#endif                        // TARGET_OS_IPHONE == 1 ... else

}  // namespace

namespace vertexai {

namespace {
class ErrToCode final : public error::ErrorVisitor {
 public:
  void Visit(const error::Cancelled& err) noexcept final { status_ = VAI_STATUS_CANCELLED; }
  void Visit(const error::Unknown& err) noexcept final { status_ = VAI_STATUS_UNKNOWN; }
  void Visit(const error::InvalidArgument& err) noexcept final { status_ = VAI_STATUS_INVALID_ARGUMENT; }
  void Visit(const error::DeadlineExceeded& err) noexcept final { status_ = VAI_STATUS_DEADLINE_EXCEEDED; }
  void Visit(const error::NotFound& err) noexcept final { status_ = VAI_STATUS_NOT_FOUND; }
  void Visit(const error::AlreadyExists& err) noexcept final { status_ = VAI_STATUS_ALREADY_EXISTS; }
  void Visit(const error::PermissionDenied& err) noexcept final { status_ = VAI_STATUS_PERMISSION_DENIED; }
  void Visit(const error::Unauthenticated& err) noexcept final { status_ = VAI_STATUS_UNAUTHENTICATED; }
  void Visit(const error::ResourceExhausted& err) noexcept final { status_ = VAI_STATUS_RESOURCE_EXHAUSTED; }
  void Visit(const error::FailedPrecondition& err) noexcept final { status_ = VAI_STATUS_FAILED_PRECONDITION; }
  void Visit(const error::Aborted& err) noexcept final { status_ = VAI_STATUS_ABORTED; }
  void Visit(const error::OutOfRange& err) noexcept final { status_ = VAI_STATUS_OUT_OF_RANGE; }
  void Visit(const error::Unimplemented& err) noexcept final { status_ = VAI_STATUS_UNIMPLEMENTED; }
  void Visit(const error::Internal& err) noexcept final { status_ = VAI_STATUS_INTERNAL; }
  void Visit(const error::Unavailable& err) noexcept final { status_ = VAI_STATUS_UNAVAILABLE; }
  void Visit(const error::DataLoss& err) noexcept final { status_ = VAI_STATUS_DATA_LOSS; }

  vai_status status() const noexcept { return status_; }

 private:
  vai_status status_ = VAI_STATUS_OK;
};
}  // namespace

void SetLastStatus(vai_status status, const char* str) noexcept {
  if (!str || !*str) {
    status = VAI_STATUS_INTERNAL;
    str = status_strings::kInternal;
  }
  last_status = status;
  try {
    last_status_str = str;
    if (status != VAI_STATUS_OK) {
      IVLOG(1, "ERROR: " << str);
    }
  } catch (...) {
    // Replacing the status code with VAI_STATUS_RESOURCE_EXHAUSTED lets
    // vai_last_status_str() return a generic out-of-resources message.
    last_status = VAI_STATUS_RESOURCE_EXHAUSTED;
    last_status_str.clear();
  }
}

void SetLastException(std::exception_ptr ep) noexcept {
  try {
    try {
      std::rethrow_exception(ep);
    } catch (const std::exception_ptr& epp) {
      // This shouldn't ever happen, but it's pretty easy to get into this situation; for example,
      // boost::exception_ptr can be constructed to wrap std::exception_ptr (as a generic object, not with
      // knowledge that it's wrapping an exception), so if you resolve a boost::promise<> with
      // std::current_exception(), you'll get a boost::exception_ptr wrapping a std::exception_ptr wrapping
      // the original exception.
      std::rethrow_exception(epp);
    }
  } catch (const error::Error& e) {
    ErrToCode coder;
    e.Accept(&coder);
    SetLastStatus(coder.status(), e.what());
  } catch (const std::bad_alloc&) {
    SetLastStatus(VAI_STATUS_RESOURCE_EXHAUSTED, status_strings::kOom);
  } catch (const std::exception& e) {
    SetLastStatus(VAI_STATUS_UNKNOWN, e.what());
  } catch (...) {
    std::string info = boost::diagnostic_information(boost::current_exception());
    SetLastStatus(VAI_STATUS_UNKNOWN, info.c_str());
  }
}

void SetLastOOM() noexcept { SetLastStatus(VAI_STATUS_RESOURCE_EXHAUSTED, status_strings::kOom); }

}  // namespace vertexai

// vai_status
extern "C" vai_status vai_last_status() { return last_status; }

extern "C" void vai_clear_status() { vertexai::SetLastStatus(VAI_STATUS_OK, vertexai::status_strings::kOk); }

extern "C" const char* vai_last_status_str() {
  if (last_status == VAI_STATUS_RESOURCE_EXHAUSTED && !last_status_str.length()) {
    return vertexai::status_strings::kOom;
  }
  return last_status_str.c_str();
}

ext: cc

max_stars_repo_name: redoclag/plaidml

max_issues_repo_name: HOZHENWAI/plaidml

max_forks_repo_name: HOZHENWAI/plaidml


text:
//          Copyright Oliver Kowalke 2017.
// 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)

#ifndef MEMORIA_FIBERS_NUMA_PIN_THREAD_H
#define MEMORIA_FIBERS_NUMA_PIN_THREAD_H

#include <cstdint>
#include <thread>

#include <boost/config.hpp>

#include <memoria/fiber/detail/config.hpp>

namespace memoria {
namespace fibers {
namespace numa {

MEMORIA_FIBERS_DECL
void pin_thread( std::uint32_t, std::thread::native_handle_type);

MEMORIA_FIBERS_DECL
void pin_thread( std::uint32_t cpuid);

}}}

#endif // MEMORIA_FIBERS_NUMA_PIN_THREAD_H

ext: hpp

max_stars_repo_name: victor-smirnov/memoria

max_issues_repo_name: victor-smirnov/memoria

max_forks_repo_name: victor-smirnov/memoria


text:
////////////////////////////////////////////////////////////////////////////////////////////////////
// env.hpp
// Contains definition of the \c env<> class and the \c make_env() helpers
//
//  Copyright 2012 Eric Niebler. 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)

#ifndef BOOST_PROTO_V5_ENV_HPP_INCLUDED
#define BOOST_PROTO_V5_ENV_HPP_INCLUDED

#include <type_traits>
#include <boost/proto/v5/proto_fwd.hpp>
#include <boost/proto/v5/utility.hpp>

namespace boost
{
    namespace proto
    {
        inline namespace v5
        {
            namespace envs
            {
                ////////////////////////////////////////////////////////////////////////////////////
                // key_not_found
                struct key_not_found
                {};

                ////////////////////////////////////////////////////////////////////////////////////
                // empty_env
                struct empty_env
                {
                    constexpr key_not_found operator()(utility::any) const noexcept
                    {
                        return key_not_found();
                    }

                    template<typename T, typename V>
                    friend constexpr env<T, V> operator,(empty_env, env<T, V> head)
                    {
                        return head;
                    }
                };

                ////////////////////////////////////////////////////////////////////////////////////
                // env
                // An env is a slot-based storage mechanism, accessible by Key.
                template<typename Key, typename Value, typename Env /*= empty_env*/>
                struct env
                  : private Env
                {
                    BOOST_PROTO_REGULAR_TRIVIAL_CLASS(env);

                    Value value_;

                    template<typename V, typename B = Env
                      , BOOST_PROTO_ENABLE_IF(!(utility::is_base_of<env, V>::value))>
                    constexpr explicit env(V && v, B && b = B())
                      : Env(static_cast<B &&>(b))
                      , value_(v) // copy, not move
                    {}

                    using Env::operator();

                    constexpr auto operator()(Key) const
                    BOOST_PROTO_AUTO_RETURN(
                        Value(this->value_)
                    )

                    template<typename V>
                    auto operator()(Key, V &&v)
                    BOOST_PROTO_AUTO_RETURN(
                        this->value_ = v // copy, don't move
                    )

                    template<typename T, typename V>
                    friend constexpr env<T, V, env> operator,(env tail, env<T, V> head)
                    {
                        return env<T, V, env>(
                            static_cast<env<T, V> &&>(head).value_
                          , static_cast<env &&>(tail)
                        );
                    }
                };

                template<typename Key, typename Value>
                constexpr auto make_env(Key, Value &&value)
                BOOST_PROTO_AUTO_RETURN(
                    env<Key, Value>(static_cast<Value &&>(value))
                )

                template<typename Key, typename Value, typename Env>
                constexpr auto make_env(Key, Value &&value, Env &&en)
                BOOST_PROTO_AUTO_RETURN(
                    env<Key, Value, utility::uncvref<Env>>(static_cast<Value &&>(value), static_cast<Env &&>(en))
                )

                template<typename Key, typename Value>
                using key_value = env<Key, Value>;

                template<typename Key, typename Value>
                constexpr key_value<Key, Value> make_key_value(Key, Value &&value)
                {
                    return key_value<Key, Value>(static_cast<Value &&>(value));
                }
            }

            ////////////////////////////////////////////////////////////////////////////////////////
            // is_env
            template<typename T>
            struct is_env
              : std::is_base_of<empty_env, T>
            {};

            template<typename T>
            struct is_env<T &>
              : std::is_base_of<empty_env, T>
            {};

            template<typename T>
            struct is_env<T &&>
              : std::is_base_of<empty_env, T>
            {};

            namespace detail
            {
                struct make_env_
                {
                    template<typename T>
                    constexpr T operator()(T && t) const
                    {
                        static_assert(is_env<T>::value, "make_env used in non-env context");
                        return t;
                    }

                    template<typename T, typename U, typename ...V, typename Impl = make_env_>
                    constexpr auto operator()(T && t, U && u, V &&... v) const
                    BOOST_PROTO_AUTO_RETURN(
                        BOOST_PROTO_TRY_CALL(Impl())(
                            (static_cast<T &&>(t), static_cast<U &&>(u))
                          , static_cast<V &&>(v)...
                        )
                    )
                };
            }

            ////////////////////////////////////////////////////////////////////////////////////////
            // make_env
            template<typename ...T>
            constexpr auto make_env(T &&... t)
            BOOST_PROTO_AUTO_RETURN(
                detail::make_env_()(static_cast<T &&>(t)...)
            )

            ////////////////////////////////////////////////////////////////////////////////////
            // env_tag
            template<typename Tag>
            struct env_tag
            {
                BOOST_PROTO_REGULAR_TRIVIAL_CLASS(env_tag);

                // So that tag objects of type (derived from) env_tag can be used
                // to create basic_action environments like (data=x, myvar=y),
                // where "data" and "myvar" are tags.
                template<
                    typename V
                  , BOOST_PROTO_ENABLE_IF(!(utility::is_base_of<env_tag, V>::value))
                >
                constexpr env<Tag, V> operator=(V && v) const
                {
                    return env<Tag, V>(static_cast<V &&>(v));
                }
            };

            /// Tag type for the data parameter in the basic_action environment
            struct data_tag
              : env_tag<data_tag>
            {
                using env_tag<data_tag>::operator=;
            };

            namespace
            {
                ////////////////////////////////////////////////////////////////////////////////
                // data
                constexpr auto const & data = utility::static_const<data_tag>::value;
            }

            BOOST_PROTO_IGNORE_UNUSED(data);
        }
    }
}

#endif


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

// Server to run and update pipelines

#include <csignal>
#include <cstdio>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

#include <boost/program_options.hpp>
#include <json/src/json.hpp>
#include <zmq.hpp>

#include "camera/camera_manager.h"
#include "common/context.h"
#include "model/model_manager.h"
#include "processor/image_transformer.h"
#include "processor/neural_net_evaluator.h"
#include "processor/throttler.h"

namespace po = boost::program_options;

std::shared_ptr<Camera> camera;
std::shared_ptr<ImageTransformer> transformer;

void SignalHandler(int) {
  std::cout << "Received SIGINT, stopping" << std::endl;
  if (transformer != nullptr) transformer->Stop();
  if (camera != nullptr) camera->Stop();

  exit(0);
}

void Run(const std::string& camera_name, const std::string& net_name,
         const std::string& models_dir) {
  CameraManager& camera_manager = CameraManager::GetInstance();
  ModelManager& model_manager = ModelManager::GetInstance();

  CHECK(camera_manager.HasCamera(camera_name))
      << "Camera " << camera_name << " does not exist";

  camera = camera_manager.GetCamera(camera_name);
  camera->SetBlockOnPush(true);

  // Start ZMQ server
  zmq::context_t context(1);
  zmq::socket_t socket(context, ZMQ_REP);
  socket.bind("tcp://*:5555");

  // Transformer
  Shape input_shape(3, 299, 299);
  transformer = std::make_shared<ImageTransformer>(input_shape, true);
  transformer->SetSource("input", camera->GetSink("output"));
  transformer->SetBlockOnPush(true);

  std::vector<std::shared_ptr<NeuralNetEvaluator>> nnes;
  std::vector<std::shared_ptr<NeuralNetEvaluator>> task_nnes;
  std::vector<std::shared_ptr<Throttler>> throttlers;

  // Run
  camera->Start();
  transformer->Start();

  while (true) {
    // Check for zmq message
    zmq::message_t request;
    int rc = socket.recv(&request, ZMQ_NOBLOCK);
    if (rc == 0 && zmq_errno() == EAGAIN) {
      // No ZMQ message
      continue;
    }

    //  Handle message
    std::cout << "ZMQ client message received" << std::endl;
    std::string msg =
        std::string(static_cast<char*>(request.data()), request.size());
    auto msg_json = nlohmann::json::parse(msg);

    // Stop old NNEs
    for (const auto& nne : nnes) {
      if (nne->IsStarted()) nne->Stop();
    }
    for (const auto& throttler : throttlers) {
      if (throttler->IsStarted()) throttler->Stop();
    }
    nnes.clear();
    task_nnes.clear();
    throttlers.clear();

    // Start new NNEs
    std::unordered_map<int, std::shared_ptr<NeuralNetEvaluator>> sink_by_id;
    for (const auto& nne_desc : msg_json) {
      int nne_id = nne_desc["net_id"];

      std::string input_layer = nne_desc["input_layer"];
      std::string output_layer = nne_desc["output_layer"];
      std::vector<std::string> output_layers;
      output_layers.push_back(output_layer);
      int parent_processor_id = nne_desc["parent_id"];
      StreamPtr sink;
      if (parent_processor_id == -1) {
        sink = transformer->GetSink("output");
      } else {
        auto it = sink_by_id.find(parent_processor_id);
        if (it == sink_by_id.end()) {
          LOG(ERROR) << "Could not find sink for nne " << parent_processor_id;
        }
        sink = it->second->GetSink(input_layer);
      }

      Shape input_shape(nne_desc["channels"], nne_desc["width"],
                        nne_desc["height"]);

      // Update model path
      std::string model_path = nne_desc["model_path"];
      std::string full_model_path = models_dir + "/" + model_path;
      auto base_model_desc = model_manager.GetModelDesc(net_name);
      auto new_model_desc = ModelDesc(
          base_model_desc.GetName(), base_model_desc.GetModelType(),
          full_model_path, base_model_desc.GetModelParamsPath(),
          base_model_desc.GetInputWidth(), base_model_desc.GetInputHeight(),
          base_model_desc.GetDefaultInputLayer(),
          base_model_desc.GetDefaultOutputLayer());

      // Make throttler and set source
      int target_fps = nne_desc["target_fps"];
      std::shared_ptr<Throttler> throttler =
          std::make_shared<Throttler>(target_fps);
      throttler->SetSource("input", sink);

      // Make nne and set source
      // TODO batch size
      std::shared_ptr<NeuralNetEvaluator> nn =
          std::make_shared<NeuralNetEvaluator>(new_model_desc, input_shape, 1,
                                               output_layers);
      nn->SetSource("input", throttler->GetSink("output"), input_layer);
      nn->SetBlockOnPush(true);

      // Add to sinks by name and nnes list
      nnes.push_back(nn);
      sink_by_id.insert({nne_id, nn});
      throttlers.push_back(throttler);

      bool shared = nne_desc["shared"].get<bool>();
      if (!shared) {
        task_nnes.push_back(nn);
      }
    }

    sink_by_id.clear();

    for (const auto& throttler : throttlers) throttler->Start();
    for (const auto& nne : nnes) nne->Start();

    LOG(INFO) << "Pipeline started.\n";

    // Log metric in 30 seconds
    double last_ms = Context::GetContext().GetTimer().ElapsedMSec();
    double now = Context::GetContext().GetTimer().ElapsedMSec();
    while (now - last_ms < 30000) {
      now = Context::GetContext().GetTimer().ElapsedMSec();
    }
    std::string message;
    std::string comma = "";
    for (const auto& throttler : throttlers) {
      double throughput = throttler->GetHistoricalProcessFps();
      std::cout << "Throttler: " << throughput << std::endl;
    }
    for (const auto& nne : nnes) {
      std::cout << "Processor: " << nne->GetHistoricalProcessFps() << std::endl;
    }
    for (const auto& task_nne : task_nnes) {
      double throughput = task_nne->GetHistoricalProcessFps();
      message += comma + std::to_string(throughput);
      comma = ",";
    }
    std::cout << message << std::endl;

    // Respond to client
    zmq::message_t reply(message.size());
    memcpy(reply.data(), message.c_str(), message.size());
    socket.send(reply);
  }

  return;
}

int main(int argc, char* argv[]) {
  gst_init(&argc, &argv);
  google::InitGoogleLogging(argv[0]);
  FLAGS_alsologtostderr = 1;
  FLAGS_colorlogtostderr = 1;

  po::options_description desc("Simple camera display test");
  desc.add_options()("help,h", "print the help message");
  desc.add_options()("camera",
                     po::value<std::string>()->value_name("CAMERA")->required(),
                     "The name of the camera to use");
  desc.add_options()(
      "config_dir,C",
      po::value<std::string>()->value_name("CONFIG_DIR")->required(),
      "The directory to find streamer's configurations");
  desc.add_options()("net,n",
                     po::value<std::string>()->value_name("NET")->required(),
                     "The name of the neural net to run");
  desc.add_options()("models,m",
                     po::value<std::string>()->value_name("MODELS")->required(),
                     "The name of the neural net to run");

  std::signal(SIGINT, SignalHandler);

  po::variables_map args;
  try {
    po::store(po::parse_command_line(argc, argv, desc), args);
    po::notify(args);
  } catch (const po::error& e) {
    std::cerr << e.what() << std::endl;
    std::cout << desc << std::endl;
    return 1;
  }

  if (args.count("help")) {
    std::cout << desc << std::endl;
    return 1;
  }

  // Parse arguments
  if (args.count("config_dir")) {
    Context::GetContext().SetConfigDir(args["config_dir"].as<std::string>());
  }

  // Init streamer context, this must be called before using streamer.
  Context::GetContext().Init();

  auto camera_name = args["camera"].as<std::string>();
  auto net_name = args["net"].as<std::string>();
  auto models_dir = args["models"].as<std::string>();

  // Run mainstream server
  Run(camera_name, net_name, models_dir);

  return 0;
}

max_stars_repo_name: GTkernel/streamer

max_issues_repo_name: GTkernel/streamer

max_forks_repo_name: GTkernel/streamer


text:
/**
 * @file
 * @author Denise Ratasich
 * @date 14.10.2013
 *
 * @brief Implement sampling from specific distributions.
 *
 * Currently only Gaussian distributions can be choosen for process
 * and covariance noise. Hence only sampling from a Gaussian
 * distribution is implemented.
 */

#include "probability/sampling.h"

#include <stdexcept>
#include <cmath>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/uniform_real.hpp>

namespace probability
{
  // create the rng only once (otherwise the numbers generated are
  // always the same)
  static boost::mt19937 rng(time(NULL));

  VectorXd sampleNormalDistribution(VectorXd mean, MatrixXd covariance)
  {
    // save last covariance (so Cholesky decomposition needs not to
    // be done every time entering this function)
    static MatrixXd cov;
    static MatrixXd T;			// transform matrix when cov is not diagonal

    // additionally needed for rng
    boost::normal_distribution<> dist(0,1);	// mean = 0, standard deviation = 1
    boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > random(rng, dist);

    VectorXd sample = VectorXd::Zero(mean.size());

    // check if covariance is diagonal (set diagonal to zero and
    // compare with MatrixXD::Zero)
    if (covariance.isDiagonal())
    {
      // sample each variable independently
      for (int i = 0; i < sample.size(); i++)
	sample[i] = mean[i] + random() * sqrt(covariance(i,i));
    }
    else
    {
      // x ~ N(0,C^-1)
      // C = L*L^T
      // sample x = L^-T * z, where z ~ N(0,I)

      // same covariance as before? (do calculation of L only once!)
      if (cov.rows() != covariance.rows() ||
	  cov.cols() != covariance.cols() ||
	  cov != covariance)
	{
	  cov = covariance;

	  // Cholesky decomposition of inverse covariance to get L;
	  // the covariance must be a symmetric and positive definite
	  // matrix (a covariance has these features, but the user
	  // must configure it so)
	  covariance = covariance.inverse();
	  MatrixXd L = covariance.llt().matrixL();

	  MatrixXd checkLL = L*L.transpose();
	  for (int r = 0; r < covariance.rows(); r++)
	    for (int c = 0; c < covariance.cols(); c++)
	      if (checkLL(r,c) - covariance(r,c)  >  0.000001)
		throw std::runtime_error("sampleNormalDistribution: Cholesky decomposition failed.");

	  T = L.inverse().transpose();
	}

      // sample z: z_i ~ N(0,1)
      for (int i = 0; i < sample.size(); i++)
	sample[i] = random();

      // transform to x (and add mean!)
      sample = mean + T * sample;
    }

    return sample;
  }

  VectorXd sampleUniformDistribution(VectorXd a, VectorXd b)
  {
    VectorXd sample = VectorXd::Zero(a.size());

    if (a.size() != b.size())
      throw std::runtime_error("sampleUniformDistribution: Bounds a and b must have equal sizes.");

    // additionally needed for rng
    boost::uniform_real<> dist(0,1);
    boost::variate_generator<boost::mt19937&, boost::uniform_real<> >
      random(rng, dist);

    // sample each variable independently
    for (int i = 0; i < sample.size(); i++)
      sample[i] = a[i] + random() * (b[i]-a[i]);

    return sample;
  }
}

max_stars_repo_name: tuw-cpsg/sf-pkg

max_issues_repo_name: ros-agriculture/sf-pkg

max_forks_repo_name: tuw-cpsg/sf-pkg


text:
// Copyright 2021 Anthony Paul Astolfi
//
#include <batteries/async/task.hpp>
//
#include <batteries/async/task.hpp>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <batteries/async/io_result.hpp>
#include <batteries/buffer.hpp>

#ifdef BATT_GLOG_AVAILABLE
#include <glog/logging.h>
#endif  // BATT_GLOG_AVAILABLE

#include <batteries/assert.hpp>
#include <batteries/segv.hpp>

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wswitch-enum"
#endif

#include <boost/asio/ip/tcp.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/exception_ptr.hpp>

#ifdef __clang__
#pragma clang diagnostic pop
#endif

#include <thread>

namespace {

using batt::ConstBuffer;
using batt::ErrorCode;
using batt::IOResult;
using batt::MutableBuffer;
using batt::Task;

namespace ip = boost::asio::ip;
using ip::tcp;

template <typename AsyncStream>
ErrorCode write_all(ConstBuffer data, AsyncStream& stream)
{
    ErrorCode ec;

    while (data.size() > 0) {
        auto result = Task::await<IOResult<std::size_t>>([&](auto&& handler) {
            stream.async_write_some(data, BATT_FORWARD(handler));
        });
        if (!result.ok()) {
            ec = result.error();
            break;
        }
        data += *result;
    }

    return ec;
}

template <typename AsyncStream>
IOResult<std::size_t> read_all(MutableBuffer buffer, AsyncStream& stream)
{
    ErrorCode ec;
    std::size_t bytes_read = 0;

    while (buffer.size() > 0) {
        auto result = Task::await<IOResult<std::size_t>>([&](auto&& handler) {
            stream.async_read_some(buffer, BATT_FORWARD(handler));
        });
        if (!result.ok()) {
            ec = result.error();
            break;
        }
        buffer += *result;
        bytes_read += *result;
    }

    return IOResult<std::size_t>{ec, bytes_read};
}

TEST(TaskTest, ClientServerAsio)
{
    constexpr std::size_t kNumIterations = 15;
    constexpr std::size_t kNumInstances = 200;
    constexpr std::size_t kNumThreads = 16;

    boost::asio::io_context io;

    for (std::size_t j = 0; j < kNumIterations; ++j) {
        std::atomic<std::size_t> client_connected = 0;
        std::atomic<std::size_t> client_sent_data = 0;
        std::atomic<std::size_t> server_accepted = 0;
        std::atomic<std::size_t> server_read_data = 0;

        std::vector<std::unique_ptr<Task>> instances;

        for (std::size_t i = 0; i < kNumInstances; ++i) {
            instances.emplace_back(std::make_unique<Task>(io.get_executor(), [&] {
                BATT_CHECK_NOT_NULLPTR(&Task::current());
                Task* self = &Task::current();

                tcp::acceptor listening{io, tcp::v4()};
                listening.set_option(tcp::no_delay{true});

                ErrorCode bind_ec;
                for (int retry = 0; retry < 100; ++retry) {
                    listening.bind(tcp::endpoint{ip::address_v4::loopback(), 0}, bind_ec);
                    if (!bind_ec) {
                        break;
                    }
                    std::cerr << "+" << std::flush;
                    Task::sleep(boost::posix_time::milliseconds(2500));
                }
                BATT_CHECK(!bind_ec) << bind_ec.message();

                ErrorCode listen_ec;
                listening.listen(boost::asio::socket_base::max_listen_connections, listen_ec);

                const std::string test_data = "testing 1, 2, 3...";

                BATT_CHECK_EQ((void*)self, (void*)&Task::current());

                Task client{io.get_executor(), [&] {
                                tcp::socket stream{io, tcp::v4()};
                                stream.set_option(tcp::no_delay{true});

                                ErrorCode connect_ec;

                                for (int retry = 0; retry < 100; ++retry) {
                                    if (!connect_ec) {
                                        connect_ec = Task::await<ErrorCode>([&](auto&& handler) {
                                            stream.async_connect(listening.local_endpoint(),
                                                                 BATT_FORWARD(handler));
                                        });
                                    }
                                    if (!connect_ec) {
                                        break;
                                    }
                                    std::cerr << "." << std::flush;
                                    stream.close(connect_ec);
                                    Task::sleep(boost::posix_time::milliseconds(2500));

                                    stream.open(tcp::v4(), connect_ec);
                                    if (!connect_ec) {
                                        stream.set_option(tcp::no_delay{true});
                                    }
                                }
                                BATT_CHECK(!connect_ec) << connect_ec.message();

                                ++client_connected;

                                auto write_ec = write_all(boost::asio::buffer(test_data), stream);

                                BATT_CHECK(!write_ec) << write_ec.message();

                                ++client_sent_data;
                            }};

                BATT_CHECK_EQ((void*)self, (void*)&Task::current());

                Task server{io.get_executor(), [&] {
                                auto accept_result = Task::await<IOResult<tcp::socket>>([&](auto&& handler) {
                                    listening.async_accept(BATT_FORWARD(handler));
                                });

                                BATT_CHECK(accept_result.ok()) << accept_result.error().message();

                                tcp::socket& stream = *accept_result;
                                stream.set_option(tcp::no_delay{true});

                                ++server_accepted;

                                std::vector<char> buffer(test_data.length());

                                auto result = read_all(boost::asio::buffer(buffer), stream);

                                BATT_CHECK(result.ok()) << result.error().message();
                                BATT_CHECK_EQ(std::string_view(buffer.data(), buffer.size()), test_data);

                                ++server_read_data;
                            }};

                BATT_CHECK_EQ((void*)self, (void*)&Task::current());

                client.join();

                BATT_CHECK_EQ((void*)self, (void*)&Task::current());

                server.join();

                BATT_CHECK_EQ((void*)self, (void*)&Task::current());
            }));
        }

        io.restart();
        std::vector<std::thread> threads;
        for (std::size_t i = 0; i < kNumThreads; ++i) {
            threads.emplace_back([&io] {
                try {
                    io.run();
                } catch (...) {
#ifdef BATT_GLOG_AVAILABLE
                    LOG(ERROR)
#else
                    std::cerr
#endif
                        << "unhandled exception: "
                        << boost::diagnostic_information(boost::current_exception());
                }
            });
        }
        for (std::thread& t : threads) {
            t.join();
        }

        EXPECT_EQ(client_connected, kNumInstances);
        EXPECT_EQ(server_accepted, kNumInstances);
        EXPECT_EQ(client_sent_data, kNumInstances);
        EXPECT_EQ(server_read_data, kNumInstances);
    }
}

}  // namespace

max_stars_repo_name: tonyastolfi/batteries

max_issues_repo_name: mihir-thakkar/batteries

max_forks_repo_name: mihir-thakkar/batteries