<div dir="auto"><div dir="auto">The script seems to be computing the particle numbers for an array of chemical potentials.</div><div dir="auto"><br></div><div dir="auto">Two ways of speeding it up, both are likely simpler then using dask:</div><div dir="auto"><br></div><div dir="auto">First: use numpy</div><div dir="auto"><br></div><div dir="auto">1. Move constructing mu_all out of the loop (np.linspace)</div><div dir="auto">2. Arrange the integrands into a 2d array</div><div dir="auto">3. np.trapz along an axis which corresponds to a single integrand array</div><div dir="auto">(Or avoid the overhead of trapz by just implementing the trapezoid formula manually)</div><div dir="auto"><br></div><div dir="auto">Second:</div><div dir="auto"><br></div><div dir="auto">Move the loop into cython.</div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"></div><div dir="auto"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">вс, 11 окт. 2020 г., 9:32 Hongyi Zhao <<a href="mailto:hongyi.zhao@gmail.com" rel="noreferrer noreferrer" target="_blank">hongyi.zhao@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Sun, Oct 11, 2020 at 2:02 PM Andrea Gavana <<a href="mailto:andrea.gavana@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">andrea.gavana@gmail.com</a>> wrote:<br>
><br>
><br>
><br>
> On Sun, 11 Oct 2020 at 07.52, Hongyi Zhao <<a href="mailto:hongyi.zhao@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">hongyi.zhao@gmail.com</a>> wrote:<br>
>><br>
>> On Sun, Oct 11, 2020 at 1:33 PM Andrea Gavana <<a href="mailto:andrea.gavana@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">andrea.gavana@gmail.com</a>> wrote:<br>
>> ><br>
>> ><br>
>> ><br>
>> > On Sun, 11 Oct 2020 at 07.14, Andrea Gavana <<a href="mailto:andrea.gavana@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">andrea.gavana@gmail.com</a>> wrote:<br>
>> >><br>
>> >> Hi,<br>
>> >><br>
>> >> On Sun, 11 Oct 2020 at 00.27, Hongyi Zhao <<a href="mailto:hongyi.zhao@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">hongyi.zhao@gmail.com</a>> wrote:<br>
>> >>><br>
>> >>> On Sun, Oct 11, 2020 at 1:48 AM Robert Kern <<a href="mailto:robert.kern@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">robert.kern@gmail.com</a>> wrote:<br>
>> >>> ><br>
>> >>> > You don't need to use vectorize() on fermi(). fermi() will work just fine on arrays and should be much faster.<br>
>> >>><br>
>> >>> Yes, it really does the trick. See the following for the benchmark<br>
>> >>> based on your suggestion:<br>
>> >>><br>
>> >>> $ time python mu.py<br>
>> >>> [-10.999 -10.999 -10.999 ...  20.     20.     20.   ] [4.973e-84<br>
>> >>> 4.973e-84 4.973e-84 ... 4.973e-84 4.973e-84 4.973e-84]<br>
>> >>><br>
>> >>> real    0m41.056s<br>
>> >>> user    0m43.970s<br>
>> >>> sys    0m3.813s<br>
>> >>><br>
>> >>><br>
>> >>> But are there any ways to further improve/increase efficiency?<br>
>> >><br>
>> >><br>
>> >><br>
>> >> I believe it will get a bit better if you don’t column_stack an array 6000 times - maybe pre-allocate your output first?<br>
>> >><br>
>> >> Andrea.<br>
>> ><br>
>> ><br>
>> ><br>
>> > I’m sorry, scratch that: I’ve seen a ghost white space in front of your column_stack call and made me think you were stacking your results very many times, which is not the case.<br>
>><br>
>> Still not so clear on your solutions for this problem. Could you<br>
>> please post here the corresponding snippet of your enhancement?<br>
><br>
><br>
> I have no solution, I originally thought you were calling “column_stack” 6000 times in the loop, but that is not the case, I was mistaken. My apologies for that.<br>
><br>
> The timings of your approach is highly dependent on the size of your “energy” and “DOS” array -<br>
<br>
The size of the “energy” and “DOS” array is Problem-related and<br>
shouldn't be reduced arbitrarily.<br>
<br>
> not to mention calling trapz 6000 times in a loop.<br>
<br>
I'm currently thinking on parallelization the execution of the for<br>
loop, say, with joblib <<a href="https://github.com/joblib/joblib" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">https://github.com/joblib/joblib</a>>, but I still<br>
haven't figured out the corresponding codes. If you have some<br>
experience on this type of solution, could you please give me some<br>
more hints?<br>
<br>
>  Maybe there’s a better way to do it with another approach, but at the moment I can’t think of one...<br>
><br>
>><br>
>><br>
>> Regards,<br>
>> HY<br>
>> ><br>
>> >><br>
>> >><br>
>> >>><br>
>> >>><br>
>> >>> Regards,<br>
>> >>> HY<br>
>> >>><br>
>> >>> ><br>
>> >>> > On Sat, Oct 10, 2020, 8:23 AM Hongyi Zhao <<a href="mailto:hongyi.zhao@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">hongyi.zhao@gmail.com</a>> wrote:<br>
>> >>> >><br>
>> >>> >> Hi,<br>
>> >>> >><br>
>> >>> >> My environment is Ubuntu 20.04 and python 3.8.3 managed by pyenv. I<br>
>> >>> >> try to run the script<br>
>> >>> >> <<a href="https://notebook.rcc.uchicago.edu/files/acs.chemmater.9b05047/Data/bulk/dft/mu.py" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">https://notebook.rcc.uchicago.edu/files/acs.chemmater.9b05047/Data/bulk/dft/mu.py</a>>,<br>
>> >>> >> but it will keep running and never end. When I use 'Ctrl + c' to<br>
>> >>> >> terminate it, it will give the following output:<br>
>> >>> >><br>
>> >>> >> $ python mu.py<br>
>> >>> >> [-10.999 -10.999 -10.999 ...  20.     20.     20.   ] [4.973e-84<br>
>> >>> >> 4.973e-84 4.973e-84 ... 4.973e-84 4.973e-84 4.973e-84]<br>
>> >>> >><br>
>> >>> >> I have to terminate it and obtained the following information:<br>
>> >>> >><br>
>> >>> >> ^CTraceback (most recent call last):<br>
>> >>> >>   File "mu.py", line 38, in <module><br>
>> >>> >>     integrand=DOS*fermi_array(energy,mu,kT)<br>
>> >>> >>   File "/home/werner/.pyenv/versions/datasci/lib/python3.8/site-packages/numpy/lib/function_base.py",<br>
>> >>> >> line 2108, in __call__<br>
>> >>> >>     return self._vectorize_call(func=func, args=vargs)<br>
>> >>> >>   File "/home/werner/.pyenv/versions/datasci/lib/python3.8/site-packages/numpy/lib/function_base.py",<br>
>> >>> >> line 2192, in _vectorize_call<br>
>> >>> >>     outputs = ufunc(*inputs)<br>
>> >>> >>   File "mu.py", line 8, in fermi<br>
>> >>> >>     return 1./(exp((E-mu)/kT)+1)<br>
>> >>> >> KeyboardInterrupt<br>
>> >>> >><br>
>> >>> >><br>
>> >>> >> Any helps and hints for this problem will be highly appreciated?<br>
>> >>> >><br>
>> >>> >> Regards,<br>
>> >>> >> --<br>
>> >>> >> Hongyi Zhao <<a href="mailto:hongyi.zhao@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">hongyi.zhao@gmail.com</a>><br>
>> >>> >> _______________________________________________<br>
>> >>> >> NumPy-Discussion mailing list<br>
>> >>> >> <a href="mailto:NumPy-Discussion@python.org" rel="noreferrer noreferrer noreferrer" target="_blank">NumPy-Discussion@python.org</a><br>
>> >>> >> <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
>> >>> ><br>
>> >>> > _______________________________________________<br>
>> >>> > NumPy-Discussion mailing list<br>
>> >>> > <a href="mailto:NumPy-Discussion@python.org" rel="noreferrer noreferrer noreferrer" target="_blank">NumPy-Discussion@python.org</a><br>
>> >>> > <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
>> >>><br>
>> >>><br>
>> >>><br>
>> >>> --<br>
>> >>> Hongyi Zhao <<a href="mailto:hongyi.zhao@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">hongyi.zhao@gmail.com</a>><br>
>> >>> _______________________________________________<br>
>> >>> NumPy-Discussion mailing list<br>
>> >>> <a href="mailto:NumPy-Discussion@python.org" rel="noreferrer noreferrer noreferrer" target="_blank">NumPy-Discussion@python.org</a><br>
>> >>> <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
>> ><br>
>> > _______________________________________________<br>
>> > NumPy-Discussion mailing list<br>
>> > <a href="mailto:NumPy-Discussion@python.org" rel="noreferrer noreferrer noreferrer" target="_blank">NumPy-Discussion@python.org</a><br>
>> > <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
>><br>
>><br>
>><br>
>> --<br>
>> Hongyi Zhao <<a href="mailto:hongyi.zhao@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">hongyi.zhao@gmail.com</a>><br>
>> _______________________________________________<br>
>> NumPy-Discussion mailing list<br>
>> <a href="mailto:NumPy-Discussion@python.org" rel="noreferrer noreferrer noreferrer" target="_blank">NumPy-Discussion@python.org</a><br>
>> <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
><br>
> _______________________________________________<br>
> NumPy-Discussion mailing list<br>
> <a href="mailto:NumPy-Discussion@python.org" rel="noreferrer noreferrer noreferrer" target="_blank">NumPy-Discussion@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
<br>
<br>
<br>
-- <br>
Hongyi Zhao <<a href="mailto:hongyi.zhao@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">hongyi.zhao@gmail.com</a>><br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" rel="noreferrer noreferrer noreferrer" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div></div>