Hi :
I got a problem of gathering dynamic arrays in MPI. I hope to process the array just like normal array ewi[i][j] instead of ewi[i*size+j], but it doesn't work.
The code looks like:
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if ( rank == 0)
{
printf("the number of processors is %d\n",size);
printf("please input a integer:=");
scanf("%d",&num);
}
double ** ewi = new double *[size];
double *jj = new double[num]
for ( i = 0 ; i< num;i++)
{ jj[i] = i*(rank+1);
printf("jj[%d]=%f\n",i,jj[i]);
}
for ( i = 0 ; i< size;i++)
{
ewi[i] = new double[num];
}
MPI_Gather(jj,num,MPI_DOUBLE,ewi[0],num,MPI_DOUBLE,0,MPI_COMM_WORLD);
if( rank == 0)
{
for ( i = 0; i<size; i++)
for( j = 0; j< num;j++)
printf("ewi[%d][%d] = %f \n",i,j,ewi[i][j]);
}
The output:
the number of processors is 2
please input a integer:=4
jj[0]=0.000000
jj[1]=1.000000
jj[2]=2.000000
jj[3]=3.000000
ewi[0][0] = 0.000000
ewi[0][1] = 1.000000
ewi[0][2] = 2.000000
ewi[0][3] = 3.000000
ewi[1][0] = 2.000000
ewi[1][1] = 4.000000
ewi[1][2] = 6.000000
ewi[1][3] = 0.000000
jj[0]=0.000000
jj[1]=2.000000
jj[2]=4.000000
jj[3]=6.000000
another input:
the number of processors is 2
please input a integer:=3
jj[0]=0.000000
jj[1]=1.000000
jj[2]=2.000000
ewi[0][0] = 0.000000
ewi[0][1] = 1.000000
ewi[0][2] = 2.000000
ewi[1][0] = 2.000000
ewi[1][1] = 4.000000
ewi[1][2] = 0.000000
jj[0]=0.000000
jj[1]=2.000000
jj[2]=4.000000
Thanks for your help.
---------------------------------
Do You Yahoo!?
150ÍòÇúMP3·è¿ñËÑ£¬´øÄú´³ÈëÒôÀÖµîÌÃ
ÃÀÅ®Ã÷ÐÇÓ¦Óо¡ÓУ¬ËѱéÃÀͼ¡¢ÑÞͼºÍ¿áͼ
1G¾ÍÊÇ1000Õ×£¬ÑÅ»¢µçÓÊ×ÔÖúÀ©ÈÝ£¡
|