openMP:并行运行所有线程会导致内存不足异常 [英] openMP: Running with all threads in parallel leads to out-of-memory-exceptions
问题描述
我想缩短冗长的图像处理算法的运行时间,该算法通过使用 openMP 并行处理应用于多个图像.
I want to shorten the runtime of an lengthy image processing algorithm, which is applied to multiple images by using parallel processing with openMP.
该算法适用于单个或有限数量 (=2) 的线程.
The algorithm works fine with single or limited number (=2) of threads.
但是:当使用最大数量的可能线程运行时,使用 openMP 的并行处理需要大量内存,从而导致内存不足异常.
But: The parallel processing with openMP requires lots of memory, leading to out-of-memory-exceptions, when running with the maximum number of possible threads.
为了解决这个问题,我将抛出异常"替换为等待空闲内存",以防内存不足,导致很多(<= all
) 线程只是在等待空闲内存...
To resolve the issue, I replaced the "throwing of exceptions" with a "waiting for free memory" in case of low memory, leading to many (<= all
) threads just waiting for free memory...
是否有任何解决方案/工具/方法可以根据可用内存动态维护内存或启动线程?
Is there any solution/tool/approach to dynamically maintain the memory or start threads depending on available memory?
推荐答案
尝试编译 64 位程序.32 位程序最多只能有 2^32 = 大约 4GB 的内存.64 位程序可以使用更多(2^64,即 18 艾字节).现在很容易达到 4GB 的内存.
Try compiling your program 64-bit. 32-bit programs can only have up to 2^32 = about 4GB of memory. 64-bit programs can use significantly more (2^64 which is 18 exabytes). It's very easy to hit 4GB of memory these days.
请注意,如果您使用的 RAM 多于可用内存,您的操作系统将不得不将一些内存分页到磁盘.这会严重影响性能.如果您到了这一点(您使用了很大一部分 RAM)并且仍然有额外的内核,您将必须更深入地研究算法以找到更细粒度的部分来并行化.
Note that if you are using more RAM than you have available, your OS will have to page some memory to disk. This can hurt performance a lot. If you get to this point (where you are using a significant portion of RAM) and still have extra cores, you would have to go deeper into the algorithm to find a more granular section to parallelize.
如果您由于某种原因无法切换到 64 位,您可以进行多处理(运行一个程序的多个实例),这样每个进程最多可以有 4GB.您需要以某种方式启动和协调流程.根据您的需要,这可能意味着使用简单的命令行参数或复杂的进程间通信 (IPC).OpenMP 不做 IPC,但 Open MPI 做.Open MPI 通常用于网络上多个节点之间的通信,但也可以将其设置为在一台机器上运行并发实例.
If you for some reason can't switch to 64-bit, you can do multiprocessing (running multiple instances of a program) so each process will have up to 4GB. You will need to launch and coordinate the processes somehow. Depending on your needs, this could mean using simple command-line arguments or complicated inter-process communication (IPC). OpenMP doesn't do IPC, but Open MPI does. Open MPI is generally used for communication between many nodes on a network, but it can be set up to run concurrent instances on one machine.
这篇关于openMP:并行运行所有线程会导致内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!