Eigen3 或其他矩阵库 2D 矩阵是否可以通过共享内存从不同的进程更新
Can an Eigen3 or other matrix library 2D matrix be updated from different processes through shared memory?
这个程序在eigen3中的等价物是什么?我需要能够从单独的进程读取/写入共享内存矩阵,然后获取矩阵的特征值。似乎 Eigen3 可以使用 OpenMP,但我还没有找到一个明显的例子。
服务器:
#include <string>
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/mman.h>
int main()
{
int rows = 10; // The number of rows of the 2D array
int columns = 30; // The number of columns of the 2D array
int row, column;
int *matrix;
key_t ipc_key = 5678; /* key to be passed to shmget() */
int shmflg = 0666; /* shmflg to be passed to shmget() */
int id_shmem; /* return value from shmget() */
int theSize; /* size to be passed to shmget() */
theSize = sizeof(int) * rows * columns;
// Create the shared memory segment
id_shmem = shmget(ipc_key, theSize, IPC_CREAT | shmflg);
// Attach the shared memory to our matrix
matrix = (int *)shmat(id_shmem, 0, 0);
int retVal = mlock(matrix, theSize);
if(retVal)
std::cout << "Error Locking" << std::endl;
// Loop through all elements in the array
for (row = 0; row < rows; row++)
{
for (column = 0; column < columns; column++)
{
matrix[row * columns + column] = column; // Equivalent to matrix[column][row]
}
}
while (matrix[0] != -1)
usleep(1);
// Loop through all elements in the array
for (row = 0; row < rows; row++)
{
std::cout << std::endl << row << "t";
for (column = 0; column < columns; column++)
{
std::cout << " " << matrix[row * columns + column]; // Equivalent to matrix[column][row]
}
}
std::cout << std::endl;
}
客户
#include <string>
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/mman.h>
// This is SXI see http://www.boost.org/doc/libs/1_50_0/doc/html/interprocess/sharedmemorybetweenprocesses.html
int main()
{
int rows = 10; // The number of rows of the 2D array
int columns = 30; // The number of columns of the 2D array
int row, column;
int *matrix;
key_t ipc_key = 5678; /* key to be passed to shmget() */
int shmflg = 0666; /* shmflg to be passed to shmget() */
int id_shmem; /* return value from shmget() */
int theSize; /* size to be passed to shmget() */
theSize = sizeof(int) * rows * columns;
// Create the shared memory segment
id_shmem = shmget(ipc_key, theSize, shmflg);
// Attach the shared memory to our matrix
matrix = (int *)shmat(id_shmem, 0, 0);
// Loop through all elements in the array
for (row = 0; row < rows; row++)
{
std::cout << std::endl << row << "t";
for (column = 0; column < columns; column++)
{
std::cout << " " << matrix[row * columns + column]; // Equivalent to matrix[column][row]
}
}
matrix[0] = -1;
std::cout << std::endl;
}
事实证明,人们可以在 Eigen3 中做到这一点。使用地图
http://eigen.tuxfamily.org/dox/classEigen_1_1Map.html
相关文章:
- 从C++本机插件更新Vector3数组
- 将字符串存储在c++中的稳定内存中
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- Win32编译器选项和内存分配
- 当vector是tje全局变量时,c++中vector的内存管理
- 带内存和隔离功能的SQLite
- 子进程更新共享 mmap 内存,但父进程没有更改
- 两个进程之间的共享映射内存在编辑时未更新
- 更新:正在输出内存位置,而不是所需的字符串
- 线程将值写入堆内存中的变量中,但该变量不会更新
- 读取进程内存问题未更新
- 从其他 CUDA 流读取更新的内存
- Eigen3 或其他矩阵库 2D 矩阵是否可以通过共享内存从不同的进程更新
- 存储数据和访问,并在C++中执行后从内存中更新
- 更新到lion和xCode后,内存集崩溃
- c++线程:共享内存不更新,尽管没有竞争
- 如何使用析构函数清除链表内存,而不会出现valgrind错误?[更新:操作员过载帮助]
- Qt QSharedMemory如何更新共享内存位置
- 通过指针传递不更新内存地址