Boost::mpi发送数组
Boost::mpi sending array
早上好我正在实现一个分布式图像规范化算法,我正在使用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);
我认为这应该能解决你的问题。。。
相关文章:
- 在 MPI 中共享数组的一部分
- 在MPI中发送动态分配的2D数组的列
- 如何解决在使用动态 2D 数组进行矩阵乘法的 MPI 进行并行编程时的问题
- 在 MPI (C++) 中共享整数数组
- C MPI创建并发送具有字段char [16]和整数的结构数组
- MPI - 当数组初始化值必须为常量时,如何为工作线程创建部分数组
- 在MPI进程之间更新数组
- C MPI:STD ::在数组上合并
- 如何通过Boost::MPI发送2d C风格的数组
- 使用MPI和C 发送静态内存2D数组
- 如何使用某些MPI命令(或组合命令)之间在两个处理器之间交换2D数组中存储的数据
- 二维数组的 MPI 数据类型
- 如何将MPI派生的数据类型用于三维数组
- Boost::mpi发送数组
- C++在MPI进程之间共享大型数组和数据结构
- MPI收集损坏的数组
- MPI:从进程渗透子数组来更新根/主进程中的主数组
- 在c++中为MPI使用平坦化3D数组
- 在MPI中拆分和传递数组块
- 数组结构的MPI-3共享内存