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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4483, Fri Sep 2 05:07:01 2016 UTC revision 4484, Fri Sep 2 20:10:56 2016 UTC
# Line 1  Line 1 
1  import sys  
2  import re  import re
3  import os  import os
4  import time  import time
5    
6  from obj_apply import *  
 from obj_ex import  *  
7  from obj_field import *  from obj_field import *
8  from obj_counter import *  from obj_counter import *
9  from obj_frame import  *  from obj_frame import  *
10    
 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  
   
11  from core import *  from core import *
12  from write import *  from write import *
13  from var_ty import *  from var_ty import *
14  from frame import  *  from frame import  *
15  from shape import *  from shape import *
16    from shape_backwards import *
17    
18  ##################################################################################################  ##################################################################################################
19  # print types on list  # print types on list
# Line 79  Line 72 
72          else:          else:
73              t = exps+[exp3]              t = exps+[exp3]
74              #print "tshape1:", tshape1, "tshape2_shape:",tshape2_shape              #print "tshape1:", tshape1, "tshape2_shape:",tshape2_shape
75              tshape2 = convert_rst_binary(tshape1, exp3, tshape2_shape, dim)              tshape2 = check2_tshape(opr_outer, tshape1, exp3, tshape2_shape, dim)
76              if(not(tshape2 == None)):              if(not(tshape2 == None)):
77                  # both operations are binary so there are three expressions                  # both operations are binary so there are three expressions
78                  core_ops(t, dim, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)                  core_ops(t, dim, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)
# Line 117  Line 110 
110          else:          else:
111              # current dim: dimension should be based on exp1*exp2              # current dim: dimension should be based on exp1*exp2
112              #print "  mark: convert_rst_binary"              #print "  mark: convert_rst_binary"
113              tshape1 = convert_rst_binary(exp1, exp2, tshape1_shape, dim)              tshape1 = check2_tshape(opr_inner, exp1, exp2, tshape1_shape, dim)
114              if(not (tshape1==None)):              if(not (tshape1==None)):
115                   # then we iterate over tys3s                   # then we iterate over tys3s
116                  if (opr_outer.arity == 2):                  if (opr_outer.arity == 2):
# Line 131  Line 124 
124    
125                   # third type not needed                   # third type not needed
126                   ## need to check actual expression                   ## need to check actual expression
127                   #print "  mark: check_tshape"                   #print "  mark: check1_tshape"
128                   #print "pre",tshape1,"op1",opr_outer.name                   #print "pre",tshape1,"op1",opr_outer.name
129                  # converts shape to type (tshape2_shape)                  # converts shape to type (tshape2_shape)
130                  # check to see if it is expression (tshape1) is a  differentiable field (if needed)                  # check to see if it is expression (tshape1) is a  differentiable field (if needed)
131                  tshape2 = check_tshape(opr_outer,  tshape1, tshape2_shape, dim)                  tshape2 = check1_tshape(opr_outer,  tshape1, tshape2_shape, dim)
132                  if(not(tshape2 == None)):                  if(not(tshape2 == None)):
133                      #print "can not be differentiated"                      #print "can not be differentiated"
134                      # go straigh to core                      # go straigh to core
# Line 303  Line 296 
296      # print " --------------second  time---------------"      # print " --------------second  time---------------"
297      # print  "opr_inner:", opr_inner.name, "opr_outer:", opr_outer.name      # print  "opr_inner:", opr_inner.name, "opr_outer:", opr_outer.name
298      titlec ="\n\t"      titlec ="\n\t"
299        n = len(op_all)
300        k_init = 2
301      for (shape2, tshape1_shape) in rtn2s:      for (shape2, tshape1_shape) in rtn2s:
302          #print "------------------------------"          #print "------------------------------"
303          # convert shape to type          # convert shape to type
304          ty2s = shape_to_fty(shape2)          ty2s = shape_to_fty(shape2, k_init)
305          # pes("ty2s", ty2s)          # pes("ty2s", ty2s)
306          tmp = title+ " * "+shape2.name+")"          tmp = title+ " * "+shape2.name+")"
307          #writenow("\n\t--Shape2:("+tmp)          #writenow("\n\t--Shape2:("+tmp)
308          # Do we need  a third type?          # Do we need  a third type?
309            def sort(opr_outer):
310          if (opr_outer.arity == 2):          if (opr_outer.arity == 2):
311              # yes if outer operation is binary              # yes if outer operation is binary
312              rtn3s = op2_to_shape(opr_outer, tshape1_shape)              rtn3s = op2_to_shape(opr_outer, tshape1_shape)
313              if (len(rtn3s)>0):              if (len(rtn3s)>0):
314                  pick_get_ty3s(ty1s, ty2s, rtn3s, tshape1_shape, opr_inner, opr_outer, tmp, testing_frame, cnt)                  pick_get_ty3s(ty1s, ty2s, rtn3s, tshape1_shape, opr_inner, opr_outer, tmp, testing_frame, cnt)
315              else:              else:
                  # no shapes make this combination of operations possible  
                 continue  
         else:  
316              # outer operation is a unary              # outer operation is a unary
317              tshape2_shape = op1_to_shape(opr_outer, tshape1_shape)              tshape2_shape = op1_to_shape(opr_outer, tshape1_shape)
318              if(not (tshape2_shape == None)):              if(not (tshape2_shape == None)):
319                  ty3s = None                  ty3s = None
320                  pick_ty_to_exp1(ty1s, ty2s, ty3s, tshape1_shape, tshape2_shape, opr_inner, opr_outer, titlec, testing_frame, cnt)                  pick_ty_to_exp1(ty1s, ty2s, ty3s, tshape1_shape, tshape2_shape, opr_inner, opr_outer, titlec, testing_frame, cnt)
321    
322    
323            if(opr_outer==None):
324                for  t_outer in range(n):
325                    startyy = time.time()
326                    opr_outer = id_toOpr(t_outer)
327                    counter.zero_locals(cnt)
328                    counter.zero_total(cnt)
329                    writeTitle_outer(opr_inner, opr_outer)
330                    sort(opr_outer)
331                    endyy = time.time()
332                    tty = "  time-outer:"+str(endyy - startyy)
333                    writeall(tty)
334                    print (tty)
335            else:
336                sort(opr_outer)
337      return      return
338    
339    #(opr_outer.arity == 1 and opr_inner.arity == 1)    #(opr_outer.arity == 1 and opr_inner.arity == 1)
340  def iter_unary_unary(ty1s, shape1, opr_inner, opr_outer, title, testing_frame, cnt):  def iter_inner_unary(ty1s, shape1, opr_inner, opr_outer, title, testing_frame, cnt):
341        def iter_unary_unary(opr_outer):
342      tshape1_shape = op1_to_shape(opr_inner, shape1)      tshape1_shape = op1_to_shape(opr_inner, shape1)
343      # print "opr_outer.arity == 1 and opr_inner.arity == 1"          print "------------------------------------------------------"
344      if(not (tshape1_shape == None)):          print "opr_outer.arity == 1 and opr_inner.arity == 1"
345          #apply unary          print "inner", opr_inner.name, "outer", opr_outer.name
346          tshape2_shape = op1_to_shape(opr_outer, tshape1_shape)          print "\n apply inner operator", opr_inner.name, "on shape:", shape1.name, tshape1_shape
         if(not (tshape2_shape == None)):  
             # single argument  
347              for exp1 in ty1s:              for exp1 in ty1s:
348                  if(not (exp1==None)):                  if(not (exp1==None)):
349                      tshape1 = check_tshape(opr_inner, exp1, tshape1_shape, exp1.dim)                  print "-------------------        ----"
350                    print "exp1:", exp1.name
351                    tshape1 = check1_tshape(opr_inner, exp1, tshape1_shape, exp1.dim)
352    
353                      if(not (tshape1 == None)):                      if(not (tshape1 == None)):
354                        print "tshape1: ", tshape1.name
355                          dim = tshape1.dim                          dim = tshape1.dim
356                          tshape2 = check_tshape(opr_inner, tshape1,  tshape2_shape, dim)                      tshape1_shape = tshape1.tensorType
357                        print "\n apply outer operator", opr_outer.name, "on shape:", tshape1_shape.name
358                        tshape2_shape = op1_to_shape(opr_outer, tshape1_shape)
359                        if(not (tshape2_shape == None)):
360                            print "\n checking outer op->"+tshape2_shape.name
361                            tshape2 = check1_tshape(opr_outer, tshape1,  tshape2_shape, dim)
362                            print "\n checking outer op tshape1:", tshape1.name,"tshape2-shape:",tshape2_shape.name, "dim",dim, "tshape2:", tshape2
363                          if(not (tshape2 == None)):                          if(not (tshape2 == None)):
364                                print "tshape2: ", tshape2.name
365                              exps = [exp1]                              exps = [exp1]
366                              core_ops(exps, dim, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)                              core_ops(exps, dim, tshape1, tshape2, opr_inner, opr_outer, title, testing_frame, cnt)
367    
368    #(opr_outer.arity == 2 and opr_inner.arity == 1)    #(opr_outer.arity == 2 and opr_inner.arity == 1)
369  def iter_binary_unary(ty1s, shape1, opr_inner, opr_outer, title, testing_frame, cnt):      def iter_binary_unary(opr_outer):
370      #(opr_outer.arity == 2 and opr_inner.arity == 1)      #(opr_outer.arity == 2 and opr_inner.arity == 1)
371      # print "binary_unary opr_inner.arity == 1 and pr_outer.arity == 2 "      # print "binary_unary opr_inner.arity == 1 and pr_outer.arity == 2 "
372      # apply unary      # apply unary
# Line 356  Line 374 
374      #print " shape1: ", shape1.name      #print " shape1: ", shape1.name
375      tshape1_shape = op1_to_shape(opr_inner, shape1)      tshape1_shape = op1_to_shape(opr_inner, shape1)
376      #print "tshape1_shap:",tshape1_shape.name      #print "tshape1_shap:",tshape1_shape.name
377            k_init = 2
378      if(not (tshape1_shape == None)):      if(not (tshape1_shape == None)):
379          #apply binary          #apply binary
380          rtn3s = op2_to_shape(opr_outer, tshape1_shape)          rtn3s = op2_to_shape(opr_outer, tshape1_shape)
381          #print "rtn3s:",len(rtn3s)          #print "rtn3s:",len(rtn3s)
382          for (shape3, tshape2_shape) in rtn3s:          for (shape3, tshape2_shape) in rtn3s:
383              # convert shape to type              # convert shape to type
384              ty3s = shape_to_fty(shape3)                  ty3s = shape_to_fty(shape3, k_init)
385              #pes("ty3s",ty3s)              #pes("ty3s",ty3s)
386              # single argument              # single argument
387              if ((len(ty3s)>0) and  (len(ty1s)>0)):              if ((len(ty3s)>0) and  (len(ty1s)>0)):
# Line 370  Line 389 
389                      if(not (exp1==None)):                      if(not (exp1==None)):
390                          dim = exp1.dim                          dim = exp1.dim
391                          exps = [exp1]                          exps = [exp1]
392                          tshape1 = check_tshape(opr_inner,  exp1, tshape1_shape, dim)                              tshape1 = check1_tshape(opr_inner,  exp1, tshape1_shape, dim)
393                          if(not (tshape1 == None)):                          if(not (tshape1 == None)):
394                              pick_ty_to_exp3(exps, dim, ty3s, tshape1, tshape2_shape, opr_inner, opr_outer, title, testing_frame, cnt)                              pick_ty_to_exp3(exps, dim, ty3s, tshape1, tshape2_shape, opr_inner, opr_outer, title, testing_frame, cnt)
395        def sort(opr_outer):
396            if(opr_inner.arity == 1 and opr_outer.arity == 1): # arity is 1
397                iter_unary_unary(opr_outer)
398            elif(opr_outer.arity == 2 and opr_inner.arity == 1):
399                iter_binary_unary(opr_outer)
400        # need to iterate over outer operator
401        if(opr_outer==None):
402            n = len(op_all)
403            for  t_outer in range(n):
404                startouter = time.time()
405                opr_outer = id_toOpr(t_outer)
406                counter.zero_locals(cnt)
407                counter.zero_total(cnt)
408                writeTitle_outer(opr_inner, opr_outer)
409                sort(opr_outer)
410                endouter = time.time()
411                ttouter = "  time-outer:"+str(endouter - startouter)
412                writeall(ttouter)
413                print (ttouter)
414        else:
415            sort(opr_outer)
416    
417  # main function  # main function
418  def pick_get_ty1s(shape1, opr_inner, opr_outer, testing_frame, cnt):  def pick_get_ty1s(ty1s, shape1, opr_inner, opr_outer, testing_frame, cnt):
419      # apply operation a first time      # apply operation a first time
420      #print " --------------first time---------------"      #print " --------------first time---------------"
421      #print  "\t\topr_inner:", opr_inner.name, opr_inner.arity,"\n\topr_outer:", opr_outer.name,opr_outer.arity      #print  "\t\topr_inner:", opr_inner.name, opr_inner.arity,"\n\topr_outer:", opr_outer.name,opr_outer.arity
     ty1s = shape_to_fty(shape1)  
422      title = "("+shape1.name      title = "("+shape1.name
423      #writenow("\n-Shape1:"+title+" * _)")      #writenow("\n-Shape1:"+title+" * _)")
424      #print "pick_get_ty1s","shape1",shape1.name  
425      #pes("ty1s",ty1s)      #pes("ty1s",ty1s)
426      #print "attempt:"+opr_inner.name+"shape1", shape1.name      #print "attempt:"+opr_inner.name+"shape1", shape1.name
427      if(opr_inner.arity == 1 and opr_outer.arity == 1): # arity is 1      if(opr_inner.arity == 1): # arity is 1
428          iter_unary_unary(ty1s, shape1, opr_inner, opr_outer, title, testing_frame, cnt)          iter_inner_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)  
429      else:      else:
430          rtn2s = op2_to_shape(opr_inner, shape1)          rtn2s = op2_to_shape(opr_inner, shape1)
431          if (len(rtn2s)>0):          if (len(rtn2s)>0):
# Line 444  Line 478 
478    
479  ##################################################################################################  ##################################################################################################
480  ##################################################################################################  ##################################################################################################
481  # main function  # main does everything fowards
   
 #handles some backwards  
482  def pick(opr_inner, opr_outer, testing_frame, cnt):  def pick(opr_inner, opr_outer, testing_frame, cnt):
     start = time.time()  
   
483      #writeTitle_outer(opr_inner, opr_outer)      #writeTitle_outer(opr_inner, opr_outer)
484      # iterate over shapes for first terms      # 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:  
485          shapes = tvs+tms+tt2+tt3+tt4          shapes = tvs+tms+tt2+tt3+tt4
486        n = len(op_all)
487        startall = time.time()
488          for shape1 in shapes:          for shape1 in shapes:
489            startshape = time.time()
490            writeall ("shape:"+shape1.name)
491              #print "shape1 ", shape1              #print "shape1 ", shape1
492              pick_get_ty1s(shape1, opr_inner, opr_outer, testing_frame, cnt)          k_init = 2
493            ty1s = shape_to_fty(shape1, k_init)
494            if(opr_inner == None):
495                for t_inner in range(n):
496                    startxx = time.time()
497                    opr_inner = id_toOpr(t_inner)
498                    counter.zero_locals(cnt)
499                    counter.zero_total(cnt)
500                    pick_get_ty1s(ty1s, shape1, opr_inner, opr_outer, testing_frame, cnt)
501                    endxx = time.time()
502                    ttxx = "  timexx-done inner: "+opr_inner.name+str(endxx - startxx)
503                    writeall(ttxx)
504                    print (ttxx)
505            else:
506                pick_get_ty1s(ty1s, shape1, opr_inner, opr_outer, testing_frame, cnt)
507        endall = time.time()
508        ttall = "  time all: "+ str(endall - startall)
509        writeall(ttall)
510        print (ttall)
511              #print "forwards"              #print "forwards"
512      writeResults_outer(opr_inner, opr_outer, testing_frame, cnt)      writeResults_outer(opr_inner, opr_outer, testing_frame, cnt)
     end = time.time()  
     t ="time:"+str(end - start)  
     writeall(t)  
     print (t)  
513      return      return
514    
515  #does everything forward  
516  def pick2(opr_inner, opr_outer, testing_frame, cnt):  #handles some backwards
517    def pickback(opr_inner, opr_outer, testing_frame, cnt):
518      #writeTitle_outer(opr_inner, opr_outer)      #writeTitle_outer(opr_inner, opr_outer)
519      # iterate over shapes for first terms      # iterate over shapes for first terms
520        backwards= False
521        if(backwards):
522            if(not (opr_outer==None)):
523                if(opr_outer.fieldop):
524                    go_backwards(opr_outer, opr_inner, testing_frame, cnt)
525    
526            elif(opr_inner.arity == 1):
527                go_backwards(opr_outer, opr_inner, testing_frame, cnt)
528        else:
529      shapes = tvs+tms+tt2+tt3+tt4      shapes = tvs+tms+tt2+tt3+tt4
530      for shape1 in shapes:      for shape1 in shapes:
531          pick_get_ty1s(shape1, opr_inner, opr_outer, testing_frame, cnt)              #print "shape1 ", shape1
532                k_init = 2
533                ty1s = shape_to_fty(shape1, k_init)
534                pick_get_ty1s(ty1s, shape1, opr_inner, opr_outer, testing_frame, cnt)
535                #print "forwards"
536      writeResults_outer(opr_inner, opr_outer, testing_frame, cnt)      writeResults_outer(opr_inner, opr_outer, testing_frame, cnt)
     writeCumulative(cnt)  
537      return      return
538    
539    #

Legend:
Removed from v.4483  
changed lines
  Added in v.4484

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