2017-10-06 87 views
0

我有一個需要在CPU上通過矩陣進行轉換的向量列表。我將它們存儲爲一個動態分配的數組(Eigen :: Vector4f *)。一旦它們被轉換,我需要在向量上運行OpenCL內核。我想知道什麼最好的方法是將這些數據傳遞到OpenCL,而不必從Eigen :: Vector - > float數組複製數據,因爲這樣做會相當昂貴。我的理解是,Eigen將矢量值存儲在某種可以訪問的緩衝區中?Eigen庫:將Vector4f的數組傳遞給OpenCL內核作爲浮點數

回答

0

的方法有很多,

1 - 最好的可能是使用一個Matrix4Xf,因爲它允許對整個組向量馬上開始工作:

Matrix4Xf vecs(4,n); 
Matrix4f transform; 
vecs = transform * vecs; 
vecs.row(1)  // read-write access to all y components 
vecs.col(i)  // read-write access to i-th vector 
float* raw_ptr = vecs.data(); 

2 - 使用std::vector<Vector4f>(與Vector4f *相同但沒有內存管理問題):

std::vector<Vector4f> vecs(n); 
for(auto& v:vecs) v = transform * v; 
float* raw_ptr = vecs[0].data(); // assuming vecs is not empty 
// you can still see it as Matrix4Xf: 
Map<Matrix4Xf> vecs_as_mat(raw_ptr,4,n);