Exemplo n.º 1
0
def static_compile_example():
    print "Frontend".center(80, '=')
    context = frontend_example()

    print "Backend".center(80, '=')

    ###########
    # Use LLVM as the backend
    backend = LLVMBackend(opt=LLVMBackend.OPT_MAXIMUM)
    backend.install(Float4BackendExt)

    #############
    # Static compiling

    # Create C Header Generator
    header = LLVMCWrapperGenerator()

    # Create static compiler
    compiler = Compiler(LLVMCompiler(), {'': backend}, wrapper=header)

    # Compile our function
    foo = context.get_function("foo")
    funcdef = foo.get_definition(("float4", "float4"))
    compiler.add_function(funcdef)

    # Show results
    print "Show assembly"
    print compiler.write_assembly()
    print

    print "Show C header"
    print header
    print
Exemplo n.º 2
0
def static_compile_example():
    print "Frontend".center(80, '=')
    context = frontend_example()

    print "Backend".center(80, '=')

    ###########
    # Use LLVM as the backend
    backend = LLVMBackend(opt=LLVMBackend.OPT_MAXIMUM)
    backend.install(Float4BackendExt)

    #############
    # Static compiling

    # Create C Header Generator
    header = LLVMCWrapperGenerator()

    # Create static compiler
    compiler = Compiler(LLVMCompiler(), {'': backend}, wrapper=header)

    # Compile our function
    foo = context.get_function("foo")
    funcdef = foo.get_definition(("float4", "float4"))
    compiler.add_function(funcdef)

    # Show results
    print "Show assembly"
    print compiler.write_assembly()
    print

    print "Show C header"
    print header
    print
Exemplo n.º 3
0
def jit_example():
    print "Frontend".center(80, '=')
    context = frontend_example()

    print "Backend".center(80, '=')

    #########
    # Use LLVM as the backend
    backend = LLVMBackend(opt=LLVMBackend.OPT_MAXIMUM)

    # Recall we have introduced a type and a intrinsic that are still
    # unimplemented.  We will do that now by install an extension.
    backend.install(Float4BackendExt)

    ###########
    # Try compiling the function
    # This section is not necessary for JIT'ing
    foo = context.get_function("foo")
    funcdef = foo.get_definition(("float4", "float4"))

    lfunc = backend.compile(funcdef)

    print "Compiled LLVM Function"
    print lfunc
    print

    lfunc = backend.link(lfunc)

    print "Linked & Optimized LLVM Function"
    print lfunc
    print


    ############
    # JIT

    # Create an execution manager
    manager = LLVMExecutionManager(opt=LLVMExecutionManager.OPT_MAXIMUM)

    # Create a JIT engine with the manager and backend
    # A JIT engine can have multiple backends.
    # The 2nd argument is a dictionary of backends with the key being
    # the name of the backend.  Use empty string "" for default backend.
    jit = JIT(manager, {'': backend})

    # Compile the definition with the default backend
    function = jit.compile(funcdef)

    # Show ctype function arguments and return type
    print "Ctype arguments and return type"
    print function.ctype.argtypes, function.ctype.restype
    print

    #############
    # Let's test the JIT'ed function
    
    A = np.arange(4, dtype=np.float32)
    B = np.arange(4, dtype=np.float32)

    Aorig = A.copy() # keep a copy of the original data

    # Run with address of data
    function(A.ctypes.data, B.ctypes.data)

    print "Result is correct:", np.allclose(Aorig + B, A)

    Aorig = A.copy()

    # Run with numpy array
    function(A, B)

    print "Result is correct:", np.allclose(Aorig + B, A)