def source_compile(self, src, compiler='gnu', flags='', opt_flags='-O3'): # Compile directly for a source code as string. # A signature file(*.pyf) is generated automatically. logger.debug('source code: {}'.format(src)) import tempfile tmpfile = tempfile.NamedTemporaryFile(mode='w', suffix='.f90', delete=True) tmpfile.file.write(src) tmpfile.file.flush() compile_using_f2py(tmpfile.name, compiler, flags, opt_flags) mod_name = basename(tmpfile.name).split('.')[0] return get_module_from_file('/tmp/build/', mod_name, self.code_type)
def test_compile_using_f2py_c(): ''' compile_using_f2py: add.c ''' from source_module import compile_using_f2py, get_module_from_file # compile and import src = ''' void add(int nx, double *a, double *b, double *c) { // size and intent of array arguments for f2py // a :: nx, in // b :: nx, in // c :: nx, inout int i; for (i=0; i<nx; i++) { c[i] = a[i] + b[i]; } } ''' code_type = 'c' dpath = os.path.join(current_dpath, 'src') build_dpath = os.path.join(dpath, 'build') src_fpath = os.path.join(dpath, 'add.'+code_type) with open(src_fpath, 'w') as f: f.write(src) ret, out, err = capture(compile_using_f2py)(src_fpath, compiler='gnu') mod = get_module_from_file(build_dpath, 'add', code_type) # setup nx = 1000000 a = np.random.rand(nx) b = np.random.rand(nx) c = np.zeros(nx) mod.add(nx, a, b, c) a_equal(a+b, c)
def test_compile_using_f2py_f90(): ''' compile_using_f2py: add.f90 ''' from source_module import compile_using_f2py, get_module_from_file # compile and import src = ''' SUBROUTINE add(nx, a, b, c) IMPLICIT NONE INTEGER, INTENT(IN) :: nx REAL(8), DIMENSION(nx), INTENT(IN) :: a, b REAL(8), DIMENSION(nx), INTENT(INOUT) :: c INTEGER :: ii DO ii=1,nx c(ii) = a(ii) + b(ii) END DO END SUBROUTINE ''' code_type = 'f90' dpath = os.path.join(current_dpath, 'src') build_dpath = os.path.join(dpath, 'build') src_fpath = os.path.join(dpath, 'add.'+code_type) with open(src_fpath, 'w') as f: f.write(src) ret, out, err = capture(compile_using_f2py)(src_fpath, compiler='gnu') mod = get_module_from_file(build_dpath, 'add', code_type) # setup nx = 1000000 a = np.random.rand(nx) b = np.random.rand(nx) c = np.zeros(nx) mod.add(nx, a, b, c) a_equal(a+b, c)
def test_check_and_build_c(): ''' check_and_build(): C directly ''' import yaml import os from build import check_and_make_parameter_header, check_and_build, clean from source_module import get_module_from_file code_type = 'c' dpath = join(current_dpath, 'src') src_dir = {'f90':'f90', 'c':'c', 'cu':'cuda', 'cl':'opencl'}[code_type] build_dpath = join(dpath, src_dir, 'build') with open(join(dpath, 'build.yaml'), 'r') as f: build_dict = yaml.load(f) # # Remove previous generated files # ret, out, err = capture(clean)(code_type, dpath) equal( len(os.listdir(build_dpath)), 0 ) # # Make and compile header file # verify stdout and file existence # ret, out, err = capture(check_and_make_parameter_header)(code_type, dpath) #check_and_build(code_type, dpath) ret, out, err = capture(check_and_build)(code_type, dpath) expect = ''' [compile] gcc -fPIC -I. -c amb_ext2.c [compile] gcc -fPIC -I. -c amb_ext1.c [compile] f2py -c --compiler=unix --f90flags= --opt=-O3 -I. amb.c.pyf amb_ext1.o amb_ext2.o amb.c [compile] gcc -fPIC -I. -c apb_ext.c [compile] f2py -c --compiler=unix --f90flags= --opt=-O3 -I. apb.c.pyf apb_ext.o apb.c ''' equal('\n'+out+'\n', expect) mod_apb = get_module_from_file(build_dpath, 'apb', code_type) mod_amb = get_module_from_file(build_dpath, 'amb', code_type) # # setup # nx = 1000000 a = np.random.rand(nx) b = np.random.rand(nx) c = np.random.rand(nx) c2 = c.copy() with open(join(dpath, 'apb.yaml'), 'r') as f: apb_dict = yaml.load(f) with open(join(dpath, 'amb.yaml'), 'r') as f: amb_dict = yaml.load(f) kk = apb_dict['kk'] lll = apb_dict['lll'] mm = amb_dict['section']['mm'] ref = kk*a + lll*b + mm*c # # verify results # mod_apb.apb(nx, a, b, c) aa_equal(ref, c, 14) mod_amb.amb(nx, a, b, c2) aa_equal(ref, c2, 14) # # verify stdout if revision # ret, out, err = capture(check_and_build)(code_type, dpath) expect = ''' ./c/build/amb_ext2.o is up to date. ./c/build/amb_ext1.o is up to date. ./c/build/amb.c.so is up to date. ./c/build/apb_ext.o is up to date. ./c/build/apb.c.so is up to date. ''' equal('\n'+out.replace(dpath,'.')+'\n', expect) # # verify stdout if partial revision # os.remove(join(build_dpath, 'amb_ext1.o')) ret, out, err = capture(check_and_build)(code_type, dpath) expect = ''' ./c/build/amb_ext2.o is up to date. [compile] gcc -fPIC -I. -c amb_ext1.c [compile] f2py -c --compiler=unix --f90flags= --opt=-O3 -I. amb.c.pyf amb_ext1.o amb_ext2.o amb.c ./c/build/apb_ext.o is up to date. ./c/build/apb.c.so is up to date. ''' equal('\n'+out.replace(dpath,'.')+'\n', expect)
def load_module(self, build_dpath, module_name): return get_module_from_file(build_dpath, module_name, self.code_type)