#NEWLINE# def _c_all( self , node , nodename , inames , onames , sub ) : #NEWLINE# #INDENT# _inames = inames #NEWLINE# _onames = onames #NEWLINE# inames = gof . utils . uniq ( inames ) #NEWLINE# inputs = gof . utils . uniq ( node . inputs ) #NEWLINE# assert ( len ( inames ) == len ( inputs ) ) #NEWLINE# ( ii , iii ) = list ( zip ( * gof . utils . uniq ( list ( zip ( _inames , node . inputs ) ) ) ) ) #NEWLINE# assert all ( [ ( x == y ) for ( x , y ) in zip ( ii , inames ) ] ) #NEWLINE# assert all ( [ ( x == y ) for ( x , y ) in zip ( iii , inputs ) ] ) #NEWLINE# defines = '' #NEWLINE# undefs = '' #NEWLINE# dmap = dict ( [ ( node . outputs [ o ] , [ node . inputs [ i ] ] ) for ( o , i ) in iteritems ( self . inplace_pattern ) ] ) #NEWLINE# idtypes = [ input . type . dtype_specs ( ) [ 1 ] for input in inputs ] #NEWLINE# real = list ( zip ( * [ ( r , s , r . type . dtype_specs ( ) [ 1 ] ) for ( r , s ) in izip ( node . outputs , onames ) if ( r not in dmap ) ] ) ) #NEWLINE# if real : #NEWLINE# #INDENT# ( real_outputs , real_onames , real_odtypes ) = real #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( real_outputs , real_onames , real_odtypes ) = ( [ ] , [ ] , [ ] ) #NEWLINE# #UNINDENT# aliased = list ( zip ( * [ ( r , s ) for ( r , s ) in izip ( node . outputs , onames ) if ( r in dmap ) ] ) ) #NEWLINE# if aliased : #NEWLINE# #INDENT# ( aliased_outputs , aliased_onames ) = aliased #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( aliased_outputs , aliased_onames ) = ( [ ] , [ ] ) #NEWLINE# #UNINDENT# orders = [ [ ( ( x and 'x' ) or i ) for ( i , x ) in enumerate ( input . type . broadcastable ) ] for input in inputs ] #NEWLINE# nnested = len ( orders [ 0 ] ) #NEWLINE# sub = dict ( sub ) #NEWLINE# for ( i , ( input , iname ) ) in enumerate ( izip ( inputs , inames ) ) : #NEWLINE# #INDENT# sub [ ( 'lv%i' % i ) ] = iname #NEWLINE# #UNINDENT# decl = cgen . make_declare ( orders , idtypes , sub ) #NEWLINE# checks = cgen . make_checks ( orders , idtypes , sub ) #NEWLINE# z = list ( zip ( inames , inputs ) ) #NEWLINE# alloc_fortran = ' && ' . join ( [ ( 'PyArray_ISFORTRAN(%s)' % arr ) for ( arr , var ) in z if ( not all ( var . broadcastable ) ) ] ) #NEWLINE# if ( len ( alloc_fortran ) == 0 ) : #NEWLINE# #INDENT# alloc_fortran = '0' #NEWLINE# #UNINDENT# alloc = '' #NEWLINE# for ( output , oname , odtype ) in izip ( real_outputs , real_onames , real_odtypes ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# sub [ ( 'lv%i' % i ) ] = oname #NEWLINE# sub [ 'olv' ] = oname #NEWLINE# alloc += cgen . make_declare ( [ list ( range ( nnested ) ) ] , [ odtype ] , dict ( sub , lv0 = oname ) ) #NEWLINE# alloc += cgen . make_alloc ( orders , odtype , sub , fortran = alloc_fortran ) #NEWLINE# alloc += cgen . make_checks ( [ list ( range ( nnested ) ) ] , [ odtype ] , dict ( sub , lv0 = oname ) ) #NEWLINE# #UNINDENT# olv_index = i #NEWLINE# for ( output , oname ) in izip ( aliased_outputs , aliased_onames ) : #NEWLINE# #INDENT# olv_index = inputs . index ( dmap [ output ] [ 0 ] ) #NEWLINE# iname = inames [ olv_index ] #NEWLINE# alloc += ( '\n            if (%(oname)s) {\n                Py_XDECREF(%(oname)s);\n            }\n            %(oname)s = %(iname)s;\n            Py_XINCREF(%(oname)s);\n            ' % locals ( ) ) #NEWLINE# defines += ( '#define %(oname)s_i %(iname)s_i' % locals ( ) ) #NEWLINE# undefs += ( '#undef %(oname)s_i' % locals ( ) ) #NEWLINE# #UNINDENT# task_code = self . scalar_op . c_code ( Apply ( self . scalar_op , [ get_scalar_type ( dtype = input . type . dtype ) . make_variable ( ) for input in node . inputs ] , [ get_scalar_type ( dtype = output . type . dtype ) . make_variable ( ) for output in node . outputs ] ) , ( nodename + '_scalar_' ) , [ ( '%s_i' % s ) for s in _inames ] , [ ( '%s_i' % s ) for s in onames ] , sub ) #NEWLINE# code = ( '\n        {\n            %(defines)s\n            %(task_code)s\n            %(undefs)s\n        }\n        ' % locals ( ) ) #NEWLINE# loop_orders = ( orders + ( [ list ( range ( nnested ) ) ] * len ( real_onames ) ) ) #NEWLINE# dtypes = ( idtypes + list ( real_odtypes ) ) #NEWLINE# if all ( ( [ ( o . ndim <= 1 ) for o in node . outputs ] or all ( node . outputs [ 0 ] . broadcastable ) ) ) : #NEWLINE# #INDENT# if nnested : #NEWLINE# #INDENT# all_code = ( ( ( [ ( '' , '' ) ] * ( nnested - 1 ) ) + [ ( '' , code ) ] ) + [ '' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# all_code = [ code ] #NEWLINE# #UNINDENT# if ( len ( all_code ) == 1 ) : #NEWLINE# #INDENT# task_decl = '' . join ( [ ( '%s& %s_i = *%s_iter;\n' % ( dtype , name , name ) ) for ( name , dtype ) in izip ( ( inames + list ( real_onames ) ) , ( idtypes + list ( real_odtypes ) ) ) ] ) #NEWLINE# preloops = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( i , ( loop_order , dtype ) ) in enumerate ( zip ( loop_orders , dtypes ) ) : #NEWLINE# #INDENT# for ( j , index ) in enumerate ( loop_order ) : #NEWLINE# #INDENT# if ( index != 'x' ) : #NEWLINE# #INDENT# preloops . setdefault ( j , '' ) #NEWLINE# preloops [ j ] += ( ( '%%(lv%(i)s)s_iter = (%(dtype)s*)(PyArray_DATA(%%(lv%(i)s)s));\n' % locals ( ) ) % sub ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# preloops . setdefault ( 0 , '' ) #NEWLINE# preloops [ 0 ] += ( ( '%%(lv%(i)s)s_iter = (%(dtype)s*)(PyArray_DATA(%%(lv%(i)s)s));\n' % locals ( ) ) % sub ) #NEWLINE# #UNINDENT# #UNINDENT# init_array = preloops . get ( 0 , ' ' ) #NEWLINE# loop = ( '\n                {\n                  %(defines)s\n                  %(init_array)s\n                  %(task_decl)s\n                  %(task_code)s\n                  %(undefs)s\n                }\n                ' % locals ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# loop = cgen . make_loop ( loop_orders = loop_orders , dtypes = dtypes , loop_tasks = all_code , sub = sub , openmp = self . openmp ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# loop = cgen . make_reordered_loop ( init_loop_orders = loop_orders , olv_index = olv_index , dtypes = dtypes , inner_task = code , sub = sub , openmp = self . openmp ) #NEWLINE# #UNINDENT# if ( all ( [ ( o . ndim >= 1 ) for o in node . outputs ] ) and ( not all ( node . outputs [ 0 ] . broadcastable ) ) ) : #NEWLINE# #INDENT# contig = None #NEWLINE# try : #NEWLINE# #INDENT# contig = self . scalar_op . c_code_contiguous ( node , ( nodename + '_scalar_contig_' ) , _inames , onames , sub ) #NEWLINE# #UNINDENT# except theano . gof . utils . MethodNotDefined : #NEWLINE# #INDENT# if all ( [ ( ( io . broadcastable == node . outputs [ 0 ] . broadcastable ) or all ( io . broadcastable ) ) for io in ( node . inputs + node . outputs ) ] ) : #NEWLINE# #INDENT# z = onames [ 0 ] #NEWLINE# contig = ( '\n                    // All output have the same size\n                    npy_intp n = PyArray_SIZE(%(z)s);\n                    ' % locals ( ) ) #NEWLINE# index = '' #NEWLINE# for ( x , var ) in zip ( ( inames + onames ) , ( inputs + node . outputs ) ) : #NEWLINE# #INDENT# if ( not all ( var . broadcastable ) ) : #NEWLINE# #INDENT# contig += ( '\n            dtype_%(x)s * %(x)s_ptr = (dtype_%(x)s*) PyArray_DATA(%(x)s);\n                            ' % locals ( ) ) #NEWLINE# index += ( '\n            dtype_%(x)s& %(x)s_i = %(x)s_ptr[i];\n                            ' % locals ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# contig += ( '\n            dtype_%(x)s& %(x)s_i = ((dtype_%(x)s*) PyArray_DATA(%(x)s))[0];\n                            ' % locals ( ) ) #NEWLINE# #UNINDENT# #UNINDENT# if self . openmp : #NEWLINE# #INDENT# contig += ( '#pragma omp parallel for if(n>=%d)' % config . openmp_elemwise_minsize ) #NEWLINE# #UNINDENT# contig += ( '\n                    for(int i=0; i<n; i++){\n                        %(index)s\n                        %(task_code)s;\n                    }\n                    ' % locals ( ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( contig is not None ) : #NEWLINE# #INDENT# z = list ( zip ( ( inames + onames ) , ( inputs + node . outputs ) ) ) #NEWLINE# cond1 = ' && ' . join ( [ ( 'PyArray_ISCONTIGUOUS(%s)' % arr ) for ( arr , var ) in z if ( not all ( var . broadcastable ) ) ] ) #NEWLINE# cond2 = ' && ' . join ( [ ( 'PyArray_ISFORTRAN(%s)' % arr ) for ( arr , var ) in z if ( not all ( var . broadcastable ) ) ] ) #NEWLINE# loop = ( '\n            if((%(cond1)s) || (%(cond2)s)){\n                %(contig)s\n            }else{\n                %(loop)s\n            }\n            ' % locals ( ) ) #NEWLINE# #UNINDENT# #UNINDENT# return ( decl , checks , alloc , loop )