def test_mpi4py(f): execute_pyccel(f, compiler='mpif90') print('\n')
def pyccel(files=None, openmp=None, openacc=None, output_dir=None, compiler=None): """ pyccel console command. """ parser = MyParser(description='pyccel command line') parser.add_argument('files', metavar='N', type=str, nargs='+', help='a Pyccel file') #... Version import pyccel version = pyccel.__version__ libpath = pyccel.__path__[0] python = 'python {}.{}'.format(*sys.version_info) message = "pyccel {} from {} ({})".format(version, libpath, python) parser.add_argument('-V', '--version', action='version', version=message) # ... # ... compiler syntax, semantic and codegen group = parser.add_argument_group('Pyccel compiling stages') group.add_argument('-x', '--syntax-only', action='store_true', help='Using pyccel for Syntax Checking') group.add_argument('-e', '--semantic-only', action='store_true', help='Using pyccel for Semantic Checking') group.add_argument('-t', '--convert-only', action='store_true', help='Converts pyccel files only without build') # group.add_argument('-f', '--f2py-compatible', action='store_true', # help='Converts pyccel files to be compiled by f2py') # ... # ... backend compiler options group = parser.add_argument_group('Backend compiler options') group.add_argument('--language', choices=('fortran', 'c', 'python'), help='Generated language') group.add_argument('--compiler', choices=('gfortran', 'ifort', 'pgfortran', \ 'gcc', 'icc'), help='Compiler name') group.add_argument('--mpi-compiler', help='MPI compiler wrapper') group.add_argument('--flags', type=str, \ help='Compiler flags.') group.add_argument('--debug', action='store_true', \ help='compiles the code in a debug mode.') group.add_argument('--include', type=str, nargs='*', dest='includes', default=(), help='list of include directories.') group.add_argument('--libdir', type=str, nargs='*', dest='libdirs', default=(), help='list of library directories.') group.add_argument('--libs', type=str, nargs='*', dest='libs', default=(), help='list of libraries to link with.') group.add_argument('--output', type=str, default = '',\ help='folder in which the output is stored.') # ... # ... Accelerators group = parser.add_argument_group('Accelerators options') group.add_argument('--openmp', action='store_true', \ help='uses openmp') group.add_argument('--openacc', action='store_true', \ help='uses openacc') # ... # ... Other options group = parser.add_argument_group('Other options') group.add_argument('--verbose', action='store_true', \ help='enables verbose mode.') group.add_argument('--developer-mode', action='store_true', \ help='shows internal messages') # ... # TODO move to another cmd line parser.add_argument('--analysis', action='store_true', \ help='enables code analysis mode.') # ... # ... args = parser.parse_args() # ... # Imports from pyccel.errors.errors import Errors, PyccelError from pyccel.errors.errors import ErrorsMode from pyccel.errors.messages import INVALID_FILE_DIRECTORY, INVALID_FILE_EXTENSION from pyccel.codegen.pipeline import execute_pyccel # ... if not files: files = args.files if args.compiler: compiler = args.compiler if not openmp: openmp = args.openmp if not openacc: openacc = args.openacc if args.convert_only or args.syntax_only or args.semantic_only: compiler = None # ... # ... if len(files) > 1: errors = Errors() # severity is error to avoid needing to catch exception errors.report('Pyccel can currently only handle 1 file at a time', severity='error') errors.check() sys.exit(1) # ... filename = files[0] # ... report error if os.path.isfile(filename): # we don't use is_valid_filename_py since it uses absolute path # file extension ext = filename.split('.')[-1] if not(ext in ['py', 'pyh']): errors = Errors() # severity is error to avoid needing to catch exception errors.report(INVALID_FILE_EXTENSION, symbol=ext, severity='error') errors.check() sys.exit(1) else: # we use Pyccel error manager, although we can do it in other ways errors = Errors() # severity is error to avoid needing to catch exception errors.report(INVALID_FILE_DIRECTORY, symbol=filename, severity='error') errors.check() sys.exit(1) # ... if compiler: if _which(compiler) is None: errors = Errors() # severity is error to avoid needing to catch exception errors.report('Could not find compiler', symbol=compiler, severity='error') errors.check() sys.exit(1) accelerator = None if openmp: accelerator = "openmp" if openacc: accelerator = "openacc" # ... # ... if args.developer_mode: # this will initialize the singelton ErrorsMode # making this settings available everywhere err_mode = ErrorsMode() err_mode.set_mode('developer') # ... base_dirpath = os.getcwd() try: # TODO: prune options execute_pyccel(filename, syntax_only = args.syntax_only, semantic_only = args.semantic_only, convert_only = args.convert_only, verbose = args.verbose, language = args.language, compiler = compiler, mpi_compiler = args.mpi_compiler, fflags = args.flags, includes = args.includes, libdirs = args.libdirs, modules = (), libs = args.libs, debug = args.debug, extra_args = '', accelerator = accelerator, folder = args.output) except PyccelError: sys.exit(1) finally: os.chdir(base_dirpath) return
def epyccel_seq(function_or_module, *, language=None, compiler=None, mpi_compiler=None, fflags=None, accelerator=None, verbose=False, debug=False, includes=(), libdirs=(), modules=(), libs=(), extra_args='', folder=None): # ... get the module source code if isinstance(function_or_module, FunctionType): pyfunc = function_or_module code = get_source_function(pyfunc) tag = random_string(8) module_name = 'mod_{}'.format(tag) while module_name in sys.modules.keys(): tag = random_string(8) module_name = 'mod_{}'.format(tag) pymod_filename = '{}.py'.format(module_name) pymod_filepath = os.path.abspath(pymod_filename) elif isinstance(function_or_module, ModuleType): pymod = function_or_module pymod_filepath = pymod.__file__ pymod_filename = os.path.basename(pymod_filepath) lines = inspect.getsourcelines(pymod)[0] code = ''.join(lines) tag = random_string(8) module_import_prefix = pymod.__name__ + '_' while module_import_prefix + tag in sys.modules.keys(): tag = random_string(n=8) module_name = pymod.__name__.split('.')[-1] + '_' + tag else: raise TypeError('> Expecting a FunctionType or a ModuleType') # ... # Store current directory base_dirpath = os.getcwd() # Define working directory 'folder' if folder is None: folder = os.path.dirname(pymod_filepath) else: folder = os.path.abspath(folder) # Define directory name and path for epyccel files epyccel_dirname = '__epyccel__' epyccel_dirpath = os.path.join(folder, epyccel_dirname) # Create new directories if not existing os.makedirs(folder, exist_ok=True) os.makedirs(epyccel_dirpath, exist_ok=True) # Change working directory to '__epyccel__' os.chdir(epyccel_dirpath) # Store python file in '__epyccel__' folder, so that execute_pyccel can run with open(pymod_filename, 'w') as f: f.writelines(code) try: # Generate shared library execute_pyccel(pymod_filename, verbose=verbose, language=language, compiler=compiler, mpi_compiler=mpi_compiler, fflags=fflags, includes=includes, libdirs=libdirs, modules=modules, libs=libs, debug=debug, extra_args=extra_args, accelerator=accelerator, output_name=module_name) finally: # Change working directory back to starting point os.chdir(base_dirpath) # Import shared library sys.path.insert(0, epyccel_dirpath) # http://ballingt.com/import-invalidate-caches # https://docs.python.org/3/library/importlib.html#importlib.invalidate_caches importlib.invalidate_caches() package = importlib.import_module(module_name) sys.path.remove(epyccel_dirpath) # Verify that we have imported the shared library, not the Python one loader = getattr(package, '__loader__', None) if not isinstance(loader, ExtensionFileLoader): raise ImportError('Could not load shared library') # If Python object was function, extract it from module if isinstance(function_or_module, FunctionType): func = getattr(package, pyfunc.__name__) else: func = None # Return accelerated Python module and function return package, func
def test_openmp(f): execute_pyccel(f, accelerator='openmp')
def test_mpi(f): execute_pyccel(f, compiler='mpif90')
def test_lapack(f): execute_pyccel(f, libs=['blas', 'lapack'])
def test_blas(f): execute_pyccel(f, libs=['blas'])