代码之家  ›  专栏  ›  技术社区  ›  baha

添加更多进程时,mpi应用程序性能会变差

  •  0
  • baha  · 技术社区  · 3 年前

    我用MPI编写了并行二进制搜索算法,它在搜索值方面如预期那样工作,但当-n为1(串行)时,总时间远低于2、4、8等值以上的任何值。。。。

    当我增加进程的数量时,当我预计时间低于1个进程时,需要更长的时间。出了什么问题,或者有人能帮我解决这个问题吗。这是我的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <mpi.h>
    
    void BinarySearch(int local_x[], int search, int lower, int heigher, int rank, int comm_sz);
    int* create_array(int n);
    
    int index = -1;
    int found_rank = -1;
    
    void main() {
        int search = 7;
        
        MPI_Init(NULL, NULL);
    
        int my_rank, comm_sz;
        double start = 0;
        double finish = 0;
        MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
        MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    
        int size =  10 * comm_sz;
        int* x = nullptr;
    
        int local_size = (size / comm_sz); //+ 1 
        int* local_x = new int[local_size];
    
        MPI_Barrier(MPI_COMM_WORLD);
        start = MPI_Wtime();
        if (my_rank == 0)
        {
            printf("size per process = %d \n", local_size);
            x = create_array(size);
            for (int i = 0; i < size; i++)
            {
                printf("%d ", x[i]);
            }
    
            //  printf("\n Please input number: \n");
            //getchar();
        }
    
        MPI_Scatter(x, local_size, MPI_INT, local_x, local_size, MPI_INT, 0, MPI_COMM_WORLD);
    
        BinarySearch(local_x, search, 0, local_size, my_rank, comm_sz);
    
    
        MPI_Barrier(MPI_COMM_WORLD);
        finish = MPI_Wtime();
    
    
        if (my_rank == 0) {
            printf("\n total time = %g \n", finish - start);
    
            if (found_rank == -1 || index == -1) {
                printf("Not found");
            }else
            printf("\n value %d located at index %d at rank %d \n", search, index,found_rank );
    
        }
    
    
        MPI_Finalize();
    }
    
    void BinarySearch(int local_x[], int search, int lower, int heigher, int rank, int comm_sz) {
        int mid = -1;
        int size = heigher;
        int correct_rank = -1;
        for (int i = lower; lower < heigher ; i++)
        {
            mid = (lower + heigher) / 2;
    
            if (local_x[mid] > search) {
    
                heigher = mid - 1;
            }
    
            if (local_x[mid] < search) {
                lower = (mid + 1);
            }
    
            if (local_x[mid] == search) {
                break;
            }
    
        }
        int value = local_x[mid];
        if (value == search) {
            mid = (rank * size) + mid;
            correct_rank = rank;
        }
        else {
            mid = -1;
        }
    
        MPI_Reduce(&mid, &index, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
        MPI_Reduce(&correct_rank, &found_rank, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
    
    
    
    
    }
    
    int* create_array(int n) {
        int* tmp = (int*)calloc(n, sizeof(int));
        for (int i = 0; i < n; i++)
        {
            tmp[i] = i + 1 ;
        }
        return (tmp);
    }
    
    0 回复  |  直到 3 年前
    推荐文章