Exemplo n.º 1
0
        def visit_For(self, node):
            debug_print("visiting a For...\n")
            # check if this is the right kind of For loop
            if (node.iter.__class__.__name__ == "Call" and
                node.iter.func.__class__.__name__ == "Attribute"):
                
                debug_print("Found something to change...\n")

                if (node.iter.func.attr == "interior_points"):
                    grid = self.visit(node.iter.func.value).id     # do we need the name of the grid, or the obj itself?
                    target = self.visit(node.target)
                    body = map(self.visit, node.body)
                    newnode = StencilKernel.StencilInteriorIter(grid, body, target)
                    return newnode

                elif (node.iter.func.attr == "neighbors"):
                    debug_print(ast.dump(node) + "\n")
                    target = self.visit(node.target)
                    body = map(self.visit, node.body)
                    grid = self.visit(node.iter.func.value).id
                    dist = self.visit(node.iter.args[1]).n
                    newnode = StencilKernel.StencilNeighborIter(grid, body, target, dist)
                    return newnode

                else:
                    return node
            else:
                return node
Exemplo n.º 2
0
	def shadow_kernel(self, *args):
		if self.pure_python:
			return self.pure_python_kernel(*args)

		#FIXME: need to somehow match arg names to args
		argnames = map(lambda x: str(x.id), self.kernel_ast.body[0].args.args)
		argdict = dict(zip(argnames[1:], args))
		#debug_print('Kernel arguments:\n' + str(argdict))

		phase2 = StencilKernel.StencilProcessAST(argdict).visit(self.kernel_ast)
		debug_print('Phase 2 AST:\n' + ast.dump(phase2))
		phase3 = StencilKernel.StencilConvertAST(argdict).visit(phase2)

		mod = self.asp_module_factory()
		self.add_libraries(mod)
		mod.add_function(phase3)

		grid_data = [grid.data for grid in args]
		mod.kernel(*grid_data)
Exemplo n.º 3
0
	def shadow_kernel(self, *args):
		if self.pure_python:
			return self.pure_python_kernel(*args)

		#FIXME: need to somehow match arg names to args
		argnames = map(lambda x: str(x.id), self.kernel_ast.body[0].args.args)
		argdict = dict(zip(argnames[1:], args))
		debug_print(argdict)

		phase2 = StencilKernel.StencilProcessAST(argdict).visit(self.kernel_ast)
		debug_print(ast.dump(phase2))
		phase3 = StencilKernel.StencilConvertAST(argdict).visit(phase2)

		from asp.jit import asp_module

		mod = asp_module.ASPModule()
		self.add_libraries(mod)
		mod.add_function(phase3)
#		mod.compile()
#		mod.compiled_module.kernel(argdict['in_grid'].data, argdict['out_grid'].data)
		myargs = [y.data for y in args]
#		mod.kernel(argdict['in_grid'].data, argdict['out_grid'].data)
		mod.kernel(*myargs)
Exemplo n.º 4
0
    def shadow_kernel(self, *args):
        if self.pure_python:
            return self.pure_python_kernel(*args)

        #FIXME: instead of doing this short-circuit, we should use the Asp infrastructure to
        # do it, by passing in a lambda that does this check
        # if already specialized to these sizes, just run
        if self.specialized_sizes and self.specialized_sizes == [y.shape for y in args]:
            print "match!"
            self.mod.kernel(*[y.data for y in args])
            return

        # otherwise, do the first-run flow

        # check if we can specialize for this data
        #FIXME: impelement.

        # ask asp infrastructure for machine and platform info, including if cilk+ is available
        #FIXME: impelement.  set self.with_cilk=true if cilk is available
        
        

        #FIXME: need to somehow match arg names to args
        argnames = map(lambda x: str(x.id), self.kernel_ast.body[0].args.args)
        argdict = dict(zip(argnames[1:], args))
        debug_print(argdict)

        phase2 = StencilKernel.StencilProcessAST(argdict).visit(self.kernel_ast)
        debug_print(ast.dump(phase2))

        
        # depending on whether cilk is available, we choose which converter to use
        if not self.with_cilk:
            Converter = StencilKernel.StencilConvertAST
        else:
            Converter = StencilKernel.StencilConvertASTCilk

        # generate variant with no unrolling, then generate variants for various unrollings
        variants = [Converter(argdict).visit(phase2)]
        variant_names = ["kernel_unroll_1"]
        for x in [2,4,8,16,32,64]:
            check_valid = max(map(
                lambda y: (y.shape[-1]-2*y.ghost_depth) % x,
                args))
            if check_valid == 0:
                to_append = Converter(argdict, unroll_factor=x).visit(phase2)
                variants.append(Converter(argdict, unroll_factor=x).visit(phase2))
                variant_names.append("kernel_unroll_%s" % x)

        from asp.jit import asp_module

        mod = self.mod = asp_module.ASPModule()
        self.add_libraries(mod)
        if self.with_cilk:
            mod.toolchain.cc = "icc"
            mod.toolchain.cflags += ["-intel-extensions", "-fast"]
            mod.toolchain.cflags += ["-I/usr/include/x86_64-linux-gnu"]
            mod.toolchain.cflags.remove('-fwrapv')
        else:
            mod.toolchain.cflags += ["-fopenmp", "-O3", "-msse3"]
#        print mod.toolchain.cflags
        if mod.toolchain.cflags.count('-Os') > 0:
            mod.toolchain.cflags.remove('-Os')
        if mod.toolchain.cflags.count('-O2') > 0:
            mod.toolchain.cflags.remove('-O2')
        debug_print("toolchain" + str(mod.toolchain.cflags))
        mod.add_function_with_variants(variants, "kernel", variant_names)

        # package arguments
        myargs = [y.data for y in args]
        # and do the call 
        mod.kernel(*myargs)

        # save parameter sizes for next run
        self.specialized_sizes = [x.shape for x in args]
Exemplo n.º 5
0
    def shadow_kernel(self, *args):
        if self.pure_python:
            return self.pure_python_kernel(*args)

        #FIXME: need to somehow match arg names to args
        argnames = map(lambda x: str(x.id), self.kernel_ast.body[0].args.args)
        argdict = dict(zip(argnames[1:], args))
        
        phase2 = NewtonKernel.NewtonProcessAST(argdict).visit(self.kernel_ast)
  
        curList = []
        def flattenAST(ast, lst):
            if isinstance(ast, list):
                for node in ast:
                    flattenAST(node, lst)
            elif isinstance(ast, phase2.body[0].__class__):
                lst.append(ast)
                def extractFunctions(fDef):
                    extractedList = []
                    removeList = []
                    for node in fDef.body:
                        if isinstance(node, NewtonKernel.MathFunctionDef):
                            extractedList.append(node)                            
                            removeList.append(node)
                    for item in removeList:
                        fDef.body.remove(item)
                    return extractedList
                lst.extend(extractFunctions(ast))
                #lst[0].body = [ast_tools.Call("newtonsMethod_in_c", ["initialGuess", "epsilon", "max_iters"])]
                lst.append(lst[0])
                lst.remove(lst[0])
            return lst
        phase2.body= flattenAST(phase2.body, [])

        debug_print('Phase 2 AST:\n' + ast.dump(phase2))
       
        phase3 = NewtonKernel.NewtonConvertAST(argdict).visit(phase2)
       
        import asp.codegen.templating.template as template
        mod = self.asp_module_factory()
        self.add_libraries(mod)
        index = self.processFunctions(mod)
        mytemplate = template.Template(filename="templates/test.c")
        rendered = mytemplate.render(length=2)

        # remember, must specify function name when using a string
        for ast_func in phase2.body[:-1]:
            mod.add_function(NewtonKernel.NewtonConvertAST(argdict).visit(ast_func))
        mod.add_function(rendered, fname="newtonsMethod_in_c")
        #mod.add_function(NewtonKernel.NewtonConvertAST(argdict).visit(phase2.body[-1]))

        #newtonsMethod_in_c = open(templates/test.c")
        #mod.add_function(newtonsMethod_in_c, fname="newtonsMethod_in_c")
        #mod.add_function(phase3)
        #mod.add_function(NewtonKernel.NewtonConvertAST(argdict).visit(phase2.body[1]))
        #mod.kernel(*grid_data)
        #mod.func0(1)
        #mod.add_function(phase3, fname="func"+str(index))#rendered)
        initialXs = args[0]
        solution = args[1]
        npInitialXs = numpy.zeros(len(initialXs), float)
        for i in range(len(initialXs)):
            npInitialXs[i] = initialXs[i]
        npSolution =  numpy.zeros(len(solution), float)
        real_args = [args[0], args[1], args[2], args[3]]
        mod.newtonsMethod_in_c(*real_args)