I am writing a c++ program using MPI and I have been having trouble passing a array with send and receive.

What I have so in my code(sorry if it is a messy(its almost 4am and i am very tired)

/* 
     * File:   main.cpp
     * Author: Melissa
     *
     * Created on July 29, 2012, 10:11 PM
     */
    #include <cmath>
    #include <cstdlib>
    #include <mpi.h>

    #include <fstream>
    #include <stack>

    #include <iostream>
    using namespace std;

    /*
     * 
     */
    const int maxN = 1;
    void print(int *input, int l)
    {

        for ( int i = 0; i < l; i++ )
            cout << input[i] << " ";
        cout << endl;
    }

    void printToFile(int *input, int l, char* filename)
    {
        ofstream outputFile(filename); 

        for ( int i = 0; i < l; i++ )
        {
             outputFile << input[i];
            outputFile<<"\r\n";
        }
        outputFile.close();

    }

    void shuffle(int a[], int l, int r)
      { int i, j, m = (l+r)/2;
        static int aux[maxN];
        for (i = l, j = 0; i <= r; i+=2, j++)
          { aux[i] = a[l+j]; aux[i+1] = a[m+1+j]; }
        for (i = l; i <= r; i++) a[i] = aux[i];

      }

    void unshuffle(int a[], int l, int r)
      { int i, j, m = (l+r)/2;
        static int aux[maxN];
        for (i = l, j = 0; i <= r; i+=2, j++)
          { aux[l+j] = a[i]; aux[m+1+j] = a[i+1]; }
        for (i = l; i <= r; i++) a[i] = aux[i];

      }

    void exch(int &A, int &B)
        { int t = A ; A = B; B = t; }

      void compexch(int &A, int &B)
        { if (B < A) exch(A, B); }

    void merge(int a[], int l, int m, int r)
      {
        if (r == l+1) compexch(a[l], a[r]);
        if (r < l+2) return;
        unshuffle(a, l, r);
        merge(a, l, (l+m)/2, m);
        merge(a, m+1, (m+1+r)/2, r);
        shuffle(a, l, r);
        for (int i = l+1; i < r; i+=2)
          compexch(a[i], a[i+1]);


      }

    void merge_sort(int* input, int l, int r)
    {
        if ( l < r )
        {
            int mid = floor((l + r) / 2);
            merge_sort(input, l, mid);
            merge_sort(input, mid + 1, r);
            merge(input,l, mid, r);
        }
    }
    int arraySize = 0;
    int* readData(char* filename)
    {
        arraySize = 0;
        stack<int> s;

        int i;
        char *inname = filename;
        ifstream infile(inname);

        if (!infile) {
            cout << "There was a problem opening file "
                 << inname
                 << " for reading."
                 << endl;
            return 0;
        }
        cout << "Opened " << inname << " for reading." << endl;
        while (infile >> i) {
           // cout << "Value from file is " << i << endl;
            s.push(i);
        }

        int* a = new int[s.size()];
        int size = s.size();
        arraySize = size;
        for (int i = 0; i < size; i++) {
            a[i] = s.top();
           // cout<<"popped: "<<s.top()<<endl;
            s.pop();
        }

      return a;
    }

    int* getSubset(int* arr, int start, int count)
    {
      int* temp = new int[count];
      for(int i = start, j = 0; i < count; i++,j++)  
      {
        temp[j] = arr[i];
        printf("Copy Over %d: %d\n",j,temp[j]);    
      }


      return temp;
    }

    int* combine(int* arr1, int* arr2, bool firstCombine)
    {
      if(!(firstCombine))
      {
        int size1 = sizeof( arr1 ) / sizeof( arr1[0] );
        int size2 = sizeof( arr2 ) / sizeof( arr2[0] );
        int* temp = new int[size1+size2];
        int  i;
        for(i = 0; i < size1; i++)
          temp[i] = arr1[i];

        for(int j = 0; j < size2; j++,i++)
        {
          temp[i] = arr2[j];
        }

        return temp;
      }
      else
      {
        return arr2;
      }
    }
    int main(int argc, char** argv) {


        char* name = argv[2];
        cout<<argv[1];


        MPI_Init(NULL, NULL);

      // Find out rank, size
        int world_rank;
        MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
        int world_size;
        MPI_Comm_size(MPI_COMM_WORLD, &world_size);;
        MPI_Status status;

    if(world_rank == 0)
    {
      int* b = readData(name);//list of ints
      int elementsPerThread = (world_size-1)/arraySize;
      int readyCount = 1;
      while(readyCount != world_size)
      {
        int active;
        MPI_Recv(&active, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);//Customer tag = 5
        readyCount++;
      }

      for(int i = 0,j =1; j < world_size; i += elementsPerThread, j++)
      {
        int* subset = getSubset(b,i,elementsPerThread);
        print(subset, elementsPerThread);
        printf("Send to rank %d \n", j);
        MPI_Send(&subset, elementsPerThread, MPI_INT, j, 0, MPI_COMM_WORLD); //barber = 1 thread id, send customer id      
      }

      int count = 0;
      int* completedSort;
      while(count < world_size-1)
      {
        int* sortedSubset;
        MPI_Recv(&sortedSubset, 50, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);//Customer tag = 5
        bool first = false;
        if(count == 0)
          first = true;
        completedSort = combine(completedSort,sortedSubset, first);
        count++;
      }

      merge_sort(completedSort,0,arraySize-1);

      printToFile(completedSort,arraySize, argv[3]);
    } 
    else
    {
      int active = 1;
           MPI_Send(&active, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); //barber = 1 thread id, send customer id
         int* subset;
        MPI_Recv(&subset, 50, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);//Customer tag = 5
         printf("Test %d   ,   %d \n", subset[0],subset[1]);
        int size = sizeof( subset ) / sizeof( subset[0] );
        merge_sort(subset,0,size-1);
           MPI_Send(&subset, size, MPI_INT, 0, 0, MPI_COMM_WORLD); //barber = 1 thread id, send customer id      


    }
        //print(b, arraySize);
        //merge_sort(b,0,arraySize-1);

        //printToFile(b,arraySize, argv[3]);

      MPI_Finalize();
    }

My output looks like this:

mpirun -n 5  ./oemsort 2 unsorted.txt sorted.txt
*** glibc detected *** ./oemsort: malloc(): memory corruption: 0x0000000001745780 ***
2Opened unsorted.txt for reading.

Send to rank 1 

Send to rank 2 

Send to rank 3 

Send to rank 4 
2Test -443987883   ,   283935560 
2Test -443987883   ,   283935560 
2Test -443987883   ,   283935560 
2Test -443987883   ,   283935560 
======= Backtrace: =========
/lib/libc.so.6(+0x775b6)[0x2b496cc6c5b6]
/lib/libc.so.6(+0x7b6d8)[0x2b496cc706d8]
/lib/libc.so.6(__libc_malloc+0x6e)[0x2b496cc7158e]
/lib/libc.so.6(+0x684cb)[0x2b496cc5d4cb]
/usr/lib/libstdc++.so.6(_ZNSt12__basic_fileIcE4openEPKcSt13_Ios_Openmodei+0x3f)[0x2b496c2f09df]
/usr/lib/libstdc++.so.6(_ZNSt13basic_filebufIcSt11char_traitsIcEE4openEPKcSt13_Ios_Openmode+0x63)[0x2b496c29d663]
/usr/lib/libstdc++.so.6(_ZNSt14basic_ofstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode+0x102)[0x2b496c29e712]
./oemsort(_Z11printToFilePiiPc+0x5b)[0x40be51]
./oemsort(main+0x241)[0x40c922]
/lib/libc.so.6(__libc_start_main+0xfd)[0x2b496cc13c4d]
./oemsort[0x40bcd9]
======= Memory map: ========
00400000-00418000 r-xp 00000000 08:06 419824                             /home/melissa/Desktop/mpi_send_recv/Task2/oemsort
00617000-00618000 r--p 00017000 08:06 419824                             /home/melissa/Desktop/mpi_send_recv/Task2/oemsort
00618000-00619000 rw-p 00018000 08:06 419824                             /home/melissa/Desktop/mpi_send_recv/Task2/oemsort
00619000-0061a000 rw-p 00000000 00:00 0 
0153c000-0175b000 rw-p 00000000 00:00 0                                  [heap]
2b496b059000-2b496b079000 r-xp 00000000 08:06 130081                     /lib/ld-2.11.1.so
2b496b079000-2b496b07c000 rw-p 00000000 00:00 0 
2b496b278000-2b496b279000 r--p 0001f000 08:06 130081                     /lib/ld-2.11.1.so
2b496b279000-2b496b27a000 rw-p 00020000 08:06 130081                     /lib/ld-2.11.1.so
2b496b27a000-2b496b27b000 rw-p 00000000 00:00 0 
2b496b27b000-2b496b293000 r-xp 00000000 08:06 299354                     /usr/lib/openmpi/lib/libmpi_cxx.so.0.0.0
2b496b293000-2b496b492000 ---p 00018000 08:06 299354                     /usr/lib/openmpi/lib/libmpi_cxx.so.0.0.0
2b496b492000-2b496b494000 r--p 00017000 08:06 299354                     /usr/lib/openmpi/lib/libmpi_cxx.so.0.0.0
2b496b494000-2b496b495000 rw-p 00019000 08:06 299354                     /usr/lib/openmpi/lib/libmpi_cxx.so.0.0.0
2b496b495000-2b496b52a000 r-xp 00000000 08:06 299353                     /usr/lib/openmpi/lib/libmpi.so.0.0.1
2b496b52a000-2b496b729000 ---p 00095000 08:06 299353                     /usr/lib/openmpi/lib/libmpi.so.0.0.1
2b496b729000-2b496b72a000 r--p 00094000 08:06 299353                     /usr/lib/openmpi/lib/libmpi.so.0.0.1
2b496b72a000-2b496b73b000 rw-p 00095000 08:06 299353                     /usr/lib/openmpi/lib/libmpi.so.0.0.1
2b496b73b000-2b496b746000 rw-p 00000000 00:00 0 
2b496b746000-2b496b78e000 r-xp 00000000 08:06 299358                     /usr/lib/openmpi/lib/libopen-rte.so.0.0.0
2b496b78e000-2b496b98d000 ---p 00048000 08:06 299358                     /usr/lib/openmpi/lib/libopen-rte.so.0.0.0
2b496b98d000-2b496b98e000 r--p 00047000 08:06 299358                     /usr/lib/openmpi/lib/libopen-rte.so.0.0.0
2b496b98e000-2b496b990000 rw-p 00048000 08:06 299358                     /usr/lib/openmpi/lib/libopen-rte.so.0.0.0
2b496b990000-2b496b992000 rw-p 00000000 00:00 0 
2b496b992000-2b496b9e0000 r-xp 00000000 08:06 299357                     /usr/lib/openmpi/lib/libopen-pal.so.0.0.0
2b496b9e0000-2b496bbe0000 ---p 0004e000 08:06 299357                     /usr/lib/openmpi/lib/libopen-pal.so.0.0.0
2b496bbe0000-2b496bbe1000 r--p 0004e000 08:06 299357                     /usr/lib/openmpi/lib/libopen-pal.so.0.0.0
2b496bbe1000-2b496bbe3000 rw-p 0004f000 08:06 299357                     /usr/lib/openmpi/lib/libopen-pal.so.0.0.0
2b496bbe3000-2b496bc06000 rw-p 00000000 00:00 0 
2b496bc06000-2b496bc08000 r-xp 00000000 08:06 130119                     /lib/libdl-2.11.1.so
2b496bc08000-2b496be08000 ---p 00002000 08:06 130119                     /lib/libdl-2.11.1.so
2b496be08000-2b496be09000 r--p 00002000 08:06 130119                     /lib/libdl-2.11.1.so
2b496be09000-2b496be0a000 rw-p 00003000 08:06 130119                     /lib/libdl-2.11.1.so
2b496be0a000-2b496be0b000 rw-p 00000000 00:00 0 
2b496be0b000-2b496be22000 r-xp 00000000 08:06 130165                     /lib/libnsl-2.11.1.so
2b496be22000-2b496c021000 ---p 00017000 08:06 130165                     /lib/libnsl-2.11.1.so
2b496c021000-2b496c022000 r--p 00016000 08:06 130165                     /lib/libnsl-2.11.1.so
2b496c022000-2b496c023000 rw-p 00017000 08:06 130165                     /lib/libnsl-2.11.1.so
2b496c023000-2b496c025000 rw-p 00000000 00:00 0 
2b496c025000-2b496c027000 r-xp 00000000 08:06 130244                     /lib/libutil-2.11.1.so
2b496c027000-2b496c226000 ---p 00002000 08:06 130244                     /lib/libutil-2.11.1.so
2b496c226000-2b496c227000 r--p 00001000 08:06 130244                     /lib/libutil-2.11.1.so
2b496c227000-2b496c228000 rw-p 00002000 08:06 130244                     /lib/libutil-2.11.1.so
2b496c228000-2b496c31e000 r-xp 00000000 08:06 18694                      /usr/lib/libstdc++.so.6.0.13
2b496c31e000-2b496c51e000 ---p 000f6000 08:06 18694                      /usr/lib/libstdc++.so.6.0.13
2b496c51e000-2b496c525000 r--p 000f6000 08:06 18694                      /usr/lib/libstdc++.so.6.0.13
2b496c525000-2b496c527000 rw-p 000fd000 08:06 18694                      /usr/lib/libstdc++.so.6.0.13
2b496c527000-2b496c53d000 rw-p 00000000 00:00 0 
2b496c53d000-2b496c5bf000 r-xp 00000000 08:06 130154                     /lib/libm-2.11.1.so
2b496c5bf000-2b496c7be000 ---p 00082000 08:06 130154                     /lib/libm-2.11.1.so
2b496c7be000-2b496c7bf000 r--p 00081000 08:06 130154                     /lib/libm-2.11.1.so
2b496c7bf000-2b496c7c0000 rw-p 00082000 08:06 130154                     /lib/libm-2.11.1.so
2b496c7c0000-2b496c7d6000 r-xp 00000000 08:06 158099                     /lib/libgcc_s.so.1
2b496c7d6000-2b496c9d5000 ---p 00016000 08:06 158099                     /lib/libgcc_s.so.1
2b496c9d5000-2b496c9d6000 r--p 00015000 08:06 158099                     /lib/libgcc_s.so.1
2b496c9d6000-2b496c9d7000 rw-p 00016000 08:06 158099                     /lib/libgcc_s.so.1
2b496c9d7000-2b496c9ef000 r-xp 00000000 08:06 130213                     /lib/libpthread-2.11.1.so
2b496c9ef000-2b496cbee000 ---p 00018000 08:06 130213                     /lib/libpthread-2.11.1.so--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 4242 on node melissa-laptop exited on signal 6 (Aborted).
--------------------------------------------------------------------------
[melissa-laptop:04242] *** Process received signal ***
[melissa-laptop:04242] Signal: Aborted (6)
[melissa-laptop:04242] Signal code:  (-6)
[melissa-laptop:04242] [ 0] /lib/libpthread.so.0(+0xf8f0) [0x2b496c9e68f0]
[melissa-laptop:04242] [ 1] /lib/libc.so.6(gsignal+0x35) [0x2b496cc28a75]
[melissa-laptop:04242] [ 2] /lib/libc.so.6(abort+0x180) [0x2b496cc2c5c0]
[melissa-laptop:04242] [ 3] /lib/libc.so.6(+0x6d4fb) [0x2b496cc624fb]
[melissa-laptop:04242] [ 4] /lib/libc.so.6(+0x775b6) [0x2b496cc6c5b6]
[melissa-laptop:04242] [ 5] /lib/libc.so.6(+0x7b6d8) [0x2b496cc706d8]
[melissa-laptop:04242] [ 6] /lib/libc.so.6(__libc_malloc+0x6e) [0x2b496cc7158e]
[melissa-laptop:04242] [ 7] /lib/libc.so.6(+0x684cb) [0x2b496cc5d4cb]
[melissa-laptop:04242] [ 8] /usr/lib/libstdc++.so.6(_ZNSt12__basic_fileIcE4openEPKcSt13_Ios_Openmodei+0x3f) [0x2b496c2f09df]
[melissa-laptop:04242] [ 9] /usr/lib/libstdc++.so.6(_ZNSt13basic_filebufIcSt11char_traitsIcEE4openEPKcSt13_Ios_Openmode+0x63) [0x2b496c29d663]
[melissa-laptop:04242] [10] /usr/lib/libstdc++.so.6(_ZNSt14basic_ofstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode+0x102) [0x2b496c29e712]
[melissa-laptop:04242] [11] ./oemsort(_Z11printToFilePiiPc+0x5b) [0x40be51]
[melissa-laptop:04242] [12] ./oemsort(main+0x241) [0x40c922]
[melissa-laptop:04242] [13] /lib/libc.so.6(__libc_start_main+0xfd) [0x2b496cc13c4d]
[melissa-laptop:04242] [14] ./oemsort() [0x40bcd9]
[melissa-laptop:04242] *** End of error message ***

Any help wouldbe appreciated

From this line:
./oemsort(_Z11printToFilePiiPc+0x5b)[0x40be51]

It looks like the problem is in the function printToFile. Looking at line 212 of your code, you pass in argv[3] as the file name. If you check the command line that you invoked the program with:

mpirun -n 5 ./oemsort 2 unsorted.txt sorted.txt

Then, argv[3] is ./oemsort. I'm not sure if this is what's happening, since I don't know how mpirun deals with the command-line args (it might end up accounting for this and sorted.txt could be your filename. If I were you, I'd put a print statement in that function and check the filename that you're trying to open :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.