#!/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 complex matrix transposition.")

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> <min> <mean> <max> <stdev>"
                    " <throughput> in that order",
                    action="store_true")
parser.add_argument("--method",
                    help = "Which threshhold method to use",
                    choices = comp_timing.trans_valid_methods,
                    default = comp_timing.trans_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_trans_problem(opt.problem, opt.method)

with ctx:
    fft.from_cli(opt)
    if (opt.fft_backends[0] == 'fftw'):
        fft.fftw.set_measure_level(0)
    # Create the problem instance:
    ourprob = prob_class(size)
    # Find the planning time:
    ourprob.setup()
    while True:
        ourprob.execute()
