如何将 Eigen 映射到共享内存?
我想使用 Eigen
库作为我的共享内存数据结构(通过 mmap
)。
这是我的代码:
Producer.cpp:
#include<sys/mman.h>
#include<sys/types.h>
#include<fcntl.h>
#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include <vector>
#include <iostream>
#include <string>
#include "eigen3/Eigen/Dense"
typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> eigen_matrix;
using namespace std;
int main(int argc,char **argv) {
int fd = open(argv[1], O_CREAT | O_RDWR | O_TRUNC, 0777);
size_t size = 100 * 100 * sizeof(typename eigen_matrix::Scalar);
lseek(fd, size, SEEK_SET);
write(fd,"",1);
eigen_matrix* p =(eigen_matrix*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
p->resize(100, 100);
(*p)(0, 0) = 1;
double s;
size_t count = 0;
while (std::cin >> s && s != 0) {
(*p)(count++, 0) = s;
}
printf("initialize over\n");
munmap(p, size);
close(fd);
}
Consumer.cpp:
#include<sys/mman.h>
#include<sys/types.h>
#include<fcntl.h>
#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include <vector>
#include <iostream>
#include <string>
#include "eigen3/Eigen/Dense"
using namespace std;
typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> eigen_matrix;
int main(int argc,char **argv) {
int fd = open(argv[1], O_RDWR, 0777);
size_t size = 100 * 100 * sizeof(typename eigen_matrix::Scalar);
lseek(fd, size, SEEK_SET);
eigen_matrix* p = (eigen_matrix*)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
printf("%zu %zu\n", p->cols(), p->rows());
cout << *p <<endl; // here crashed !!
std::string s;
while (std::cin >> s && s != "quit") {
cout << *p << endl;
}
munmap(p, size);
}
正如您在代码中看到的,消费者崩溃了,
cout << *p << endl;
您能帮忙解决这个问题吗?有什么我忽略的吗?
I want to use Eigen
library as my shared memory data structure (by mmap
).
here is my code:
producer.cpp:
#include<sys/mman.h>
#include<sys/types.h>
#include<fcntl.h>
#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include <vector>
#include <iostream>
#include <string>
#include "eigen3/Eigen/Dense"
typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> eigen_matrix;
using namespace std;
int main(int argc,char **argv) {
int fd = open(argv[1], O_CREAT | O_RDWR | O_TRUNC, 0777);
size_t size = 100 * 100 * sizeof(typename eigen_matrix::Scalar);
lseek(fd, size, SEEK_SET);
write(fd,"",1);
eigen_matrix* p =(eigen_matrix*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
p->resize(100, 100);
(*p)(0, 0) = 1;
double s;
size_t count = 0;
while (std::cin >> s && s != 0) {
(*p)(count++, 0) = s;
}
printf("initialize over\n");
munmap(p, size);
close(fd);
}
consumer.cpp:
#include<sys/mman.h>
#include<sys/types.h>
#include<fcntl.h>
#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include <vector>
#include <iostream>
#include <string>
#include "eigen3/Eigen/Dense"
using namespace std;
typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> eigen_matrix;
int main(int argc,char **argv) {
int fd = open(argv[1], O_RDWR, 0777);
size_t size = 100 * 100 * sizeof(typename eigen_matrix::Scalar);
lseek(fd, size, SEEK_SET);
eigen_matrix* p = (eigen_matrix*)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
printf("%zu %zu\n", p->cols(), p->rows());
cout << *p <<endl; // here crashed !!
std::string s;
while (std::cin >> s && s != "quit") {
cout << *p << endl;
}
munmap(p, size);
}
As you can see in the code, consumer crashed on
cout << *p << endl;
could you help on this? Is there anything i ignored?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
动态特征矩阵类似于 std::vector。它们不保存实际数据,而是包含指向数据的指针以及大小信息。您映射了对象,而不是实际数据。
像这样的东西应该有效:
Dynamic Eigen matrices are like std::vector. They don't hold the actual data, they contain pointers to the data plus the size information. You mmapped the object, not the actual data.
Something like this should work: