簡體   English   中英

如何使用MPI程序從命令行讀取參數?

[英]How do I read arguments from the command line with an MPI program?

如何從C ++中的命令行讀取參數?

我目前有這個代碼:

int data_size = 0;
std::cout << "Please enter an integer value: ";
std::cin >> data_size;
std::cout << "The value you entered is " << data_size;

主要:

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

        int data_size = 0;
        std::cout << "Please enter an integer value: ";
        std::cin >> data_size;
        std::cout << "The value you entered is " << data_size; 


    // initialise the MPI library
    MPI_Init(NULL, NULL);

    // determine the world size
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    // determine our rank in the world
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

    std::cout << "rank " << world_rank << " size " << world_size << std::endl;

    if (world_rank == 0){
        coordinator(world_size);
    }
    else{
        participant(world_rank, world_size);
    }

    MPI_Finalize();

    return 0;
}

它工作,但它一直要求我輸入一個整數值4次然后當我輸入一個數字命令行凍結。

這是我在命令行中得到的

C:\Users\Roland\Documents\Visual Studio 2013\Projects\DistributedSystems\Debug>m
piexec -n 4 .\DistributedSystems.exe
Please enter an integer value:
Please enter an integer value:
Please enter an integer value:
Please enter an integer value: 

使用MPI程序,使用std::cin讀取內容並不是一個好主意。 我不知道你怎么能讓它以這種方式工作而你不應該這樣做。

以下是您的替代選擇:

如果代碼的輸入小到足以作為命令行參數傳遞,請執行此操作。 在您的示例中,輸入代碼塊將更改為

// Do some error handling if needed, then
int data_size = std::atoi(argv[1]);

並開始工作

mpiexec -n 4 .\DistributedSystems.exe k

k是你想要data_size的數字。

如果為了方便使用,你應該達到輸入量大的點,將其寫入文件並傳遞輸入文件名,如上所述。 然后,每個進程都可以在自己的std::ifstream打開該文件,並從那里讀取數據。

根據Rob Latham的說法,這項工作是一種特定於實現的行為。 但是,如果您的系統使用命令行界面,您通常可以期望它能夠正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM