C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-26-2009, 05:15 AM   #1
Registered User
 
Join Date: Mar 2009
Posts: 3
MPI in C

Hi all

I am using MPI and c programming. i want to collect strings from processors to root processor
now i have writen the code as below but does not display the strings. Can anyone help me with this?
The code is as below:
Code:
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main(int argc, char *argv[]){
int numProcs;
int myRank, a, *data, i;
char **DATA;
char *b;
//initialize MPI
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD,&myRank);
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
// do some computation and MPI communications
a = 1000+myRank;
 b = "sdfsd";
if (myRank == 1){
data = (int *) malloc(numProcs*sizeof(int));
DATA = (char **) calloc(numProcs, sizeof(char **));
for (i=0;i<numProcs;i++){
data[i] = 0;
DATA[i] = "helloP";
 }
printf("\n contents of data array before gather:\n");
for (i=0;i<numProcs;i++)
printf("%s\n", DATA[i]);

}

MPI_Gather(&b, 5, MPI_CHAR, DATA, 5,  MPI_CHAR,1, MPI_COMM_WORLD);

printf("here are contents %s\n",DATA[1]); 
if (myRank == 1){
printf("\n contents of data array after gather:\n");
for (i=0;i<numProcs;i++){
	printf("%s\n", DATA[i]);
 /*printf("2. DATA[%d] = %s \n",i, DATA[i]);*/}}

//shutdown MPI
MPI_Finalize();
return 0;
}
ltee is offline   Reply With Quote
Old 03-26-2009, 05:18 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Don't see anything DIRECTLY wrong. What are you actually receiving?

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 03-26-2009, 05:31 AM   #3
Registered User
 
Join Date: Mar 2009
Posts: 3
Thanks matsp for quick response. Actually bellow is the message i receive:

rank 2 in job 8 cslab117-002_50788 caused collective abort of all ranks
exit status of rank 2: killed by signal 11
rank 0 in job 8 cslab117-002_50788 caused collective abort of all ranks
exit status of rank 0: killed by signal 11
ltee is offline   Reply With Quote
Old 03-26-2009, 05:49 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
signal 11 is a "seg-fault", which means your app tried to write to memory that doesn't exist.

I suspect it is because your DATA is actually pointing to read-only memory. Maybe you would be better off allocating some memory for DATA[i] instead of setting it to "HELLO". I don't KNOW that this is the problem, but it seems reasonable that your processes can not write to read-only memory, and that would cause a Seg-fault signal.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 03-26-2009, 06:05 AM   #5
Registered User
 
Join Date: Mar 2009
Posts: 3
well, when i remove the line
Code:
 DATA[i] = "helloP";
i still get the message:
rank 1 in job 11 cslab117-002_50788 caused collective abort of all ranks
exit status of rank 1: killed by signal 11

Clear allocation of memory is not a problem here. so im wondering exactly what i need to change. If you look at line:
Code:
 MPI_Gather(&b, 5, MPI_CHAR, DATA, 5,  MPI_CHAR,1, MPI_COMM_WORLD);
, can you pick something that might be causing this error, because i suspect that the error is there
ltee is offline   Reply With Quote
Old 03-26-2009, 06:10 AM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
So where is DATA[i] pointing now? Just removing the assignment IS NOT ENOUGH. You need each process to have some space to write the data into.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Reply

Tags
/code, code

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Communication using MPI Cell Linux Programming 9 08-13-2009 02:28 AM
MPI, which way will be best? maverick_starst C++ Programming 5 06-30-2009 02:17 PM
Sorting whit MPI isato C Programming 0 03-03-2009 10:38 AM
Malloc and MPI moddinati C Programming 17 03-07-2008 07:55 PM
MPI programming kris.c Tech Board 1 12-08-2006 12:25 PM


All times are GMT -6. The time now is 09:11 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22