|
The code can correctly display the bmp image that comes with the IDE, but can't display the image I added.
The IDE is visual studio 2012 and I use mfc to display bmp pictures.
This is the first piece of code in OnInitDialog():
static CImageList imgList;
imgList.Create(48,48,ILC_COLOR32,1,1);
imgList.Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));
m_List.SetImageList(&imgList,LVSIL_NORMAL);
m_List.InsertItem(0,_T("1th Test item"));
and the result of debug ——able to display the picture
while the second piece of code in OnInitDialog():
static CImageList imgList;
imgList.Create(40,48,ILC_COLOR32,1,1);
imgList.Add(AfxGetApp()->LoadIcon(IDB_BITMAP1));
m_List.SetImageList(&imgList,LVSIL_NORMAL);
m_List.InsertItem(0,_T("1th Test item"));
and the result of debug ——unable to display the picture,only blanks are displayed.
I am really sorry that I am a new man here and I do not know how to stick debug screenshots here.
|
|
|
|
|
tianzhili4399 wrote: static CImageList imgList;
imgList.Create(40,48,ILC_COLOR32,1,1);
imgList.Add(AfxGetApp()->LoadIcon(IDB_BITMAP1));
m_List.SetImageList(&imgList,LVSIL_NORMAL);
m_List.InsertItem(0,_T("1th Test item"));
Is the image IDB_BITMAP1 an icon or a bitmap? If it is a bitmap then you have to use CBitmap::LoadBitmap method (or LoadBitmap API function) instead.
BTW, what does the
AfxGetApp()->LoadIcon(IDB_BITMAP1) return?
|
|
|
|
|
I really appreciate you that I finally achieve my goal!Thank you very much!
|
|
|
|
|
You are welcome!
|
|
|
|
|
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 <stdio.h>
#include <math.h>
#include <mpi.h>
#include <time.h>
#include <stdlib.h>
//#include <vector.h>
//using namespace std;
// Sorts the input row into chunks to be scattered two all the processors.
void sortByProcess(/*vector<double>*/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>*/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 #####@");
data = (double **) malloc(num_cols * sizeof(double *));
/*???? data #####@");*/
for(int i = 0; i < num_cols; i++)
data[i] = (double *) malloc(num_rows * sizeof(double));
/* ??? data[i] = new double[num_rows]; */
for(int i = 0; i < num_cols; i++)
{
for(int j = 0; j < num_rows; j++)
data[i][j] = 0;
}
//printf("Before recv_buffer $$$$$$$@");
recv_buffer = (double *) malloc(num_cols * sizeof(double));
/*???? recv_buffer = new double[num_cols];*/
// Scatter the data.
for(int i = 0; i < num_rows; i++)
{
if(!rank)
{
for(int j = 0; j < num_rows; j++){
fgets(strNum_rows, 20, inFile);
file_buffer[j] = atof(strNum_rows);
}
/*????? inFile >> file_buffer[j];*/
sortByProcess(file_buffer, send_buffer, num_rows);
}
//printf("After sortByProcess ^^^^^^@");
// Scatters the data so that each process gets the next value for their columns.
MPI_Scatter(send_buffer, num_cols, MPI_DOUBLE, recv_buffer, num_cols, MPI_DOUBLE, 0, MPI_COMM_WORLD );
for(int j = 0; j < num_cols; j++)
{
data[j][i] = recv_buffer[j];
}
}
//printf("After Scatter ^^^^^^@");
/*delete []*/
free(recv_buffer);
/*delete []*/
free(send_buffer);
// Begin timing.
MPI_Barrier(MPI_COMM_WORLD);
sTime = MPI_Wtime();
printf("After Barrier ^^^^^^@");
MPI_Finalize();
return 0;
The error which I am getting is:
$ mpicc gaussian.c
$ mpirun -np 4 ./a.out matrix.3200.txt
free(): invalid size
[lc2530hz:08268] *** Process received signal ***
[lc2530hz:08268] Signal: Aborted (6)
[lc2530hz:08268] Signal code: (-6)
[lc2530hz:08268] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7f107b56af20]
[lc2530hz:08268] [ 1] /lib/x86_64-linux-gnu/libc.so.6(gsignal+0xc7)[0x7f107b56ae97]
[lc2530hz:08268] [ 2] /lib/x86_64-linux-gnu/libc.so.6(abort+0x141)[0x7f107b56c801]
[lc2530hz:08268] [ 3] /lib/x86_64-linux-gnu/libc.so.6(+0x89897)[0x7f107b5b5897]
[lc2530hz:08268] [ 4] /lib/x86_64-linux-gnu/libc.so.6(+0x9090a)[0x7f107b5bc90a]
[lc2530hz:08268] [ 5] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4dc)[0x7f107b5c3e2c]
[lc2530hz:08268] [ 6] ./a.out(+0x1056)[0x564381392056]
[lc2530hz:08268] [ 7] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f107b54db97]
[lc2530hz:08268] [ 8] ./a.out(+0xbda)[0x564381391bda]
[lc2530hz:08268] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 0 on node lc2530hz exited on signal 6 (Aborted).
--------------------------------------------------------------------------
$
Somebody please guide me how to remove this error.
Zulfi.
|
|
|
|
|
Something, somewhere is corrupting one of your malloc() blocks. First off, I count 5 calls to malloc() , but only 2 calls to free() 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 to free() and a hex stack dump, we don't know which free() 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 <file-name> 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 is
mpirun -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 file vg_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.
|
|
|
|
|
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_DOUBLE, 0, MPI_COMM_WORLD); Should probably written as follows:
MPI_Scatter(send_buffer, num_rows, MPI_DOUBLE, recv_buffer, num_cols, MPI_DOUBLE, 0, MPI_COMM_WORLD);
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 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 <stdio.h>
#include <math.h>
#include <mpi.h>
#include <time.h>
#include <stdlib.h>
//#include <vector.h>
//using namespace std;
// Sorts the input row into chunks to be scattered two all the processors.
void sortByProcess(/*vector<double>*/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>*/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 *));//1
if(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 * sizeof(double *));
if(send_buffer == NULL) {
printf("malloc can't allocate memory for send_buffer");
return -1;
}
/*?????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 #####@");
data = (double **) malloc(num_cols * sizeof(double *));
if(data == NULL) {
printf("malloc can't allocate memory for data");
return -1;
}
/*???? data #####@");*/
for(int i = 0; i < num_cols; i++){
data[i] = (double *) malloc(num_rows * sizeof(double *));
if(data[i] == NULL) {
printf("malloc can't allocate memory for data[%d]", i);
return -1;
}
}
/* ??? data[i] = new double[num_rows]; */
for(int i = 0; i < num_cols; i++)
{
for(int j = 0; j < num_rows; j++)
data[i][j] = 0;
}
//printf("Before recv_buffer $$$$$$$@");
recv_buffer = (double *) malloc(num_cols * sizeof(double *));
if(recv_buffer == NULL) {
printf("malloc can't allocate memory for recv_buffer");
return -1;
}
/*???? recv_buffer = new double[num_cols];*/
// Scatter the data.
for(int i = 0; i < num_rows; i++)
{
if(!rank)
{
for(int j = 0; j < num_rows; j++){
fgets(strNum_rows, 20, inFile);
file_buffer[j] = atof(strNum_rows);
}
/*????? inFile >> file_buffer[j];*/
sortByProcess(file_buffer, send_buffer, num_rows);
}
//printf("After sortByProcess ^^^^^^@");
// Scatters the data so that each process gets the next value for their columns.
MPI_Scatter(send_buffer, num_cols/* NOTE num_rows gives SCATTER ERROR &deviates from original code */, MPI_DOUBLE, recv_buffer, num_cols, MPI_DOUBLE, 0, MPI_COMM_WORLD );
for(int j = 0; j < num_cols; j++)
{
data[j][i] = recv_buffer[j];
}
}
//printf("After Scatter ^^^^^^@");
fclose(inFile);
/*delete []*/ free(recv_buffer);
/*delete []*/ free(send_buffer);
//free(file_buffer);
/*delete []*/ //free(send_buffer);
for(int i = 0; i < num_cols; i++)
/*delete []*/ free( data[i]);
/*delete []*/ free(data);
// Begin timing.
MPI_Barrier(MPI_COMM_WORLD);
sTime = MPI_Wtime();
printf("After Barrier ^^^^^^@");
MPI_Finalize();
return 0;
And the errors which I am gettingare:
$ mpicc gaussian.c
$ mpirun -np 4 ./a.out matrix.3200.txt
[lc2530hz:05635] *** Process received signal ***
[lc2530hz:05635] Signal: Segmentation fault (11)
[lc2530hz:05635] Signal code: (128)
[lc2530hz:05635] Failing at address: (nil)
[lc2530hz:05636] *** Process received signal ***
[lc2530hz:05636] Signal: Segmentation fault (11)
[lc2530hz:05636] Signal code: (128)
[lc2530hz:05636] Failing at address: (nil)
[lc2530hz:05637] *** Process received signal ***
[lc2530hz:05637] Signal: Segmentation fault (11)
[lc2530hz:05637] Signal code: (128)
[lc2530hz:05637] Failing at address: (nil)
[lc2530hz:05636] [ 0] [lc2530hz:05637] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7f7ac9b11f20]
[lc2530hz:05637] [ 1] [lc2530hz:05635] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7fd882900f20]
[lc2530hz:05635] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7fdeb9110f20]
[lc2530hz:05636] [ 1] [ 1] /lib/x86_64-linux-gnu/libc.so.6(fclose+0xd4)[0x7fdeb9150324]
[lc2530hz:05636] [ 2] ./a.out(+0x116c)[0x/lib/x86_64-linux-gnu/libc.so.6(fclose+0xd4)[0x7f7ac9b51324]
[lc2530hz:05637] [ 2] ./a.out(+0x116c)[0x5651bd24c16c]
[lc2530hz:05637] [ 3] /lib/x86_64-linux-gnu/libc.so.6(fclose+0xd4)[0x7fd882940324]
[lc2530hz:05635] [ 2] 56176a25a16c]
[lc2530hz:05636] [ 3] /lib/x86_64-linux-gnu/libc.so.6(./a.out(+0x116c)[0x55ee0e31b16c]
[lc2530hz:05635] [ 3] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f7ac9af4b97]
[lc2530hz:05637] __libc_start_main+0xe7)[0x7fdeb90f3b97]
[lc2530hz:05636] [ 4] ./a.out(+0xc1a)[0x56176a259c1a[ 4] ./a.out(+0xc1a)[0x5651bd24bc1a]
[lc2530hz:05637] *** End of error message ***
e7)[0x7fd8828e3b97]
[lc2530hz:05635] [ 4] ./a.out(+0xc1a]
[lc2530hz:05636] *** End of error message ***
)[0x55ee0e31ac1a]
[lc2530hz:05635] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 1 with PID 0 on node lc2530hz exited on signal 11 (Segmentation fault).
-----------------------------------------------
Somebody please guide me.
Zulfi.
|
|
|
|
|
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 *)); should be
file_buffer = (double *) malloc(num_rows * sizeof(double)); 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); 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)
{
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 <stdio.h>
#include <math.h>
#include <mpi.h>
#include <time.h>
#include <stdlib.h>
//#include <vector.h>
//using namespace std;
// Sorts the input row into chunks to be scattered two all the processors.
void sortByProcess(/*vector<double>*/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>*/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 ));//1
if(file_buffer == NULL) {
printf("malloc can't allocate memory for file_buffer");
MPI_Finalize();
return -1;
}
/*???? inFile.open(argv[1]);
inFile >> num_rows;
file_buffer.resize(num_rows);*/
}
//printf("After rank outside @@@@@@@@#");
send_buffer = (double *)malloc(num_rows * sizeof(double ));
if(send_buffer == NULL) {
printf("malloc can't allocate memory for send_buffer");
MPI_Finalize();
return -1;
}
/*?????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 #####@");
//data = (double **) malloc(num_cols * sizeof(double *));
//calloc initializes the allocated memory to zero
data = (double **)calloc(num_rows, sizeof(double*));
if(data == NULL) {
printf("malloc can't allocate memory for data");
MPI_Finalize();
return -1;
}
/*???? data #####@");*/
for(int i = 0; i < num_cols; i++){
data[i] = (double *) malloc(num_rows * sizeof(double ));
if(data[i] == NULL) {
printf("data[%d] = malloc(%lu)", i, num_rows * sizeof(double));
printf("malloc can't allocate memory for data[%d]", i);
//
if(i>0) {
for(int j = i-1; j>=0; j--)
free( data[j]);
free(data);
MPI_Finalize();
return -2;
}
}
}
/* ??? data[i] = new double[num_rows]; */
for(int i = 0; i < num_cols; i++)
{
for(int j = 0; j < num_rows; j++)
data[i][j] = 0;
}
//printf("Before recv_buffer $$$$$$$@");
recv_buffer = (double *) malloc(num_cols * sizeof(double *));
if(recv_buffer == NULL) {
printf("malloc can't allocate memory for recv_buffer");
MPI_Finalize();
return -1;
}
/*???? recv_buffer = new double[num_cols];*/
// Scatter the data.
for(int i = 0; i < num_rows; i++)
{
if(!rank)
{
for(int j = 0; j < num_rows; j++){
fgets(strNum_rows, 20, inFile);
file_buffer[j] = atof(strNum_rows);
}
/*????? inFile >> file_buffer[j];*/
sortByProcess(file_buffer, send_buffer, num_rows);
}
//printf("After sortByProcess ^^^^^^@");
// Scatters the data so that each process gets the next value for their columns.
MPI_Scatter(send_buffer, num_cols/* NOTE num_rows gives SCATTER ERROR &deviates from original code */, MPI_DOUBLE, recv_buffer, num_cols, MPI_DOUBLE, 0, MPI_COMM_WORLD );
for(int j = 0; j < num_cols; j++)
{
data[j][i] = recv_buffer[j];
}
}
//printf("After Scatter ^^^^^^@");
fclose(inFile);
/*delete []*/ free(recv_buffer);
/*delete []*/ free(send_buffer);
//free(file_buffer);
/*delete []*/ //free(send_buffer);
for(int i = 0; i < num_cols; i++)
/*delete []*/ free( data[i]);
/*delete []*/ free(data);
// Begin timing.
MPI_Barrier(MPI_COMM_WORLD);
sTime = MPI_Wtime();
printf("After Barrier ^^^^^^@");
MPI_Finalize();
return 0;
My compilation and execution statements are:
$mpicc gaussian.c
$mpirun -np 4 ./a.out matrix.3200.txt>output
Some body please guide me.
Zulfi.
|
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
|
Hi,
I have following C++ code:
double ** data = NULL;
data = new double*[num_cols];for(int i = 0; i < num_cols; i++)
data[i] = new double[num_rows];
I have converted them into following C-language code:
double ** data = NULL;
data = (double *) malloc(num_cols * sizeof(double));
for(int i = 0; i < num_cols; i++)
data[i] = (double *) malloc(num_rows * sizeof(double));
I am getting following error message with gcc:
Quote: In function ‘main’:
gaussian.c:73:8: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
data = (double *) malloc(num_cols * sizeof(double));
Some body please guide me.
Zulfi.
|
|
|
|
|
zak100 wrote:
double ** data = NULL;
data = (double *) malloc(num_cols * sizeof(double));
for(int i = 0; i < num_cols; i++)
data[i] = (double *) malloc(num_rows * sizeof(double));
Shouldn't it be
data = (double *) malloc(num_cols * sizeof(double*));
|
|
|
|
|
It should ... it's a standard dynamic 2D array of double in C.
The code as written is stupid for C++ it's clearly a matrix and there are a dozen better ways to do that.
You can also just flatten it in C to a linear 1D allocation if you are happy to manually do the indexing.
In vino veritas
modified 25-Apr-19 8:24am.
|
|
|
|
|
Victor Nijegorodov wrote: Shouldn't it be
data = (double *) malloc(num_cols * sizeof(double*));
I think that should be
data = (double **)malloc(num_cols * sizeof(double *));
The warning he is getting is about an incompatible pointer assignment. For myself I'd write that as
data = (double **)malloc(num_cols * sizeof *data); Since C doesn't require malloc() to be cast, you could omit the cast to (double **) when calling malloc() .
|
|
|
|
|
Looking at this, and your other post prompts the obvious question: why are you trying to convert working code from C++ to C? Since all modern compilers support C++ it would be better to stick with what works.
|
|
|
|
|
That is a rash statement in the embedded space there is rarely a C++ compiler.
Then the second kicker few in the industry would accept C++ code because of the notorious problems with memory on a confined system.
Definitely not a problem on the PC market but you can't make that sweeping statement.
In vino veritas
|
|
|
|
|
|
Thanks. I want to do some work instead of using the cooked code. Also it would provide me two versions so I can test both and find which one is better.
Zulfi.
|
|
|
|
|
I've yet to work on an embedded project where there wasn't a C++ computer. (Even in the late 90s, I was using C++ in the embedded space. Yes, it was mostly procedural C++, but it was still C++.)
|
|
|
|
|
|
leon de boer wrote: Lets list the ones I know that don't have a c++ compiler
Whereupon only one doesn't have a C++ compiler. 
|
|
|
|
|
Hi,
I am still getting the same message:
Quote: $ mpicc gaussian.c
gaussian.c: In function ‘main’:
gaussian.c:73:8: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
data = (double *) malloc(num_cols * sizeof(double *));
^
$
I forget my C++ so I want to do this conversion.
Some body please guide me.
Zulfi.
|
|
|
|
|