Example #1
0
def gen_bench_from_code(f, typ, code, bench_with_timestamp):
    header = ''
    header += common.pprint_includes(f.gen_includes(_lang))
    header += \
    '''

    // Required for random generation
    #include "../benches.hpp"

    // Google benchmark
    #ifndef DISABLE_GOOGLE_BENCHMARK
    #include <benchmark/benchmark.h>
    #endif

    #include <ctime>
    double timestamp_ns() {
      timespec ts;
      clock_gettime(CLOCK_MONOTONIC, &ts);
      return double(ts.tv_sec) * 1000000000.0 + double(ts.tv_nsec);
    }

    // std
    #include <cmath>
    // #include <map>
    #include <numeric>
    // #include <fstream>

    // Sleef
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wignored-qualifiers"
    #include <sleef.h>
    #pragma GCC diagnostic pop

    // MIPP
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wconversion"
    #pragma GCC diagnostic ignored "-Wsign-conversion"
    #pragma GCC diagnostic ignored "-Wdouble-promotion"
    #pragma GCC diagnostic ignored "-Wunused-parameter"
    #if defined(__clang__)
    #pragma GCC diagnostic ignored "-Wzero-length-array"
    #endif
    #include <mipp.h>
    #pragma GCC diagnostic pop
    '''
    return \
    '''{header}

    // -------------------------------------------------------------------------

    static const int sz = 1024;

    template <typename Random>
    static {type}* make_data(int sz, Random r) {{
      {type}* data = ({type}*)nsimd_aligned_alloc(sz * {sizeof});

      for (int i = 0; i < sz; ++i) {{
        data[i] = r();
      }}
      return data;
    }}

    static {type}* make_data(int sz) {{
      {type}* data = ({type}*)nsimd_aligned_alloc(sz * {sizeof});

      for (int i = 0; i < sz; ++i) {{
        data[i] = {type}(0);
      }}
      return data;
    }}

    {random_code}

    {code}

    int main(int argc, char** argv)
    {{
      std::vector<std::string> args(argv, argv + argc);

      if (std::find(args.begin(), args.end(), "--use_timestamp_ns")
          != args.end()) {{
        {bench_with_timestamp}
      }}
      #ifndef DISABLE_GOOGLE_BENCHMARK
      else {{
        ::benchmark::Initialize(&argc, argv);
        ::benchmark::RunSpecifiedBenchmarks();
      }}
      #endif

      return 0;
    }}

    '''.format(
            name=f.name,
            type=typ,
            year=date.today().year,
            random_code=f.domain.code('rand_param', typ),
            code=code,
            bench_with_timestamp=bench_with_timestamp,
            sizeof=common.sizeof(typ),
            header=header,
    )
Example #2
0
def gen_bench_from_code(f, typ, code):
    header = ''
    header += common.pprint_includes(f.gen_includes(_lang))
    header += \
    '''

    // Required for random generation
    #include "../benches.hpp"

    // Google benchmark
    #include <benchmark/benchmark.h>

    // std
    #include <cmath>

    // Sleef
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wignored-qualifiers"
    #include <sleef.h>
    #pragma GCC diagnostic pop

    // MIPP
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wconversion"
    #pragma GCC diagnostic ignored "-Wsign-conversion"
    #pragma GCC diagnostic ignored "-Wdouble-promotion"
    #pragma GCC diagnostic ignored "-Wunused-parameter"
    #if defined(__clang__)
    #pragma GCC diagnostic ignored "-Wzero-length-array"
    #endif
    #include <mipp.h>
    #pragma GCC diagnostic pop
    '''
    return \
    '''{header}

    // -------------------------------------------------------------------------

    static const int sz = 1024;

    template <typename Random>
    static {type}* make_data(int sz, Random r) {{
      {type}* data = ({type}*)nsimd_aligned_alloc(sz * {sizeof});

      for (int i = 0; i < sz; ++i) {{
        data[i] = r();
      }}
      return data;
    }}

    static {type}* make_data(int sz) {{
      {type}* data = ({type}*)nsimd_aligned_alloc(sz * {sizeof});

      for (int i = 0; i < sz; ++i) {{
        data[i] = {type}(0);
      }}
      return data;
    }}

    {random_code}

    // -------------------------------------------------------------------------

    {code}

    BENCHMARK_MAIN();

    '''.format(
            name=f.name,
            type=typ,
            year=date.today().year,
            code=code,
            random_code=f.domain.code('rand_param', typ),
            sizeof=common.sizeof(typ),
            header=header,
    )