<commit_before>#ifndef VEXCL_PROFILER_HPP
#define VEXCL_PROFILER_HPP

/*
The MIT License

Copyright (c) 2012-2013 <NAME> <<EMAIL>>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**
 * \file   profiler.hpp
 * \author <NAME> <<EMAIL>>
 * \brief  Simple OpenCL/Host profiler.
 */

#ifdef WIN32
#  pragma warning(push)
#  pragma warning(disable : 4267 4290)
#  define NOMINMAX
#endif

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <memory>
#include <stack>
#include <vector>
#include <cassert>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/global_fun.hpp>

#ifndef __CL_ENABLE_EXCEPTIONS
#  define __CL_ENABLE_EXCEPTIONS
#endif

#include <CL/cl.hpp>

#ifdef WIN32
#  include <sys/timeb.h>
#else
#  include <chrono>
#endif

namespace vex {

/// Class for gathering and printing OpenCL and Host profiling info.
class profiler {
    private:
        class profile_unit {
            public:
                profile_unit(std::string name) : length(0), hit(0), name(name) {}
                virtual ~profile_unit() {}
                virtual void tic() = 0;
                virtual double toc() = 0;

                double length;
                size_t hit;
                std::string name;

                static std::string _name(const std::shared_ptr<profile_unit> &u) {
                    return u->name;
                }
                boost::multi_index_container<
                    std::shared_ptr<profile_unit>,
                    boost::multi_index::indexed_by<
                        boost::multi_index::sequenced<>,
                        boost::multi_index::ordered_unique<boost::multi_index::global_fun<
                                const std::shared_ptr<profile_unit> &, std::string, _name>>
                    >
                > children;

                double children_time() const {
                    double tm = 0;

                    for(auto c = children.begin(); c != children.end(); c++)
                        tm += (*c)->length;

                    return tm;
                }

                uint max_line_width(uint level) const {
                    uint w = name.size() + level;

                    for(auto c = children.begin(); c != children.end(); c++)
                        w = std::max(w, (*c)->max_line_width(level + shift_width));

                    return w;
                }

                void print(std::ostream &out,
                        uint level, double total, uint width) const
                {
                    using namespace std;
                    out << "[" << setw(level) << "";
                    print_line(out, name, length, 100 * length / total, width - level);

                    if (!children.empty()) {
                        double sec = length - children_time();
                        double perc = 100 * sec / total;

                        if (perc > 1e-1) {
                            out << "[" << setw(level + 1) << "";
                            print_line(out, "self", sec, perc, width - level - 1);
                        }
                    }

                    for(auto c = children.begin(); c != children.end(); c++)
                        (*c)->print(out, level + shift_width, total, width);
                }

                void print_line(std::ostream &out, const std::string &name,
                        double time, double perc, uint width) const
                {
                    using namespace std;
                    out << name << ":";
                    out << setw(width - name.size()) << "";
                    out << setiosflags(ios::fixed);
                    out << setw(10) << setprecision(3) << time << " sec.";
                    out << "] (" << setprecision(2) << setw(6) << perc << "%)";
                    if(hit > 1) out << " (" << hit << "x)";
                    out << endl;
                }
            private:
                static const uint shift_width = 2U;
        };

        class cpu_profile_unit : public profile_unit {
            public:
                cpu_profile_unit(const std::string &name) : profile_unit(name) {}

                void tic() {
#ifdef WIN32
                    ftime(&start);
#else
                    start = std::chrono::high_resolution_clock::now();
#endif
                }

                double toc() {
#ifdef WIN32
                    timeb now;
                    ftime(&now);
                    double delta = now.time - start.time + 1e-3 * (now.millitm - start.millitm);
#else
                    double delta = std::chrono::duration<double>(
                            std::chrono::high_resolution_clock::now() - start).count();
#endif

                    length += delta;
                    hit++;

                    return delta;
                }

            private:
#ifdef WIN32
                timeb start;
#else
                std::chrono::time_point<std::chrono::high_resolution_clock> start;
#endif<commit_msg>Use boost.chrono in profiler<commit_after>#ifndef VEXCL_PROFILER_HPP
#define VEXCL_PROFILER_HPP

/*
The MIT License

Copyright (c) 2012-2013 <NAME> <<EMAIL>>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**
 * \file   profiler.hpp
 * \author <NAME> <<EMAIL>>
 * \brief  Simple OpenCL/Host profiler.
 */

#ifdef WIN32
#  pragma warning(push)
#  pragma warning(disable : 4267 4290)
#  define NOMINMAX
#endif

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <memory>
#include <stack>
#include <vector>
#include <cassert>
#include <boost/chrono.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/global_fun.hpp>

#ifndef __CL_ENABLE_EXCEPTIONS
#  define __CL_ENABLE_EXCEPTIONS
#endif

#include <CL/cl.hpp>


namespace vex {

/// Class for gathering and printing OpenCL and Host profiling info.
class profiler {
    private:
        class profile_unit {
            public:
                profile_unit(std::string name) : length(0), hit(0), name(name) {}
                virtual ~profile_unit() {}
                virtual void tic() = 0;
                virtual double toc() = 0;

                double length;
                size_t hit;
                std::string name;

                static std::string _name(const std::shared_ptr<profile_unit> &u) {
                    return u->name;
                }
                boost::multi_index_container<
                    std::shared_ptr<profile_unit>,
                    boost::multi_index::indexed_by<
                        boost::multi_index::sequenced<>,
                        boost::multi_index::ordered_unique<boost::multi_index::global_fun<
                                const std::shared_ptr<profile_unit> &, std::string, _name>>
                    >
                > children;

                double children_time() const {
                    double tm = 0;

                    for(auto c = children.begin(); c != children.end(); c++)
                        tm += (*c)->length;

                    return tm;
                }

                uint max_line_width(uint level) const {
                    uint w = name.size() + level;

                    for(auto c = children.begin(); c != children.end(); c++)
                        w = std::max(w, (*c)->max_line_width(level + shift_width));

                    return w;
                }

                void print(std::ostream &out,
                        uint level, double total, uint width) const
                {
                    using namespace std;
                    out << "[" << setw(level) << "";
                    print_line(out, name, length, 100 * length / total, width - level);

                    if (!children.empty()) {
                        double sec = length - children_time();
                        double perc = 100 * sec / total;

                        if (perc > 1e-1) {
                            out << "[" << setw(level + 1) << "";
                            print_line(out, "self", sec, perc, width - level - 1);
                        }
                    }

                    for(auto c = children.begin(); c != children.end(); c++)
                        (*c)->print(out, level + shift_width, total, width);
                }

                void print_line(std::ostream &out, const std::string &name,
                        double time, double perc, uint width) const
                {
                    using namespace std;
                    out << name << ":";
                    out << setw(width - name.size()) << "";
                    out << setiosflags(ios::fixed);
                    out << setw(10) << setprecision(3) << time << " sec.";
                    out << "] (" << setprecision(2) << setw(6) << perc << "%)";
                    if(hit > 1) out << " (" << hit << "x)";
                    out << endl;
                }
            private:
                static const uint shift_width = 2U;
        };

        class cpu_profile_unit : public profile_unit {
            public:
                cpu_profile_unit(const std::string &name) : profile_unit(name) {}

                void tic() {
                    start = boost::chrono::high_resolution_clock::now();
                }

                double toc() {
                    double delta = boost::chrono::duration<double>(
                            boost::chrono::high_resolution_clock::now() - start).count();

                    length += delta;
                    hit++;

                    return delta;
                }

            private:
                boost::chrono::time_point<boost::chrono::high_resolution_clock> start;
{'fraction_non_alphanumeric': 0.10492939883317833, 'fraction_numerical': 0.007609706603534286, 'mean_word_length': 1.6591726618705036, 'pattern_counts': {'":': 2, '<': 160, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4468593', 'n_tokens_mistral': 3274, 'n_tokens_neox': 2970, 'n_words': 933}