我正在尝试在C++中创建线程。我当然觉得在for循环内创建线程并不意味着并行。但是我想并行化下面的代码逻辑。
for(int i = 0; i < 100000; i++) // for each instance in the dataset
{
for(int j = 0; j < 100000; j++) // target each other instance
{
if(i == j) continue;
float distance = 0;
for(int k = 0; k < 2000; k++)
{
float a = dataset->get_instance(i)->get(k)->operator float();
float b = dataset->get_instance(j)->get(k)->operator float();
float diff = a - b
distance += diff * diff;
}
distance = distance + 10;
}
}
上面的代码段中是否存在并行性的可能性?或者任何人都可以给我提供一些代码示例,以了解类似的线程并行化。
最佳答案
如果显示的功能均没有副作用,则可以在i
循环的每个迭代中仅运行一个线程,可以创建N个线程并将外部i
循环的迭代数除以每个线程,或者可以使用std::async
:
struct ShortestDistance {
float distance;
int distClass;
};
ShortestDistance inner_loop(const Dataset* dataset, int i)
{
ShortestDistance dist { MAX_FLT, 0 };
for(int j = 0; j < dataset->num_instances(); j++) // target each other instance
{
if(i == j) continue;
float distance = 0;
for(int k = 0; k < dataset->num_attributes() - 1; k++) // compute the distance between the two instances
{
float a = dataset->get_instance(i)->get(k)->operator float();
float b = dataset->get_instance(j)->get(k)->operator float();
float diff = a - b
distance += diff * diff;
}
distance = sqrt(distance);
if (distance < dist.distance) {
dist.distance = distance;
dist.distClass = dataset->get_instance(j)->get(dataset->num_attributes() - 1)->operator int32();
}
}
return dist;
}
void outer_loop(const Dataset* dataset)
{
std::vector<std::future<ShortestDistance>> vec;
for(int i = 0; i < dataset->num_instances(); i++) // for each instance in the dataset
{
vec[i] = std::async(inner_loop, dataset, i);
}
DistanceResult overallResult { FLT_MAX, 0 };
for (auto&& fut : vec)
{
DistanceResult threadResult = fut.get();
if (threadResult.distance < overallResult.distance)
overallResult = threadResult);
}
}
关于c++ - 提高C++的性能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39444011/