2015-01-26 75 views
0

我使用magick ++庫來管理圖像。我想使用openMPI分發我的算法,是否可以發送對象?使用openMPI發送對象

例如在我的代碼,我有

Image image(imgName); 
    int w = image.columns(); 
    int h = image.rows(); 
    PixelPacket *pixels = image.getPixels(0, 0, w, h); 

我可以發送pixels與MPI_SEND或什麼的分散?如果是與哪個數據類型?

回答

1

一般來說,除非你有一個專門爲你打包的庫,否則在MPI中發送一個專門的對象是不可能的。內置的數據類型的MPI標準列(MPI 3.0 665頁,定義的值和把手有列表),但在較高的水平,他們是:

  • MPI_CHAR
  • MPI_INT
  • MPI_FLOAT
  • MPI_DOUBLE
  • MPI_BYTE

有比這更多,但其中大部分最終都是這樣的。

您可以將這些類型放在一起製作自己的自定義數據類型。舉例來說,如果你知道你要送一束包含類似結構的:

{ 
    int index; 
    char[100] name; 
    double value; 
} 

您可以構建一個類型來認爲,這將是一個連續的類型,名稱,然後一整體數據類型的結構類型(它將包含int,爲名稱構造的類型和double)。

您只需要創建一個數據類型來描述您的PixelPacket

1

我不是C++程序員,但以下是你所要求的。基本上,我開始使用Magick ++開始8 MPI過程(我碰巧使用mpich),主人讀取圖像(Lena當然是從PNG文件),然後將其發送給每個從機。奴隸接收Lena並從他們收到的數據中重建她,並且每個奴隸用不同的名字寫出自己的本地副本作爲JPEG

我欺騙了尺寸,因爲計算尺寸和傳遞這些尺寸很簡單,但與我所展示的無關。

#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; 
} 

Makefile看起來是這樣的:

all: main 

run: main 
     mpirun -n 8 ./main 

main: main.cpp 
     mpic++ main.cpp -o main $$(Magick++-config --cxxflags --libs) 

它運行是這樣的:

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 

,輸出是這樣的:

[email protected] 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 
[email protected] 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-7.jpg 
[email protected] 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-6.jpg 
[email protected] 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-5.jpg 
[email protected] 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-4.jpg 
[email protected] 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-3.jpg 
[email protected] 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-2.jpg 
[email protected] 1 mark staff 64755 23 Oct 17:14 Lena-Rebuilt-1.jpg