Re: How to send dynamic vector?
dopiotr wrote:
> On 21 Oct 2007 at 19:56, santosh wrote:
>> dopiotr wrote:
>>
>>> Hello Group,
>>>
>>> int Msg[size];
>>> MPI_Send(Msg, size, MPI_INT, DEST, TAG, MPI_COMM_WORLD);
>>> this is working fine.
>>>
>>> But how to send Msg declared like this:
>>> int *Msg=(int*)malloc(sizeof(int)*size);
>>>
>>> It gives me
>>> p4_error: interrupt SIGSEGV: 11
>>> not_send: could not write to fd=4, errno = 32
>>
>> Without further code I can't really tell what your problem is. It may
>> not be a C problem but an incorrect use of MPI functions. You might
>> do better to ask in a forum dedicated to your MPI implementation.
>>
>><http://en.wikipedia.org/wiki/Message_Passing_Interface>
>><http://www.mpi-forum.org/docs/>
>><http://www.open-mpi.org/>
>><http://www.personal.leeds.ac.uk/~bgy1mm/MPITutorial/MPIHome.html>
>>
>
> Working wersion:
> #include "mpi.h"
> #include<stdio.h>
> #include<stdlib.h>
> uint n_sigm;
> float *suma_sigm;
>
> void main(int c, char *v[])
Use int main
> {
> int size,rank;
> int j;
> float *s_tmp;
> MPI_Init(&c,&v);
> MPI_Comm_size(MPI_COMM_WORLD,&size);
> MPI_Comm_rank(MPI_COMM_WORLD,&rank);
> n_sigm=1000;
>
> suma_sigm=(float *)malloc(sizeof(float)*n_sigm);
> s_tmp=(float *)malloc(sizeof(float)*n_sigm);//difference
In C the cast of malloc's return value is not needed. I'd write your
statements as:
suma_sigm = malloc(sizeof *suma_sigm * n_sigm);
s_tmp = malloc(sizeof *s_tmp * n_sigm);
Also be sure to check that malloc actually succeeded.
> if(rank != 0)
> {
> for(j=0;j<n_sigm;j++)
> suma_sigm[j]=rank*1000+j;
> }
> else
> {
> for(j=0;j<n_sigm;j++)
> s_tmp[j]=0;
> }
> MPI_Reduce(suma_sigm,s_tmp ,n_sigm,MPI_FLOAT,MPI_SUM,
> 0,MPI_COMM_WORLD);
> if( rank == 0 )
> for(j=0;j<n_sigm;j++)
> printf("%i %f\n",j,s_tmp[j]);
> free(suma_sigm);
> free(s_tmp);
> MPI_Finalize();
Return a value here.
> }
>
> Why it is not working:
> #include "mpi.h"
> #include<stdio.h>
> #include<stdlib.h>
> uint n_sigm;
> float *suma_sigm;
> int main(int c, char *v[])
> {
> int size,rank;
> int j;
> float *s_tmp;
> MPI_Init(&c,&v);
> MPI_Comm_size(MPI_COMM_WORLD,&size);
> MPI_Comm_rank(MPI_COMM_WORLD,&rank);
> n_sigm=1000;
>
> suma_sigm=(float *)malloc(sizeof(float)*n_sigm);
>
> if(rank != 0)
> {
> for(j=0;j<n_sigm;j++)
> suma_sigm[j]=rank*1000+j;
> }
> else
> {
> for(j=0;j<n_sigm;j++)
> s_tmp[j]=0;
Well, here s_tmp is not yet pointing to any valid memory. You're writing
to memory you don't own or should not be writing to. Initialise s_tmp
to a valid object or an array of objects before using it.
> }
> s_tmp=(float *)malloc(sizeof(float)*n_sigm);//difference
/Now/ you're doing it, but it's too late, unless rank was not zero.
> MPI_Reduce(suma_sigm,s_tmp ,n_sigm,MPI_FLOAT,MPI_SUM,
> 0,MPI_COMM_WORLD);
> if( rank == 0 )
> for(j=0;j<n_sigm;j++)
> printf("%i %f\n",j,s_tmp[j]);
> free(suma_sigm);
> free(s_tmp);
> MPI_Finalize();
> }
>
> And why value of s_tmp[0] & s_tmp[1] is incorrect (in working
> version).
How should I know? What value did you expect? The obvious bug waiting to
happen in your second version is the use of the pointer s_tmp without
making it point to anything valid beforehand.
|