Open MPI logo

Open MPI User's Mailing List Archives

  |   Home   |   Support   |   FAQ   |   all Open MPI User's mailing list

Subject: [OMPI users] Can't read values with MPI-IO
From: Luís Miranda (luistm_at_[hidden])
Date: 2010-01-13 12:01:05


Hi, i have this program, that write the rank of a process on a file. Every
processes write the rank on the same file. Whe i try to read that value, is
always zero.

#include <stdio.h> /*IO stuff lives here*/
#include <stdlib.h> /*exit lives here*/
#include <mpi.h> /*MPI and MPI-IO live here*/

//GLobal variables
int rank;

void mpi(int error_code){

  if (error_code != MPI_SUCCESS) {

    char error_string[BUFSIZ];
    int length_of_error_string, error_class;

    MPI_Error_class(error_code, &error_class);
    MPI_Error_string(error_class, error_string, &length_of_error_string);
    fprintf(stderr, "%3d: %s\n", rank, error_string);
    MPI_Error_string(error_code, error_string, &length_of_error_string);
    fprintf(stderr, "%3d: %s\n", rank, error_string);
  // send_error = TRUE;
  }
}

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

  int size;

  MPI_File fh;

  MPI_Init(&argc, &argv);

  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &size);

  MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);

/*
  MPI_MODE_RDONLY --- read only,
  MPI_MODE_RDWR --- reading and writing,
  MPI_MODE_WRONLY --- write only,
  MPI_MODE_CREATE --- create the file if it does not exist,
  MPI_MODE_EXCL --- error if creating file that already exists,
  MPI_MODE_DELETE_ON_CLOSE --- delete file on close,
  MPI_MODE_UNIQUE_OPEN --- file will not be concurrently opened elsewhere,
  MPI_MODE_SEQUENTIAL --- file will only be accessed sequentially,
  MPI_MODE_APPEND --- set initial position of all file pointers to end of
file.
 */

  MPI_Barrier(MPI_COMM_WORLD);

//OPEN FILE
// abertura do ficheiro
  //printf("[%d] - A abrir ficheiro\n", rank);
  mpi(MPI_File_open(MPI_COMM_WORLD, "buffer.io", MPI_MODE_CREATE |
MPI_MODE_WRONLY, MPI_INFO_NULL, &fh));
  MPI_Barrier(MPI_COMM_WORLD);

//calcular quantidade de bytes a escrever no ficheiro
  //MPI_Offset totalNumberOfIntegers

//DEFINE OFFSET
//definir o offset
  MPI_Offset myOffset = (MPI_Offset)(rank * sizeof(int));
  mpi(MPI_File_seek(fh, myOffset, MPI_SEEK_SET));
  MPI_Barrier(MPI_COMM_WORLD);

//CHECK OFFSET
// verificar a posição no ficheiro
  MPI_Offset myCurrentOffset;

  MPI_File_get_position(fh, &myCurrentOffset);
  printf ("[%d] - My current offset is %lld\n", rank, myCurrentOffset);
  MPI_Barrier(MPI_COMM_WORLD);

//WRITE RANK IN THE FILE
// escrita do ficheiro
  MPI_Status mpiStatus;
  int write=9;

  mpi(MPI_File_write(fh, &write, sizeof(int), MPI_BYTE, &mpiStatus));
  printf("[%d] - Escreveu no ficheiro o valor '%d'\n", rank, rank );
  MPI_Barrier(MPI_COMM_WORLD);

//CALCULATE FILE SIZE
//calcular quantidade de bytes a ler do ficheiro
  int numberOfBytes;
  MPI_Offset totalNumberOfBytes, numberOfBytesToRead;

  MPI_File_get_size(fh, &totalNumberOfBytes);
  numberOfBytesToRead = totalNumberOfBytes / size;
  numberOfBytes = (int) numberOfBytesToRead;
  printf("[%d] - A preparar para ler %d bytes\n", rank, numberOfBytes);
  MPI_Barrier(MPI_COMM_WORLD);

//RESET OFFSET
//restaurar valor inicial do offset
  myOffset = (MPI_Offset)(rank * sizeof(int));
  mpi(MPI_File_seek(fh, myOffset, MPI_SEEK_SET));
  MPI_Barrier(MPI_COMM_WORLD);

//CHECK OFFSET
// verificar a posição no ficheiro
  MPI_File_get_position(fh, &myCurrentOffset);
  printf ("[%d] - My current offset is %lld\n", rank, myCurrentOffset);
  MPI_Barrier(MPI_COMM_WORLD);

//READ FILE
//leitura do ficheiro
  int readBuffer=0;
  // MPI_Status mpiStatus;
  MPI_File_read(fh, &readBuffer, sizeof(int) , MPI_INT, MPI_STATUS_IGNORE);
  MPI_Barrier(MPI_COMM_WORLD);
  printf("[%d] - Leu do ficheiro o valor '%d'\n", rank, readBuffer );
  MPI_Barrier(MPI_COMM_WORLD);

//CLOSE FILE
// fecho do ficheiro
  MPI_File_close(&fh);
  MPI_File_delete("buffer.io", MPI_INFO_NULL );

//end program
  MPI_Finalize();

  return(0);

}

-- 
Cumprimentos
Luís Miranda