I made a simple program in which I compare two images pixel by pixel and determine if the pictures are the same. I'm trying to adapt it to MPI, but I'm afraid that the communications are taking too long making it way more inefficient than its sequential counterpart. I have tried with images of very big resolution and the result is the same: the sequential code is more efficient than the parallel code. Is there's a way of making it more efficient?
Sequential Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
unsigned char* BMP(char* filename,int* sizes)
{
int i;
FILE* f = fopen(filename, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f);
int ancho = *(int*)&info[18];
int alto = *(int*)&info[22];
int size = 3 * ancho * alto;
*sizes = size;
unsigned char* data = new unsigned char[size];
fread(data, sizeof(unsigned char), size, f);
fclose(f);
for(i = 0; i < size; i += 3)
{
unsigned char tmp = data[i];
data[i] = data[i+2];
data[i+2] = tmp;
}
return data;
}
int main(int argc,char **argv){
int sizes,i,bol;
clock_t t1,t2;
double tiemp;
t1 = clock();
bol=1;
unsigned char* data1= BMP(argv[1],&sizes);
unsigned char* data2= BMP(argv[2],&sizes);
for (i =0; i<sizes; i += 3)
{
if(data1[i]!=data2[i]){
printf("The images are not the same\n");
bol=0;
break;}
}
if(bol==1)
printf("The images are the same\n");
t2 = clock();
tiemp = ((double) (t2 - t1)) / (CLOCKS_PER_SEC);
printf("%f\n",tiemp );
return 0;
}
MPI counter part
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <time.h>
unsigned char* BMP(char* filename,int* sizes)
{
int i;
FILE* f = fopen(filename, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f);
int ancho = *(int*)&info[18];
int alto = *(int*)&info[22];
int size = 3 * ancho * alto;
*sizes = size;
unsigned char* data = new unsigned char[size];
fread(data, sizeof(unsigned char), size, f);
fclose(f);
for(i = 0; i < size; i += 3)
{
unsigned char tmp = data[i];
data[i] = data[i+2];
data[i+2] = tmp;
}
return data;
}
int main(int argc,char **argv){
int sizes,i,world_rank,world_size;
clock_t t1,t2;
double tiemp;
t1 = clock();
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
unsigned char* data1;
unsigned char* data2;
int root = 0;
if(world_rank==0){
data1= BMP(argv[1],&sizes);
data2= BMP(argv[2],&sizes);
printf("%d",sizes);
}
MPI_Bcast(&sizes,1,MPI_INT,root,MPI_COMM_WORLD);
int num_elements_por_proc = sizes/world_size;
unsigned char* subdata2=new unsigned char[num_elements_por_proc];
unsigned char* subdata1=new unsigned char[num_elements_por_proc];
MPI_Scatter( data1, num_elements_por_proc, MPI_UNSIGNED_CHAR, subdata1, num_elements_por_proc, MPI_UNSIGNED_CHAR, root, MPI_COMM_WORLD );
MPI_Scatter( data2, num_elements_por_proc, MPI_UNSIGNED_CHAR, subdata2, num_elements_por_proc, MPI_UNSIGNED_CHAR, root, MPI_COMM_WORLD );
int bol = 0;
if(world_rank!=0){
for(i=0;i<=num_elements_por_proc;i++){
if(subdata1[i]!=subdata2[i]){
bol = 1;
break;
}
}
}
int bolls;
MPI_Reduce(&bol,&bolls,1, MPI_INT, MPI_SUM, 0,MPI_COMM_WORLD);
if(world_rank==0){
if(bolls !=0){
printf("The images are not the samen");}
else{
printf("The images are the same \n" );}
t2 = clock();
tiemp = ((double) (t2 - t1)) / (CLOCKS_PER_SEC);
printf("%f\n",tiemp );
}
MPI_Finalize();
return 0;
}