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

SCM Repository

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

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

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

revision 3873, Wed May 18 13:38:23 2016 UTC revision 3874, Wed May 18 16:45:54 2016 UTC
# Line 6  Line 6 
6  import sys  import sys
7  import os  import os
8  import re  import re
9  from test_examples import value_probe  
 from test_examples import value_negation  
 from test_examples import value_gradient  
10  from test_examples import value_scalarFd2  from test_examples import value_scalarFd2
11  from test_examples import value_vec2Fd2  from test_examples import value_vec2Fd2
12    from test_examples import value_mat2x2Fd2
13  from test_examples import value_scalarT  from test_examples import value_scalarT
14  from test_examples import value_vec2T  from test_examples import value_vec2T
15    from test_examples import value_mat2x2T
16    from test_examples import optostr
17    from test_examples import applyUnaryOp
18    from test_examples import applyBinaryOp
19  template="template/foo.ddro"     # template  template="template/foo.ddro"     # template
20    
21  #strings in diderot template  #strings in diderot template
# Line 27  Line 29 
29  foo_out="out"  foo_out="out"
30    
31  #fixed variables  #fixed variables
 op_probe = value_probe()  
 op_negation = value_negation()  
 op_gradient = value_gradient()  
32  ty_scalarF_d2 = value_scalarFd2()  ty_scalarF_d2 = value_scalarFd2()
33  ty_vec2F_d2 = value_vec2Fd2()  ty_vec2F_d2 = value_vec2Fd2()
34    ty_mat2x2F_d2=value_mat2x2Fd2()
35  ty_scalarT = value_scalarT()  ty_scalarT = value_scalarT()
36  ty_vec2T = value_vec2T()  ty_vec2T = value_vec2T()
37    ty_mat2x2T = value_mat2x2T()
38    
39    def fieldName(i):
40        return "F"+str(i)
41    opfieldname1="G"
42    
43  # type of field  # type of field
44  def fieldShape(f, k, type):  def fieldShape(f, k, itype):
45        print "shape inside fieldshape()",type
46      foo="field#"+str(k)      foo="field#"+str(k)
47      #write dimension for input field      #write dimension for input field
48      #write shape for input field      #write shape for input field
49      if (type==ty_scalarF_d2):      if (itype==ty_scalarF_d2):
50          foo+="(2)[]"          foo+="(2)[]"
51      elif(type==ty_vec2F_d2):      elif(itype==ty_vec2F_d2):
52          foo+="(2)[2]"          foo+="(2)[2]"
53        elif(itype==ty_mat2x2F_d2):
54            foo+="(2)[2,2]"
55    
56      else:      else:
57          raise "type of input field not supported"          raise Exception("type of input field not supported")
58      f.write(foo.encode('utf8'))      f.write(foo.encode('utf8'))
59    
60    
61  #field input line  #field input line
62  def inShape(f,k, type,inputfile):  #f: file to write to
63      fieldShape(f, k, type)  #k:continuity
64      foo="F = ctmr"+u'⊛'+"  image(\""+inputfile+".nrrd\");\n"  #itypes: types for input field
65    #inputlist: name for input data
66    def inShape(f,k, itypes,inputs):
67        print "shape inside inShape()",itypes
68        i=0
69        for (itype,inputfile) in zip(itypes, inputs):
70            fieldShape(f, k,itype)
71            foo=fieldName(i)+" = ctmr"+u'⊛'+"  image(\""+inputfile+".nrrd\");\n"
72      f.write(foo.encode('utf8'))      f.write(foo.encode('utf8'))
73            i+=1
74    
75    
76  #instaniate output tensor  #instaniate output tensor
# Line 63  Line 80 
80          foo+="tensor []"+foo_out+" = 0.0"          foo+="tensor []"+foo_out+" = 0.0"
81      elif(type==ty_vec2T):      elif(type==ty_vec2T):
82          foo+= "tensor [2] "+foo_out+" = [0.0, 0.0]"          foo+= "tensor [2] "+foo_out+" = [0.0, 0.0]"
83        elif(type==ty_mat2x2T):
84            foo+= "tensor [2,2] "+foo_out+" = [[0.0, 0.0],[0.0, 0.0]]"
85      else:      else:
86          raise "unsupported input type",type          raise Exception("unsupported input type",type)
87      foo+= ";\n"      foo+= ";\n"
88      f.write(foo.encode('utf8'))      f.write(foo.encode('utf8'))
89    
90  #op1 variable:[probe, negation, gradient]  
91  def gotop1(f,k,type, op1):  def gotop1(f,k1,typs, op1):
92      foo = "G = "      (opname,arity)=op1
93      if(op1==op_probe):      foo = opfieldname1+" = "
94          fieldShape(f,k, type)      if (arity==1):
95          foo += "F"          (typ2, k2) = applyUnaryOp(op1,typs,k1)
96      if(op1==op_negation):          fieldShape(f, k2, typ2)
97          fieldShape(f,k, type)          foo += optostr(op1)+fieldName(0)
98          foo +="-F"          #   print "typ1:sending to apply unary op",typ1
99      if(op1==op_gradient):          #print "typ2:sending to fieldshape()",typ2
100          if(type==ty_scalarF_d2):      elif(arity==2):
101              fieldShape(f,k-1, ty_vec2F_d2)          (typ2, k2) = applyBinaryOp(op1,typs,k1)
102            fieldShape(f, k2, typ2)
103            foo += fieldName(0)+optostr(op1)+fieldName(1)
104          else:          else:
105              raise "can not take gradient of this field type"          raise Exception("unsupported arity")
         foo+= u'∇'+"F"  
106      foo +=";\n"      foo +=";\n"
107      f.write(foo.encode('utf8'))      f.write(foo.encode('utf8'))
108    
109  def indexG(f, pos):  def indexG(f, pos):
110      i=0      i=0
111      foo=""      foo=""
112      pos.insert(0,pos[0])      #pos.insert(0,pos[0])
113      p=str(pos[0])      p=str(pos[0])
114      foo += "\t\tif(i=="+str(i)+"){\n"      foo += "\t\tif(i=="+str(i)+"){\n"
115      foo += "\t\t\t"+foo_out+" = G("+p+");\n"      foo += "\t\t\t"+foo_out+" = "+opfieldname1+"("+p+");\n"
116      foo += "\t\t}\n"      foo += "\t\t}\n"
117      i=i+1      i=i+1
118      for p1 in pos:      for p1 in pos:
119          p=str(p1)          p=str(p1)
120          foo += "\t\tif(i=="+str(i)+"){\n"          foo += "\t\tif(i=="+str(i)+"){\n"
121          foo += "\t\t\t"+foo_out+" = G("+p+");\n"          foo += "\t\t\t"+foo_out+" = "+opfieldname1+"("+p+");\n"
122          foo += "\t\t}\n"          foo += "\t\t}\n"
123          i=i+1          i=i+1
124      f.write(foo.encode('utf8'))      f.write(foo.encode('utf8'))
# Line 111  Line 131 
131  #otype: output tensor  #otype: output tensor
132  #op1: unary operation involved  #op1: unary operation involved
133  def readDiderot(p_out, itype,otype,op1,inputfile,pos):  def readDiderot(p_out, itype,otype,op1,inputfile,pos):
134        print "input to readDiderot()",itype
135      #read diderot template      #read diderot template
136      ftemplate = open(template, 'r')      ftemplate = open(template, 'r')
137      ftemplate.readline()      ftemplate.readline()
# Line 122  Line 143 
143          a0 = re.search(foo_in, line)          a0 = re.search(foo_in, line)
144          if a0:          if a0:
145              #replace field input line              #replace field input line
146                print "send to inShape()",itype
147              inShape(f,k, itype,inputfile)              inShape(f,k, itype,inputfile)
148              continue              continue
149          # is it output tensor line?          # is it output tensor line?
# Line 152  Line 174 
174      f.close()      f.close()
175    
176  def writeDiderot(p_out, itype,otype,op1,inputfile,pos,output):  def writeDiderot(p_out, itype,otype,op1,inputfile,pos,output):
177        print "input to writeDiderot()",itype
178      readDiderot(p_out,itype,otype,op1,inputfile,pos)      readDiderot(p_out,itype,otype,op1,inputfile,pos)
179      # compile and run diderot program      # compile and run diderot program
180      os.system(" echo \"pout "+p_out+"\"")      os.system(" echo \"pout "+p_out+"\"")
# Line 161  Line 184 
184      os.system("./"+p_out)      os.system("./"+p_out)
185      os.system("cp "+p_out+".txt "+output+".txt")      os.system("cp "+p_out+".txt "+output+".txt")
186      os.system("cp "+p_out+".diderot "+output+".diderot")      os.system("cp "+p_out+".diderot "+output+".diderot")
187        os.system("cp "+p_out+".c "+output+".c")
188      os.system(" echo \"copied diderot program to "+output+".diderot \"")      os.system(" echo \"copied diderot program to "+output+".diderot \"")
189      os.system(" echo \"copied o-out "+output+".txt \"")      os.system(" echo \"copied o-out "+output+".txt \"")
190      os.system("rm "+p_out+"*")      os.system("rm "+p_out+"*")

Legend:
Removed from v.3873  
changed lines
  Added in v.3874

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