Source code for mandelbrot_benchmark.backends.numba
fromtypingimportAnyimportnumbafromnumba.cudaimportas_cuda_arrayfromnumba.cuda.cudadrv.errorimportCudaSupportErrordef_mandelbrot_32(c:Any)->Any:"""Pure Python implementation of the Mandelbrot set."""counter=numba.int32(200)z=numba.complex64(0)foriinrange(200):z=z*z+cifz.real**2+z.imag**2>=4:counter=ibreakreturncounter_mandelbrot_numba=numba.vectorize([numba.int32(numba.complex64)],target="parallel",nopython=True,fastmath=True)(_mandelbrot_32)try:_mandelbrot_numba_cuda=numba.vectorize([numba.int32(numba.complex64)],target="cuda",)(_mandelbrot_32)exceptCudaSupportError:_mandelbrot_numba_cuda=None
[docs]defmandelbrot_numba(c:Any)->Any:""" Numba implementation of the Mandelbrot set. Parameters ---------- c : Any Input array of complex numbers. Returns ------- Any Output array of integers representing the Mandelbrot set. """if"cuda"instr(c.device):c=as_cuda_array(c)return_mandelbrot_numba_cuda(c)else:return_mandelbrot_numba(c)