def gen_index_array(index_array): from iegen.codegen import Statement,Comment,calc_size_string #Calculate the size of this index array #Assumes only one set in the union... input_bounds=index_array.input_bounds if 1!=len(input_bounds): raise ValueError("IndexArray's input bounds have multiple terms in the disjunction") #Assumes the index array dataspace is 1D... if 1!=input_bounds.arity(): raise ValueError("IndexArray's dataspace does not have arity 1") #Get the single input tuple variable's name input_var_name=input_bounds.tuple_set[0] #Calculate the output range of this index array #Assumes only one set in the union... output_bounds=index_array.output_bounds if 1!=len(output_bounds): raise ValueError("IndexArray's output bounds have multiple terms in the disjunction") #Assumes the index array dataspace is 1D... if 1!=output_bounds.arity(): raise ValueError("IndexArray's dataspace does not have arity 1") #Get the single output tuple variable's name output_var_name=output_bounds.tuple_set[0] #Get the strings that calculate the size of input domain and # output range of the ER at runtime input_size_string=calc_size_string(input_bounds,input_var_name) output_size_string=calc_size_string(output_bounds,output_var_name) stmts=[] stmts.append(Comment('Wrapping index array %s'%(index_array.name))) stmts.append(Statement('%s=%s(%s,%s,%s,%s);'%(index_array.get_var_name(),index_array.get_ctor_str(),index_array.get_param_name(),input_size_string,output_size_string,str(index_array.is_permutation).lower()))) stmts.append(Statement()) return stmts
def gen_output_er_1dto1d(output_er_spec,is_call_input,mapir): import iegen.pycloog from iegen.pycloog import codegen from iegen.codegen import Statement,Comment iegen.print_progress("Generating code for output ERSpec '%s'..."%(output_er_spec.name)) iegen.print_detail(str(output_er_spec)) print 'Abstract relation: %s'%(output_er_spec.relation) print 'Input bounds for abstract relation: %s'%(output_er_spec.input_bounds) print 'Output bounds for abstract relation: %s'%(output_er_spec.output_bounds) #Calculate the necessary information before generating code output_bounds_var_name=output_er_spec.output_bounds.tuple_set[0] output_lower_bound=str(list(output_er_spec.output_bounds.lower_bounds(output_bounds_var_name)[0])[0]) output_upper_bound=str(list(output_er_spec.output_bounds.upper_bounds(output_bounds_var_name)[0])[0]) input_bounds_var_name=output_er_spec.input_bounds.tuple_set[0] input_lower_bound=str(list(output_er_spec.input_bounds.lower_bounds(input_bounds_var_name)[0])[0]) input_upper_bound=str(list(output_er_spec.input_bounds.upper_bounds(input_bounds_var_name)[0])[0]) num_entries=calc_size_string(list(output_er_spec.output_bounds)[0],output_bounds_var_name) input_equal_value=calc_equality_value(input_bounds_var_name,list(output_er_spec.relation)[0],mapir) input_var_name=output_er_spec.relation.tuple_in[0] output_var_name=output_er_spec.relation.tuple_out[0] stmts=[] stmts.append(Comment('Creation of ER_1Dto1D for abstract relation:')) stmts.append(Comment(str(output_er_spec.relation))) stmts.append(Comment('Bounds for output domain: '+str(output_er_spec.output_bounds))) #Generate the call to the constructor stmts.append(Statement('%s=%s(%s,%s,%s,%s,%s);'%(output_er_spec.get_var_name(),output_er_spec.get_ctor_str(),input_lower_bound,input_upper_bound,output_lower_bound,output_upper_bound,num_entries))) #Generate the count loop stmts.append(Statement('#define S0(%s) %s(%s,%s)'%(output_var_name,output_er_spec.get_count_name(),output_er_spec.get_var_name(),input_equal_value))) loop_stmts=codegen([iegen.pycloog.Statement(output_er_spec.output_bounds)]).split('\n') for loop_stmt in loop_stmts: stmts.append(Statement(loop_stmt)) stmts.append(Statement('#undef S0')) #Generate the finalize count call stmts.append(Statement('%s(%s)'%(output_er_spec.get_count_finalize_name(),output_er_spec.get_var_name()))) #Generate the insert loop stmts.append(Statement('#define S0(%s) %s(%s,%s,%s)'%(output_var_name,output_er_spec.get_setter_str(),output_er_spec.get_var_name(),input_equal_value,output_var_name))) loop_stmts=codegen([iegen.pycloog.Statement(output_er_spec.output_bounds)]).split('\n') for loop_stmt in loop_stmts: stmts.append(Statement(loop_stmt)) stmts.append(Statement('#undef S0')) stmts.append(Statement('*%s=%s;'%(output_er_spec.get_param_name(),output_er_spec.get_var_name()))) stmts.append(Statement()) return stmts
def gen_data_dep(data_dep,mapir): from iegen.pycloog import codegen #Get the dependence relation from the data dependence object dep_rel=data_dep.dep_rel #Ensure the data dep is 2d -> 2d if (2,2)!=dep_rel.arity(): raise ValueError('Code generation for data dependences that are not 2d -> 2d is not supported') stmts=[] #Input constant value (from loop) in_const=calc_equality_value(dep_rel.tuple_in[0],list(dep_rel)[0],mapir) #Determine if the target loop is the input or output of the relation if in_const==data_dep.target: bounds_var=dep_rel.tuple_in[1] else: bounds_var=dep_rel.tuple_out[1] #Get the lower and upper bounds for the bounds variable lbs=calc_single_bounds_from_bounds(dep_rel.lower_bounds(bounds_var)) ubs=calc_single_bounds_from_bounds(dep_rel.upper_bounds(bounds_var)) stmts.append(Comment('Dependences for %s'%(data_dep.name))) stmts.append(Comment(str(dep_rel))) #Declare the explicit dependence variable const_in=calc_equality_value(dep_rel.tuple_in[0],list(dep_rel)[0],mapir) input_size_string=calc_size_string(list(dep_rel)[0],dep_rel.tuple_in[1]) const_out=calc_equality_value(dep_rel.tuple_out[0],list(dep_rel)[0],mapir) output_size_string=calc_size_string(list(dep_rel)[0],dep_rel.tuple_out[1]) stmts.append(Statement('%s %s=%s(%s,%s,%s,%s,%s);'%(data_dep.get_type(),data_dep.get_var_name(),data_dep.get_ctor_str(),const_in,const_out,input_size_string,output_size_string,len(dep_rel)))) #Generate the define/undefine statements cloog_stmts=[] define_stmts=[] undefine_stmts=[] var_in_name=dep_rel.tuple_in[1] var_out_name=dep_rel.tuple_out[1] for conjunction_index,single_relation in enumerate(dep_rel): bounds_set=Set('{[%s]: %s<=%s and %s<=%s}'%(bounds_var,lbs[conjunction_index],bounds_var,bounds_var,ubs[conjunction_index]),mapir.get_symbolics()) #Get the value to insert if in_const==data_dep.target: value=calc_equality_value(var_out_name,single_relation,mapir,only_eqs=True) define_stmts.append(Statement('#define S%d(%s) %s(%s,%s,%s);'%(conjunction_index,bounds_var,data_dep.get_setter_str(),data_dep.get_var_name(),bounds_var,value))) else: value=calc_equality_value(var_in_name,single_relation,mapir,only_eqs=True) define_stmts.append(Statement('#define S%d(%s) %s(%s,%s,%s);'%(conjunction_index,bounds_var,data_dep.get_setter_str(),data_dep.get_var_name(),value,bounds_var))) cloog_stmts.append(iegen.pycloog.Statement(bounds_set)) undefine_stmts.append(Statement('#undef S%d'%(conjunction_index,))) #Generate the whole set of statements stmts.append(Comment('Define loop body statements')) stmts.extend(define_stmts) stmts.append(Statement()) loop_stmts=codegen(cloog_stmts).split('\n') for loop_stmt in loop_stmts: stmts.append(Statement(loop_stmt)) stmts.append(Statement()) stmts.append(Comment('Undefine loop body statements')) stmts.extend(undefine_stmts) stmts.append(Statement()) return stmts