Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/ein16/synth/d2/fold.py
ViewVC logotype

View of /branches/ein16/synth/d2/fold.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4483 - (download) (as text) (annotate)
Fri Sep 2 05:07:01 2016 UTC (2 years, 6 months ago) by cchiw
File size: 18885 byte(s)
shape based iteration
import sys
import re
import os
import time

from obj_apply import *
from obj_ex import  *
from obj_field import *
from obj_counter import *
from obj_frame import  *

from test_createField import createField
from test_writeDiderot import writeDiderot
from test_eval import eval
from test_compare import compare
from test_observed import observed

from core import *
from write import *
from var_ty import *
from frame import  *
from shape import *


##################################################################################################
# print types on list
def pes(pre, es):
    n = len(es)
    l = pre+" length: "+str(n)+" : "
    for i in es:
        if(i==None):
            l+=" None,"
        else:
            l+=(i.name)+","
    #print l



##################################################################################################
##################################################################################################
# shape1 : shapes (ty_vecT, ty_matT) not a type
# ty1 : types with specific shapes (ty_vecFT, ty_vecF_d1..)
# exp1: specific types on ty1s list ^

def core_ops(exps, dim, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt):
    # can apply (shape1*shape2)* shape3 ->tshape2
    # assuming shape_to_fty() is ordered by dimension
    counter.inc_total(cnt)
    convert_fields(exps, testing_frame)
    create_embed_app_passed(exps, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)
    return



# inner operation is binary
# outer operation is unary
def core_binary_unary(exp1, exp2, dim, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt):
    #print "\n--------------------------- core_binary_unary  ---------------------------"
    exps =  [exp1,  exp2]
    core_ops(exps, dim, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)
    return


##################################################################################################
##################################################################################################

# all of the type choosing split into multiple function
# iterate over third type
def pick_ty_to_exp3(exps, dim, ty3s, tshape1, tshape2_shape, opr_inner, opr_outer, title, testing_frame, cnt):
    # terating over tyes that are possible
    #print "\n---------------------------pick_third_type ---------------------------"
    # can apply (shape1*shape2)* shape3 ->tshape2
    # assuming shape_to_fty() is ordered by dimension
    [t3, f3d1, f3d2, f3d3] = ty3s
    # now get exp3
    def instance(exp3, dim):
        if(exp3 == None):
            return
        else:
            t = exps+[exp3]
            #print "tshape1:", tshape1, "tshape2_shape:",tshape2_shape
            tshape2 = convert_rst_binary(tshape1, exp3, tshape2_shape, dim)
            if(not(tshape2 == None)):
                # both operations are binary so there are three expressions
                core_ops(t, dim, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)
                return
    if(dim==0):
        # tensors for exp1,exp2
        for exp3 in ty3s:
            instance(exp3, dim)
            dim+=1
    else:
        # choose a field as first|second argument so the rest of the choices must have same dimension
        exp3s = [t3, ty3s[dim]]
        for exp3 in exp3s:
            instance(exp3, dim)
        return


#opr_inner arity == 2,  opr_outer.arity == _
def pick_ty_to_exp2(exp1, dim, ty2s, ty3s, tshape1_shape, tshape2_shape, opr_inner, opr_outer, title, testing_frame, cnt):
    # terating over tyes that are possible
    #writeCumulative(cnt)
    #print "\n---------------------------pick_second_type ---------------------------"
    #m = title + "Types: ("+exp1.name+", _)"
    #writenow(m)
    title = ""

    # can apply (shape1*shape2)* shape3 ->tshape2
    # assuming shape_to_fty() is ordered by dimension
    [t2, f2d1, f2d2, f2d3] = ty2s
   
   # run single instance
    def instance(exp2, dim):
        if(exp2 == None):
            return
        else:
            # current dim: dimension should be based on exp1*exp2
            #print "  mark: convert_rst_binary"
            tshape1 = convert_rst_binary(exp1, exp2, tshape1_shape, dim)
            if(not (tshape1==None)):
                 # then we iterate over tys3s
                if (opr_outer.arity == 2):
                     # opr_outer.arity == 2, opr_inner arity == 2
                     
                    # so then we do iterate over tys3s
                    exps = [exp1, exp2]
                    pick_ty_to_exp3(exps, dim, ty3s, tshape1, tshape2_shape, opr_inner, opr_outer, title, testing_frame, cnt)
            else:
                # opr_outer.arity == 1,opr_inner arity == 2
                
                 # third type not needed
                 ## need to check actual expression
                 #print "  mark: check_tshape"
                 #print "pre",tshape1,"op1",opr_outer.name
                # converts shape to type (tshape2_shape)
                # check to see if it is expression (tshape1) is a  differentiable field (if needed)
                tshape2 = check_tshape(opr_outer,  tshape1, tshape2_shape, dim)
                if(not(tshape2 == None)):
                    #print "can not be differentiated"
                    # go straigh to core
                    #print "dim", dim, "post",tshape1.name
                    core_binary_unary(exp1, exp2, dim, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)

    # run multiple instance
    def iter(exp2s, dim):
        # [tensor, field,_]
        for exp2 in exp2s:
            instance(exp2, dim)
    # choose t1 as first argument
    if(dim==0):
        # [tensor,_, _]
        # [tensor, tensor,_]
        exp2 = t2
        instance(exp2, dim)
        # [tensor, field,_]
        # create second field type
        for d in range(3):
            d+=1
            exp2 = ty2s[d]
            instance(exp2, d)
    else:
        #[field, _,_]
        exp2s = [t2, ty2s[dim]]
        iter(exp2s, dim)


##################################################################################################
# iterate over third type
def backwards_ty_to_exp2(exp1, dim, ty2s, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt):
    # terating over tyes that are possible
    #writeCumulative(cnt)
    #print "\n---------------------------pick_second_type ---------------------------"
    #m = title + "Types: ("+exp1.name+", _)"
    #writenow("Types: ("+exp1.name+", _)")
    title = ""
    # can apply (shape1*shape2)* shape3 ->tshape2
    # assuming shape_to_fty() is ordered by dimension
    [t2, f2d1, f2d2, f2d3] = ty2s
    #print "t2:",t2, "f2d1",f2d1
    # run single instance
    def instance(exp2, dim):
        if(exp2 == None):
            return
        else:
            #print "\n\t exp2"+exp2.name
            core_binary_unary(exp1, exp2, dim, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)
            return

    # run multiple instance
    def iter(exp2s, dim):
        # [tensor, field,_]
        for exp2 in exp2s:
            #print "\n\t exp2-attempt"
            instance(exp2, dim)
    # choose t1 as first argument
    if(dim==0):
        #print "dim=0"
        # [tensor,_, _]
        # [tensor, tensor,_]
        exp2 = t2
        instance(exp2, dim)
        # [tensor, field,_]
        # create second field type
        for d in range(3):
            d+=1
            exp2 = ty2s[d]
            instance(exp2, d)
    else:
        #[field, _,_]
        #print "dimmorethan 0"
        exp2s = [t2, ty2s[dim]]
        iter(exp2s, dim)

def backwards_ty_to_exp1(ty1s, dim, exp2, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt):
    # terating over tyes that are possible
    #writeCumulative(cnt)
    #print "\n---------------------------pick_second_type ---------------------------"
    #m = title + "Types: ("+exp1.name+", _)"
    #writenow(m)
    title = ""
    # can apply (shape1*shape2)* shape3 ->tshape2
    # assuming shape_to_fty() is ordered by dimension
    [t2, f2d1, f2d2, f2d3] = ty1s
    
    # run single instance
    def instance(exp1, dim):
        if(exp1 == None):
            return
        else:
            core_binary_unary(exp1, exp2, dim, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)
            return

    # run multiple instance
    def iter(exp1s, dim):
        # [tensor, field,_]
        for exp1 in exp1s:
            instance(exp1, dim)
        # choose t1 as first argument
    if(dim==0):
        # [tensor,_, _]
        # [tensor, tensor,_]
        exp1 = t2
        instance(exp1, dim)
        # [tensor, field,_]
        # create second field type
        for d in range(3):
            d+=1
            exp2 = ty1s[d]
            instance(exp1, d)
    else:
        #[field, _,_]
        exp1s = [t2, ty1s[dim]]
        iter(exp1s, dim)



##################################################################################################
# iterate over third type
def pick_ty_to_exp1(ty1s, ty2s, ty3s, tshape1_shape, tshape2_shape, opr_inner, opr_outer, title, testing_frame, cnt):
    # terating over tyes that are possible
    #writeCumulative(cnt)
    #print "\n---------------------------pick_first_type ---------------------------"
    # can apply (shape1*shape2)* shape3 ->tshape2
    # assuming shape_to_fty() is ordered by dimension
    dim = 0
    for exp1 in ty1s:
         #[t1, f1d1, f1d2, f1d3] = ty1s
        if(not(exp1 == None)):
            # expressions must be created
            pick_ty_to_exp2(exp1, dim, ty2s, ty3s, tshape1_shape, tshape2_shape, opr_inner, opr_outer, title, testing_frame, cnt)
            dim+=1
##################################################################################################
##################################################################################################
# shape1 : shapes (ty_vecT)
# ty1 : types with specific shapes (ty_vecFT, ty_vecF_d1..)
# exp1: specific types on ty1s list ^

# apply second operation
def pick_get_ty3s(ty1s, ty2s, rtn3s, tshape1_shape, opr_inner, opr_outer, title, testing_frame, cnt):
    # can apply (shape1*shape2)* shape3 ->tshape2
    titlec ="\n\t"
    for (shape3, tshape2_shape) in rtn3s:
        #print "-------------"
        tmp = ""
        tmp = title+ " * "+shape3.name+")"
        #writenow("\n\t---Shape3:("+tmp)
        #writenow("\n shape3: "+shape3.name+"  tshape2_shape: "+tshape2_shape.name)
        # convert shape to type
        ty3s = shape_to_fty(shape3)
        #pes("ty3s", ty3s)
        #make sure non-tmpty third types
        if (len(ty3s)>3):
            pick_ty_to_exp1(ty1s, ty2s, ty3s, tshape1_shape, tshape2_shape, opr_inner, opr_outer, titlec, testing_frame, cnt)
        else:
            # no shapes make this combination of operations possible
            continue



# main function
def pick_get_ty2s(ty1s, rtn2s, opr_inner, opr_outer, title, testing_frame, cnt):
    # print " --------------second  time---------------"
    # print  "opr_inner:", opr_inner.name, "opr_outer:", opr_outer.name
    titlec ="\n\t"

    for (shape2, tshape1_shape) in rtn2s:
        #print "------------------------------"
        # convert shape to type
        ty2s = shape_to_fty(shape2)
        # pes("ty2s", ty2s)
        tmp = title+ " * "+shape2.name+")"
        #writenow("\n\t--Shape2:("+tmp)
        # Do we need  a third type?
        if (opr_outer.arity == 2):
            # yes if outer operation is binary
            rtn3s = op2_to_shape(opr_outer, tshape1_shape)
            if (len(rtn3s)>0):
                pick_get_ty3s(ty1s, ty2s, rtn3s, tshape1_shape, opr_inner, opr_outer, tmp, testing_frame, cnt)
            else:
                 # no shapes make this combination of operations possible
                continue
        else:
            # outer operation is a unary
            tshape2_shape = op1_to_shape(opr_outer, tshape1_shape)
            if(not (tshape2_shape == None)):
                ty3s = None
                pick_ty_to_exp1(ty1s, ty2s, ty3s, tshape1_shape, tshape2_shape, opr_inner, opr_outer, titlec, testing_frame, cnt)
    return

  #(opr_outer.arity == 1 and opr_inner.arity == 1)
def iter_unary_unary(ty1s, shape1, opr_inner, opr_outer, title, testing_frame, cnt):
    tshape1_shape = op1_to_shape(opr_inner, shape1)
    # print "opr_outer.arity == 1 and opr_inner.arity == 1"
    if(not (tshape1_shape == None)):
        #apply unary
        tshape2_shape = op1_to_shape(opr_outer, tshape1_shape)
        if(not (tshape2_shape == None)):
            # single argument
            for exp1 in ty1s:
                if(not (exp1==None)):
                    tshape1 = check_tshape(opr_inner, exp1, tshape1_shape, exp1.dim)
                    if(not (tshape1 == None)):
                        dim = tshape1.dim
                        tshape2 = check_tshape(opr_inner, tshape1,  tshape2_shape, dim)
                        if(not (tshape2 == None)):
                            exps = [exp1]
                            core_ops(exps, dim, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)

  #(opr_outer.arity == 2 and opr_inner.arity == 1)
def iter_binary_unary(ty1s, shape1, opr_inner, opr_outer, title, testing_frame, cnt):
    #(opr_outer.arity == 2 and opr_inner.arity == 1)
    # print "binary_unary opr_inner.arity == 1 and pr_outer.arity == 2 "
    # apply unary
    #pes("ty1s",ty1s)
    #print " shape1: ", shape1.name
    tshape1_shape = op1_to_shape(opr_inner, shape1)
    #print "tshape1_shap:",tshape1_shape.name
    if(not (tshape1_shape == None)):
        #apply binary
        rtn3s = op2_to_shape(opr_outer, tshape1_shape)
        #print "rtn3s:",len(rtn3s)
        for (shape3, tshape2_shape) in rtn3s:
            # convert shape to type
            ty3s = shape_to_fty(shape3)
            #pes("ty3s",ty3s)
            # single argument
            if ((len(ty3s)>0) and  (len(ty1s)>0)):
                for exp1 in ty1s:
                    if(not (exp1==None)):
                        dim = exp1.dim
                        exps = [exp1]
                        tshape1 = check_tshape(opr_inner,  exp1, tshape1_shape, dim)
                        if(not (tshape1 == None)):
                            pick_ty_to_exp3(exps, dim, ty3s, tshape1, tshape2_shape, opr_inner, opr_outer, title, testing_frame, cnt)
        



# main function
def pick_get_ty1s(shape1, opr_inner, opr_outer, testing_frame, cnt):
    # apply operation a first time
    #print " --------------first time---------------"
    #print  "\t\topr_inner:", opr_inner.name, opr_inner.arity,"\n\topr_outer:", opr_outer.name,opr_outer.arity
    ty1s = shape_to_fty(shape1)
    title = "("+shape1.name
    #writenow("\n-Shape1:"+title+" * _)")
    #print "pick_get_ty1s","shape1",shape1.name
    #pes("ty1s",ty1s)
    #print "attempt:"+opr_inner.name+"shape1", shape1.name
    if(opr_inner.arity == 1 and opr_outer.arity == 1): # arity is 1
        iter_unary_unary(ty1s, shape1, opr_inner, opr_outer, title, testing_frame, cnt)
    elif(opr_outer.arity == 2 and opr_inner.arity == 1):
        iter_binary_unary(ty1s, shape1, opr_inner, opr_outer, title, testing_frame, cnt)
    else:
        rtn2s = op2_to_shape(opr_inner, shape1)
        if (len(rtn2s)>0):
            pick_get_ty2s(ty1s, rtn2s, opr_inner, opr_outer, title, testing_frame, cnt)
        else:
            return
    return
##################################################################################################

# go backwards
def go_backwards(opr_outer, opr_inner, testing_frame, cnt):
    rst2  =  expected_inshape(opr_outer)
    # assumes binary outer
    # assume unary inner
    for  (tshape1, tshape2) in rst2:
        # (_,_)-> tshape
        dim = tshape2.dim
        # set one of the expressions to be a field
        ty1s = d_tofield(dim)
         # (exp1, _)-> tshapes
         # print "##############################"
        # print "dim: ", dim, "tshape1:",tshape1.name, "tshape2:",tshape2.name
        # arity is 2
        for exp  in ty1s:
            # needs to find 2nd argument
            #print "#############"
            #print "exp:",exp.name
            exp1 = exp
            shape2 = backwards_op2_arg1(opr_inner, tshape1, exp1)
            if(not(shape2==None)):
                title= "\t\tex.(exp1:"+exp1.name+", shape2:"+shape2.name+")-> "+tshape2.name
                #print(title)
                # convert shape to type
                ty2s = shape_to_fty(shape2)
                #print "(shape2",shape2.name,"ty2s",ty2s
                backwards_ty_to_exp2(exp1, dim, ty2s, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)
            exp2 = exp
            shape1 = backwards_op2_arg2(opr_inner, tshape1, exp1)
            if(not (shape1==None)):
                title= "\t\tex.(shape1:"+shape1.name+", exp2:"+exp2.name+")-> "+tshape2.name
                #print(title)
                # convert shape to type
                # gets duplicated
                #ty1s = shape_to_fty(shape1)
                #backwards_ty_to_exp1(ty1s, dim, exp2, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)
                exp1 = shape_to_tensor(shape1)
                core_binary_unary(exp1, exp2, dim, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)



##################################################################################################
##################################################################################################
# main function

#handles some backwards
def pick(opr_inner, opr_outer, testing_frame, cnt):
    start = time.time()
    
    #writeTitle_outer(opr_inner, opr_outer)
    # iterate over shapes for first terms
    backwards= False
    if(opr_outer.fieldop and backwards):
        go_backwards(opr_outer, opr_inner, testing_frame, cnt)
    #print "backwards"
    elif(opr_inner.arity == 1 and backwards):
            #print "backwards"
        go_backwards(opr_outer, opr_inner, testing_frame, cnt)
        #print "backwards"
    else:
        shapes = tvs+tms+tt2+tt3+tt4
        for shape1 in shapes:
            #print "shape1 ", shape1
            pick_get_ty1s(shape1, opr_inner, opr_outer, testing_frame, cnt)
            #print "forwards"
    writeResults_outer(opr_inner, opr_outer, testing_frame, cnt)
    end = time.time()
    t ="time:"+str(end - start)
    writeall(t)
    print (t)
    return

#does everything forward
def pick2(opr_inner, opr_outer, testing_frame, cnt):
    #writeTitle_outer(opr_inner, opr_outer)
    # iterate over shapes for first terms
    shapes = tvs+tms+tt2+tt3+tt4
    for shape1 in shapes:
        pick_get_ty1s(shape1, opr_inner, opr_outer, testing_frame, cnt)
    writeResults_outer(opr_inner, opr_outer, testing_frame, cnt)
    writeCumulative(cnt)
    return

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0