C++如何设置环境变量,因此OpenBlas运行多线程
作者建议以下内容: https://github.com/xianyi/openblas
Setting the number of threads using environment variables
Environment variables are used to specify a maximum number of threads. For example,
export OPENBLAS_NUM_THREADS=4
export GOTO_NUM_THREADS=4
export OMP_NUM_THREADS=4
The priorities are OPENBLAS_NUM_THREADS > GOTO_NUM_THREADS > OMP_NUM_THREADS.
If you compile this library with USE_OPENMP=1, you should set the OMP_NUM_THREADS environment variable; OpenBLAS ignores OPENBLAS_NUM_THREADS and GOTO_NUM_THREADS when compiled with USE_OPENMP=1.
当我使用“ export opent opent opent” main.cpp,我收到有关模板的错误。
因此,我将cmakelist.txt文件更改为:
set($ENV{OPENBLAS_NUM_THREADS} 16)
这似乎对我的应用程序的线程没有影响。我只看到100%的CPU核心。
The author recommends the following:
https://github.com/xianyi/OpenBLAS
Setting the number of threads using environment variables
Environment variables are used to specify a maximum number of threads. For example,
export OPENBLAS_NUM_THREADS=4
export GOTO_NUM_THREADS=4
export OMP_NUM_THREADS=4
The priorities are OPENBLAS_NUM_THREADS > GOTO_NUM_THREADS > OMP_NUM_THREADS.
If you compile this library with USE_OPENMP=1, you should set the OMP_NUM_THREADS environment variable; OpenBLAS ignores OPENBLAS_NUM_THREADS and GOTO_NUM_THREADS when compiled with USE_OPENMP=1.
When I use "export OPENBLAS_NUM_THREADS=16" in my main.cpp, I get an error about templates.
So, I changed my CMakeList.txt file to include:
set($ENV{OPENBLAS_NUM_THREADS} 16)
This seemed to have no effect on the threading of my application. I only see 1 CPU core at 100%.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OpenBlas_num_threads
是一个运行时定义的变量从运行时)。请注意,
导出OpenBlas_num_threads = 16
是 bash命令,而不是要放入C ++文件中的东西。它的目的是设置环境变量OpenBlas_num_threads
,以便在您的应用程序调用BLAS函数时,可以在运行时读取它。您应该做类似的事情:这确实没有效果,因为变量不应在编译时使用。
请注意,设置
OpenBlas_num_threads
可能不足以在实践中使用多个线程。如果您的矩阵很小,请考虑阅读此非常重新发布的帖子关于OpenBlas如何与多个线程一起使用。OPENBLAS_NUM_THREADS
is a runtime defined variable so it should not impact the build of an application unless the build scripts explicitly use this variable which is very unusual and a very bad idea (since the compile-time environment can be different from the run-time one).Note that
export OPENBLAS_NUM_THREADS=16
is a bash command and not something to put in a C++ file. Its purpose is to set the environment variableOPENBLAS_NUM_THREADS
so it can be read at runtime by OpenBLAS when your application call a BLAS function. You should do something like:This should have not effect indeed because the variable should not be used at compile time.
Note that setting
OPENBLAS_NUM_THREADS
may not be enough to use multiple threads in practice. If your matrices are small, then consider reading this very-recent post about how OpenBLAS works with multiple threads.