python - Using openMP with Cython: parallelising an inner loop -
(as see not familiar concepts python gil , multithreading in python (or cython))
i have written function in cython consists of fragment of code double loop function f called repeatedly.
for in range(i): j in range(j): res=f(a[i],b[j]) i have machine 4 cpus cores , want parallelise not first second loop. found this wonderful website not treat case of inner loop , not go details. in opinion can write:
for in range(i): #in case can release gil safely ? necessary @ ? nogil, parallel(num_threads=4): j in prange(j,shedule="dynamic"): res=f(a[i],b[j]) would work ? have put nogil outside of both loops doesn't run repeatedely releasing , "capturing" gil thing ? explain me how , logic behind writing such statements able generalize unseeen problems.
there time cost releasing , recapturing gil , time cost setting parallel loop. reason it's better make outermost loop parallel one. however, if have reason why want parallelize inner loop work , cost should small compared real work contained in f.
releasing gil prevents accessing python variables , calling python functions. typed cython variables, cdef functions , cython memoryviews work fine. you'll small speed-up putting with nogil: far out possible. therefore put round outer loop if possible, if isn't possible it's ok you've shown it.
it necessary release gil prange loop. if necessary can reclaim inside loop (with gil) try small fractions of loop , if needed (code needs gil can't run in parallel other code needs gil).
the line res=f(a[i],b[j]) odd parallel code since res last loop saved. typically you'd write elements of array (e.g. res[i,j]=f(a[i],b[j])). however, there may reason doing you've shown...
cython (usually) warn if try requires gil idea try , see.
Comments
Post a Comment