numba jit nopython (@njit) allows to reduce process time and works quite well with numpy.

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
Post Reply
JeffWitz
Posts: 54
Joined: Fri Mar 27, 2015 9:14 am
Location: Lille, France

numba jit nopython (@njit) allows to reduce process time and works quite well with numpy.

Post by JeffWitz »

Kunda1 wrote: Wed Jul 28, 2021 10:44 pm + we definitely could us a cplusplus dev or more to translate some of these awesome python NURBS based code to speed up their execution.

Edit: @JeffWitz would you be interested in making a chart comparison ? That way we can grok where each workbench is at?
@Kunda1 : Sorry I am no Cpp dev. I use python. But when I have issues on speed I usually try numba jit nopython (@njit), it really allows to reduce process time and works quite well with numpy. So may be it could be possible to speed up the slow part of the workbench with numba ?
Last edited by Kunda1 on Tue Aug 10, 2021 12:42 pm, edited 1 time in total.
Reason: Split off from https://forum.freecadweb.org/viewtopic.php?f=8&t=58132
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: [Request] A dedicated NURBS workbench

Post by Kunda1 »

What's one more dependency to our ever-growing list?!? :lol: :lol: :lol: :lol: :(

But nevertheless, thanks for the heads up. It would be cool to see it in action as a PoC in FreeCAD
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
JeffWitz
Posts: 54
Joined: Fri Mar 27, 2015 9:14 am
Location: Lille, France

Re: [Request] A dedicated NURBS workbench

Post by JeffWitz »

Kunda1 wrote: Sat Jul 31, 2021 10:16 pm What's one more dependency to our ever-growing list?!? :lol: :lol: :lol: :lol: :(

But nevertheless, thanks for the heads up. It would be cool to see it in action as a PoC in FreeCAD
I agree ! Here is a little example of code that will help you to see how it works and what are the gains :

Code: Select all

from numba import njit, jit, vectorize,prange
import numpy as np
import time
A=np.ones((6000,7000))
B=A.copy()
C=B.copy()

@njit(parallel=True,fastmath=True)
def add_mat_njit(A,B):
  return A+2.*B/A+6*A/B 


@njit(parallel=True,fastmath=True)
def add_mat_loop_njit(A,B,C):
  for i in prange(0,A.shape[0]):
    for j in prange(0,A.shape[1]):
      C[i,j]=A[i,j]+2.*B[i,j]/A[i,j]+6*A[i,j]/B[i,j]
  return C
  
@vectorize
def add_mat_vec(A,B):
  return A+2.*B/A+6*A/B 
  
add_mat_vec(A,B)
add_mat_njit(A,B)
add_mat_loop_njit(A,B,C)

t0=time.time()
for i in range(0,10):
   add_mat_njit(A,B) 
print("time in ms for numba nopython parallel with numpy version: {}".format((time.time()-t0)*100)) 

t0=time.time() 
for i in range(0,10):
   add_mat_vec(A,B)
print("time in ms for numba vectorize version: {}".format((time.time()-t0)*100))

t0=time.time() 
for i in range(0,10):
   add_mat_loop_njit(A,B,C) 
print("time in ms for numba nopython parallel with double loop over array version: {}".format((time.time()-t0)*100))

t0=time.time() 
for i in range(0,10):
   A+2.*B/A+6*A/B 
print("time in ms for numpy version: {}".format((time.time()-t0)*100))
With this res on my laptop :
time in ms for numba nopython parallel with numpy version: 65.48645496368408
time in ms for numba vectorize version: 75.65851211547852
time in ms for numba nopython parallel with double loop over array version: 59.387898445129395
time in ms for numpy version: 291.48826599121094

Note that if you want to // the code with explicit loops, you have to use prange instead of range

You can see that it is easy to change, easy to test and that it is simple to change for loops that can help, because working with array in numpy can lead to poor readability of the code.

It is absolutely necessary to be able to use nopython in order to have an increase of performances, it means that it could be necessary to isolate some object or create one that allows nopython.
Post Reply