Re: How to send dynamic vector?
On Oct 21, 1:06 pm, dopiotr <nos...@nospam.invalid> wrote:
snip
> > 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
snip
> Working wersion:
This is not a working version. It invokes undefined behavior and you
state it produces incorrect results.
> #include "mpi.h"
> #include<stdio.h>
> #include<stdlib.h>
> uint n_sigm;
> float *suma_sigm;
>
> void main(int c, char *v[])
main should return int.
> {
> 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);
Do not cast the return from malloc. It can never and the only
effect would be to suppress a diagnostic you really want to see if it
applies to your code.
> s_tmp=(float *)malloc(sizeof(float)*n_sigm);//difference
You should always check that malloc succeeds.
>
> 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;
> }
At this point, the elements of suma_sigm or s_tmp (but not both) have
been assigned values. The elements of the other array are
indeterminate (uninitialized).
> MPI_Reduce(suma_sigm,s_tmp ,n_sigm,MPI_FLOAT,MPI_SUM,
> 0,MPI_COMM_WORLD);
Since you don't pass rank to this function, how is it supposed to
determine which of the arrays has valid values and which doesn't. Any
attempt by the function to evaluate an indeterminate element will
invoke undefined behavior.
> 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();
>
> }
>
> 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;
At this point, s_tmp is still indeterminate; it does not yet point to
any memory you can access. s_tmp[j] does not exist. This invokes
undefined behavior.
> }
> s_tmp=(float *)malloc(sizeof(float)*n_sigm);//difference
And if the previous undefined behavior were not sufficient, this would
replace the address that s_tmp was pointing to with the address of the
newly allocated memory making all the elements of the array
indeterminate again.
> 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).
By definition, a working version would produce correct results. What
is the vlaue of rank when you see these incorrect values? What is the
function MPI_Reduce supposed to do with the two arrays you pass?
|