2015-01-26 2 views
0

저는 magick ++ 라이브러리를 사용하여 이미지를 관리하고 있습니다. openMPI를 사용하여 알고리즘을 배포하고 싶습니다. 객체를 보낼 수 있습니까? 내가openMPI로 객체 보내기

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

내가 MPI_Send이든의 분산과 pixels를 보낼 수있는 내 코드에서 예를 들어

? 그렇다면 어떤 데이터 유형입니까?

답변

1

일반적으로 포장을하고있는 특별한 라이브러리가 없다면 MPI에서 특별한 개체를 보낼 수 없습니다. 내장 된 데이터 유형은 (가치와 핸들 목록이 있습니다를 정의, MPI 3.0 페이지 665)를 MPI 표준에 나와 있지만, 높은 수준에서, 그들은 것 같습니다

  • MPI_CHAR
  • MPI_INT
  • MPI_FLOAT
  • MPI_DOUBLE
  • MPI_BYTE

있습니다 그보다 훨씬 더 많은 것들이 있지만, 그들 중 대부분은 그런 식으로 끝납니다.

이러한 유형을 사용하여 사용자 정의 데이터 유형을 만들 수 있습니다. 예를 들어, 당신은 같은 것을 포함하는 구조체의 무리 보낼 거라고 알고있는 경우 : 당신은 종류가를 유지하기 위해 구성 할 수

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

을, 그런 다음 이름과에 대한 연속 된 형태가 될 것입니다 전체 데이터 유형의 struct 유형 (int, 이름을 구성하는 유형 및 double 포함).

당신이해야 할 일은 PixelPacket을 설명하는 데이터 유형을 만드는 것입니다.

1

나는 C++ 프로그래머가 아니지만 다음 내용은 사용자가 요구하는 것을 수행합니다. 기본적으로 저는 8 MPI 프로세스를 시작합니다 (나는 mpich을 사용합니다). 마스터는 Magick ++을 사용하여 이미지 (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