执行时间工作共享vs spmd

时间:2018-05-03 08:27:42

标签: openmp

我正在编写两个类似的程序来比较spmd与prosharing的执行时间。 令我惊讶的是,我在工作共享中获得了更多的执行时间,而spmd的时间则相当少。

我做错了什么?

以下是我的代码:

SPMD代码:

#define N 1000
float A[N][N], B[N][N], C[N][N]; // declaring matrices of NxN size
int main() {
    /* DECLARING VARIABLES */
    int i, j, k; // indices for matrix multiplication

    /* FILLING MATRICES WITH RANDOM NUMBERS */
    srand ( time(NULL) );
    for(i=0;i<N;i++) {
        for(j=0;j<N;j++) {
            A[i][j]= (rand()%10);
            B[i][j]= (rand()%10);
        }
    }
    /* MATRIX MULTIPLICATION */
    omp_set_num_threads(4);
    int id;
    int m;
    int nthrds;  
    #pragma omp parallel // num_threads(4)
    {
        id=omp_get_thread_num();
        nthrds=omp_get_num_threads();
        printf("number of threads %d",nthrds);
        double wtime = omp_get_wtime();
        for (i=id;i<N;i=i+nthrds) {
            //dummy=i+nthrds;
            for(j=id;j<N;j=j+nthrds) {
                C[i][j]=0;
                for(k=id;k<N;k=k+nthrds) {
                    C[i][j]+=A[i][k]*B[k][j];
                }
                // printf("%d ", C[i][j]);  
            }
            //printf("\n");
        }

        wtime = omp_get_wtime() - wtime;
        printf( "Time taken  is %f\n", wtime );
        getchar(); 
    }
}

工作共享代码是:

.......
int main() {
    /* DECLARING VARIABLES */
    int i, j, k; // indices for matrix multiplication

    /* FILLING MATRICES WITH RANDOM NUMBERS */
    srand ( time(NULL) );
    for(i=0;i<N;i++) {
        for(j=0;j<N;j++) {
            A[i][j]= (rand()%10);
            B[i][j]= (rand()%10);
        }
    }
    /* MATRIX MULTIPLICATION */
    //printf("Max number of threads: %i \n",omp_get_max_threads()); 

    double wtime = omp_get_wtime();
    #pragma omp parallel private(j,k) num_threads(8)
    {   
        #pragma omp for 
        for (i=0;i<N;i++) {
            for(j=0;j<N;j++) {
                C[i][j]=0;
                for(k=0;k<N;k++) {
                    C[i][j]+=A[i][k]*B[k][j];
                } 
                // printf("%d ", C[i][j]);  
            }
            //printf("\n");
        }
        double etime = omp_get_wtime() - wtime;
        printf( "Time taken  is %f\n", etime );
        getchar(); 
    }
}

0 个答案:

没有答案