bin_distance_shape[i_idx] = std::numeric_limits<double>::quiet_NaN ();
时间: 2024-05-27 15:10:16 浏览: 143
这段代码是将一个数组中的某个元素设置为NaN(Not a Number),NaN是一种特殊的浮点数,表示一个无效或未定义的数值。这通常用于表示数据缺失或无效值的情况。在C++中,std::numeric_limits<double>::quiet_NaN()可以用来生成一个NaN值,它与其他NaN值不同,不会引发浮点异常。在这种情况下,将该数组元素设置为NaN可能是为了标记该元素对应的数据未知或无效。
相关问题
加速这一段代码#include <thread> #include <mutex> // 用于保护m_vpdEdgePoints和m_vdEdgeGradient的锁 std::mutex g_mutex; void process_edges(const cv::Mat& RoiMat, const std::vector<cv::Point2d>& m_vpdEquinoxPoints, const double m_dMeasureLength, const double m_dMeasureHeight, const double m_dSigma, const int m_nThresholdCircle, const int m_nTranslationCircle, const std::vector<double>& m_vdMeasureAngle, std::vector<cv::Point2d>& m_vpdEdgePoints, std::vector<double>& m_vdEdgeGradient, int start_idx, int end_idx, Extract1DEdgeCircle Extract1DEdgeCircle) { std::vector<Edge1D_Result> edges; for (int i = start_idx; i < end_idx; i++) { edges = Extract1DEdgeCircle.Get1DEdge(RoiMat, m_vpdEquinoxPoints[i], m_dMeasureLength, m_dMeasureHeight,m_vdMeasureAngle[i], m_dSigma, m_nThresholdCircle, m_nTranslationCircle == 1 ? Translation::Poisitive : Translation::Negative, Selection::Strongest); // 使用锁保护m_vpdEdgePoints和m_vdEdgeGradient //std::lock_guard<std::mutex> lock(g_mutex); for (int j = 0; j < edges.size(); j++) { m_vpdEdgePoints.push_back(edges[j].m_pdEdgePoint); m_vdEdgeGradient.push_back(edges[j].m_dGradient); } } } const int num_threads = 10; std::vector<std::thread> threads(num_threads); std::vector<std::vector<cv::Point2d>> edge_points(num_threads); std::vector<std::vector<double>> edge_gradients(num_threads); for (int i = 0; i < num_threads; i++) { int start_idx = i * m / num_threads; int end_idx = (i + 1) * m / num_threads; threads[i] = std::thread(process_edges, std::ref(RoiMat), std::ref(m_vpdEquinoxPoints), m_dMeasureLength, m_dMeasureHeight, m_dSigma, m_nThresholdCircle, m_nTranslationCircle, std::ref(m_vdMeasureAngle), std::ref(edge_points[i]), std::ref(edge_gradients[i]), start_idx, end_idx, Extract1DEdgeCircle); } for (int i = 0; i < num_threads; i++) { threads[i].join(); // 合并结果 m_vpdEdgePoints.insert(m_vpdEdgePoints.end(), edge_points[i].begin(), edge_points[i].end()); m_vdEdgeGradient.insert(m_vdEdgeGradient.end(), edge_gradients[i].begin(), edge_gradients[i].end()); }
可以尝试使用并行 STL 算法来加速代码,例如 `std::for_each()` 或 `std::transform()`。这些算法可以利用多线程来并行化循环。具体来说,你可以将 `process_edges()` 函数中的循环替换为一个并行算法,例如:
```cpp
std::vector<std::vector<Edge1D_Result>> edges(num_threads);
std::for_each(std::execution::par, std::begin(threads), std::end(threads), [&](auto& thread) {
thread.join();
});
std::for_each(std::execution::par, std::begin(threads), std::end(threads), [&](auto& thread) {
const auto i = std::distance(std::begin(threads), std::addressof(thread));
int start_idx = i * m / num_threads;
int end_idx = (i + 1) * m / num_threads;
edges[i] = Extract1DEdgeCircle.Get1DEdge(RoiMat, m_vpdEquinoxPoints, m_dMeasureLength, m_dMeasureHeight, m_vdMeasureAngle, m_dSigma, m_nThresholdCircle, m_nTranslationCircle == 1 ? Translation::Poisitive : Translation::Negative, Selection::Strongest, start_idx, end_idx);
});
for (const auto& thread_edges : edges) {
for (const auto& edge : thread_edges) {
m_vpdEdgePoints.push_back(edge.m_pdEdgePoint);
m_vdEdgeGradient.push_back(edge.m_dGradient);
}
}
```
这里我们使用了 `std::for_each()` 并行地执行所有的线程。然后我们再次使用 `std::for_each()` 并行地执行每个线程中的循环,将结果存储在 `edges` 向量中。最后,我们遍历 `edges` 向量并将所有的边缘点和梯度值插入到输出向量中。
注意,这里使用了 C++17 中的并行 STL 算法,需要在编译时指定 `-std=c++17` 或更高的选项。如果你的编译器不支持 C++17,也可以使用 OpenMP 或其他多线程库来手动实现并行化循环。
加速这一段代码#include <thread> #include <mutex> // 用于保护m_vpdEdgePoints和m_vdEdgeGradient的锁 std::mutex g_mutex; void process_edges(const cv::Mat& RoiMat, const std::vectorcv::Point2d& m_vpdEquinoxPoints, const double m_dMeasureLength, const double m_dMeasureHeight, const double m_dSigma, const int m_nThresholdCircle, const int m_nTranslationCircle, const std::vector<double>& m_vdMeasureAngle, std::vectorcv::Point2d& m_vpdEdgePoints, std::vector<double>& m_vdEdgeGradient, int start_idx, int end_idx, Extract1DEdgeCircle Extract1DEdgeCircle) { std::vector<Edge1D_Result> edges; for (int i = start_idx; i < end_idx; i++) { edges = Extract1DEdgeCircle.Get1DEdge(RoiMat, m_vpdEquinoxPoints[i], m_dMeasureLength, m_dMeasureHeight,m_vdMeasureAngle[i], m_dSigma, m_nThresholdCircle, m_nTranslationCircle == 1 ? Translation::Poisitive : Translation::Negative, Selection::Strongest); // 使用锁保护m_vpdEdgePoints和m_vdEdgeGradient //std::lock_guardstd::mutex lock(g_mutex); for (int j = 0; j < edges.size(); j++) { m_vpdEdgePoints.push_back(edges[j].m_pdEdgePoint); m_vdEdgeGradient.push_back(edges[j].m_dGradient); } } } const int num_threads = 10; std::vectorstd::thread threads(num_threads); std::vector<std::vectorcv::Point2d> edge_points(num_threads); std::vector<std::vector<double>> edge_gradients(num_threads); for (int i = 0; i < num_threads; i++) { int start_idx = i * m / num_threads; int end_idx = (i + 1) * m / num_threads; threads[i] = std::thread(process_edges, std::ref(RoiMat), std::ref(m_vpdEquinoxPoints), m_dMeasureLength, m_dMeasureHeight, m_dSigma, m_nThresholdCircle, m_nTranslationCircle, std::ref(m_vdMeasureAngle), std::ref(edge_points[i]), std::ref(edge_gradients[i]), start_idx, end_idx, Extract1DEdgeCircle); } for (int i = 0; i < num_threads; i++) { threads[i].join(); // 合并结果 m_vpdEdgePoints.insert(m_vpdEdgePoints.end(), edge_points[i].begin(), edge_points[i].end()); m_vdEdgeGradient.insert(m_vdEdgeGradient.end(), edge_gradients[i].begin(), edge_gradients[i].end()); }
可以使用并行化技术来加速这段代码。具体来说,可以使用std::thread和std::mutex来创建多个线程处理不同部分的数据,并通过锁来保护共享数据的访问。下面是一个可能的实现:
```c++
#include <thread>
#include <mutex>
// 用于保护m_vpdEdgePoints和m_vdEdgeGradient的锁
std::mutex g_mutex;
void process_edges(const cv::Mat& RoiMat,
const std::vector<cv::Point2d>& m_vpdEquinoxPoints,
const double m_dMeasureLength,
const double m_dMeasureHeight,
const double m_dSigma,
const int m_nThresholdCircle,
const int m_nTranslationCircle,
const std::vector<double>& m_vdMeasureAngle,
std::vector<cv::Point2d>& m_vpdEdgePoints,
std::vector<double>& m_vdEdgeGradient,
int start_idx,
int end_idx,
Extract1DEdgeCircle Extract1DEdgeCircle) {
std::vector<Edge1D_Result> edges;
for (int i = start_idx; i < end_idx; i++) {
edges = Extract1DEdgeCircle.Get1DEdge(RoiMat,
m_vpdEquinoxPoints[i],
m_dMeasureLength,
m_dMeasureHeight,
m_vdMeasureAngle[i],
m_dSigma,
m_nThresholdCircle,
m_nTranslationCircle == 1 ? Translation::Poisitive : Translation::Negative,
Selection::Strongest);
// 使用锁保护m_vpdEdgePoints和m_vdEdgeGradient
std::lock_guard<std::mutex> lock(g_mutex);
for (int j = 0; j < edges.size(); j++) {
m_vpdEdgePoints.push_back(edges[j].m_pdEdgePoint);
m_vdEdgeGradient.push_back(edges[j].m_dGradient);
}
}
}
void parallel_process_edges(const cv::Mat& RoiMat,
const std::vector<cv::Point2d>& m_vpdEquinoxPoints,
const double m_dMeasureLength,
const double m_dMeasureHeight,
const double m_dSigma,
const int m_nThresholdCircle,
const int m_nTranslationCircle,
const std::vector<double>& m_vdMeasureAngle,
std::vector<cv::Point2d>& m_vpdEdgePoints,
std::vector<double>& m_vdEdgeGradient,
int num_threads,
Extract1DEdgeCircle Extract1DEdgeCircle) {
std::vector<std::thread> threads(num_threads);
std::vector<std::vector<cv::Point2d>> edge_points(num_threads);
std::vector<std::vector<double>> edge_gradients(num_threads);
for (int i = 0; i < num_threads; i++) {
int start_idx = i * m_vpdEquinoxPoints.size() / num_threads;
int end_idx = (i + 1) * m_vpdEquinoxPoints.size() / num_threads;
threads[i] = std::thread(process_edges,
std::ref(RoiMat),
std::ref(m_vpdEquinoxPoints),
m_dMeasureLength,
m_dMeasureHeight,
m_dSigma,
m_nThresholdCircle,
m_nTranslationCircle,
std::ref(m_vdMeasureAngle),
std::ref(edge_points[i]),
std::ref(edge_gradients[i]),
start_idx,
end_idx,
Extract1DEdgeCircle);
}
for (int i = 0; i < num_threads; i++) {
threads[i].join();
// 合并结果
m_vpdEdgePoints.insert(m_vpdEdgePoints.end(), edge_points[i].begin(), edge_points[i].end());
m_vdEdgeGradient.insert(m_vdEdgeGradient.end(), edge_gradients[i].begin(), edge_gradients[i].end());
}
}
```
在这个实现中,我们定义了一个新的函数parallel_process_edges来处理并行化。它接受与原始函数相同的参数,并使用std::thread和std::mutex来创建多个线程执行process_edges函数。每个线程处理输入点集的一个子集,并将结果存储在本地向量中。最后,我们将所有线程的结果合并到主向量中。
请注意,我们使用了一个简单的均匀分割策略来将输入点集划分为不同的子集,但这可能会导致不平衡的工作负载。更好的方法是使用动态划分策略,即根据实际工作负载将任务动态地分配给可用的线程。不过,这需要更复杂的实现。
阅读全文