在C++中用parallel多次运行函数的简单方法



我想知道是否有一种简单的方法可以在parallel中多次运行一个函数。我尝试过多线程,但要么是有一些我不理解的地方,要么就是它实际上没有加快计算速度(实际上恰恰相反(。我在这里有我想在parallel中运行的功能:

void heun_update_pos(vector<planet>& planets, vector<double> x_i, vector<double> y_i, vector<double> mass, size_t n_planets, double h, int i)

{

if (planets[i].mass != 0) {
double sum_gravity_x = 0;
double sum_gravity_y = 0;

//loop for collision check and gravitational contribution
for (int j = 0; j < n_planets; j++) {

if (planets[j].mass != 0) {

double delta_x = planets[i].x_position - x_i[j];
double delta_y = planets[i].y_position - y_i[j];

//computing the distances between two planets in x and y
if (delta_x != 0 && delta_y != 0) {
//collision test
if (collision_test(planets[i], planets[j], delta_x, delta_y) == true) {
planets[i].mass += planets[j].mass;
planets[j].mass = 0;
}

//sum of the gravity contributions from other planets
sum_gravity_x += gravity_x(delta_x, delta_y, mass[j]);
sum_gravity_y += gravity_y(delta_x, delta_y, mass[j]);

}
}
};
double sx_ip1 = planets[i].x_speed + (h / 2) * sum_gravity_x;
double sy_ip1 = planets[i].y_speed + (h / 2) * sum_gravity_y;
double x_ip1 = planets[i].x_position + (h / 2) * (planets[i].x_speed + sx_ip1);
double y_ip1 = planets[i].y_position + (h / 2) * (planets[i].y_speed + sy_ip1);
planets[i].update_position(x_ip1, y_ip1, sx_ip1, sy_ip1);
};

}

以下是我尝试使用多线程的方法:

const int cores = 6;
vector<thread> threads(cores);
int active_threads = 0;
int closing_threads = 1;
for (int i = 0; i < n_planets; i++) {
threads[active_threads] = thread(&Heun_update_pos, ref(planets), x_i, y_i, mass, n_planets, h, i);
if (i > cores - 2) threads[closing_threads].join();
//There should only be as many threads as there are cores
closing_threads++;
if (closing_threads > cores - 1) closing_threads = 0;
active_threads++; // counting the number of active threads
if (active_threads >= cores) active_threads = 0;
};
//CLOSING REMAINING THREADS
for (int k = 0; k < cores; k++) {
if (threads[k].joinable()) threads[k].join();
};

我今天刚开始学习C++(以前用过Python(,这是我的第一段代码,所以我对C++的所有功能都不太熟悉。

创建新线程需要花费大量时间,通常为50-100微秒。根据你的串行版本需要多长时间,它真的没有太大帮助。如果您多次运行此代码,则值得尝试使用线程池,因为唤醒线程最多需要5微秒。

在这里查看类似的答案:

与简单地创建线程相比,使用线程池是否有性能优势?

C++中有一个用于多线程计算的框架,称为OpenMP。你可能会考虑使用它。

https://bisqwit.iki.fi/story/howto/openmp/

最新更新