My MPI_Scatterv
doesn't always work. I get an error message
status of rank 0: killed by signal 11.
Below is my code:
#include <mpi.h>
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <cstring>
#include <cstdlib>
#include <math.h>
#include "bmp.h"
using namespace std;
//定義平滑運算的次數
#define NSmooth 1000
BMPHEADER bmpHeader;
BMPINFO bmpInfo;
RGBTRIPLE **BMPSaveData = NULL;
RGBTRIPLE **BMPData = NULL;
RGBTRIPLE **BMPRecv = NULL;
int readBMP( char *fileName); //read file
int saveBMP( char *fileName); //save file
void swap(RGBTRIPLE *a, RGBTRIPLE *b);
RGBTRIPLE **alloc_memory( int Y, int X ); //allocate memory
int main(int argc,char *argv[])
{
char *infileName = "input.bmp";
char *outfileName = "output2.bmp";
int rem, *displs, new_height, width, id, process, sum=0, *sendcounts;
int count = 3;
int lengths[3] = {1, 1, 1};
MPI_Aint offsets[3] = {0, 1, 2};
MPI_Datatype types[3] = {MPI_BYTE, MPI_BYTE, MPI_BYTE};
MPI_Datatype datatype;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &id);
MPI_Comm_size(MPI_COMM_WORLD, &process);
MPI_Type_struct(count, lengths, offsets, types, &datatype);
MPI_Type_commit(&datatype);
double startwtime = 0.0, endwtime=0;
startwtime = MPI_Wtime();
if(id == 0)
{
if ( readBMP( infileName) )
{
cout << "Read file successfully!!" << endl;
rem = (bmpInfo.biHeight*bmpInfo.biWidth)%process;
new_height = bmpInfo.biHeight / process;
width = bmpInfo.biWidth;
}
else
{
cout << "Read file fails!!" << endl;
MPI_Finalize();
return 0;
}
}
MPI_Bcast(&rem, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&new_height, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&width, 1, MPI_INT, 0, MPI_COMM_WORLD);
sendcounts = (int *)malloc(sizeof(int ) *process);
displs = (int *)malloc(sizeof(int ) *process);
for (int i =0; i < process; i++)
{
sendcounts[i] = new_height*width;
if (rem > 0)
{
sendcounts[i]++;
rem--;
}
displs[i] = sum;
sum += sendcounts[i];
}
BMPRecv = alloc_memory(new_height, width);
#####MPI_Scatterv(&BMPSaveData, sendcounts, displs, datatype, &BMPRecv, (new_height*width), datatype, 0, MPI_COMM_WORLD);
BMPData = alloc_memory(new_height, width);
for(int count = 0; count < NSmooth ; count ++)
{
swap(BMPRecv,BMPData);
for(int i = 0; new_height ; i++)
{
for(int j =0; j<width ; j++)
{
int Top = i>0 ? i-1 : new_height-1;
int Down = i<new_height-1 ? i+1 : 0;
int Left = j>0 ? j-1 : width-1;
int Right = j<width-1 ? j+1 : 0;
BMPRecv[i][j].rgbBlue = (double) (BMPData[i][j].rgbBlue+BMPData[Top][j].rgbBlue+BMPData[Down][j].rgbBlue+BMPData[i][Left].rgbBlue+BMPData[i][Right].rgbBlue)/5+0.5;
BMPRecv[i][j].rgbGreen = (double) (BMPData[i][j].rgbGreen+BMPData[Top][j].rgbGreen+BMPData[Down][j].rgbGreen+BMPData[i][Left].rgbGreen+BMPData[i][Right].rgbGreen)/5+0.5;
BMPRecv[i][j].rgbRed = (double) (BMPData[i][j].rgbRed+BMPData[Top][j].rgbRed+BMPData[Down][j].rgbRed+BMPData[i][Left].rgbRed+BMPData[i][Right].rgbRed)/5+0.5;
}
}
}
MPI_Gatherv(&BMPRecv, (new_height*width), datatype, &BMPSaveData, sendcounts, displs, datatype, 0,MPI_COMM_WORLD);
if (id == 0)
{
if ( saveBMP( outfileName ) )
{
cout << "Save file successfully!!" << endl;
}
else
{
cout << "Save file fails!!" << endl;
}
endwtime = MPI_Wtime();
cout << "The execution time = "<< endwtime-startwtime <<endl ;
}
free(BMPData);
free(BMPSaveData);
free(BMPRecv);
MPI_Finalize();
return 0;
}