0

I've got this Python function which I'm trying to @jit, but I can't really get around it (it's the first time I try jitting something, so I'm here to learn more than anything). I'll include the code (commented) and I'll explain what it does briefly:

def pwrchk(n, m):
no_prob = [] #The list that will contain the exponents

    for i in range(2, m+1): #Start from 2 because 1 is useless
        power = n**i

        is_good = True #If this becomes false later, there's a zero.

        for j in range(math.floor(math.log(power, 10)) + 1): 
            #The formula in the range is the number of digits of 'power'

            digit = power % 10 #Returns the last digit so it can be checked

            if digit in digits:
                is_good = False #This is the check
                power = 0
                break

            power = power // 10 #Gets rid of the digit just checked

        if is_good:
            no_prob.append(i) #Append to the list of "good" exponents

    return no_prob

This function computes n^i , with 2 < i < m, and checks if n^i contains a zero in its digits, and then returns a list of which exponents are such that n^i contains no zeros. It works absolutely fine in normal Python compilation.

Since for big values of m the execution time gets really long (I've tried m = 10^6 and it goes to a crawl), I thought of putting it in Anaconda to @jit it. The problem is that when I use the @jit decorator it tells me that it keeps falling back to object mode, so I can't compile it in nopython mode.

I tried changing the lists to numpy arrays and populating them with the powers of n out of the for cycle, changing math.floor(math.log(power, 10)) using numpy to manage the arrays, nothing.

Am I doing something wrong? I'm sure there's a simple explanation to it that's just going over my head, but as said before I'm kinda new to Numba, so if I'm doing something really dumb please tell me, so I won't do it again in the future, and if I need to provide something else I'll update with anything needed.

talonmies
  • 70,661
  • 34
  • 192
  • 269
  • numba can't determine the type of `digits` because you did not define it within the function's scope – bug_spray May 15 '20 at 12:11
  • Also `@jit`, by itself, has nothing to do with GPU acceleration, so if you imagine anything you have shown here is magically going to run on a GPU, unfortunately you are mistaken – talonmies May 15 '20 at 12:19
  • @talonmies as I said, I'm a complete newbie to this concept, and I actually didn't know that. What I thought the decorator would do was re-compile the function in such a way that it's more suited for running on a GPU, so thank you, because this definetely means I need to look much deeper into the subject. – Francesco Bambina May 15 '20 at 12:25
  • @bug_spray I'll try and get that sorted, but with the information talonmies shared it's most probably not the only issue. – Francesco Bambina May 15 '20 at 12:26
  • @FrancescoBambina: If you want GPU acceleration, then you need to explicitly specific it (either `@cuda.jit` or target="cuda") – talonmies May 15 '20 at 12:53
  • Here's a post about [JIT compilation](https://stackoverflow.com/questions/95635/what-does-a-just-in-time-jit-compiler-do) – bug_spray May 15 '20 at 17:27

0 Answers0