我试图将一个 1-dim n*n 数组分散到 p 个处理器,然后使用收集来获取数据。但问题是它在我运行程序时显示垃圾输出。我正在使用一个 n*n 的数组 A 和一个大小为 n 的数组 b 。
// num_rows is the number of rows each process will be receiving,base is the base index and last is the last index .
// curr_index_proce is the index of the processor which contains the index i of Array A.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <mpi.h>
#define NUM_EQN 10
#define NUM_ROWS(i,n,p) ((((i)+1)*(n)/(p)) - ((i)*(n)/(p)))
#define BASE(i,n,p) (((i)*(n)/(p)))
#define LAST(i,n,p) ((((i)+1)*(n)/(p)) - 1)
#define CUR_INDEX_PROC(i,n,p) ( ((p) * ( (i) + 1 ) - 1 )/(n))
#define MIN(a,n) ((a) > (b) ? (a) : (b))
void gather(float *A,float *b,int n);
void print_array(float *A,float *b,int n){
int i,j;
for ( i = 0 ; i < n;i++){
// printf("i = %d ",i);
for(j = 0 ; j < n;j++){
printf("%6.2f " ,A[i*n+j]);
}
printf("\n");
//printf(" : %6.2f\n",b[i]);
}
printf("\n\n\n");
}
int SIZE_A(int n,int p ){
if ( n%p == 0 )
return n*n/p;
else
return n*(n/p+1);
}
int SIZE_B(int n,int p){
if ( n%p == 0 )
return n/p;
else
return n/p + 1;
}
int main(int argc,char **argv){
MPI_Init(&argc,&argv);
int size,rank;
MPI_Comm_rank( MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
double time_start, time_finish;
float *A = malloc(sizeof(float)*NUM_EQN*NUM_EQN);
float *b = malloc(sizeof(float)*NUM_EQN);
float *X;
int i,j;
FILE *fp = fopen("A","r");
if (fp == NULL ){
printf("Error %s Not Found\n",argv[1]);
exit(0);
MPI_Finalize();
}
for ( i = 0 ; i < NUM_EQN;i++)
for(j = 0 ; j < NUM_EQN;j++)
fscanf(fp,"%f",&A[i*NUM_EQN+j]);
FILE *fp2 = fopen("b","r");
if (fp2 == NULL ){
printf("Error %s Not Found\n",argv[2]);
exit(0);
}
for ( i = 0 ; i < NUM_EQN;i++)
fscanf(fp2,"%f",&b[i]);
time_start = - MPI_Wtime();
gather(A,b,NUM_EQN);
time_finish = MPI_Wtime();
char name[100];
int length;
MPI_Finalize();
}
void gather(float *A,float *b,int n){
int pivot,i,col;
int row, j;
int size,rank;
MPI_Comm_rank( MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
float global_max;
float *A_recv,*b_recv;
A_recv = malloc(sizeof(float)*(SIZE_A(n,size)));
b_recv = malloc(sizeof(float)*(SIZE_B(n,size)));
printf("%d %d \n",SIZE_A(n,size),SIZE_B(n,size));
if ( rank == 0 ){
print_array(A,b,n);
}
int send_count[size];
int disp[size];
int send_count_b[size];
int disp_b[size];
for ( i = 0 ; i < size ; i++ ){
send_count[i] = 0 ;
send_count_b[i] = 0;
}
for ( i = 0 ; i < size ; i++ ){
send_count[i] = n*NUM_ROWS(i,n,size);
if ( i == 0 )
disp[i] = 0;
else
disp[i] = disp[i-1] + send_count[i-1];
send_count_b[i] = NUM_ROWS(i,n,size);
if ( i == 0 )
disp_b[i] = 0 ;
else
disp_b[i] = disp_b[i-1] + send_count_b[i-1];
}
MPI_Scatterv(A, send_count, disp,MPI_FLOAT, A_recv,SIZE_A(n,size),MPI_FLOAT,0, MPI_COMM_WORLD);
MPI_Scatterv(b, send_count_b, disp_b,MPI_FLOAT, b_recv,send_count_b[rank], MPI_FLOAT, 0, MPI_COMM_WORLD);
for ( i = 0 ; i < send_count[rank]; i++ ){
for ( j = 0 ; j < n ; j++)
printf("%3.2f : ",A_recv[i*n+j]);
printf("\n\n");
}
MPI_Gatherv(A_recv,SIZE_A(n,size),MPI_FLOAT,A,send_count,disp,MPI_FLOAT,0,MPI_COMM_WORLD);
MPI_Gatherv(b_recv,send_count_b[rank],MPI_FLOAT,b,send_count_b,disp_b,MPI_FLOAT,0,MPI_COMM_WORLD);
}
请帮帮我。