c++ - 是否可以通过共享内存从不同进程更新 Eigen3 或其他矩阵库 2D 矩阵?

标签 c++ openmp shared-memory eigen3

这个程序在 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 中做到这一点。使用 map

http://eigen.tuxfamily.org/dox/classEigen_1_1Map.html

关于c++ - 是否可以通过共享内存从不同进程更新 Eigen3 或其他矩阵库 2D 矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27683336/

相关文章:

c++ - 实例化 COM 对象时出错

c++ - 如何在程序中设置 OpenMP 线程数?

Intel i7 上的 OpenMP

c - 编译算法的 OpenMP 和单线程版本的更好解决方法是什么?

c# - 从另一个进程访问对象

c++ - 强制 boost 使用 POSIX 共享内存而不是 System V?

c++ - Levenshtein Edit Distance 不计算编辑距离

c++ - 寻找代码 stub 生成器(来自头文件)

c - main函数在返回0后不会退出,shmctl不会删除段

c++ - 如何为 C++ 设置跨平台编译(win7 中的 NetBeans 和 linux 中的 make)