title: sobel edge detection using mpi
body: <p>I'm an mpi newbie. I'm trying to parallelize my code with mpi (need to run some experiments faster). It should work like this: master sends an array of strings to the slaves, they do some job and send status_ready back to the master. When all slaves are ready, master goes into a loop and iterativelly sends a vector of doubles to the slaves, slaves process this vector and send their results (2 vectors) back to the master. When all tje messages are received, master will process it and the loop iterates (master sends the results to the slaves, etc.) It should work like this</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;mpi.h&gt;
  #include &lt;cmath&gt;
  #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  
  using namespace std;
  using namespace cv;
  //int mod(int z, int l);
  
  int xGradient(Mat image, int x, int y)
  {
    return image.at&lt;uchar&gt;(y-1, x-1) +
      2*image.at&lt;uchar&gt;(y, x-1) +
      image.at&lt;uchar&gt;(y+1, x-1) -
      image.at&lt;uchar&gt;(y-1, x+1) -
      2*image.at&lt;uchar&gt;(y, x+1) -
      image.at&lt;uchar&gt;(y+1, x+1);
  }
  
  int yGradient(Mat image, int x, int y)
  {
    return image.at&lt;uchar&gt;(y-1, x-1) +
      2*image.at&lt;uchar&gt;(y-1, x) +
      image.at&lt;uchar&gt;(y-1, x+1) -
      image.at&lt;uchar&gt;(y+1, x-1) -
      2*image.at&lt;uchar&gt;(y+1, x) -
      image.at&lt;uchar&gt;(y+1, x+1);
  }
  
  int main()
  {
    Mat src, grey, dst;
    double start, end;
    int i, gx, gy, sum, argc, awal,akhir, size, rank, slave;
    int master=0;
    char **argv;
    // MPI_Status status;
    awal= MPI_Init(&amp;argc, &amp;argv);
    MPI_Comm_size(MPI_COMM_WORLD, &amp;size);
    MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);
    slave=size-1;
    start=MPI_Wtime();
    if( rank == master )
      {
        // start=MPI_Wtime();
        src= imread("E:/tigaout/Debug/jari.jpg");  
        cvtColor(src,grey,CV_BGR2GRAY);
  
        //MPI_Send(&amp;(row_pointers[i*share+done][0]), 1, newtype, i, 1, MPI_COMM_WORLD);
        dst = grey.clone();
        if( !grey.data )
          {
            return -1;
          }
        for (i=1; i&lt;slave; i++)
          {
            MPI_Send(&amp;dst, 1, MPI_DOUBLE, i, 1, MPI_COMM_WORLD);
            cout&lt;&lt;"master mengirim data ke rank 1"&lt;&lt;dst&lt;&lt;endl;
          }
      }
    MPI_Barrier(MPI_COMM_WORLD);
    if (rank != master)
      {
        MPI_Recv(&amp;dst, 1, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        cout&lt;&lt;"rank 1 menerima data"&lt;&lt;dst&lt;&lt;endl;
      }
    for(int y = 0; y &lt; grey.rows; y++)
      for(int x = 0; x &lt; grey.cols; x++)
        dst.at&lt;uchar&gt;(y,x) = 0;  
  
    for(int y = 1; y &lt; grey.rows - 1; y++)
      {
        for(int x = 1; x &lt; grey.cols - 1; x++)
          {
            gx = xGradient(grey, x, y);
            gy = yGradient(grey, x, y);
            sum = abs(gx) + abs(gy);
            sum = sum &gt; 255 ? 255:sum;
            sum = sum &lt; 0 ? 0 : sum;
            dst.at&lt;uchar&gt;(y,x) = sum;
  
  
          }
      }
  
  
    /*    namedWindow("deteksi tepi sobel");
              imshow("deteksi tepi sobel", dst);
  
              namedWindow("grayscale");
              imshow("grayscale", grey);
  
              namedWindow("Original");
              imshow("Original", src);*/
  
    imwrite( "E:/tigaout/Debug/deteksi jari.jpg", dst );
    MPI_Barrier(MPI_COMM_WORLD);
    end=MPI_Wtime();
    cout&lt;&lt;"waktu eksekusi adalah: "&lt;&lt; end-start &lt;&lt; " detik " &lt;&lt;endl;
    akhir=MPI_Finalize();
  
    //waitKey();
  
    return 0;
  }
  </code></pre>
  
  <p>I already try make this code using mpi point-to-point send/receive. but my code always wrong, where is my wrong,  How do I fix this?</p>