Boost::mpi发送数组

Boost::mpi sending array

本文关键字:数组 mpi Boost      更新时间:2023-10-16

早上好我正在实现一个分布式图像规范化算法,我正在使用Boost::mpi和一个包含序列化代码的类Pixel

#ifndef PIXEL_H
#define PIXEL_H
#include <boost/mpi.hpp>
#include <boost/serialization/access.hpp>
class Pixel
{
    private:
        unsigned char m_red;
        unsigned char m_green;
        unsigned char m_blue;
        friend class boost::serialization::access;
        template <class Archive>
        void serialize(Archive &ar, const unsigned int version) {
            ar & m_red;
            ar & m_green;
            ar & m_blue;
        }
    public:
        Pixel();
        Pixel(unsigned char red,unsigned char green,unsigned char blue) : m_red(red), m_green(green), m_blue(blue) {};
        virtual ~Pixel();
        unsigned char getRed();
        void setRed(unsigned char val);
        unsigned char getGreen();
        void setGreen(unsigned char val);
        unsigned char getBlue();
        void setBlue(unsigned char val);
        void setColor (unsigned char red,unsigned char green,unsigned char blue);
};

主.cpp是

#include <iostream>
#include <boost/mpi.hpp>
#include <vector>
#include "include/Pixel.h"
#include <cstdlib>
#include <ctime>
#define ALTEZZA 2
#define LARGHEZZA 2
namespace mpi=boost::mpi;
int main(int argc, char * argv[]) {
    std::cout<<"Inizializzazione dell'ambiente MPI"<<std::endl;
    mpi::environment env;
    mpi::communicator world;
    Pixel **vettore;
    int i,j;
    //Inizializzazione della matrice di test
    if(world.rank() == 0){
        std::cout<<"Inizializzazione matrice di test..."<<std::endl;
        std::srand(std::time(0));
        vettore = new Pixel *[ALTEZZA];
        for (i = 0; i < ALTEZZA; i++) {
            vettore[i] = new Pixel[LARGHEZZA];
        }
        for (i = 0; i < ALTEZZA; i++) {
            for (j = 0; j < LARGHEZZA; j++) {
                vettore[i][j].setColor(std::rand() % 256, std::rand() % 256, std::rand() % 256);
                std::cout<<"Vettore["<<i<<"]["<<j<<"] = ("<<int(vettore[i][j].getRed())<<","<<int(vettore[i][j].getGreen())<<","<<int(vettore[i][j].getBlue())<<");"<<std::endl;
            }
        }
    }
    if (world.rank() == 0) {
        std::cout<<"Invio matrice.."<<std::endl;
        world.send(1, 0, vettore[0]);
    }else {
        Pixel *px;
        world.recv(0, 0, px);
            for (j = 0; j < LARGHEZZA; j++) {
                std::cout<<int(px[j].getRed())<<" "<<int(px[j].getGreen())<<" "<<int(px[j].getBlue())<<std::endl;
            }
    }
    return 0;
}

但当我运行程序时,接收过程中的cout打印出错误的值,就像这个

Inizializzazione dell'ambiente MPI
Inizializzazione dell'ambiente MPI
Inizializzazione matrice di test...
Vettore[0][0] = (170,103,165);
Vettore[0][1] = (84,0,186);
Vettore[1][0] = (93,228,162);
Vettore[1][1] = (31,100,204);
Invio matrice..
170 103 165
217 1 0

我认为问题在于2d数组,因为如果我使用std::vector,我没有这个问题,但我不明白为什么。

我想你会遇到几个问题(我不能测试,因为我没有一个功能强大的MPI安装。)

首先,您的send()错误,当前您正在触发过载:

template<typename T> void send(int, int, const T &) const;

但是你试图发送一个原始数组,我想这里的解决方案必须是通过计数,例如:

world.send(1, 0, vettore[0], 2);  // 2 Pixels

其次,在接收器端(我不确定),但我想你需要一个合适的数组来将数据读取到。。,例如:

Pixel px[LARGHEZZA];
world.recv(0, 0, px, 2);

我认为这应该能解决你的问题。。。