Conversion from- C++ to C-language < free(): invalid size >
-
Hi, I am converting C++ mpi program into C: I am free() error because I am using malloc at several places. I have data stored in a file. The first line of the file tells about number of lines of data in the file. I am getting that error correct i.e. 3200. I have also corrected several errors with the cooperation of code project's developers. I am testing my code with: print statements followed by:
finalize();
return;My code upto the point where I am getting invalid free() error is:
#include
#include
#include
#include
#include
//#include//using namespace std;
// Sorts the input row into chunks to be scattered two all the processors.
void sortByProcess(/*vector*/double* list1, double* list2, int count);// Swaps two rows.
void swap(double** list, int count, int row1, int row2);int rank, size;
int main(int argc, char * argv[])
{
double sTime, eTime, rTime;
/*ifstream*/ FILE* inFile;
int num_rows = 3200;
int num_cols = 3200;
int cur_control = 0;
double * send_buffer = NULL;
double * recv_buffer = NULL;
double ** data = NULL;
double determinant;
char strNum_rows[20];
/*vector*/double* file_buffer;// Just get the initialization of the program going.
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);// If the input file is not given, print message and exit.
if(argc < 2)
{
/*cout <<*/ printf("No input file given.\n");// << endl;
MPI_Finalize();
return 0;
}
// If the root node (0), then open the input file and read in the
// number of rows.
if(!rank)
{
printf("After rank inside if @@@@@");
inFile = fopen(argv[1], "r");
fgets(strNum_rows, 20, inFile);
num_rows = atoi(strNum_rows);
printf("num_rows???? =%d",num_rows);
file_buffer = malloc(num_rows * sizeof(int));/\*???? inFile.open(argv\[1\]); inFile >> num\_rows; file\_buffer.resize(num\_rows);\*/
}
//printf("After rank outside @@@@@@@@#");
send_buffer = (double *)malloc(num_rows * sizeof(double));
/*?????send_buffer = new double[num_rows];*/
//printf("After send_buffer #####@");
// Broadcasts the number of rows to each processor.
MPI_Bcast (&num_rows, 1, MPI_INT, 0, MPI_COMM_WORLD);
num_cols = num_rows / size;
// Allocate the memory on each processor.
//printf("After Bcast #####@");da
-
Hi, I am converting C++ mpi program into C: I am free() error because I am using malloc at several places. I have data stored in a file. The first line of the file tells about number of lines of data in the file. I am getting that error correct i.e. 3200. I have also corrected several errors with the cooperation of code project's developers. I am testing my code with: print statements followed by:
finalize();
return;My code upto the point where I am getting invalid free() error is:
#include
#include
#include
#include
#include
//#include//using namespace std;
// Sorts the input row into chunks to be scattered two all the processors.
void sortByProcess(/*vector*/double* list1, double* list2, int count);// Swaps two rows.
void swap(double** list, int count, int row1, int row2);int rank, size;
int main(int argc, char * argv[])
{
double sTime, eTime, rTime;
/*ifstream*/ FILE* inFile;
int num_rows = 3200;
int num_cols = 3200;
int cur_control = 0;
double * send_buffer = NULL;
double * recv_buffer = NULL;
double ** data = NULL;
double determinant;
char strNum_rows[20];
/*vector*/double* file_buffer;// Just get the initialization of the program going.
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);// If the input file is not given, print message and exit.
if(argc < 2)
{
/*cout <<*/ printf("No input file given.\n");// << endl;
MPI_Finalize();
return 0;
}
// If the root node (0), then open the input file and read in the
// number of rows.
if(!rank)
{
printf("After rank inside if @@@@@");
inFile = fopen(argv[1], "r");
fgets(strNum_rows, 20, inFile);
num_rows = atoi(strNum_rows);
printf("num_rows???? =%d",num_rows);
file_buffer = malloc(num_rows * sizeof(int));/\*???? inFile.open(argv\[1\]); inFile >> num\_rows; file\_buffer.resize(num\_rows);\*/
}
//printf("After rank outside @@@@@@@@#");
send_buffer = (double *)malloc(num_rows * sizeof(double));
/*?????send_buffer = new double[num_rows];*/
//printf("After send_buffer #####@");
// Broadcasts the number of rows to each processor.
MPI_Bcast (&num_rows, 1, MPI_INT, 0, MPI_COMM_WORLD);
num_cols = num_rows / size;
// Allocate the memory on each processor.
//printf("After Bcast #####@");da
Something, somewhere is corrupting one of your malloc() blocks. First off, I count 5 calls to
malloc()
, but only 2 calls tofree()
in the code given. That's unlikely to be the source of your problem here, but it does mean you have a memory leak, unless that memory is freed up elsewhere. Since you have 2 calls tofree()
and a hex stack dump, we don't know whichfree()
is causing the problem. In order to get better data on where and why you have the corruption first compile your program with debug info enabled. Google suggests that you should be able to pass the-g
flag to mpicc. You might also want to use the-o
option to write the output to file-name instead of a.out e.g.mpicc -g gaussian.c -o gaussian
There is an excellent tool to help debug memory allocation problems available for linux called valgrind. Once again a little googling about suggest that the way to run valgrind with mpirun ismpirun -n 2 valgrind --tool=memcheck ./a.out
You may need to figure out how to install valgrind on your system. Update: you can send the output from valgrind to a log file using
--logfile=vg_log.%p
, which would send the valgrind output to a filevg_log.1479
, where 1479 is the process id of the running program. AIUI, you would get a vg_log.nnnn for each process spawned by mpirun. Take a look at the man page for valgrind and/or see the online documentation for further info. -
Hi, I am converting C++ mpi program into C: I am free() error because I am using malloc at several places. I have data stored in a file. The first line of the file tells about number of lines of data in the file. I am getting that error correct i.e. 3200. I have also corrected several errors with the cooperation of code project's developers. I am testing my code with: print statements followed by:
finalize();
return;My code upto the point where I am getting invalid free() error is:
#include
#include
#include
#include
#include
//#include//using namespace std;
// Sorts the input row into chunks to be scattered two all the processors.
void sortByProcess(/*vector*/double* list1, double* list2, int count);// Swaps two rows.
void swap(double** list, int count, int row1, int row2);int rank, size;
int main(int argc, char * argv[])
{
double sTime, eTime, rTime;
/*ifstream*/ FILE* inFile;
int num_rows = 3200;
int num_cols = 3200;
int cur_control = 0;
double * send_buffer = NULL;
double * recv_buffer = NULL;
double ** data = NULL;
double determinant;
char strNum_rows[20];
/*vector*/double* file_buffer;// Just get the initialization of the program going.
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);// If the input file is not given, print message and exit.
if(argc < 2)
{
/*cout <<*/ printf("No input file given.\n");// << endl;
MPI_Finalize();
return 0;
}
// If the root node (0), then open the input file and read in the
// number of rows.
if(!rank)
{
printf("After rank inside if @@@@@");
inFile = fopen(argv[1], "r");
fgets(strNum_rows, 20, inFile);
num_rows = atoi(strNum_rows);
printf("num_rows???? =%d",num_rows);
file_buffer = malloc(num_rows * sizeof(int));/\*???? inFile.open(argv\[1\]); inFile >> num\_rows; file\_buffer.resize(num\_rows);\*/
}
//printf("After rank outside @@@@@@@@#");
send_buffer = (double *)malloc(num_rows * sizeof(double));
/*?????send_buffer = new double[num_rows];*/
//printf("After send_buffer #####@");
// Broadcasts the number of rows to each processor.
MPI_Bcast (&num_rows, 1, MPI_INT, 0, MPI_COMM_WORLD);
num_cols = num_rows / size;
// Allocate the memory on each processor.
//printf("After Bcast #####@");da
The code is not very well written to begin with, so please do not write something like this for a real world application. Be sure to check if each malloc succeeded before using it or you may end up crashing your application - safety first. I recommend breaking out the pencil an paper and working through the code. Drawing out the arrays (both 1D and 2D) would help in analyzing the code - along with the debugger. It is a bit confusing when the traditional row/column layout is backwards: Normal: buffer[row][column] In this code: buffer[column][row] <<<< this is backwards. file_buffer is not initialized which could lead to an issue if it does not actually get allocate it.
file_buffer = NULL;
file_buffer size allocation is not correct - it should be using sizeof(double) not sizeof(int). This is important, because you are copying double values into the buffer. Depending on the compiler, the size of a double is probably 2 x sizeof(int). This could cause massive memory corruption. When memory buffers are allocated one after another, then there is a strong possibility that they follow each other in memory - like so [file_buffer][send_buffer][data]. This means that the loop filling in the file_buffer would be over writing the send_buffer, due to the fact that the file_buffer is not large enough to hold an array of double values. A little more information is required here about the most like memory lay out. Buffer allocation layout: [[allocation size][allocated memory]] When malloc allocates the memory, it reserves the start of the allocated block to hold the size of memory block being allocated and returns a pointer to the memory location following the allocation size data. Therefore, given the above, your code probably allocated the file_buffer and overwrote the allocation size of the send_buffer, leading to the invalid size error. If the file_buffer is allocated, then ensure that it is freed. You need to free each data[i] in a loop and then free the data buffer itself. Since I do not see anything else filling in the send_buffer or recv_buffer, I am going to assume that MPI_Scatter() is filling those buffers. If that is the case, then keep in mind that the number of row does not equal the number of columns unless the size value is equal to 1.
num_cols = num_rows / size;
That implies that the following call is incorrect:
MPI_Scatter(send_buffer, num_cols, MPI_DOUBLE, recv_buffer, num_cols, MPI_DOUB
-
The code is not very well written to begin with, so please do not write something like this for a real world application. Be sure to check if each malloc succeeded before using it or you may end up crashing your application - safety first. I recommend breaking out the pencil an paper and working through the code. Drawing out the arrays (both 1D and 2D) would help in analyzing the code - along with the debugger. It is a bit confusing when the traditional row/column layout is backwards: Normal: buffer[row][column] In this code: buffer[column][row] <<<< this is backwards. file_buffer is not initialized which could lead to an issue if it does not actually get allocate it.
file_buffer = NULL;
file_buffer size allocation is not correct - it should be using sizeof(double) not sizeof(int). This is important, because you are copying double values into the buffer. Depending on the compiler, the size of a double is probably 2 x sizeof(int). This could cause massive memory corruption. When memory buffers are allocated one after another, then there is a strong possibility that they follow each other in memory - like so [file_buffer][send_buffer][data]. This means that the loop filling in the file_buffer would be over writing the send_buffer, due to the fact that the file_buffer is not large enough to hold an array of double values. A little more information is required here about the most like memory lay out. Buffer allocation layout: [[allocation size][allocated memory]] When malloc allocates the memory, it reserves the start of the allocated block to hold the size of memory block being allocated and returns a pointer to the memory location following the allocation size data. Therefore, given the above, your code probably allocated the file_buffer and overwrote the allocation size of the send_buffer, leading to the invalid size error. If the file_buffer is allocated, then ensure that it is freed. You need to free each data[i] in a loop and then free the data buffer itself. Since I do not see anything else filling in the send_buffer or recv_buffer, I am going to assume that MPI_Scatter() is filling those buffers. If that is the case, then keep in mind that the number of row does not equal the number of columns unless the size value is equal to 1.
num_cols = num_rows / size;
That implies that the following call is incorrect:
MPI_Scatter(send_buffer, num_cols, MPI_DOUBLE, recv_buffer, num_cols, MPI_DOUB
Hi John & K5054, Thanks a lot. I have modified the code as John told me but still I am getting errors but no pointer error. I tried changing num_cols to num_rows but it gave me following errors:
$ mpicc gaussian.c
$ mpirun -np 4 ./a.out matrix.3200.txt
[lc2530hz:5502] *** An error occurred in MPI_Scatter
[lc2530hz:5502] *** reported by process [2594701313,0]
[lc2530hz:5502] *** on communicator MPI_COMM_WORLD
[lc2530hz:5502] *** MPI_ERR_TRUNCATE: message truncated
[lc2530hz:5502] *** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
[lc2530hz:5502] *** and potentially your MPI job)So I changed back to num_cols. The changed code is now:
#include
#include
#include
#include
#include
//#include//using namespace std;
// Sorts the input row into chunks to be scattered two all the processors.
void sortByProcess(/*vector*/double* list1, double* list2, int count);// Swaps two rows.
void swap(double** list, int count, int row1, int row2);int rank, size;
int main(int argc, char * argv[])
{
double sTime, eTime, rTime;
/*ifstream*/ FILE* inFile;
int num_rows = 3200;
int num_cols = 3200;
int cur_control = 0;
double * send_buffer = NULL;
double * recv_buffer = NULL;
double ** data = NULL;
double determinant;
char strNum_rows[20];
/*vector*/double* file_buffer=NULL;// Just get the initialization of the program going.
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);// If the input file is not given, print message and exit.
if(argc < 2)
{
/*cout <<*/ printf("No input file given.\n");// << endl;
MPI_Finalize();
return 0;
}
// If the root node (0), then open the input file and read in the
// number of rows.
if(!rank)
{
printf("After rank inside if @@@@@");
inFile = fopen(argv[1], "r");
fgets(strNum_rows, 20, inFile);
num_rows = atoi(strNum_rows);
printf("num_rows???? =%d",num_rows);
file_buffer = (double *) malloc(num_rows * sizeof(double *));//1if(file\_buffer == NULL) { printf("malloc can't allocate memory for file\_buffer"); return -1; } /\*???? inFile.open(argv\[1\]); inFile >> num\_rows; file\_buffer.resize(num\_rows);\*/
}
//printf("After rank outside @@@@@@@@#");
send_buffer = (double *)malloc(num_rows *
-
Hi John & K5054, Thanks a lot. I have modified the code as John told me but still I am getting errors but no pointer error. I tried changing num_cols to num_rows but it gave me following errors:
$ mpicc gaussian.c
$ mpirun -np 4 ./a.out matrix.3200.txt
[lc2530hz:5502] *** An error occurred in MPI_Scatter
[lc2530hz:5502] *** reported by process [2594701313,0]
[lc2530hz:5502] *** on communicator MPI_COMM_WORLD
[lc2530hz:5502] *** MPI_ERR_TRUNCATE: message truncated
[lc2530hz:5502] *** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
[lc2530hz:5502] *** and potentially your MPI job)So I changed back to num_cols. The changed code is now:
#include
#include
#include
#include
#include
//#include//using namespace std;
// Sorts the input row into chunks to be scattered two all the processors.
void sortByProcess(/*vector*/double* list1, double* list2, int count);// Swaps two rows.
void swap(double** list, int count, int row1, int row2);int rank, size;
int main(int argc, char * argv[])
{
double sTime, eTime, rTime;
/*ifstream*/ FILE* inFile;
int num_rows = 3200;
int num_cols = 3200;
int cur_control = 0;
double * send_buffer = NULL;
double * recv_buffer = NULL;
double ** data = NULL;
double determinant;
char strNum_rows[20];
/*vector*/double* file_buffer=NULL;// Just get the initialization of the program going.
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);// If the input file is not given, print message and exit.
if(argc < 2)
{
/*cout <<*/ printf("No input file given.\n");// << endl;
MPI_Finalize();
return 0;
}
// If the root node (0), then open the input file and read in the
// number of rows.
if(!rank)
{
printf("After rank inside if @@@@@");
inFile = fopen(argv[1], "r");
fgets(strNum_rows, 20, inFile);
num_rows = atoi(strNum_rows);
printf("num_rows???? =%d",num_rows);
file_buffer = (double *) malloc(num_rows * sizeof(double *));//1if(file\_buffer == NULL) { printf("malloc can't allocate memory for file\_buffer"); return -1; } /\*???? inFile.open(argv\[1\]); inFile >> num\_rows; file\_buffer.resize(num\_rows);\*/
}
//printf("After rank outside @@@@@@@@#");
send_buffer = (double *)malloc(num_rows *
It looks like you may be accessing a null pointer location - that is just a guess based on the line "Failing at address: (nil)". Also, given that the first argument failure calls MPI_Finalize() before returning. You should be calling it before returning from anywhere in the main function - consistancy is important. The following is still and issue
file_buffer = (double *) malloc(num_rows * sizeof(double *));//1
should be
file_buffer = (double *) malloc(num_rows * sizeof(double));//1
because you are storing double values, not pointer values. That also applies to your other buffers, with the exception of of the data buffer. The data buffer is a pointer to an memory allocated array of pointers. Although the individual entries in the array should be allocated like so
data[i] = malloc(num_rows * sizeof(double); /* not sizeof(double *)
You may want to consider using calloc, when allocating the data array, to ensure that all the pointer variables are initialized to 0.
data = (double **)calloc(num_rows, sizeof(double*));
The main reason you want all pointer types initialized to null (a.k.a. 0), is that free function ignores null pointers. But if the pointer contains some random value, then the behavior is undefined and anything could happen - compiler dependent behavior. What may help you better understand what is happening; you could always add a print statement everywhere you allocate or modify the data. This is what you would call old school debugging - the print statements are removed before releasing the finished product. Example:
printf("data (double**)calloc(%d, sizeof(double*)", num_col);
OR
printf("data (double**)calloc(%d, %u)", num_col, sizeof(double *));
For very small data sets, doing this in the data allocation loop will also be helpfull.
for(int i=0; i < num_cols; ++i)
{
printf("data[%d] = malloc(%u)", i, num_rows * sizeof(double));
data[i] = (double*) malloc(num_rows * sizeof(double));
if (data[i] == NULL)
{
// print error
// clean up
return -2;
}
}If you run the program from the command line, then you can just pipe the output to a file for latter study. application.exe >> output.txt
INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone
-
It looks like you may be accessing a null pointer location - that is just a guess based on the line "Failing at address: (nil)". Also, given that the first argument failure calls MPI_Finalize() before returning. You should be calling it before returning from anywhere in the main function - consistancy is important. The following is still and issue
file_buffer = (double *) malloc(num_rows * sizeof(double *));//1
should be
file_buffer = (double *) malloc(num_rows * sizeof(double));//1
because you are storing double values, not pointer values. That also applies to your other buffers, with the exception of of the data buffer. The data buffer is a pointer to an memory allocated array of pointers. Although the individual entries in the array should be allocated like so
data[i] = malloc(num_rows * sizeof(double); /* not sizeof(double *)
You may want to consider using calloc, when allocating the data array, to ensure that all the pointer variables are initialized to 0.
data = (double **)calloc(num_rows, sizeof(double*));
The main reason you want all pointer types initialized to null (a.k.a. 0), is that free function ignores null pointers. But if the pointer contains some random value, then the behavior is undefined and anything could happen - compiler dependent behavior. What may help you better understand what is happening; you could always add a print statement everywhere you allocate or modify the data. This is what you would call old school debugging - the print statements are removed before releasing the finished product. Example:
printf("data (double**)calloc(%d, sizeof(double*)", num_col);
OR
printf("data (double**)calloc(%d, %u)", num_col, sizeof(double *));
For very small data sets, doing this in the data allocation loop will also be helpfull.
for(int i=0; i < num_cols; ++i)
{
printf("data[%d] = malloc(%u)", i, num_rows * sizeof(double));
data[i] = (double*) malloc(num_rows * sizeof(double));
if (data[i] == NULL)
{
// print error
// clean up
return -2;
}
}If you run the program from the command line, then you can just pipe the output to a file for latter study. application.exe >> output.txt
INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone
Hi, Thanks for your interest in my problem. God bless you. I am till getting same error messages in my redirected output file. The messages are:
[lc2530hz:03857] *** Process received signal ***
[lc2530hz:03857] Signal: Segmentation fault (11)
[lc2530hz:03857] Signal code: (128)
[lc2530hz:03857] Failing at address: (nil)
[lc2530hz:03858] *** Process received signal ***
[lc2530hz:03858] Signal: Segmentation fault (11)
[lc2530hz:03858] Signal code: (128)
[lc2530hz:03858] Failing at address: (nil)
[lc2530hz:03859] *** Process received signal ***
[lc2530hz:03859] Signal: Segmentation fault (11)
[lc2530hz:03859] Signal code: (128)
[lc2530hz:03859] Failing at address: (nil)
[lc2530hz:03857] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7f7c3323df20]
[lc2530hz:03857] [ 1] /lib/x86_64-linux-gnu/libc.so.6(fclose+0xd4)[0x7f7c3327d324]
[lc2530hz:03857] [ 2] ./a.out(+0x125b)[0x5581b8fa925b]
[lc2530hz:03857] [ 3] [lc2530hz:03858] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7fa90fa0af20]
[lc2530hz:03858] [ 1] /lib/x86_64-linux-gnu/libc.so.6(fclose+0xd4)[0x7fa90fa4a324]
[lc2530hz:03858] [ 2] ./a.out(+0x125b)[0x557a7d5c725b]
[lc2530hz:03858] [lc2530hz:03859] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7f157c799f20]
[lc2530hz:03859] [ 1] /lib/x86_64-linux-gnu/libc.so.6(fclose+0xd4)[0x7f157c7d9324]
[lc2530hz:03859] [ 2] ./a.out(+0x125b)[0x55dd2168325b]
[lc2530hz:03859] [ 3] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f7c33220b97]
[ 3] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f157c77cb97]
[lc2530hz:03859] [ 4] [lc2530hz:03857] [ 4] ./a.out(+0xc6a)[0x5581b8fa8c6a]
[lc2530hz:03857] *** End of error message ***
+0xe7)[0x7fa90f9edb97]
[lc2530hz:03858] [ 4] ./a.out(+0xc6a)[0x557a7d5c6c6a]
[lc2530hz:03858] *** End of error message ***
./a.out(+0xc6a)[0x55dd21682c6a]
[lc2530hz:03859] *** End of error message ***mpirun noticed that process rank 3 with PID 0 on node lc2530hz exited on signal 11 (Segmentation fault).
My modified code is:
#include
#include
#include
#include
#include
//#include//using namespace std;
// Sorts the input row into chunks to be scattered two all the processors.
void sortByProcess(/*vector*/double* list1, -
Hi, Thanks for your interest in my problem. God bless you. I am till getting same error messages in my redirected output file. The messages are:
[lc2530hz:03857] *** Process received signal ***
[lc2530hz:03857] Signal: Segmentation fault (11)
[lc2530hz:03857] Signal code: (128)
[lc2530hz:03857] Failing at address: (nil)
[lc2530hz:03858] *** Process received signal ***
[lc2530hz:03858] Signal: Segmentation fault (11)
[lc2530hz:03858] Signal code: (128)
[lc2530hz:03858] Failing at address: (nil)
[lc2530hz:03859] *** Process received signal ***
[lc2530hz:03859] Signal: Segmentation fault (11)
[lc2530hz:03859] Signal code: (128)
[lc2530hz:03859] Failing at address: (nil)
[lc2530hz:03857] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7f7c3323df20]
[lc2530hz:03857] [ 1] /lib/x86_64-linux-gnu/libc.so.6(fclose+0xd4)[0x7f7c3327d324]
[lc2530hz:03857] [ 2] ./a.out(+0x125b)[0x5581b8fa925b]
[lc2530hz:03857] [ 3] [lc2530hz:03858] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7fa90fa0af20]
[lc2530hz:03858] [ 1] /lib/x86_64-linux-gnu/libc.so.6(fclose+0xd4)[0x7fa90fa4a324]
[lc2530hz:03858] [ 2] ./a.out(+0x125b)[0x557a7d5c725b]
[lc2530hz:03858] [lc2530hz:03859] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7f157c799f20]
[lc2530hz:03859] [ 1] /lib/x86_64-linux-gnu/libc.so.6(fclose+0xd4)[0x7f157c7d9324]
[lc2530hz:03859] [ 2] ./a.out(+0x125b)[0x55dd2168325b]
[lc2530hz:03859] [ 3] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f7c33220b97]
[ 3] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f157c77cb97]
[lc2530hz:03859] [ 4] [lc2530hz:03857] [ 4] ./a.out(+0xc6a)[0x5581b8fa8c6a]
[lc2530hz:03857] *** End of error message ***
+0xe7)[0x7fa90f9edb97]
[lc2530hz:03858] [ 4] ./a.out(+0xc6a)[0x557a7d5c6c6a]
[lc2530hz:03858] *** End of error message ***
./a.out(+0xc6a)[0x55dd21682c6a]
[lc2530hz:03859] *** End of error message ***mpirun noticed that process rank 3 with PID 0 on node lc2530hz exited on signal 11 (Segmentation fault).
My modified code is:
#include
#include
#include
#include
#include
//#include//using namespace std;
// Sorts the input row into chunks to be scattered two all the processors.
void sortByProcess(/*vector*/double* list1, -
The code is not very well written to begin with, so please do not write something like this for a real world application. Be sure to check if each malloc succeeded before using it or you may end up crashing your application - safety first. I recommend breaking out the pencil an paper and working through the code. Drawing out the arrays (both 1D and 2D) would help in analyzing the code - along with the debugger. It is a bit confusing when the traditional row/column layout is backwards: Normal: buffer[row][column] In this code: buffer[column][row] <<<< this is backwards. file_buffer is not initialized which could lead to an issue if it does not actually get allocate it.
file_buffer = NULL;
file_buffer size allocation is not correct - it should be using sizeof(double) not sizeof(int). This is important, because you are copying double values into the buffer. Depending on the compiler, the size of a double is probably 2 x sizeof(int). This could cause massive memory corruption. When memory buffers are allocated one after another, then there is a strong possibility that they follow each other in memory - like so [file_buffer][send_buffer][data]. This means that the loop filling in the file_buffer would be over writing the send_buffer, due to the fact that the file_buffer is not large enough to hold an array of double values. A little more information is required here about the most like memory lay out. Buffer allocation layout: [[allocation size][allocated memory]] When malloc allocates the memory, it reserves the start of the allocated block to hold the size of memory block being allocated and returns a pointer to the memory location following the allocation size data. Therefore, given the above, your code probably allocated the file_buffer and overwrote the allocation size of the send_buffer, leading to the invalid size error. If the file_buffer is allocated, then ensure that it is freed. You need to free each data[i] in a loop and then free the data buffer itself. Since I do not see anything else filling in the send_buffer or recv_buffer, I am going to assume that MPI_Scatter() is filling those buffers. If that is the case, then keep in mind that the number of row does not equal the number of columns unless the size value is equal to 1.
num_cols = num_rows / size;
That implies that the following call is incorrect:
MPI_Scatter(send_buffer, num_cols, MPI_DOUBLE, recv_buffer, num_cols, MPI_DOUB
-
Hi friends, This problem got solved. Thanks for your efforts. God bless you guys. Solution is:
if (!rank)
fclose(inFile);Zulfi.
Good to here. But you need to fix this
recv_buffer = (double *) malloc(num_cols * sizeof(double *));
it should be
recv_buffer = (double *) malloc(num_cols * sizeof(double));
INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone
-
Good to here. But you need to fix this
recv_buffer = (double *) malloc(num_cols * sizeof(double *));
it should be
recv_buffer = (double *) malloc(num_cols * sizeof(double));
INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone
Hi, Thanks for your good intentions and for correcting my code through defensive programming techniques. I was missing you. I am not getting correct results. C++ code is giving correct results. I would take up this problem again but I have to complete the assigned task first. Wish you all the best. Zulfi.