예제 #1
0
def create_nlp(var, par, obj, con, options, name=''):
    codegen = options['codegen']
    if options['verbose'] >= 1:
        print('Building nlp ... ', end=' ')
    t0 = time.time()
    nlp = {'x': var, 'p': par, 'f': obj, 'g': con}
    slv_opt = options['solver_options'][options['solver']]
    opt = {}
    for key, value in slv_opt.items():
        opt[key] = value
    opt.update({'expand': True})
    print('opt updated')
    solver = nlpsol('solver', options['solver'], nlp, opt)
    print('solver created')
    name = 'nlp' if name == '' else 'nlp_' + name
    if codegen['build'] == 'jit':
        if options['verbose'] >= 1:
            print(('[jit compilation with flags %s]' % (codegen['flags'])), end=' ')
        solver.generate_dependencies(name+'.c')
        compiler = Compiler(
            name+'.c', 'clang', {'flags': codegen['flags']})
        problem = nlpsol('solver', options['solver'], compiler, slv_opt)
        os.remove(name+'.c')
    elif codegen['build'] == 'shared':
        if os.name == 'nt':
            raise ValueError('Build option is not supported for Windows!')
        directory = os.path.join(os.getcwd(), 'build')
        if not os.path.isdir(directory):
            os.makedirs(directory)
        path = os.path.join(directory, name)
        if options['verbose'] >= 1:
            print(('[compile to .so with flags %s]' % (codegen['flags'])), end=' ')
        if os.path.isfile(path+'.so'):
            os.remove(path+'.so')
        solver.generate_dependencies(name+'.c')
        shutil.move(name+'.c', path+'.c')
        os.system('gcc -fPIC -shared %s %s.c -o %s.so' %
                  (codegen['flags'], path, path))
        problem = nlpsol('solver', options['solver'], path+'.so', slv_opt)
        os.remove(path+'.c')
    elif codegen['build'] == 'existing':
        if os.name == 'nt':
            raise ValueError('Build option is not supported for Windows!')
        directory = os.path.join(os.getcwd(), 'build')
        path = os.path.join(directory, name)
        if not os.path.isfile(path+'.so'):
            raise ValueError('%s.so does not exist!', path)
        if options['verbose'] >= 1:
            print(('[using shared object %s.so]' % path), end=' ')
        problem = nlpsol('solver', options['solver'], path+'.so', slv_opt)
    elif codegen['build'] is None:
        problem = solver
    else:
        raise ValueError('Invalid build option.')
    print('nlpsol done.')
    t1 = time.time()
    if options['verbose'] >= 1:
        print('in %5f s' % (t1-t0))
    return problem, (t1-t0)
예제 #2
0
def create_function(name, inp, out, options):
    codegen = options['codegen']
    if options['verbose'] >= 1:
        print('Building function %s ... ' % name, end=' ')
    t0 = time.time()
    fun = Function(name, inp, out).expand()
    if codegen['build'] == 'jit':
        if options['verbose'] >= 1:
            print(('[jit compilation with flags %s]' % (codegen['flags'])),
                  end=' ')
        fun.generate(name)
        compiler = Compiler(name + '.c', 'clang', {'flags': codegen['flags']})
        fun = external(name, compiler)
        os.remove(name + '.c')
    elif codegen['build'] == 'shared':
        if os.name == 'nt':
            raise ValueError('Build option is not supported for Windows!')
        directory = os.path.join(os.getcwd(), 'build')
        if not os.path.isdir(directory):
            os.makedirs(directory)
        path = os.path.join(directory, name)
        if options['verbose'] >= 1:
            print(('[compile to .so with flags %s]' % (codegen['flags'])),
                  end=' ')
        if os.path.isfile(path + '.so'):
            os.remove(path + '.so')
        fun.generate(name + '.c')
        shutil.move(name + '.c', path + '.c')
        os.system('gcc -fPIC -shared %s %s.c -o %s.so' %
                  (codegen['flags'], path, path))
        fun = external(name, path + '.so')
        os.remove(path + '.c')
    elif codegen['build'] == 'existing':
        if os.name == 'nt':
            raise ValueError('Build option is not supported for Windows!')
        directory = os.path.join(os.getcwd(), 'build')
        path = os.path.join(directory, name)
        if not os.path.isfile(path + '.so'):
            raise ValueError('%s.so does not exist!', path)
        if options['verbose'] >= 1:
            print(('[using shared object %s.so]' % path), end=' ')
        fun = external(name, path + '.so')
    elif codegen['build'] is None:
        fun = fun
    else:
        raise ValueError('Invalid build option.')
    t1 = time.time()
    if options['verbose'] >= 1:
        print('in %5f s' % (t1 - t0))
    return fun, (t1 - t0)