2013-10-19 46 views
0

我試圖從處理器0向不同的處理器發送多列「B」矩陣。我試圖發送使用MPI_Send但它不工作。有人可以幫我嗎?使用MPI_Send發送多列矩陣

例如:矩陣B的大小是7. 這樣,它應該是分佈式的。

處理器0:3個欄

處理器1:2個欄

處理器2:2欄

#include <stdlib.h> 
#include <mpi.h> 
#include <stdio.h> 
#define ERR_BADORDER 255 
#define TAG_INIT  31337 
#define TAG_RESULT  42 
#define DISP_MAXORDER 12 

int mm(double *A, double *B, double *C, int n, int n1); 
int rc(int rt,int rank, int size); 
int main(int argc, char *argv[]) { 
double *A, *B, *C,t,tt; 
int n = 0, n0, n1, n2, i,ss,sts; 
int rank = 0, size = 1,prev,next,k,z,jcol,ix=0,m,j; 
MPI_Datatype column; 
MPI_Request reqs[4]; 
MPI_Status stats[2]; 
MPI_Init(&argc, &argv); 
MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
MPI_Comm_size(MPI_COMM_WORLD, &size); 
if (!rank) { 
    if (argc > 1) { 
     n = atoi(argv[1]); 
    } 
    } 
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); 
if (!n) { 
    MPI_Finalize(); 
    return 0; 
} 
n1 = rc(n, rank,size); 
n0 = n * n1; 
n2  = n * n; 
A = (double *) malloc(sizeof(double) * (rank ? n0 : n2)); 
B = (double *) malloc(sizeof(double) * n2);  
C = (double *) malloc(sizeof(double) * (rank ? n0 : n2)); 
if (!rank) { 
    for (i=0; i<n2; i++) { 
     A[i] = 1.0; 
     B[i] = 1.0; 
    } 
} 
t = MPI_Wtime(); 
if (!rank) { 
    ss = n0; 
    for (i=1; i<size; i++) { 
     sts = n * rc(n, i, size); 
     MPI_Send(A + ss, sts, MPI_DOUBLE, i, TAG_INIT, 
       MPI_COMM_WORLD); 
     ss += sts; 
    } 
} 
else { 
    MPI_Recv(A, n0, MPI_DOUBLE, 0, TAG_INIT, MPI_COMM_WORLD, 
      MPI_STATUS_IGNORE); 
} 
MPI_Type_vector(n,1,n,MPI_DOUBLE, &column); 
MPI_Type_commit(&column); 

if (!rank) { 

    for (i=1; i<size; i++) { 

     for(m=0;m<=i-1;m++) 
     ix+=rc(n,m,size);   
     ss=rc(n,i,size);   
     for(j=ix;j<ss+ix;j++) 
     MPI_Send(&B[j], 1, column, i, TAG_INIT, MPI_COMM_WORLD); 
     /* MPI_Send(&B[i+(n-1)*n], 1, column, i, TAG_INIT, 
       MPI_COMM_WORLD);*/ 
    } 
} 
else { 
     printf("hello"); 
    MPI_Recv(B, n, MPI_DOUBLE, 0, TAG_INIT, MPI_COMM_WORLD, 
      MPI_STATUS_IGNORE); 
} 
for (i=0; i<n0; i++) { 
    printf("Processor: %d and matrix %lf \n ",rank, B[i]); 
} 

for (i=0; i<n0; i++) { 
    C[i] = 0.0; 
} 
MPI_Finalize(); 
return 0; 
} 
int rc(int rt, int rank, int size) { 
return (rt/size) + (rt % size > rank); 
} 

回答

0

請不要來電具有兩三個字母的值,是因爲我不明白你想做什麼。 您可以通過不同的方式解決問題。 當n = 7,我有3個進程,我發送2列到不同主進程0的每個進程。我希望這對你有所幫助。最好的祝福。

#include <stdlib.h> 
#include <mpi.h> 
#include <stdio.h> 
#define ERR_BADORDER 255 
#define TAG_INIT  31337 
#define TAG_RESULT  42 
#define DISP_MAXORDER 12 

int main(int argc, char *argv[]) { 
    double *B; 
    int n = 0; 
    int rank , size; 
    int i; 
    int columnToSend; 
    MPI_Datatype column; 
    MPI_Init(&argc, &argv); 
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
    MPI_Comm_size(MPI_COMM_WORLD, &size); 
    if (!rank)printf ("rank = %d , size = %d\n", rank, size); 
    if (!rank) { 
     if (argc > 1) { 
      n = atoi(argv[1]); 
     } 

    } 
    MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); 
    if (!n) { 
     printf ("n = %d!!\n", n); 
     MPI_Finalize(); 
     return 0; 
    } 

    int offset = n%size; 
    int gap = n/size; 
    if (!rank)printf ("n = %d, offset = %d , gap = %d\n", n, offset, gap); 

    MPI_Type_vector(n,gap,n,MPI_DOUBLE, &column); 
    MPI_Type_commit(&column); 
    B = (double *) malloc(sizeof(double) * n*n); 
    for (i = 0 ; i < n * n ; i++) { 
      B[i] = -1.0; 
    } 

    if (!rank) { 
     for (i = 0 ; i < n * n ; i++) { 
      B[i] = i;//<----- I put i instead one 
     } 
     for (i=1; i < size; i++) { 
      columnToSend = gap *i + offset; 
      printf ("columnToSend = %d to i = %d \n", columnToSend, i); 
      MPI_Send(&B[columnToSend], 1, column, i, TAG_INIT, MPI_COMM_WORLD); 

     } 
    } 
    if (rank) { 
     printf ("in rank = %d n*gap = %d \n", rank, n*gap); 
     MPI_Recv(B, n*gap, MPI_DOUBLE, 0, TAG_INIT, MPI_COMM_WORLD, MPI_STATUS_IGNORE); 
     for (i=0; i < n*gap; i++) { 
      printf("Processor: %d and matrix %lf \n ",rank, B[i]); 
     } 
    } 

    MPI_Finalize(); 
    return 0; 
}