Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding 0 to a variable fixes compilation error #2267

Open
gamask opened this issue Dec 5, 2024 · 0 comments
Open

Adding 0 to a variable fixes compilation error #2267

gamask opened this issue Dec 5, 2024 · 0 comments

Comments

@gamask
Copy link

gamask commented Dec 5, 2024

Hi, me again with my insane function that crashes all over the place.
This time I got a real fun one!

import math
import numpy



input_order = [
    'nym', 'nf',
    'ebci', 'ebui', 'bbi', 'eaci', 'eaui', 'bai', 'egc', 'egu', 'bg', 'edc', 'edu', 'bi', 'edgc', 'edgu', 'erc', 'eru', 'br', 'chu', 'af', 'lp', 'ly',
    'acti',
    
    's'
]

input_indices = dict([(k,i) for i,k in enumerate(input_order)])


def get_index (acti, acc, cnum):
    for item in acti:
        if item[0] == acc and item[1] == cnum:
            return item[2]
    return None


#pythran export n_steps
n_steps = 9

#pythran export n_state_vars
n_state_vars = 12

cache_indices = {
    'ec'        : 0,
    'eu'        : 1,
    'b'         : 2,
    'cgc'       : 3,
    'cgu'       : 4,
    'cgb'       : 5,
    'c'         : 6,
    'poc'       : 7,
    'ed'        : 8,
    'ta'        : 9,
    'rdwa'      : 10,
    'max_rdwa'  : 11,
}


#pythran export set_cache (
#   float64 [], int, int, float64 [][][][][][] order(C),
#   (
#       int, int,
#       float64 [], float64 [], float64 [], float64 [], float64 [], float64 [], float64 [][] order(C), float64 [][] order(C), float64 [][] order(C), float64 [][] order(C), float64 [][] order(C), float64 [][] order(C), float64 [][] order(C), float64 [][] order(C), float64 [][] order(C), float64 [][] order(C), float64 [][] order(C), float64 [], float64 [], float64 [], float64 [],
#       (str, int, int) list,
#       
#       float64
#   )
#)
def set_cache (x, cnum, cache_index, cache, inputs):
    nym     = inputs[input_indices['nym']]
    nf      = inputs[input_indices['nf']]
    acti    = inputs[input_indices['acti']]
    s       = inputs[input_indices['s']]
    
    index = get_index (acti, 'a', cnum)
    #if index is None:
        #return
    
    ebci    = inputs[input_indices['ebci']][cnum]
    ebui    = inputs[input_indices['ebui']][cnum]
    bbi     = inputs[input_indices['bbi']][cnum]
    eaci    = inputs[input_indices['eaci']][cnum]
    eaui    = inputs[input_indices['eaui']][cnum]
    bai     = inputs[input_indices['bai']][cnum]
    egc     = inputs[input_indices['egc']][cnum] / 100.
    egu     = inputs[input_indices['egu']][cnum] / 100.
    bg      = inputs[input_indices['bg']][cnum] / 100.
    edc     = inputs[input_indices['edc']][cnum] / 100.
    edu     = inputs[input_indices['edu']][cnum] / 100.
    bi      = inputs[input_indices['bi']][cnum] / 100.
    edgc    = inputs[input_indices['edgc']][cnum] / 100.
    edgu    = inputs[input_indices['edgu']][cnum] / 100.
    erc     = inputs[input_indices['erc']][cnum] / 100.
    eru     = inputs[input_indices['eru']][cnum] / 100.
    br      = inputs[input_indices['br']][cnum] / 100.
    chu     = inputs[input_indices['chu']][cnum] / 100.
    af      = inputs[input_indices['af']][cnum] / 100.
    lp      = inputs[input_indices['lp']][cnum]
    ly      = inputs[input_indices['ly']][cnum]
    
    ebc = ebci
    ebu = ebui
    b   = bbi
    cgc = ebc - eaci
    cgu = ebu - eaui
    cgb = b - bai
    
    
    tbi = ebci + ebui + bbi
    
    if tbi != 0:
        ecf = ebci / tbi
        euf = ebui / tbi
        bf = bbi / tbi
    else:
        #iec = egc != 0 or edc != 0 or edgc != 0
        #ieu = egu != 0 or edu != 0 or edgu != 0
        #ib = bg != 0 or bi != 0
        iec = False
        ieu = False
        ib = False
        if egc != 0 or edc != 0 or edgc != 0:
            iec = True
        if egu != 0 or edu != 0 or edgu != 0:
            ieu = True
        if bg != 0 or bi != 0:
            ib = True
        
        ni = [iec, ieu, ib].count(True)
        
        if ni == 0:
            ecf = 1
            euf = 0
            bf = 0
        else:
            ecf = 1 / ni
            euf = 1 / ni
            bf = 1 / ni
    
    # ---------------------------------------
    
    def f1 (ats, which, c, ta, ebc, ebu, b, cgc, cgu, cgb):
        if which == 'c':
            ba = ebc
            cg = cgc
        elif which == 'u':
            ba = ebu
            cg = cgu
        elif which == 'b':
            ba = b
            cg = cgb
        else:
            raise Exception('which = %s' % (str(which)))
            
        fs = ats / ba
        
        tag = min(cg * fs, 10 * s) * 0.2 + max(cg * fs - 10 * s, 0) * 0.35
        
        c_delta = ats
        c += ats
        ta += tag
        if which == 'c':
            ebc -= ats
            cgc *= (1 - fs)
        elif which == 'u':
            ebu -= ats
            cgu *= (1 - fs)
        elif which == 'b':
            b -= ats
            cgb *= (1 - fs)
        else:
            raise Exception('which = %s' % (str(which)))
        
        return c_delta, c, ta, ebc, ebu, b, cgc, cgu, cgb
    
    # ---------------------------------------
    
    if index is not None:
        if cache_index >= 2:
            ny_min = cache_index
        else:
            ny_min = 0
        
        if ny_min != 0:
            for ny in range(0, ny_min):
                cache[cache_index,ny,cnum,:,:,:] = cache[0,ny,cnum,:,:,:]
        
        
        for ny in range(ny_min, nym + 1):
            if ny != 0:
                y = ny - 1
            else:
                y = 0
            
            
            c = 0.
            poc = 0.
            ed = 0.
            ta = 0.
            max_rdwa = 0.
            
            
            if ny != 0:
                ebc = cache[cache_index][ny - 1][cnum][0][n_steps - 1][cache_indices['ec']]
                ebu = cache[cache_index][ny - 1][cnum][0][n_steps - 1][cache_indices['eu']]
                b   = cache[cache_index][ny - 1][cnum][0][n_steps - 1][cache_indices['b']]
                cgc = cache[cache_index][ny - 1][cnum][0][n_steps - 1][cache_indices['cgc']]
                cgu = cache[cache_index][ny - 1][cnum][0][n_steps - 1][cache_indices['cgu']]
                cgb = cache[cache_index][ny - 1][cnum][0][n_steps - 1][cache_indices['cgb']]
            
            # ----------------
            
            for j in range(2):
                
                
                
                if ny == 0 or j == 1:
                    rdwa = 0.
                else:
                    rdwa = x[index + y * nf]
                
                
                if j == 1:
                    li = True
                else:
                    li = False
                
                
                if j == 0:
                    tbjls = ebc + ebu + b
                
                # -------------------
                
                if ny != 0 and j == 0:
                    
                    # AAA
                    
                    if ebc > 0:
                        delta = ebc * egc[y]
                        ebc += delta
                        cgc += delta
                    
                    if ebu > 0:
                        delta = ebu * egu[y]
                        ebu += delta
                        cgu += delta
                    
                    if b > 0:
                        delta = b * bg[y]
                        b += delta
                        cgb += delta
                    
                    if ebc > 0:
                        delta = ebc * edgc[y]
                        c += delta
                        ta += delta * 1.3
                    
                    if ebu > 0:
                        delta = ebu * edgu[y]
                        c += delta
                        ta += delta * 1.4
                    
                    if b > 0:
                        delta = b * bi[y]
                        c += delta
                    
                    # /AAA
                    
                    # ---------
                    
                    # 5.
                    
                    if ebc > 0:
                        full_delta = ebc * erc[y]
                        delta1 = min(full_delta, ebc - cgc)
                        delta2 = full_delta - delta1
                        c += delta1
                        ebc -= delta1
                        if delta2 != 0:
                            #delta, c, ta, ebc, ebu, b, cgc, cgu, cgb = f1 (delta2, 'c', c, ta, ebc, ebu, b, cgc, cgu, cgb)
                            
                            delta, c_, ta_, ebc_, ebu_, b_, cgc_, cgu_, cgb_ = f1 (delta2, 'c', c, ta, ebc, ebu, b, cgc, cgu, cgb)
                            c = c_; ta = ta_; ebc = ebc_; ebu = ebu_; b = b_; cgc = cgc_; cgu = cgu_; cgb = cgb_
                            #c = c_; ta = ta_; ebc = ebc_; ebu = ebu_; b = b_; cgc = cgc_; cgu = cgu_; cgb = cgb_ + 0.

This code fails to compile as is.
At the very end there's a commented out line that's identical to the one above it, except that we add 0. to one variable.
Doing that fixes the crash.

Also should mention that this line:

#delta, c, ta, ebc, ebu, b, cgc, cgu, cgb = f1 (delta2, 'c', c, ta, ebc, ebu, b, cgc, cgu, cgb)

also breaks compilation.
That's why I'm doing this ridiculous stuff in the first place:

delta, c_, ta_, ebc_, ebu_, b_, cgc_, cgu_, cgb_ = f1 (delta2, 'c', c, ta, ebc, ebu, b, cgc, cgu, cgb)
c = c_; ta = ta_; ebc = ebc_; ebu = ebu_; b = b_; cgc = cgc_; cgu = cgu_; cgb = cgb_

If we move some stuff around randomly in section AAA or just get rid of some random lines there, the crash also might go away.
Like sometimes I have to add 0. to two or more variables to make the crash go away, depending on what random changes I made elsewhere.
This is what I meant in #2216 when I said that this function was a nightmare to compile.
Things make no sense at all, tiny changes here or there either break it completely or fix it.

I have many more examples, but it takes a lot of time to recreate them.
The code where I originally encountered them is long gone, and tiny changes can magically fix things.
Should I invest more time into this?
It might be the case that all of this insanity has the same cause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant