#!/usr/bin/env python

# Copyright (C) 2014 Josh Willis
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import multibench
import cProfile
import comp_timing
import sys, argparse, math
from scipy import mean, std
from numpy import array
from pycbc import scheme, fft

parser = argparse.ArgumentParser(description = "Benchmark hand-coded FFT")

parser.add_argument("--problem",
                    help = "String describing the problem to benchmark, in the form"
                    " <size>.")
parser.add_argument("--long-output",
                    help = "Give a verbose, explanatory output for the results."
                    " Otherwise, print <problem> <plan_time> <min> <mean> <max> <stdev>"
                    " <throughput> in that order",
                    action="store_true")
parser.add_argument("--method",
                    help = "Which threshhold method to use",
                    choices = comp_timing.custom_valid_methods,
                    default = comp_timing.custom_valid_methods[0])
parser.add_argument('--profile-outfile',
                    help = "File to which to write PStats output",
                    default=None)

# Now call functions to parse arguments from modules
multibench.insert_timing_option_group(parser)
scheme.insert_processing_option_group(parser)
fft.insert_fft_option_group(parser)

# And parse
opt = parser.parse_args()

# Check that the values returned for the options make sense
scheme.verify_processing_options(opt, parser)
fft.verify_fft_options(opt,parser)

# Do what we can with command line options
ctx = scheme.from_cli(opt)

prob_class, size = comp_timing.parse_custom_problem(opt.problem, opt.method)

with ctx:
    fft.from_cli(opt)
    # Create the problem instance:
    ourprob = prob_class(size)
    # Find the planning time:
    ourprob.setup()
    setup_time = ourprob.setup_time
    # Find the needed number of repetitions:
    nexecute = ourprob.needed_n(opt.mbench_time)
    nrepeats = opt.mbench_repeats
    try:
        ncores = ctx.num_threads
    except AttributeError:
        ncores = 1
    # Now use the Timer (imported by multibench from timeit) class
    # to make our measurements
    t = multibench.Timer(ourprob.execute)
    if opt.profile_outfile is not None:
        prof = cProfile.Profile()
        args = [nrepeats, nexecute]
        timings_array = prof.runcall(t.repeat, *args)
        prof.dump_stats(opt.profile_outfile)
    else:
        timings_array = t.repeat(repeat=nrepeats, number=nexecute)

timings_array = array(timings_array)
timings_array = timings_array/float(nexecute)
custom_mean = mean(timings_array)
custom_min = min(timings_array)
custom_max = max(timings_array)
custom_std = std(timings_array)
# The following should be *per core*
throughput = size/(custom_mean*ncores)

if opt.long_output:
    mean_str, min_str, max_str, std_str = multibench.format_time_strings([custom_mean, custom_min, custom_max, custom_std])
    print("Problem: {0}, setup: {1:g} s, mean time: {2},"
          " throughput/core: {3:g}".format(
            opt.problem, setup_time, mean_str, throughput))
    print("    min time: {0}, max time: {1}, std. dev: {2}\n".format(min_str, max_str, std_str))
else:
    print("{0} {1} {2:g} {3} {4} {5} {6} {7:g}".format(opt.problem, opt.method, setup_time, 
                                                       custom_min, custom_mean, 
                                                       custom_max, custom_std, throughput))

if (opt.fft_backends[0] == 'fftw'):
    # We know we gave FFTW parameters on the command line
    if opt.fftw_output_float_wisdom_file:
        fft.fftw.export_single_wisdom_to_filename(opt.fftw_output_float_wisdom_file)
