C++如何设置环境变量,因此OpenBlas运行多线程

发布于 2025-02-08 10:54:02 字数 876 浏览 3 评论 0原文

作者建议以下内容: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮酒 2025-02-15 10:54:02

当我在Main.cpp中使用“ Export OpenBlas_num_threads = 16”时,我会收到有关模板的错误。

OpenBlas_num_threads是一个运行时定义的变量从运行时)。

请注意,导出OpenBlas_num_threads = 16 bash命令,而不是要放入C ++文件中的东西。它的目的是设置环境变量OpenBlas_num_threads,以便在您的应用程序调用BLAS函数时,可以在运行时读取它。您应该做类似的事情:

# Build part
cmake  # with the correct parameters
make

# Running part
export OPENBLAS_NUM_THREADS=4
./your_application  # with the correct parameters

# Alternative solution:
# OPENBLAS_NUM_THREADS=4 ./your_application

所以,我更改了我的cmakelist.txt文件,包括:

这确实没有效果,因为变量不应在编译时使用。

我只看到100%

的1个CPU核心

请注意,设置OpenBlas_num_threads可能不足以在实践中使用多个线程。如果您的矩阵很小,请考虑阅读此非常重新发布的帖子关于OpenBlas如何与多个线程一起使用。

When I use "export OPENBLAS_NUM_THREADS=16" in my main.cpp, I get an error about templates.

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 variable OPENBLAS_NUM_THREADS so it can be read at runtime by OpenBLAS when your application call a BLAS function. You should do something like:

# Build part
cmake  # with the correct parameters
make

# Running part
export OPENBLAS_NUM_THREADS=4
./your_application  # with the correct parameters

# Alternative solution:
# OPENBLAS_NUM_THREADS=4 ./your_application

So, I changed my CMakeList.txt file to include:

This should have not effect indeed because the variable should not be used at compile time.

I only see 1 CPU core at 100%

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文