使用openMPI发送对象 [英] Send object with openMPI
问题描述
我使用magick ++库来管理图片。我想使用openMPI分发我的算法,是否可以发送对象?
例如在我的代码中我有
图片图片(imgName);
int w = image.columns();
int h = image.rows();
PixelPacket * pixels = image.getPixels(0,0,w,h);
我可以使用MPI_Send或Scatter发送像素
什么?如果是,使用哪种数据类型?
我不是C ++程序员,基本上,我开始8 MPI过程(我碰巧使用 mpich
),主人读取图像(Lena当然,从 PNG
文件)使用Magick ++,然后将她发送给每个奴隶。奴隶收到Lena并从他们收到的数据重建她,每个奴隶写出自己的本地副本作为
JPEG
在不同的名称下。
code> #include< cstdlib>
#include< iostream>
#include< Magick ++。h>
#includempi.h
using namespace std;
int main(int argc,char * argv [])
{
int id,p;
int number;
//初始化MPI
MPI :: Init(argc,argv);
//初始化ImageMagick和图像处理的东西
Magick :: InitializeMagick(* argv);
int row,col
Magick :: Image image;
int bytes = 512 * 512 * 3; //我碰巧知道Lena是512x512和RGB - 即3个字节/像素
无符号字符缓冲区[字节];
//获取进程数量
p = MPI :: COMM_WORLD.Get_size();
//获取单个进程ID
id = MPI :: COMM_WORLD.Get_rank();
// Master将读取Lena并将其发送给所有的奴隶
if(id == 0)
{
cout< MASTER:进程数为< p < endl;
//读入Lena并把她放在缓冲区中通过MPI发送
image.read(lena.png);
//将Lena转换为一堆字节
image.write(0,0,512,512,RGB,Magick :: CharPixel,buffer);
//将美好的Lena发送给所有的奴隶
for(int z = 1; z cout< MASTER:Lena to slave<< z < endl;
MPI_Send(buffer,bytes,MPI_BYTE,z,0,MPI_COMM_WORLD);
}
} else {
//所有的奴隶都会收到Lena并把它写成JPEG的格式
cout<< Process:< id<< 开始并等待莉娜...< endl;
MPI_Recv(缓冲区,字节,MPI_BYTE,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
cout<< Process:< id<< Received Lena< endl;
//从一堆字节重建Lena,并写入Lena-Rebuilt-< id> .jpg
Magick :: Image rebuild(512,512,RGB,Magick: :CharPixel,缓冲液);
char filename [100];
sprintf(filename,Lena-Rebuil-%d.jpg,id);
rebuilt.write(filename);
}
//终止MPI
MPI :: Finalize();
return 0;
}
我的 Makefile
像这样:
all:main
run:main
mpirun -n 8。 / main
main:main.cpp
mpic ++ main.cpp -o main $$(Magick ++ - config --cxxflags --libs)
它运行如下:
b $ b mpirun -n 8 ./main
MASTER:进程数为8
进程:1开始并等待Lena ...
进程:3开始并等待Lena。 ..
过程:4开始并等待Lena ...
过程:5开始并等待Lena ...
过程:7开始并等待Lena ...
过程:6开始并等待Lena ...
过程:2开始并等待Lena ...
MASTER:将Lena发送到奴隶1
MASTER:将Lena发送到奴隶2
过程:1收到Lena
MASTER:发送Lena到奴隶3
过程:2收到Lena
MASTER:发送Lena到奴隶4
过程:3收到Lena
MASTER:发送Lena到奴隶5
过程:4收到Lena
MASTER:发送Lena到奴隶6
过程:5收到Lena
主人:发送莉娜到奴隶7
过程:6收到Lena
过程:7收到Lena
像这样:
-rw-r - r - @ 1 mark staff 150467 23 Oct 15:34 lena.png
-rw-r - r-- 1 mark staff 1786 23 Oct 17:04 main.cpp
-rwxr-xr-x 1 mark staff 51076 23 Oct 17:14 main
-rw -r - r - @ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-7.jpg
-rw-r - r - @ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt -6.jpg
-rw-r - r - @ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-5.jpg
-rw-r - r - @ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-4.jpg
-rw-r - r - @ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-3.jpg
-rw -r - r - @ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-2.jpg
-rw-r - r - @ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt -1.jpg
I'm using magick++ library to manage images. I want to distribute my algorithm using openMPI, is it possible to send objects?
for example in my code I have
Image image(imgName);
int w = image.columns();
int h = image.rows();
PixelPacket *pixels = image.getPixels(0, 0, w, h);
Can I send pixels
with MPI_Send or Scatter of whatever? if yes with which datatype?
I am no C++ programmer, but the following does what you are asking. Basically, I start 8 MPI process (I happen to use mpich
) and the master reads in an image (Lena of course, from a PNG
file) using Magick++ and then sends her to each of the slaves. The slaves receive Lena and rebuild her from the data they receive and each slave writes out its own local copy as a JPEG
under a different name.
I cheated on the sizes because working out the sizes and passing those is easy but not germane to what I am demonstrating.
#include <cstdlib>
#include <iostream>
#include <Magick++.h>
#include "mpi.h"
using namespace std;
int main ( int argc, char *argv[] )
{
int id,p;
int number;
// Initialise MPI
MPI::Init (argc,argv);
// Initialize ImageMagick and image processing stuff
Magick::InitializeMagick(*argv);
int row,col;
Magick::Image image;
int bytes=512*512*3; // I happen to know Lena is 512x512 and RGB - i.e. 3 bytes/pixel
unsigned char buffer[bytes];
// Get the number of processes
p = MPI::COMM_WORLD.Get_size();
// Get the individual process ID
id = MPI::COMM_WORLD.Get_rank();
// Master will read in Lena and send her to all slaves
if(id==0)
{
cout << "MASTER: The number of processes is " << p << endl;
// Read in Lena and put her in a buffer to send via MPI
image.read("lena.png");
// Convert Lena to a bunch of bytes
image.write(0,0,512,512,"RGB",Magick::CharPixel,buffer);
// Send the luscious Lena to all slaves
for(int z=1;z<p;z++){
cout << "MASTER: Sending Lena to slave " << z << endl;
MPI_Send(buffer,bytes,MPI_BYTE,z,0,MPI_COMM_WORLD);
}
}else{
// All slaves will receive Lena and write her out as a JPEG
cout << "Process:" << id << " Started and waiting for Lena..." << endl;
MPI_Recv(buffer,bytes,MPI_BYTE,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
cout << "Process:" << id << " Received Lena" << endl;
// Rebuild Lena from the bunch of bytes and write to "Lena-Rebuilt-<id>.jpg"
Magick::Image rebuilt(512,512,"RGB",Magick::CharPixel,buffer);
char filename[100];
sprintf(filename,"Lena-Rebuilt-%d.jpg",id);
rebuilt.write(filename);
}
// Terminate MPI
MPI::Finalize();
return 0;
}
My Makefile
looks like this:
all: main
run: main
mpirun -n 8 ./main
main: main.cpp
mpic++ main.cpp -o main $$(Magick++-config --cxxflags --libs)
It runs like this:
make run
mpirun -n 8 ./main
MASTER: The number of processes is 8
Process:1 Started and waiting for Lena...
Process:3 Started and waiting for Lena...
Process:4 Started and waiting for Lena...
Process:5 Started and waiting for Lena...
Process:7 Started and waiting for Lena...
Process:6 Started and waiting for Lena...
Process:2 Started and waiting for Lena...
MASTER: Sending Lena to slave 1
MASTER: Sending Lena to slave 2
Process:1 Received Lena
MASTER: Sending Lena to slave 3
Process:2 Received Lena
MASTER: Sending Lena to slave 4
Process:3 Received Lena
MASTER: Sending Lena to slave 5
Process:4 Received Lena
MASTER: Sending Lena to slave 6
Process:5 Received Lena
MASTER: Sending Lena to slave 7
Process:6 Received Lena
Process:7 Received Lena
And the outputs look like this:
-rw-r--r--@ 1 mark staff 150467 23 Oct 15:34 lena.png
-rw-r--r-- 1 mark staff 1786 23 Oct 17:04 main.cpp
-rwxr-xr-x 1 mark staff 51076 23 Oct 17:14 main
-rw-r--r--@ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-7.jpg
-rw-r--r--@ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-6.jpg
-rw-r--r--@ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-5.jpg
-rw-r--r--@ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-4.jpg
-rw-r--r--@ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-3.jpg
-rw-r--r--@ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-2.jpg
-rw-r--r--@ 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-1.jpg
这篇关于使用openMPI发送对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!