2012-05-30 97 views
1

我想創造出我的程序在這裏中止這裏是我的程序的一部分 我需要一個優雅地退出我的代碼 在這部分我想做一個閱讀輸入錯誤,在Rdinput()我如果發生錯誤,返回1 。隨着MPI優雅地退出

#include <mpi.h> 
#include <stdio.h> 
#include <stdlib.h> 
#define MASTER 0 
#define Abort(x) MPI_Abort(MPI_COMM_WORLD, x) 
#define Bcast(send_data, count, type) MPI_Bcast(send_data, count, type, MASTER, GROUP) //root --> MASTER 
#define Finalize() MPI_Finalize() 
#define Init(x,y) MPI_Init(x,y) 
#define Rank(x) MPI_Comm_rank(MPI_COMM_WORLD, x) 
#define Size(x) MPI_Comm_size(MPI_COMM_WORLD, x) 

int main(int argc, char **argv) /* Main routine of MGA program*/ 
{ 

    if(rank == MASTER){ 

    time (&start); 
    printf("Initialized at %s\n", ctime (&start)); 

    printf("\t***********************************************************\n"); 
    printf("\t**      Program MGA      **\n"); 
    printf("\t**    (Molecular Genetic Algorithm)    **\n"); 
    printf("\t**              **\n"); 
    printf("\t**              **\n"); 
    printf("\t** Principal Author:          **\n"); 
    printf("\t**  Freddy F. Guimaraes       **\n"); 
    printf("\t**  email: [email protected]     **\n"); 
    printf("\t**              **\n"); 
    printf("\t** Authors:            **\n"); 
    printf("\t**  Rafael Carvalho Couto, Fabrício Sanches Paranhos, **\n"); 
    // printf("\t**  Robson de Souza Vieira.       **\n"); 
    printf("\t**              **\n"); 
    printf("\t***********************************************************\n\v"); 

    printf("Reading input file: |"); 
    printf("#");fflush(stdout); 
    error = RdInput(Infile, CHARMAX); 
    } 

    Bcast(&error, 1, INT); 

    Wait(); 

    if(error = 1) MPI_Abort(1); 

    REST OF THE CODE 

Finalize(); 
} 

這裏走出來的......

[email protected]:~/MGA/MGA-2011-mpi$ mpirun -np 2 mga-2011.x 
-------------------------------------------------------------------------- 
MPI_ABORT was invoked on rank 0 in communicator MPI_COMM_WORLD 
with errorcode 1. 

NOTE: invoking MPI_ABORT causes Open MPI to kill all MPI processes. 
You may or may not see output from other processes, depending on 
exactly when Open MPI kills them. 
-------------------------------------------------------------------------- 
Initialized at Wed May 30 11:34:46 2012 

    *********************************************************** 
    **      Program MGA      ** 
    **    (Molecular Genetic Algorithm)    ** 
    **              ** 
    **              ** 
    ** Principal Author:          ** 
    **  Freddy F. Guimaraes       ** 
    **  email: [email protected]     ** 
    **              ** 
    ** Authors:            ** 
    **  Rafael Carvalho Couto, Fabrício Sanches Paranhos, ** 
    **              ** 
    *********************************************************** 

Reading input file: |#| 

Error [RdInput]: The file "input.mga" is not available! 
-------------------------------------------------------------------------- 
mpirun has exited due to process rank 0 with PID 7369 on 
node einstein exiting improperly. There are two reasons this could occur: 

1. this process did not call "init" before exiting, but others in 
the job did. This can cause a job to hang indefinitely while it waits 
for all processes to call "init". By rule, if one process calls "init", 
then ALL processes must call "init" prior to termination. 

2. this process called "init", but exited without calling "finalize". 
By rule, all processes that call "init" MUST call "finalize" prior to 
exiting or it will be considered an "abnormal termination" 

This may have caused other processes in the application to be 
terminated by signals sent by mpirun (as reported here). 
-------------------------------------------------------------------------- 
[einstein:07368] 1 more process has sent help message help-mpi-api.txt/mpi-abort 
[einstein:07368] Set MCA parameter "orte_base_help_aggregate" to 0 to see all help/error messages 

我如何做才能正常退出蒙山錯誤代碼爲例

計劃完成蒙山錯誤代碼10

+3

你在哪裏隱藏mpi_init? –

+1

您共享的所有代碼與問題無關。所有與該問題相關的代碼都丟失了。我們不知道你是如何初始化或最終化MPI的,我們甚至不知道'rank'的定義在哪裏,或者你如何填充它。現在,它看起來像MPI從未被初始化。 – ArjunShankar

+1

完全偏離主題的建議:考慮放棄大橫幅。當你打印幫助信息時,你可以有一個像「--help」這樣的開關,幷包含作者列表,或者你可以在你的程序的'man'頁面中包含作者列表。有人可能希望在腳本中使用你的程序的輸出,然後橫幅將是一個痛苦的脖子。 - 舉例來說,想象一下,如果'cat'程序在打印橫幅時就會發生什麼情況,只要您問它「貓」文件即可。 – ArjunShankar

回答

10

如果您有這您代碼中的邏輯:

Bcast(&error, 1, INT); 
if(error = 1) MPI_Abort(1); 

然後你就完成了(雖然在廣播後你不需要任何等待)。訣竅,你已經發現,MPI_Abort()確實不是做「優雅」;它基本上就是在那裏以某種可能的方式關閉事情,當某些事情發生了可怕的錯誤時。

在這種情況下,因爲現在大家都在播出後的錯誤代碼一致,只是做你的程序的優美結束:

MPI_Bcast(&error, 1, MPI_INT, MASTER, MPI_COMM_WORLD); 
    if (error != 0) { 
     if (rank == 0) { 
      fprintf(stderr, "Error: Program terminated with error code %d\n", error); 
     } 
     MPI_Finalize(); 
     exit(error); 
    } 

這是一個錯誤調用MPI_Finalize(),並繼續與更多的MPI的東西去,但這不是你在這裏做的,所以你沒事。

+1

謝謝,它的工作原理,但它返回 「mpirun注意到工作中止,但沒有關於造成這種情況的過程的信息。」我認爲這是MPI默認味精,我使用Open MPI –

+0

在這種情況下,並非所有MPI進程都在其他地方的同一時間完成/退出。它不應該是必要的,但是在廣播之後放置一個障礙,看看它是否有點不同步,或者某些進程真的停留在某個地方。 –

+0

fprintf行末尾應該有分號。 – JC1