MPI和OpenMP混合程序

时间:2018-12-10 16:39:43

标签: c mpi openmp openmpi hpc

我正在尝试运行OpenMP和MPI混合程序。这是我的代码,应该并行处理矩阵/向量乘法:

#include <mpi.h>
#include <omp.h>
#include <stdio.h>
#define N 5000

double A[N][N], x[N], y[N], alpha, beta, result[N];

int main (int argc, char *argv[])
{

    int i, j, error, rank, size;
    double startt, endt;
    error = MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Barrier(MPI_COMM_WORLD);
    startt=MPI_Wtime();


    int count = N / size; 
    int remainder = N % size;
    int start, stop; 
    int displs[size]; 
    int recv_counts[size]; 


    if(rank<size-1){
        //first n-1 processes do N/size amount of work
        start=rank*(count);
        stop=start+(count-1);
    }

    else if (rank==(size-1)){
        //last process does N/size+N%size amount of work
        start=rank*(count);
        stop=N-1;
    }
    for (i=0; i<size; i++) {
            if(i<size-1){
                recv_counts[i] =count;
            }
            else
            {
                recv_counts[i]=count+remainder;
            }
            displs[i] = i*count;

        }

    #pragma omp parallel for shared(A, x, y, alpha, beta, result) private(i, j)  
    for (int i = start; i <= stop; ++i) {
                    result[i] = 0;
                    for(j=0; j<N; j++){
                            result[i] += A[i][j] * x[j];
                    }
            result[i] = alpha * result[i] + beta * y[i];
    }


    MPI_Allgatherv(&result[start], stop-start+1, MPI_DOUBLE,
                                &result[0], recv_counts, displs,
                                MPI_DOUBLE, MPI_COMM_WORLD);

    MPI_Barrier(MPI_COMM_WORLD);
    endt = MPI_Wtime();
    if(rank==0){
            printf("Done in %f seconds.\n", endt - startt);
    }
    error = MPI_Finalize();
    return  0;
}

我使用mpicc -openmp test.c -o test编译程序,但是,我收到以下警告消息: 警告:ignoring #pragma omp parallel [-Wunknown pragmas-]

如果没有OpenMP,该代码似乎可以正常工作。谁能给我提示我做错了什么?

0 个答案:

没有答案