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
#include <iostream>
#include <mpi.h>
#include <cmath>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
//int mod(int z, int l);
int xGradient(Mat image, int x, int y)
{
return image.at<uchar>(y-1, x-1) +
2*image.at<uchar>(y, x-1) +
image.at<uchar>(y+1, x-1) -
image.at<uchar>(y-1, x+1) -
2*image.at<uchar>(y, x+1) -
image.at<uchar>(y+1, x+1);
}
int yGradient(Mat image, int x, int y)
{
return image.at<uchar>(y-1, x-1) +
2*image.at<uchar>(y-1, x) +
image.at<uchar>(y-1, x+1) -
image.at<uchar>(y+1, x-1) -
2*image.at<uchar>(y+1, x) -
image.at<uchar>(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(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &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(&(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<slave; i++)
{
MPI_Send(&dst, 1, MPI_DOUBLE, i, 1, MPI_COMM_WORLD);
cout<<"master mengirim data ke rank 1"<<dst<<endl;
}
}
MPI_Barrier(MPI_COMM_WORLD);
if (rank != master)
{
MPI_Recv(&dst, 1, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
cout<<"rank 1 menerima data"<<dst<<endl;
}
for(int y = 0; y < grey.rows; y++)
for(int x = 0; x < grey.cols; x++)
dst.at<uchar>(y,x) = 0;
for(int y = 1; y < grey.rows - 1; y++)
{
for(int x = 1; x < grey.cols - 1; x++)
{
gx = xGradient(grey, x, y);
gy = yGradient(grey, x, y);
sum = abs(gx) + abs(gy);
sum = sum > 255 ? 255:sum;
sum = sum < 0 ? 0 : sum;
dst.at<uchar>(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<<"waktu eksekusi adalah: "<< end-start << " detik " <<endl;
akhir=MPI_Finalize();
//waitKey();
return 0;
}
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?