2013-03-31 16 views
6

Sto provando a creare un file di registro da ciascun processore e quindi inviarlo alla radice come array di caratteri. Prima invio la lunghezza e poi invio i dati. La lunghezza invia bene, ma i dati sono sempre spazzatura! Qui è il mio codice:Provare a MPI_Send e MPI_Recv char array ottenere immondizia

MPI_Barrier (MPI_COMM_WORLD); 
string out = ""; 

MPI_Status status[2]; 
MPI_Request reqs[num_procs]; 

string log = "TEST"; 
int length = log.length(); 
char* temp = (char *) malloc(length+1); 
strcpy(temp, log.c_str()); 

if (my_id != 0) 
{ 
    MPI_Send (&length, 1, MPI_INT, 0, 1, MPI_COMM_WORLD); 
    MPI_Send (&temp, length+1, MPI_CHAR, 0, 1, MPI_COMM_WORLD); 
} 
else {  
    int length; 
    for (int i = 1; i < num_procs; i++) 
    { 
     MPI_Recv (&length, 2, MPI_INT, i, 1, MPI_COMM_WORLD, &status[0]); 
     char* rec_buf; 
     rec_buf = (char *) malloc(length+1); 
     MPI_Recv (rec_buf, length+1, MPI_CHAR, i, 1, MPI_COMM_WORLD, &status[1]); 
     out += rec_buf; 
     free(rec_buf); 
    } 
} 

MPI_Barrier (MPI_COMM_WORLD); 
free(temp); 
+3

È possibile aggiungere una risposta. Devi solo aspettare un certo periodo di tempo. Se torni indietro e aggiungi la tua risposta ora, questa non verrà visualizzata come non risposta e potrai contrassegnarla come risposta (dopo un altro periodo di attesa). –

+0

sì per favore aggiungi la tua risposta sotto –

+0

Perché stai inviando una parola con 'MPI_Send (& length, 1, ...)' ma ricevendo due parole 'MPI_Recv (& length, 2, ...)'? –

risposta

3

si passa un char** a MPI_Send invece di un char* questo provoca il danneggiamento della memoria, o nel vostro caso l'uscita distorta che si stanno ottenendo. Tutto dovrebbe andare bene se si utilizza

MPI_Send (temp, length+1, MPI_CHAR, 0, 1, MPI_COMM_WORLD); 

(notare il rimosso & davanti al primo argomento, temp.)