Reputation: 587
Computing higher order derivatives is a nested function and can be quite consuming. I tried to use the parallel programming to speed up the nested function.
from concurrent.futures import ThreadPoolExecutor
import os
def numerical_derivative_par(f, x, n=1, h=mp.mpf("1e-6"), num_workers=None):
"""
Compute the numerical derivative of a function at a given point using the central difference method.
Parameters:
- f: The function to differentiate.
- x: The point at which to compute the derivative.
- n: The order of the derivative to compute (default: 1).
- h: The step size for the finite difference approximation (default: 1e-6).
- num_workers: The number of parallel workers to use (default: None, uses all available CPU cores).
Returns:
- The numerical derivative of the function at the given point.
"""
if num_workers is None:
num_workers = os.cpu_count()
if n == 0:
return f(x)
elif n == 1:
return (f(x + h) - f(x - h)) / (mp.mpf("2") * h)
else:
with ThreadPoolExecutor(max_workers=num_workers) as executor:
futures = []
for sign in [+1, -1]:
future = executor.submit(numerical_derivative_par, f, x + sign * h, n - 1, h, num_workers)
futures.append(future)
results = [future.result() for future in futures]
return (results[0] - results[1]) / (mp.mpf("2") * h)
However, somehow the parallel function was much slower than the original nested function. I suspected that it's because something went wrong with the with ThreadPoolExecutor(max_workers=num_workers) as executor:
assignment. But by changing the num_workers
it's still much slower than the original nested function.
Is the original nested function already parallelized by python mechanically, i.e. does the nested calling automatically separated to different threads? What went wrong with the above code and how to improve it?
Is it necessary to parallelize the nested function in python?
Upvotes: 0
Views: 24