Esempio n. 1
0
def build_system(n: int) -> (SystemInstance, Statement):

    WRITE = AccessMode.WRITE
    READ = AccessMode.READ
    INC = AccessMode.INC
    RW = AccessMode.RW

    system = SystemSpecification()

    xvals = system.create_set("xvals")
    yvals = system.create_set("yvals")
    points = system.create_set("points")
    oddeven = system.create_set("oddeven")

    p_x = system.create_dat(xvals, "p_x", DataType(shape=(1, )))
    p_y = system.create_dat(yvals, "p_y", DataType(shape=(1, )))
    p_prod = system.create_dat(points, "p_prod", DataType(shape=(1, )))
    p_sums = system.create_dat(oddeven, "p_sums", DataType(shape=(1, )))

    xlink = system.create_map("xlink", points, xvals, 1)
    ylink = system.create_map("ylink", points, yvals, 1)
    sumlink = system.create_map("sumlink", points, oddeven, 1)

    instDict = {
        "xvals": n,
        "yvals": n,
        "points": n,
        "oddeven": 2,
        "p_x": numpy.array([[i * 1.0] for i in range(n)]),
        "p_y": numpy.array([[i * 1.0] for i in range(n)]),
        "p_prod": numpy.array([[0.0] for i in range(n)]),
        "xlink": numpy.array([[i] for i in range(n)]),
        "ylink": numpy.array([[i] for i in range(n)]),
        "sumlink": numpy.array([[i % 2] for i in range(n)])
    }

    inst = SystemInstance(system, instDict)

    instOutput = {"output": {"final": copy.copy(instDict)}, **instDict}

    expected = [i * i * 1.0 for i in range(n)]
    sumeven = sum(expected[0::2])
    sumodd = sum(expected[1::2])

    sys.stderr.write("exp=[{},{}]".format(sumeven, sumodd))

    instOutput["output"]["final"]["p_prod"] = numpy.array([[e]
                                                           for e in expected])
    instOutput["output"]["final"]["p_sums"] = numpy.array([[sumeven],
                                                           [sumodd]])

    code = Seq(
        ParFor(dot_product, points, p_x(READ, xlink, 0), p_y(READ, ylink, 0),
               p_prod(WRITE), p_sums(INC, sumlink, 0)),
        CheckState(instOutput, "/output/final"),
    )
    code.on_bind_spec(sys)
    return (system, inst, code)
Esempio n. 2
0
def build_system(n: int) -> (SystemInstance, Statement):

    WRITE = AccessMode.WRITE
    READ = AccessMode.READ
    INC = AccessMode.INC
    RW = AccessMode.RW

    sys = SystemSpecification()

    g_sum = sys.create_mutable_global("g_sum", DataType(shape=(1, )))

    xvals = sys.create_set("xvals")
    yvals = sys.create_set("yvals")
    points = sys.create_set("points")

    p_x = sys.create_dat(xvals, "p_x", DataType(shape=(1, )))
    p_y = sys.create_dat(yvals, "p_y", DataType(shape=(1, )))
    p_prod = sys.create_dat(points, "p_prod", DataType(shape=(1, )))

    xlink = sys.create_map("xlink", points, xvals, 1)
    ylink = sys.create_map("ylink", points, yvals, 1)

    instDict = {
        "xvals": n,
        "yvals": n,
        "points": n,
        "p_x": numpy.array([[i * 1.0] for i in range(n)]),
        "p_y": numpy.array([[i * 1.0] for i in range(n)]),
        "p_prod": numpy.array([[0.0] for i in range(n)]),
        "g_sum": numpy.array([0.0]),
        "xlink": numpy.array([[i] for i in range(n)]),
        "ylink": numpy.array([[i] for i in range(n)])
    }

    inst = SystemInstance(sys, instDict)

    instOutput = {"output": {"final": copy.copy(instDict)}, **instDict}

    expected = [i * i * 1.0 for i in range(n)]

    instOutput["output"]["final"]["g_sum"] = numpy.array([sum(expected)])
    instOutput["output"]["final"]["p_prod"] = numpy.array([[e]
                                                           for e in expected])

    code = Seq(
        """
        g_sum[0] = 0
        """,
        ParFor(dot_product, points, p_x(READ, xlink, 0), p_y(READ, ylink, 0),
               p_prod(WRITE), g_sum(INC)),
        CheckState(instOutput, "/output/final"),
    )
    code.on_bind_spec(sys)
    return (sys, inst, code)
Esempio n. 3
0
def build_system(n:int) -> (SystemInstance,Statement):

    WRITE=AccessMode.WRITE
    READ=AccessMode.READ
    INC=AccessMode.INC
    RW=AccessMode.RW
    
    sys=SystemSpecification()
    
    sum=sys.create_mutable_global("sum", DataType(shape=(1,)))
    
    nodes=sys.create_set("cells")
    
    p_iota=sys.create_dat(nodes, "p_iota", DataType(shape=(1,)))
    
    instDict={
        "cells" : n,
        "p_iota" : numpy.array( [[i] for i in range(n)] ),
        "sum" : numpy.array( [0.0] )
    }
    
    instOutput={
        "output" : {
            "final" : copy.copy(instDict)
        },
        **instDict
    }
    
    instOutput["output"]["final"]["sum"] = numpy.array( [ n*(n-1)/2 ] )
    
    inst=SystemInstance(sys, instOutput)
    

    code=Seq(
        """
        sum[0] = 0
        """,
        ParFor(
            iota_sum,
            nodes,
            p_iota(READ),
            sum(INC)
        ),
        CheckState(instOutput, "/output/final"),
    )
    code.on_bind_spec(sys)
    return (sys,inst,code)
Esempio n. 4
0
def build_system(srcFile: str = "./meshes/FE_mesh.hdf5",
                 total_iter=20) -> (SystemInstance, Statement):

    WRITE = AccessMode.WRITE
    READ = AccessMode.READ
    INC = AccessMode.INC
    RW = AccessMode.RW

    sys = SystemSpecification()

    gam = sys.create_const_global("gam", DataType(shape=(1, )))
    gm1 = sys.create_const_global("gm1", DataType(shape=(1, )))
    gm1i = sys.create_const_global("gm1i", DataType(shape=(1, )))
    m2 = sys.create_const_global("m2", DataType(shape=(1, )))
    wtg1 = sys.create_const_global("wtg1", DataType(shape=(2, )))
    xi1 = sys.create_const_global("xi1", DataType(shape=(2, )))
    Ng1 = sys.create_const_global("Ng1", DataType(shape=(4, )))
    Ng1_xi = sys.create_const_global("Ng1_xi", DataType(shape=(4, )))
    wtg2 = sys.create_const_global("wtg2", DataType(shape=(4, )))
    Ng2 = sys.create_const_global("Ng2", DataType(shape=(16, )))
    Ng2_xi = sys.create_const_global("Ng2_xi", DataType(shape=(32, )))
    minf = sys.create_const_global("minf", DataType(shape=(1, )))
    freq = sys.create_const_global("freq", DataType(shape=(1, )))
    kappa = sys.create_const_global("kappa", DataType(shape=(1, )))
    nmode = sys.create_const_global("nmode", DataType(shape=(1, )))
    mfan = sys.create_const_global("mfan", DataType(shape=(1, )))

    c1 = sys.create_mutable_global("c1", DataType(shape=(1, )))
    c2 = sys.create_mutable_global("c2", DataType(shape=(1, )))
    alpha = sys.create_mutable_global("alpha", DataType(shape=(1, )))
    c3 = sys.create_mutable_global("c3", DataType(shape=(1, )))
    beta = sys.create_mutable_global("beta", DataType(shape=(1, )))
    rms = sys.create_mutable_global("rms", DataType(shape=(1, )))

    iter = sys.create_mutable_global("iter",
                                     DataType(dtype=numpy.uint32, shape=(1, )))
    iterm1 = sys.create_mutable_global(
        "iterm1", DataType(dtype=numpy.uint32, shape=(1, )))
    res0 = sys.create_mutable_global("res0")
    res = sys.create_mutable_global("res")
    inner_iter = sys.create_mutable_global("inner_iter",
                                           DataType(dtype=numpy.uint32))
    maxiter = sys.create_mutable_global("maxiter",
                                        DataType(dtype=numpy.uint32))

    cells = sys.create_set("cells")
    bedges = sys.create_set("bedges")
    nodes = sys.create_set("nodes")

    p_xm = sys.create_dat(nodes, "p_x", DataType(shape=(2, )))
    p_phim = sys.create_dat(nodes, "p_phim", DataType(shape=(1, )))
    p_resm = sys.create_dat(nodes, "p_resm", DataType(shape=(1, )))
    p_K = sys.create_dat(cells, "p_K", DataType(shape=(16, )))
    p_V = sys.create_dat(nodes, "p_V", DataType(shape=(1, )))
    p_P = sys.create_dat(nodes, "p_P", DataType(shape=(1, )))
    p_U = sys.create_dat(nodes, "p_U", DataType(shape=(1, )))

    pbedge = sys.create_map("pbedge", bedges, nodes, 1)
    pcell = sys.create_map("pcell", cells, nodes, 4)

    inst = load_hdf5_instance(sys, srcFile)
    refFile = h5py.File(srcFile)

    code = RepeatForCount(
        total_iter, iterm1, """
        iter[0]=iterm1[0]+1 # Original code uses 1 base start
        """,
        ParFor(res_calc, cells, Ng2_xi(READ), wtg2(READ), gm1(READ),
               gm1i(READ), m2(READ), p_xm(READ, pcell, -4),
               p_phim(READ, pcell, -4), p_K(WRITE), p_resm(INC, pcell, -4)),
        CheckState(refFile, "/output/iter{iter}_post_res_calc"),
        ParFor(dirichlet, bedges, p_resm(WRITE, pbedge, 0)),
        CheckState(refFile, "/output/iter{iter}_post_dirichlet"), """
        c1[0]=0
        c2[0]=0
        c3[0]=0
        alpha[0]=0
        beta[0]=0
        """,
        ParFor(init_cg, nodes, p_resm(READ), c1(INC), p_U(WRITE), p_V(WRITE),
               p_P(WRITE)),
        CheckState(refFile, "/output/iter{iter}_post_init_cg"), """
        #print("  c1 = %g" % (c1[0]) )
        res0[0]=sqrt(c1[0])
        res[0]=res0[0]
        inner_iter[0]=0
        maxiter[0]=200
        #print("res=%g, 0.1*res0=%g,  inner_iter=%u, maxiter=%u" % (res[0],0.1*res0[0], inner_iter[0], maxiter[0]) )
        """,
        While(
            """ (res[0]>0.1*res0[0]) and inner_iter[0] < maxiter[0] """,
            """fprintf_stderr("  res=%g, 0.1*res0=%g,  inner_iter=%u, maxiter=%u, outer_iter=%u", res[0],0.1*res0[0], inner_iter[0], maxiter[0], iter[0])""",
            ParFor(spMV, cells, p_V(INC, pcell, -4), p_K(READ),
                   p_P(READ, pcell, -4)),
            CheckState(refFile,
                       "/output/iter{iter}_inner{inner_iter}_post_spMV"),
            ParFor(dirichlet, bedges, p_V(WRITE, pbedge, 0)),
            CheckState(refFile,
                       "/output/iter{iter}_inner{inner_iter}_post_dirichlet"),
            """
            c2[0]=0
            """, ParFor(dotPV, nodes, p_P(READ), p_V(READ), c2(INC)),
            CheckState(refFile,
                       "/output/iter{iter}_inner{inner_iter}_post_dotPV"), """
            alpha[0]=c1[0]/c2[0]
            """,
            ParFor(updateUR, nodes, p_U(INC), p_resm(INC), p_P(READ), p_V(RW),
                   alpha(READ)),
            CheckState(refFile,
                       "/output/iter{iter}_inner{inner_iter}_post_updateUR"),
            """
            c3[0]=0
            """, ParFor(dotR, nodes, p_resm(READ), c3(INC)),
            CheckState(refFile,
                       "/output/iter{iter}_inner{inner_iter}_post_dotR"), """
            beta[0]=c3[0]/c1[0]
            """, ParFor(updateP, nodes, p_resm(READ), p_P(RW), beta(READ)), """
            c1[0] = c3[0]
            res[0] = sqrt(c1[0])
            inner_iter[0]+=1
            """, CheckState(refFile, "/output/iter{iter}_inner{inner_iter}")),
        """
        rms[0]=0
        """,
        ParFor(update, nodes, p_phim(RW), p_resm(WRITE), p_U(READ),
               rms(INC)), """
        rmsV=sqrt(rms[0])/sqrt(sizeof_nodes[0])
        iterV=iter[0]
        # handler_log(2, "rms = %10.5g iter: %d" % ( rmsV, iterV) )
        """, CheckState(refFile, "/output/iter{iter}"))
    code.on_bind_spec(sys)
    return (sys, inst, code)
Esempio n. 5
0
def build_system(srcFile:str="../airfoil/new_grid.h5", maxiter=1000) -> (SystemInstance,Statement):

    WRITE=AccessMode.WRITE
    READ=AccessMode.READ
    INC=AccessMode.INC
    RW=AccessMode.RW
    
    sys=SystemSpecification()

    gam=sys.create_const_global("gam", DataType(shape=(1,)))
    gm1=sys.create_const_global("gm1", DataType(shape=(1,)))
    eps=sys.create_const_global("eps", DataType(shape=(1,)))
    cfl=sys.create_const_global("cfl", DataType(shape=(1,)))
    qinf=sys.create_const_global("qinf",DataType(shape=(4,)))

    rms=sys.create_mutable_global("rms", DataType(shape=(1,)))
    iterm1=sys.create_mutable_global("iterm1",DataType(shape=(1,),dtype=numpy.uint32))
    iter=sys.create_mutable_global("iter",DataType(shape=(1,),dtype=numpy.uint32))
    k=sys.create_mutable_global("k",DataType(shape=(1,),dtype=numpy.uint32))
    
    cells=sys.create_set("cells")
    edges=sys.create_set("edges")
    bedges=sys.create_set("bedges")
    nodes=sys.create_set("nodes")
    
    sizeof_cells=sys.get_sizeof_set(cells)

    p_x=sys.create_dat(nodes, "p_x", DataType(shape=(2,)))
    p_q=sys.create_dat(cells, "p_q", DataType(shape=(4,)))
    p_qold=sys.create_dat(cells, "p_qold", DataType(shape=(4,)))
    p_adt=sys.create_dat(cells, "p_adt", DataType(shape=(1,)))
    p_res=sys.create_dat(cells, "p_res", DataType(shape=(4,)))
    p_bound=sys.create_dat(bedges, "p_bound", DataType(dtype=numpy.int,shape=(1,)))

    pcell=sys.create_map("pcell", cells, nodes, 4)
    pedge=sys.create_map("pedge", edges, nodes, 2)
    pbedge=sys.create_map("pbedge", bedges, nodes, 2)
    pecell=sys.create_map("pecell", edges, cells, 2)
    pbecell=sys.create_map("pbecell", bedges, cells, 1)

    inst=load_hdf5_instance(sys,srcFile)

    refFile=h5py.File(srcFile)
        

    print_iter=0

    def debug_post_adt(instance:SystemInstance):
        global print_iter
        vals=instance.dats[p_adt]
        for i in range(instance.sets[cells]):
            print('<CP dev="c{}" key="post-adt-{}">"adt": {}</CP>'.format(i,print_iter,vals[i]))

    def debug_pre_update(instance:SystemInstance):
        global print_iter
        vals=instance.dats[p_res]
        for i in range(instance.sets[cells]):
            print('<CP dev="c{}" key="pre-update-{}">"res": {}</CP>'.format(i,print_iter,vals[i]))
        
    def debug_post_update(instance:SystemInstance):
        global print_iter
        vals=instance.dats[p_q]
        for i in range(instance.sets[cells]):
            print('<CP dev="c{}" key="post-update-{}">"q": {}</CP>'.format(i,print_iter,vals[i]))
        print_iter+=1

        

    code=RepeatForCount(maxiter,iterm1,
        """
        iter[0]=iterm1[0]+1 # Original code uses 1-based for loops
        """,
        ParFor(
            save_soln,
            cells,
            p_qold(WRITE),
            p_q(READ)
        ),
        RepeatForCount(2, k,        
            ParFor(
                adt_calc,
                cells,
                gam(READ), gm1(READ), cfl(READ),
                p_x(READ,pcell,0),
                p_x(READ,pcell,1),
                p_x(READ,pcell,2),
                p_x(READ,pcell,3),
                p_q(RW),
                p_adt(WRITE)
            ),
            #Debug(
            #    debug_post_adt
            #),
            ParFor(
                res_calc,
                edges,
                gm1(READ), eps(READ),
                p_x(READ,pedge,0),
                p_x(READ,pedge,1),
                p_q(READ,pecell,0),
                p_q(READ,pecell,1),
                p_adt(READ,pecell,0),
                p_adt(READ,pecell,1),
                p_res(INC,pecell,0),
                p_res(INC,pecell,1)
            ),
            ParFor(
                bres_calc,
                bedges,
                eps(READ), qinf(READ), gm1(READ), 
                p_bound(READ),
                p_x(READ,pbedge,0),
                p_x(READ,pbedge,1),
                p_q(READ,pbecell),
                p_adt(READ,pbecell),
                p_res(INC,pbecell)
            ),
            #Debug(
            #    debug_pre_update
            #),
            #UserCode(
            #    reset_rms_to_zero,
            #    rms(RW)
            #),
            """
            rms[0]=0
            """,
            ParFor(
                update,
                cells,
                p_qold(READ),
                p_q(RW),
                p_res(RW),
                p_adt(READ),
                rms(INC)
            ),
            #Debug(
            #    debug_post_update
            #)
            CheckState(refFile, "/output/iter{iter}_k{k}")
        ),
        """
        #handler_log(4, " %d  %10.5e ", iter[0], sqrt(rms[0] / sizeof_cells[0] ))
        #if (iter[0]%100)==0:
        #    handler_log(3, " %d  %10.5e ", iter[0], sqrt(rms[0] / sizeof_cells[0] ))
        rms[0]=rms[0]
        """
    )
    code.on_bind_spec(sys)
    return (sys,inst,code)