[英]Parallel order-preserving selection from an array using tbb
我有一個范圍圖像 ,想將其轉換為libpointmatcher點雲 。 雲是一個Eigen::Matrix
,每個點有4行(x,y,z,1)和幾列。 范圍圖像是包含范圍值(z)的unsigned char*
unsigned short*
數組和包含有關像素可見性信息的unsigned char*
數組。
以串行方式,我的代碼如下所示:
//container to hold the data
std::vector<Eigen::Vector4d> vec;
vec.reserve(this->Height*this->Width);
//contains information about pixel visibility
unsigned char* mask_data = (unsigned char*)range_image.mask.ToPointer();
//contains the actual pixel data
unsigned short* pixel_data = (unsigned short*)range_image.pixel.ToPointer();
for (int y =0;y < range_image.Height; y++)
{
for (int x = 0; x < range_image.Width; x++)
{
int index =x+y*range_image.Width;
if(*(mask_data+index) != 0)
{
vec.push_back(Eigen::Vector4d(x,y,(double)*(data+index),1));
}
}
}
// libpointmatcher point cloud with size of visible pixel
PM::Matrix features(4,vec.size());
PM::DataPoints::Labels featureLabels;
featureLabels.resize(4);
featureLabels[0] = PM::DataPoints::Label::Label("x");
featureLabels[1] = PM::DataPoints::Label::Label("y");
featureLabels[2] = PM::DataPoints::Label::Label("z");
featureLabels[3] = PM::DataPoints::Label::Label("pad");
//fill with data
for(int i = 0; i<vec.size(); i++)
{
features.col(i) = vec[i];
}
由於圖像較大,因此該循環需要500ms才能獲得840000點,這太慢了。 現在我的想法是將上面的代碼集成到一個並行函數中。 問題在於Eigen::Matrix
不提供push_back
功能,我事先不知道可見點的數量,因此我需要以正確的順序處理這些點雲。
因此,我需要一種並行算法來從我的距離圖像中提取可見的3D點,並以正確的順序將其插入到Eigen :: Matrix中。 我正在使用Microsoft Visual Studio 2012,並且可以使用OpenMP 2.0或TBB 。 感謝您的幫助:)
更新
在Arch D. Robison的建議下,我嘗試了tbb::parallel_scan
。 我通過了mask數組和一個double數組來保存3D coodinates。 輸出數組的大小是輸入數組的四倍,以存儲同類3D數據(x,y,z,1)。 然后我將otput數組映射到Eigen :: Matrix。行數是固定的,並且cols來自parallel_scan的結果。
size_t vec_size = width*height;
double* out = new double[vec_size * 4];
size_t m1 = Compress(mask, pixel, out, height, width,
[](unsigned char x) {return x != 0; });
Map<MatrixXd> features(out, 4, m1);
。 這是來自operator()
的代碼:
void operator()(const tbb::blocked_range2d<size_t, size_t>& r, Tag) {
// Use local variables instead of member fields inside the loop,
// to improve odds that values will be kept in registers.
size_t j = sum;
const unsigned char* m = in;
const unsigned short* p = in2;
T* values = out;
size_t yend = r.rows().end();
for (size_t y = r.rows().begin(); y != yend; ++y)
{
size_t xend = r.cols().end();
for (size_t x = r.cols().begin(); x != xend; ++x)
{
size_t index = x + y*width;
if (pred(m[index]))
{
if (Tag::is_final_scan())
{
size_t idx = j*4;
values[idx] = (double)x;
values[idx + 1] = (double)y;
values[idx + 2] = p[index];
values[idx + 3] = 1.0;
}
++j;
}
}
}
sum = j;
}
我現在的速度是串行版本的4倍。 您如何看待這種方法? 我想念任何東西了嗎? 謝謝
這是一個std::copy_if using
tbb::parallel_scan
std::copy_if using
tbb::parallel_scan
類的std::copy_if using
tbb::parallel_scan
。 關鍵方法是operator()
,通常每個子范圍調用兩次,一次用於預掃描,一次用於最終掃描。 (但是請注意,TBB在不需要時會省略預掃描。)在這里,預掃描只是進行計數,而最終掃描則完成最終工作(包括重播計數)。 有關該方法的更多詳細信息,請參見https://software.intel.com/sites/default/files/bc/2b/parallel_scan.pdf 。 另一個很好的參考是https://www.cs.cmu.edu/~guyb/papers/Ble93.pdf ,它顯示了您可以使用並行掃描(也稱為前綴和)執行的許多操作。
```
#include "tbb/parallel_scan.h"
#include "tbb/blocked_range.h"
#include <cstddef>
template<typename T, typename Pred>
class Body {
const T* const in;
T* const out;
Pred pred;
size_t sum;
public:
Body( T* in_, T* out_, Pred pred_) :
in(in_), out(out_), pred(pred_), sum(0)
{}
size_t getSum() const {return sum;}
template<typename Tag>
void operator()( const tbb::blocked_range<size_t>& r, Tag ) {
// Use local variables instead of member fields inside the loop,
// to improve odds that values will be kept in registers.
size_t j = sum;
const T* x = in;
T* y = out;
for( size_t i=r.begin(); i<r.end(); ++i ) {
if( pred(x[i]) ) {
if( Tag::is_final_scan() )
y[j] = x[i];
++j;
}
}
sum = j;
}
// Splitting constructor used for parallel fork.
// Note that it's sum(0), not sum(b.sum), because this
// constructor will be used to compute a partial sum.
// Method reverse_join will put together the two sub-sums.
Body( Body& b, tbb::split ) :
in(b.in), out(b.out), pred(b.pred), sum(0)
{}
// Join partial solutions computed by two Body objects.
// Arguments "this" and "a" correspond to the splitting
// constructor arguments "b" and "this". That's why
// it's called a reverse join.
void reverse_join( Body& a ) {
sum += a.sum;
}
void assign( Body& b ) {sum=b.sum;}
};
// Copy to out each element of in that satisfies pred.
// Return number of elements copied.
template<typename T, typename Pred>
size_t Compress( T* in, T* out, size_t n, Pred pred ) {
Body<T,Pred> b(in,out,pred);
tbb::parallel_scan(tbb::blocked_range<size_t>(0,n), b);
return b.getSum();
}
#include <cmath>
#include <algorithm>
#include <cassert>
int main() {
const size_t n = 10000000;
float* a = new float[n];
float* b = new float[n];
float* c = new float[n];
for( size_t i=0; i<n; ++i )
a[i] = std::cos(float(i));
size_t m1 = Compress(a, b, n, [](float x) {return x<0;});
size_t m2 = std::copy_if(a, a+n, c, [](float x) {return x<0;})-c;
assert(m1==m2);
for( size_t i=0; i<n; ++i )
assert(b[i]==c[i]);
}
```
為什么不在m_features(0,index) = x;
之前檢查條件*(m_maskData+index)==0
m_features(0,index) = x;
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.