Open MPI logo

Open MPI User's Mailing List Archives

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

Subject: [OMPI users] Unexpected Behavior with C++ New Memory Allocation and MPI IO
From: Corey Allen (corey.allen_at_[hidden])
Date: 2013-06-12 19:44:51


I have done a search on this and I haven't found an explanation. I am not a
C/C++ nor MPI expert. I am getting a strange error when I use the C++ new
keyword to allocate memory and then call a file read on OpenMPI. (Tech
detail: 4-core Intel running Ubuntu 64-bit and OpenMPI 1.4.5. Everything is
local. Total processes is 5.)

In the source code you can see three different approaches to allocating the
memory for the char array where the file's contents will go. With the
C-style and MPI Alloc choices, there is no error even with buffer sizes in
the MB range. However, if I use the C++ new keyword, I get an error for any
read larger than 24 bytes.

As I say, I'm not a C/C++ expert, but if I'm writing C++ code I was under
the impression it was wise to use the C++ new approach rather than malloc.
Am I missing something in getting this to work with MPI?

Error (the important bit):

*** glibc detected *** MPIIO_Test: free(): invalid next size (fast):
0x00000000015524e0 ***

Code:

int main(int argc, char* argv[]){
    int my_rank;
    MPI_Status status ;
    int errorCode;
    MPI_File _filehandle;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    // Open File
    errorCode = MPI_File_open(MPI_COMM_WORLD, "TEST1A", MPI_MODE_RDONLY,
MPI_INFO_NULL, &_filehandle);

    if (errorCode != 0)
    {
        int errorStringLen;
        char *errorString = (char*) malloc(MPI_MAX_ERROR_STRING);

        // Get MPI file I/O error string and print it
        MPI_Error_string(errorCode, errorString, &errorStringLen);

        printf("MPI Error opening Data File: %s \n", errorString);

        free(errorString);
    }

    // Works with new at 24, fails at 25
    int dataLength = 25, numBytesRead;
    char *data = NULL;
// data = (char*) malloc(dataLength);
    data = new char(dataLength);
// MPI_Alloc_mem(dataLength, MPI_INFO_NULL, &data);

    // File I/O
    errorCode = MPI_File_read_at(_filehandle, my_rank*dataLength, data,
dataLength, MPI_CHAR, &status);
    MPI_Get_count(&status, MPI_CHAR, &numBytesRead);
    printf("Rank: %d Start: %d Expected: %d Read: %d \n", my_rank,
my_rank*dataLength, dataLength, numBytesRead);

    if (errorCode != 0)
    {
        int errorStringLen;
        char *errorString = (char*) malloc(MPI_MAX_ERROR_STRING);

        // Get MPI file I/O error string and print it
        MPI_Error_string(errorCode, errorString, &errorStringLen);

        printf("MPI Error reading from Data File: %s \n", errorString);

        free(errorString);
    }

    // Free memory
// free(data);
    delete[] data;
// MPI_Free_mem(data);

    // Close file
    MPI_File_close(&_filehandle);

    MPI_Finalize();
    return 0;
}