2012-12-13 2 views
0

저는 MPI 물건에 정말로 멍청합니다. 대학 과제를 끝내기 만하면됩니다. 누군가 나를 도울 수 있습니까?매트릭스 MPI에서 최소값과 최대 값을 찾으십시오.

작업은 N * M 매트릭스에서 최소값과 최대 값을 찾는 것입니다. 프로세스 수는 N과 같아야합니다. MPI_Reduce을 사용하여 최소값과 최대 값을 찾으려고 시도하지만 오류 결과가 발생합니다. 여기 내 코드가있다.

#include <mpi.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

const int n = 5; 
const int m = 4; 

void fill_matrix_randomly(int matrix[n][m], int max_value); 
void write_matrix(int matrix[n][m]); 
int find_max(int* vector, int vector_size); 
int find_min(int* vector, int vector_size); 
void write_vector(int* vector, int vector_size); 

int main(int argc, char* argv[]) 
{ 
    int my_rank = 0; 
    int comm_size = 0; 

    int a[n][m]; 
    int receive_buffer[m]; 
    int partial_max[m]; 
    int partial_min[m]; 

    MPI_Init(&argc, &argv); 

    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); 
    MPI_Comm_size(MPI_COMM_WORLD, &comm_size); 

    if (comm_size != n) 
    { 
     printf("Please set process count = %d and run again.", n); 
     MPI_Finalize(); 
     return 0; 
    } 

    if (my_rank == 0) 
    { 
     fill_matrix_randomly(a, 10); 
     write_matrix(a);  
    } 
    /* MPI Scatter(address of send buffer, number of elements sent to each process, data type of send buffer, address of receive buffer, number of elements in receive buffer, data type of receive buffer, rank of sending process, communicators space) */ 
    MPI_Scatter(a, n, MPI_INT, receive_buffer, n, MPI_INT, 0, MPI_COMM_WORLD); 
    /* MPI_Reduce(address of send buffer, address of receive buffer, number of elements in send buffer, data type of elements in send buffer, reduce operation, rank of root process, communicators space) */ 
    MPI_Reduce(receive_buffer, partial_max, n, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD); 
    MPI_Reduce(receive_buffer, partial_min, n, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD); 
    if (my_rank == 0) 
    { 
     printf("Vector of partial max values.\n"); 
     write_vector(partial_max, n); 
     printf("Vector of partial min values.\n"); 
     write_vector(partial_min, n); 

     int max = find_max(partial_max, n); 
     int min = find_min(partial_min, n); 
     printf("Matrix boundaries = [%d..%d]\n", min, max); 
    } 
    MPI_Barrier(MPI_COMM_WORLD); 
    MPI_Finalize(); 

    return 0; 
} 

int find_max(int* vector, int vector_size) 
{ 
    int max = vector[0]; 
    int i = 0; 
    for (i = 0; i < vector_size; i++) 
    { 
     if (vector[i] > max) 
     { 
      max = vector[i]; 
     }  
    } 
    return max; 
} 

int find_min(int* vector, int vector_size) 
{ 
    int min = vector[0]; 
    int i = 0; 
    for (i = 0; i < vector_size; i++) 
    { 
     if (vector[i] < min) 
     { 
      min = vector[i]; 
     } 
    } 
    return min; 
} 

void fill_matrix_randomly(int matrix[n][m], int max_value) 
{ 
    int i = 0; 
    int j = 0; 
    srand(time(NULL)); 
    for (i = 0; i < n; i++) 
    { 
     for (j = 0; j < m; j++) 
     { 
      matrix[i][j] = rand() % max_value; 
     } 
    } 
} 

void write_matrix(int matrix[n][m]) 
{ 
    int i = 0; 
    int j = 0; 
    for (i = 0; i < n; i++) 
    { 
     for (j = 0; j < m; j++) 
     { 
      printf("%4d", matrix[i][j]);    
     } 
     printf("\n"); 
    } 
} 

void write_vector(int* vector, int vector_size) 
{ 
    int i = 0; 
    for (i = 0; i < vector_size; i++) 
    { 
     printf("vector[%d] = %d\n", i, vector[i]); 
    } 
} 

출력은 다음과 같습니다.

$ mpiexec -n 5 ./main 
    8 1 3 9 
    1 0 3 1 
    4 9 5 5 
    1 7 4 9 
    8 5 7 4 
Vector of partial max values. 
vector[0] = 9 
vector[1] = 8 
vector[2] = 5 
vector[3] = 9 
vector[4] = 16 
Vector of partial min values. 
vector[0] = -1 
vector[1] = -1 
vector[2] = -1 
vector[3] = -1 
vector[4] = 1 
Matrix boundaries = [-1..16] 

내가 잘못 들었습니까? 무엇을 바꾸어야합니까? 나는 하루 종일 해결책을 찾으려고 노력한다.

답변

2

귀하의 매트릭스는 n 행 by m 칼럼입니다. 이 행렬을 n 개의 프로세스에 배포 할 때 각 프로세스는 m 개의 요소를 처리해야하지만 대신 모든 벡터 호출에서 n의 수를 사용합니다. 너는 m 길이를 다음과 같이 전달해야합니다 :

  • MPI_Scatter 호출;
  • 모두 MPI_Reduce 호출;
  • 모두 write_vector calls;
  • find_maxfind_min 호출. 제대로 m 요소로 receive_buffer, partial_max, partial_min을 선언 한

참고.

+0

정말 고마워요! 지금은 잘 작동합니다. –

관련 문제