2013-12-09 42 views
0

我想允許用戶通過命令行在應用程序中設置設置。MPI程序中的初始設置

在實驗中,我發現我只能使用此方法在第一個方法中設置變量;因此我意識到我需要使用發送和接收。但是我的代碼does not工作

下面是我的代碼:

#include <mpi.h> 
#include <iostream> 
#include <string.h> 

using namespace std; 

int main(int argc, char *argv[]){ 

    cout << "Enter v" << endl; 
    double v; 
    cin >> v; 
    cout << v << endl; 

    MPI::Init(argc,argv); 

    int rank = MPI::COMM_WORLD.Get_rank(); 
    int size = MPI::COMM_WORLD.Get_size(); 

    if (rank == 0) 
    { 
    MPI::COMM_WORLD.Send(&v, 8, MPI_DOUBLE, v, 1); 
    } 
    else 
    { 
    MPI::COMM_WORLD.Recv(&v, 8, MPI_DOUBLE, v, 1,); 
    } 

    cout << "My rank is " << rank <<". My v is " << v << endl; 
    MPI::Finalize(); 
    return 0; 
} 

它報告如下:

enter v 
5 
5 
enter v 
2.23533e-317 
enter v 
2.23533e-317 
enter v 
2.23533e-317 
0 - MPI_SEND : Invalid rank 5 
[0] Aborting program ! 
[0] Aborting program! 
p0_11420: p4_error: : 8262 
Killed by signal 2. 
Killed by signal 2. 
Killed by signal 2. 

回答

1

錯誤來自:

MPI::COMM_WORLD.Send(&v, 8, MPI_DOUBLE, v, 1); 

如果你想送一個雙那麼:

MPI::COMM_WORLD.Send(&v, 1, MPI_DOUBLE, v, 1); 

爲了避免錯誤,請首先檢查v <大小...您提供的代碼將發送值v到proc v。要將相同的參數值發送到所有進程,請使用MPI_Bcast()(廣播)。

http://www.mcs.anl.gov/research/projects/mpi/www/www3/MPI_Bcast.html

MPI::COMM_WORLD.Bcast(&v, 1, MPI_DOUBLE, 0); 

再見,

弗朗西斯