Ejemplo n.º 1
0
 def compile(self, filename):
     import pythran, os
     os.chdir(self.output_dir)
     try:
         pythran.compile_pythranfile(os.path.join('..', filename))
     finally:
         os.chdir('..')
Ejemplo n.º 2
0
    def run(self):
        import glob
        import timeit
        from pythran import test_compile, compile_pythranfile
        import random
        import numpy

        # Do not include current directory, validate using installed pythran
        current_dir = _exclude_current_dir_from_import()
        os.chdir("pythran/tests")
        where = os.path.join(current_dir, 'pythran', 'tests', 'cases')

        test_compile()

        candidates = glob.glob(os.path.join(where, '*.py'))
        sys.path.append(where)
        random.shuffle(candidates)
        for candidate in candidates:
            with file(candidate) as content:
                runas = [line for line in content.readlines()
                         if line.startswith(BenchmarkCommand.runas_marker)]
                if runas:
                    modname, _ = os.path.splitext(os.path.basename(candidate))
                    runas_commands = runas[0].replace(
                        BenchmarkCommand.runas_marker, '').split(";")
                    runas_context = ";".join(["import {0}".format(
                        modname)] + runas_commands[:-1])
                    runas_command = modname + '.' + runas_commands[-1]

                    # cleaning
                    sopath = os.path.splitext(candidate)[0] + ".so"
                    if os.path.exists(sopath):
                        os.remove(sopath)

                    ti = timeit.Timer(runas_command, runas_context)

                    print(modname + ' running ...')

                    # pythran part
                    if self.mode.startswith('pythran'):
                        cxxflags = ["-O2", "-DNDEBUG", "-DUSE_BOOST_SIMD",
                                    "-march=native"]
                        if self.mode == "pythran+omp":
                            cxxflags.append("-fopenmp")
                        begin = time.time()
                        compile_pythranfile(candidate,
                                            cxxflags=cxxflags)
                        print('Compilation in : ', (time.time() - begin))

                    sys.stdout.flush()
                    timing = numpy.array(ti.repeat(self.nb_iter, number=1))
                    print('median :', numpy.median(timing))
                    print('min :', numpy.min(timing))
                    print('max :', numpy.max(timing))
                    print('std :', numpy.std(timing))
                    del sys.modules[modname]
                else:
                    print('* Skip ', candidate, ', no ',end='')
                    print(BenchmarkCommand.runas_marker, ' directive')
Ejemplo n.º 3
0
    def run(self):
        import glob
        import timeit

        # Do not include current directory, validate using installed pythran
        current_dir = _exclude_current_dir_from_import()
        os.chdir("pythran/tests")
        where = os.path.join(current_dir, 'pythran', 'tests', 'cases')

        from pythran import test_compile, compile_pythranfile
        test_compile()

        candidates = glob.glob(os.path.join(where, '*.py'))
        sys.path.append(where)
        median = lambda x: sorted(x)[len(x) / 2]
        for candidate in candidates:
            with file(candidate) as content:
                runas = [line for line in content.readlines()
                         if line.startswith(BenchmarkCommand.runas_marker)]
                if runas:
                    modname, _ = os.path.splitext(os.path.basename(candidate))
                    runas_commands = runas[0].replace(
                        BenchmarkCommand.runas_marker, '').split(";")
                    runas_context = ";".join(["import {0}".format(
                        modname)] + runas_commands[:-1])
                    runas_command = modname + '.' + runas_commands[-1]

                    # cleaning
                    sopath = os.path.splitext(candidate)[0] + ".so"
                    if os.path.exists(sopath):
                        os.remove(sopath)

                    ti = timeit.Timer(runas_command, runas_context)

                    # pythran part
                    if self.mode.startswith('pythran'):
                        cxxflags = ["-Ofast", "-DNDEBUG"]
                        if self.mode == "pythran+omp":
                            cxxflags.append("-fopenmp")
                        compile_pythranfile(candidate,
                                            cxxflags=cxxflags)

                    print modname + " running ...",
                    sys.stdout.flush()
                    timing = median(ti.repeat(self.nb_iter, number=1))
                    print timing
                else:
                    print "* Skip '" + candidate + ', no #runas directive'
Ejemplo n.º 4
0
 def compile(self, filename):
     import pythran
     pythran.compile_pythranfile(filename)
Ejemplo n.º 5
0
def run():
    parser = argparse.ArgumentParser(prog='pythran',
                                     description='pythran: a python to C++ '
                                     'compiler',
                                     epilog="It's a megablast!",
                                     fromfile_prefix_chars="@")

    parser.add_argument('input_file', type=str,
                        help='the pythran module to compile, '
                             'either a .py or a .cpp file')

    parser.add_argument('-o', dest='output_file', type=str,
                        help='path to generated file')

    parser.add_argument('-E', dest='translate_only', action='store_true',
                        help='only run the translator, do not compile')

    parser.add_argument('-e', dest='raw_translate_only', action='store_true',
                        help='similar to -E, '
                             'but does not generate python glue')

    parser.add_argument('-v', dest='verbose', action='store_true',
                        help='be verbose')

    parser.add_argument('-p', dest='opts', metavar='pass',
                        action='append',
                        help='any pythran optimization to apply before code '
                        'generation',
                        default=list())

    parser.add_argument('-I', dest='include_dirs', metavar='include_dir',
                        action='append',
                        help='any include dir relevant to the underlying C++ '
                        'compiler',
                        default=list())

    parser.add_argument('-L', dest='libraries_dir', metavar='ldflags',
                        action='append',
                        help='any search dir relevant to the linker',
                        default=list())

    parser.add_argument('-D', dest='defines', metavar='macro_definition',
                        action='append',
                        help='any macro definition relevant to '
                             'the underlying C++ compiler',
                        default=list())

    parser.convert_arg_line_to_args = convert_arg_line_to_args

    args, extra = parser.parse_known_args(sys.argv[1:])
    args.extra_flags = extra
    if args.raw_translate_only:
        args.translate_only = True

    if args.verbose:
        logger.setLevel(logging.INFO)

    try:
        if not os.path.exists(args.input_file):
            raise ValueError("input file `{0}' not found".format(
                args.input_file))

        module_name, ext = os.path.splitext(os.path.basename(args.input_file))

        # FIXME: do we want to support other ext than .cpp?
        if ext not in ['.cpp', '.py']:
            raise SyntaxError("Unsupported file extension: '{0}'".format(ext))

        if ext == '.cpp':
            if args.translate_only:
                raise ValueError("Do you really ask for Python-to-C++ "
                                 "on this C++ input file: '{0}'?".format(
                                     args.input_file))
            pythran.compile_cxxfile(module_name,
                                    args.input_file, args.output_file,
                                    **compile_flags(args))

        else:  # assume we have a .py input file here

            pythran.compile_pythranfile(args.input_file,
                                        output_file=args.output_file,
                                        cpponly=args.translate_only,
                                        **compile_flags(args))

    except IOError as e:
        logger.critical("I've got a bad feeling about this...\n"
                        "E: " + str(e))
        sys.exit(1)
    except ValueError as e:
        logger.critical("Chair to keyboard interface error\n"
                        "E: " + str(e))
        sys.exit(1)
    except SyntaxError as e:
        logger.critical("I am in trouble. Your input file does not seem "
                        "to match Pythran's constraints...\n"
                        "E: " + str(e))
        sys.exit(1)
    except CompileError as e:
        logger.critical("Cover me Jack. Jack? Jaaaaack!!!!\n"
                        "E: " + str(e))
        sys.exit(1)
    except NotImplementedError as e:
        logger.critical("MAYDAY, MAYDAY, MAYDAY; pythran compiler; "
                        "code area out of control\n"
                        "E: not implemented feature needed, "
                        "bash the developers")
        raise  # Why ? we may instead display the stacktrace and exit?
    except EnvironmentError as e:
        logger.critical("By Jove! Your environment does not seem "
                        "to provide all what we need\n"
                        "E: " + str(e))
        sys.exit(1)
Ejemplo n.º 6
0
def run():

    prefix_chars = "-"
    if os.name == "nt":
        prefix_chars += "/"

    parser = argparse.ArgumentParser(prog='pythran',
                                     description='pythran: a python to C++ '
                                     'compiler',
                                     epilog="It's a megablast!",
                                     prefix_chars=prefix_chars,
                                     fromfile_prefix_chars="@")

    parser.add_argument('input_file', type=str,
                        help='the pythran module to compile, '
                             'either a .py or a .cpp file')

    parser.add_argument('-o', dest='output_file', type=str,
                        help='path to generated file')

    parser.add_argument('-P', dest='optimize_only', action='store_true',
                        help='only run the high-level optimizer, '
                        'do not compile')

    parser.add_argument('-E', dest='translate_only', action='store_true',
                        help='only run the translator, do not compile')

    parser.add_argument('-e', dest='raw_translate_only', action='store_true',
                        help='similar to -E, '
                             'but does not generate python glue')

    parser.add_argument('-v', dest='verbose', action='store_true',
                        help='be more verbose')

    parser.add_argument('-w', dest='warn_off', action='store_true',
                        help='be less verbose')

    parser.add_argument('-V', '--version',
                        action='version',
                        version=pythran.version.__version__)

    parser.add_argument('-p', dest='opts', metavar='pass',
                        action='append',
                        help='any pythran optimization to apply before code '
                        'generation',
                        default=list())

    parser.add_argument('-I', dest='include_dirs', metavar='include_dir',
                        action='append',
                        help='any include dir relevant to the underlying C++ '
                        'compiler',
                        default=list())

    parser.add_argument('-L', dest='libraries_dir', metavar='ldflags',
                        action='append',
                        help='any search dir relevant to the linker',
                        default=list())

    parser.add_argument('-D', dest='defines', metavar='macro_definition',
                        action='append',
                        help='any macro definition relevant to '
                             'the underlying C++ compiler',
                        default=list())

    parser.add_argument('-U', dest='undefs', metavar='macro_definition',
                        action='append',
                        help='any macro undef relevant to '
                             'the underlying C++ compiler',
                        default=list())

    parser.add_argument('--config', dest='config', metavar='config',
                        action='append',
                        help='config additional params',
                        default=list())

    parser.convert_arg_line_to_args = convert_arg_line_to_args

    args, extra = parser.parse_known_args(sys.argv[1:])
    args.extra_flags = extra

    if args.raw_translate_only:
        args.translate_only = True
        args.undefs.append('ENABLE_PYTHON_MODULE')

    if args.verbose and args.warn_off:
        logger.critical("Unexpected combination: -w and -v? Daoubennek?")
        sys.exit(1)

    if args.verbose:
        logger.setLevel(logging.INFO)

    if args.warn_off:
        logger.setLevel(logging.ERROR)

    if args.config:
        pythran.config.update_cfg(pythran.config.cfg, args.config)

    if args.verbose and not args.warn_off:
        pythran.config.lint_cfg(pythran.config.cfg)

    try:
        if not os.path.exists(args.input_file):
            raise ValueError("input file `{0}' not found".format(
                args.input_file))

        module_name, ext = os.path.splitext(os.path.basename(args.input_file))

        # FIXME: do we want to support other ext than .cpp?
        if ext not in ['.cpp', '.py']:
            raise SyntaxError("Unsupported file extension: '{0}'".format(ext))

        if ext == '.cpp':
            if args.optimize_only:
                raise ValueError("Do you really ask for Python-to-Python "
                                 "on this C++ input file: '{0}'?".format(
                                     args.input_file))
            if args.translate_only:
                raise ValueError("Do you really ask for Python-to-C++ "
                                 "on this C++ input file: '{0}'?".format(
                                     args.input_file))
            pythran.compile_cxxfile(module_name,
                                    args.input_file, args.output_file,
                                    **compile_flags(args))

        else:  # assume we have a .py input file here

            pythran.compile_pythranfile(args.input_file,
                                        output_file=args.output_file,
                                        cpponly=args.translate_only,
                                        pyonly=args.optimize_only,
                                        **compile_flags(args))

    except IOError as e:
        logger.critical("I've got a bad feeling about this...\n"
                        "E: " + str(e))
        sys.exit(1)
    except ValueError as e:
        logger.critical("Chair to keyboard interface error\n"
                        "E: " + str(e))
        sys.exit(1)
    except pythran.types.tog.PythranTypeError as e:
        logger.critical("You shall not pass!\n"
                        "E: " + str(e))
        sys.exit(1)
    except pythran.syntax.PythranSyntaxError as e:
        logger.critical("I am in trouble. Your input file does not seem "
                        "to match Pythran's constraints...\n"
                        "E: " + str(e))
        sys.exit(1)
    except CompileError as e:
        logger.critical("Cover me Jack. Jack? Jaaaaack!!!!\n"
                        "E: " + str(e))
        sys.exit(1)
    except NotImplementedError:
        logger.critical("MAYDAY, MAYDAY, MAYDAY; pythran compiler; "
                        "code area out of control\n"
                        "E: not implemented feature needed, "
                        "bash the developers")
        raise  # Why ? we may instead display the stacktrace and exit?
    except EnvironmentError as e:
        logger.critical("By Jove! Your environment does not seem "
                        "to provide all what we need\n"
                        "E: " + str(e))
        sys.exit(1)