[英]Zero-copy construction of an Eigen SparseMatrix
I have the following problem: 我有以下问题:
I have an Eigen::SparseMatrix
I need to send over the network, and my network library only supports sending arrays of primitive types. 我有一个需要通过网络发送的
Eigen::SparseMatrix
,我的网络库只支持发送基本类型的数组。
I can retrieve the pointers to the backing arrays of my SparseMatrix by doing something like (here's the backing object's code ): 我可以通过执行类似(这里是支持对象的代码 )之类的操作来检索指向SparseMatrix的支持数组的指针:
// Get pointers to the indices and values, send data over the network
int num_items = sparse_matrix.nonZeros()
auto values_ptr = sparse_matrix.data().valuePtr()
auto index_ptr = sparse_matrix.data().indexPtr()
network_lib::send(values_ptr, num_items)
network_lib::send(index_ptr, 2 * num_items) // Times two b/c we have 2 indices per value
Now on the other side I have access to these two arrays. 现在在另一边我可以访问这两个数组。 But AFAIK there is no way to create a SparseArray without copying all the data into a new SparseMatrix (see docs for construction).
但是AFAIK没有办法创建SparseArray而不将所有数据复制到新的SparseMatrix中(参见构造文档 )。
I'd like to do something like: 我想做点什么:
Eigen::SparseMatrix<float> zero_copy_matrix(num_rows, num_cols);
zero_copy_matrix.data().valuePtr() = received_values_ptr;
zero_copy_matrix.data().indexPtr() = received_index_ptr;
But this throws a compiler error: 但这会引发编译器错误:
error: lvalue required as left operand of assignment zero_copy_matrix.data().valuePtr() = received_values_ptr;
Any idea on how we could zero-copy construct a sparse Eigen matrix from existing arrays of indexes and data? 关于我们如何零拷贝从现有索引和数据数组构造稀疏特征矩阵的任何想法?
Another approach I tried that didn't work (this is local, no communication): 我试过的另一种方法不起作用(这是本地的,没有通信):
zero_copy_matrix.reserve(num_non_zeros);
zero_copy_matrix.data().swap(original_matrix.data());
When I try to print out the zero_copy_matrix
it has no values in it. 当我尝试打印出
zero_copy_matrix
它没有值。
After digging around I think a good option for me would be to use an Eigen::Map<Eigen::SparseMatrix<float>>
as such: 在挖掘之后我认为对我来说一个好的选择是使用
Eigen::Map<Eigen::SparseMatrix<float>>
:
Eigen::Map<Eigen::SparseMatrix<float>> sparse_map(num_rows, num_cols, num_non_zeros,
original_outer_index_ptr, original_inner_index_ptr,
original_values_ptr);
AFAIK, this should be zero-copy. AFAIK,这应该是零拷贝。 Answer from here .
从这里回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.