我有以下Fortran代码调用C ++函数parallel_klu
。 parallel_klu
每次调用时都会创建八个线程(执行另一个名为factor
的函数),在它返回Fortran之后,线程就会被销毁。
program linkFwithCPP
use iso_c_binding
implicit none
interface
subroutine my_routine (Ax, Ai, Ap, b, n, nz) bind (C, name = "parallel_klu")
import :: c_double
import :: c_int
integer (c_int), intent (out), dimension (*) :: Ap
integer (c_int), intent (out), dimension (*) :: Ai
real (c_double), intent (out), dimension (*) :: Ax
real (c_double), intent (out), dimension (*) :: b
integer(c_int), value :: n
integer(c_int), value :: nz
end subroutine
end interface
integer,parameter ::n=10
integer,parameter ::nz=34
! declare the 4 arrays
real (c_double), dimension (0:nz-1) :: Ax
real (c_double), dimension (0:n-1) :: b
integer (c_int), dimension (0:n) :: Ap
integer (c_int), dimension (0:nz-1) :: Ai
!Inputing array values
open(unit = 1, file = 'Ax.txt')
read(1,*) Ax
close(1)
open(unit = 2, file = 'Ai.txt')
read(2,*) Ai
close(2)
open(unit = 3, file = 'Ap.txt')
read(3,*) Ap
close(3)
open(unit = 4, file = 'b.txt')
read(4,*) b
close(4)
! Call C++ function
call my_routine(Ax, Ai, Ap, b, n, nz)
write (*, *) "Fortran: b:", b
pause
end program linkFwithCPP
C ++中的函数如下:
void parallel_klu(double Ax[], int Ai[], int Ap[], double b[], const int n, const int nz)
{
threadList.push_back(std::thread(factor, Ap, Ai, Ax, b, Q, P, Pinv, n, 0, 5000));
threadList.push_back(std::thread(factor, Ap, Ai, Ax, b, Q, P, Pinv, n, 5000, 10000));
threadList.push_back(std::thread(factor, Ap, Ai, Ax, b, Q, P, Pinv, n, 10000, 15000));
threadList.push_back(std::thread(factor, Ap, Ai, Ax, b, Q, P, Pinv, n, 15000, 20000));
threadList.push_back(std::thread(factor, Ap, Ai, Ax, b, Q, P, Pinv, n, 20000, 25000));
threadList.push_back(std::thread(factor, Ap, Ai, Ax, b, Q, P, Pinv, n, 25000, 30000));
threadList.push_back(std::thread(factor, Ap, Ai, Ax, b, Q, P, Pinv, n, 30000, 35000));
threadList.push_back(std::thread(factor, Ap, Ai, Ax, b, Q, P, Pinv, n, 35000, 40000));
// wait for all threads to finish
for (auto& threadID : threadList){
threadID.join();
}
}
每次调用函数parallel_klu
时,有没有办法避免创建8个线程?有点像为第一次调用创建它们然后只是发送一个信号让线程再次执行。
答案 0 :(得分:0)
首先,我强烈建议弄清楚线程创建是否会破坏应用程序的性能。
如果是,你可以使用一个称为线程池的概念,它基本上是一组可以发送任务的线程,并且当没有任何事情要做时它会保持线程的构建/活动。此概念还可用于修复工作线程的数量。 this answer中给出了一些很好的建议。