I am trying to cythonise something I did which involves random number generation inside a parallelised loop. I wanted to use mtrand
but since it's Python code it can't work from a nogil
block and for some reason mtrand
's .pyx isn't exposed for the rest of us to use.
我试图cythonise我做的一些涉及并行循环内的随机数生成。我想使用mtrand,但由于它是Python代码,因此无法使用nogil块,并且出于某种原因mtrand的.pyx不会暴露给我们其他人使用。
I know I can use rand
or any other C RNG (e.g. gsl
); is there a more standard way?
我知道我可以使用rand或任何其他C RNG(例如gsl);有更标准的方法吗?
1 个解决方案
#1
2
You have summed up the situation correctly. As of this writing, you can do one of three things:
你已经正确地总结了这种情况。在撰写本文时,您可以执行以下三种操作之一:
-
Modify NumPy to allow sharing the declarations in mtrand.pxd
修改NumPy以允许在mtrand.pxd*享声明
-
Use NumPy's random generators through their default interface (perhaps you could store all the random numbers in advance outside of the
nogil
block?)通过NumPy的默认界面使用NumPy的随机生成器(也许您可以在nogil块之外预先存储所有随机数?)
-
Use a random number generator written in C (or possibly C++ if you are having Cython generate C++ code).
使用用C编写的随机数生成器(如果你有Cython生成C ++代码,可能使用C ++)。
Honestly, I'd probably do the last one. If you can use C++ 11, there are several good random number generators now included in the C++ standard library that you could use.
老实说,我可能会做最后一个。如果你可以使用C ++ 11,那么你可以使用C ++标准库中包含的几个好的随机数生成器。
#1
2
You have summed up the situation correctly. As of this writing, you can do one of three things:
你已经正确地总结了这种情况。在撰写本文时,您可以执行以下三种操作之一:
-
Modify NumPy to allow sharing the declarations in mtrand.pxd
修改NumPy以允许在mtrand.pxd*享声明
-
Use NumPy's random generators through their default interface (perhaps you could store all the random numbers in advance outside of the
nogil
block?)通过NumPy的默认界面使用NumPy的随机生成器(也许您可以在nogil块之外预先存储所有随机数?)
-
Use a random number generator written in C (or possibly C++ if you are having Cython generate C++ code).
使用用C编写的随机数生成器(如果你有Cython生成C ++代码,可能使用C ++)。
Honestly, I'd probably do the last one. If you can use C++ 11, there are several good random number generators now included in the C++ standard library that you could use.
老实说,我可能会做最后一个。如果你可以使用C ++ 11,那么你可以使用C ++标准库中包含的几个好的随机数生成器。