#NEWLINE# def code_gen( self ) : #NEWLINE# #INDENT# '\n        WRITEME\n\n        Generates code for a struct that does the computation of the fgraph and\n        stores it in the struct_code field of the instance.\n\n        If reuse_storage is True, outputs and temporaries will be stored in\n        the struct so they can be reused each time a function returned by\n        make_function is called, which means that the output of a call will\n        be invalidated by the next. If reuse_storage is False, that problem\n        is avoided.\n\n        This method caches its computations.\n\n        ' #NEWLINE# if getattr ( self , 'struct_code' , False ) : #NEWLINE# #INDENT# return self . struct_code #NEWLINE# #UNINDENT# no_recycling = self . no_recycling #NEWLINE# c_support_code_apply = [ ] #NEWLINE# c_init_code_apply = [ ] #NEWLINE# symbol = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# init_tasks = [ ] #NEWLINE# tasks = [ ] #NEWLINE# init_blocks = [ ] #NEWLINE# blocks = [ ] #NEWLINE# failure_var = '__failure' #NEWLINE# id = 1 #NEWLINE# for variable in self . variables : #NEWLINE# #INDENT# sub = dict ( failure_var = failure_var ) #NEWLINE# if ( variable in self . consts ) : #NEWLINE# #INDENT# symbol [ variable ] = ( ( '(' + variable . type . c_literal ( variable . data ) ) + ')' ) #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( variable in self . inputs ) : #NEWLINE# #INDENT# policy = [ [ get_nothing , get_nothing , get_nothing ] , [ get_c_declare , get_c_extract , get_c_cleanup ] ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( variable in self . orphans ) : #NEWLINE# #INDENT# if ( not isinstance ( variable , graph . Constant ) ) : #NEWLINE# #INDENT# raise TypeError ( 'All orphans to CLinker must be Constant instances.' , variable ) #NEWLINE# #UNINDENT# policy = [ [ get_c_declare , get_c_extract , get_c_cleanup ] , [ get_nothing , get_nothing , get_nothing ] ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( variable in self . temps ) : #NEWLINE# #INDENT# if ( variable . type . c_is_simple ( ) or ( variable in no_recycling ) ) : #NEWLINE# #INDENT# policy = [ [ get_nothing , get_nothing , get_nothing ] , [ get_c_declare , get_c_init , get_c_cleanup ] ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# policy = [ [ get_c_declare , get_c_init , get_c_cleanup ] , [ get_nothing , get_nothing , get_nothing ] ] #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( variable in self . outputs ) : #NEWLINE# #INDENT# if ( variable . type . c_is_simple ( ) or ( variable in no_recycling ) ) : #NEWLINE# #INDENT# policy = [ [ get_nothing , get_nothing , get_nothing ] , [ get_c_declare , get_c_init , ( get_c_sync , get_c_cleanup ) ] ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# policy = [ [ get_nothing , get_nothing , get_nothing ] , [ get_c_declare , get_c_extract_out , ( get_c_sync , get_c_cleanup ) ] ] #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise Exception ( 'what the fuck' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# ( builder , block ) = struct_variable_codeblocks ( variable , policy , id , symbol , sub ) #NEWLINE# init_tasks . append ( ( variable , 'init' , id ) ) #NEWLINE# init_blocks . append ( builder ) #NEWLINE# tasks . append ( ( variable , 'get' , ( id + 1 ) ) ) #NEWLINE# blocks . append ( block ) #NEWLINE# id += 2 #NEWLINE# #UNINDENT# for ( node_num , node ) in enumerate ( self . node_order ) : #NEWLINE# #INDENT# sub = dict ( failure_var = failure_var ) #NEWLINE# params = node . run_params ( ) #NEWLINE# if ( params is not graph . NoParams ) : #NEWLINE# #INDENT# params_var = symbol [ self . node_params [ params ] ] #NEWLINE# #UNINDENT# name = ( 'node_<<<<HASH_PLACEHOLDER>>>>_%i' % node_num ) #NEWLINE# isyms = [ symbol [ r ] for r in node . inputs ] #NEWLINE# osyms = [ symbol [ r ] for r in node . outputs ] #NEWLINE# sub [ 'id' ] = id #NEWLINE# sub [ 'fail' ] = failure_code ( sub ) #NEWLINE# if ( params is not graph . NoParams ) : #NEWLINE# #INDENT# sub [ 'params' ] = params_var #NEWLINE# #UNINDENT# sub_struct = dict ( ) #NEWLINE# sub_struct [ 'id' ] = ( id + 1 ) #NEWLINE# sub_struct [ 'fail' ] = failure_code_init ( sub ) #NEWLINE# if ( params is not graph . NoParams ) : #NEWLINE# #INDENT# sub_struct [ 'params' ] = params_var #NEWLINE# #UNINDENT# struct_support = '' #NEWLINE# struct_init = '' #NEWLINE# struct_cleanup = '' #NEWLINE# op = node . op #NEWLINE# try : #NEWLINE# #INDENT# c_support_code_apply . append ( op . c_support_code_apply ( node , name ) ) #NEWLINE# #UNINDENT# except utils . MethodNotDefined : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# assert isinstance ( c_support_code_apply [ ( - 1 ) ] , string_types ) , ( str ( node . op ) + " didn't return a string for c_support_code_apply" ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# c_init_code_apply . append ( op . c_init_code_apply ( node , name ) ) #NEWLINE# #UNINDENT# except utils . MethodNotDefined : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# assert isinstance ( c_init_code_apply [ ( - 1 ) ] , string_types ) , ( str ( node . op ) + " didn't return a string for c_init_code_apply" ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# struct_init = op . c_init_code_struct ( node , name , sub_struct ) #NEWLINE# assert isinstance ( struct_init , string_types ) , ( str ( node . op ) + " didn't return a string for c_init_code_struct" ) #NEWLINE# #UNINDENT# except utils . MethodNotDefined : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# struct_support = op . c_support_code_struct ( node , name ) #NEWLINE# assert isinstance ( struct_support , string_types ) , ( str ( node . op ) + " didn't return a string for c_support_code_struct" ) #NEWLINE# #UNINDENT# except utils . MethodNotDefined : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# struct_cleanup = op . c_cleanup_code_struct ( node , name ) #NEWLINE# assert isinstance ( struct_cleanup , string_types ) , ( str ( node . op ) + " didn't return a string for c_cleanup_code_struct" ) #NEWLINE# #UNINDENT# except utils . MethodNotDefined : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# behavior = op . c_code ( node , name , isyms , osyms , sub ) #NEWLINE# #UNINDENT# except utils . MethodNotDefined : #NEWLINE# #INDENT# raise NotImplementedError ( ( '%s cannot produce C code' % op ) ) #NEWLINE# #UNINDENT# assert isinstance ( behavior , string_types ) , ( str ( node . op ) + " didn't return a string for c_code" ) #NEWLINE# behavior = ( ( ( '// Op class ' + node . op . __class__ . __name__ ) + '\n' ) + behavior ) #NEWLINE# try : #NEWLINE# #INDENT# cleanup = op . c_code_cleanup ( node , name , isyms , osyms , sub ) #NEWLINE# #UNINDENT# except utils . MethodNotDefined : #NEWLINE# #INDENT# cleanup = '' #NEWLINE# #UNINDENT# _logger . info ( 'compiling un-versioned Apply %s' , str ( node ) ) #NEWLINE# blocks . append ( CodeBlock ( '' , behavior , cleanup , sub ) ) #NEWLINE# tasks . append ( ( node , 'code' , id ) ) #NEWLINE# id += 1 #NEWLINE# init_blocks . append ( CodeBlock ( struct_support , struct_init , struct_cleanup , { #NEWLINE# #INDENT# 'id' : id , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# init_tasks . append ( ( node , 'init' , id ) ) #NEWLINE# id += 1 #NEWLINE# #UNINDENT# args = [ ] #NEWLINE# args += [ ( 'storage_%s' % symbol [ blocks ] ) for variable in utils . uniq ( ( ( self . inputs + self . outputs ) + self . orphans ) ) ] #NEWLINE# struct_name = ( '__struct_compiled_op_%s' % '<<<<HASH_PLACEHOLDER>>>>' ) #NEWLINE# struct_code = struct_gen ( args , init_blocks , blocks , dict ( failure_var = failure_var , name = struct_name ) ) #NEWLINE# self . struct_code = struct_code #NEWLINE# self . struct_name = struct_name #NEWLINE# self . args = args #NEWLINE# self . r2symbol = symbol #NEWLINE# self . init_blocks = init_blocks #NEWLINE# self . init_tasks = init_tasks #NEWLINE# self . blocks = blocks #NEWLINE# self . tasks = tasks #NEWLINE# all_info = ( ( self . inputs + self . outputs ) + self . orphans ) #NEWLINE# self . c_support_code_apply = c_support_code_apply #NEWLINE# self . c_init_code_apply = c_init_code_apply #NEWLINE# if ( ( self . init_tasks , self . tasks ) != self . get_init_tasks ( ) ) : #NEWLINE# #INDENT# print ( 'init_tasks\n' , self . init_tasks , file = sys . stderr ) #NEWLINE# print ( self . get_init_tasks ( ) [ 0 ] , file = sys . stderr ) #NEWLINE# print ( 'tasks\n' , self . tasks , file = sys . stderr ) #NEWLINE# print ( self . get_init_tasks ( ) [ 1 ] , file = sys . stderr ) #NEWLINE# assert ( ( self . init_tasks , self . tasks ) == self . get_init_tasks ( ) ) #NEWLINE# #UNINDENT# self . dupidx = [ i for ( i , x ) in enumerate ( all_info ) if ( ( all_info . count ( x ) > 1 ) and ( all_info . index ( x ) != i ) ) ] #NEWLINE# return self . struct_code