世界,
我尝试使用多个线程运行一个C++应用程序(用VS作为.exe编译),并为此使用QThread或omp并行化。每个线程在用umfpack求解由这些矩阵构建的方程系统之前,都会对内存进行多次分配/释放,以完成大量的矩阵计算。现在,当我使用太多的线程时,我失去了性能,因为线程在执行此操作时彼此阻塞。我已经读过内存(de)-allocation一次只能用于一个线程(就像互斥条件)。
我已经尝试过的是:
在我的设置中,在性能下降之前,我可以使用~4个线程(每个线程使用~1.5GB RAM)。有趣的是--但有些事情我还不能想清楚--只有在几个线程完成后,新的线程才会取代它,表演才会减少。还请注意,线程之间不相互依赖,没有其他阻塞条件,每个线程运行的时间大致相同(~2分钟)。
是否有一种“简单的方法”--例如以某种方式设置堆/堆栈--来解决这个问题?
下面是一些代码片段:
// Loop to start threads
forever
{
if (sem.tryAcquire(1)) {
QThread *t = new QThread();
connect(t, SIGNAL(started()), aktBer, SLOT(doWork()));
connect(aktBer, SIGNAL(workFinished()), t, SLOT(quit()));
connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
aktBer->moveToThread(t);
t->start();
sleep(1);
}
else {
//... wait for threads to end before starting new ones
//... eventually break
}
qApp->processEvents();
}
void doWork() {
// Do initial matrix stuff...
// Initializing array pointers for umfpack-lib
static int *Ap=0;
static int *Ai=0;
static int *Ax=0;
static int *x=0;
static int *b=0;
// Private static Variablen per thread
#pragma omp threadprivate(Ap, Ai, Acol, Arow)
// Solving -> this is the part where the threads block each other, note, that
there are other functions with matrix operations, which also (de-)/allocate a
lot
status = umfpack_di_solve (UMFPACK_A, Ap,Ai,Ax,x,b, /*...*/);
emit(workFinished());
}发布于 2022-06-29 08:48:09
对于那些对我的解决方案感兴趣的人:我在我的应用程序中添加了另一个分配器(正如@Ben建议的那样)。在我的例子中,我选择了mimalloc,因为它似乎得到了正常的维护(即使是由microsoft本身),并且可以很容易地包含进来。见此处: Mimalloc
https://stackoverflow.com/questions/71516613
复制相似问题