C openMP 程序的完全空白输出

标签 c output openmp

我编写了以下矩阵乘法程序:

#include <stdio.h>
#include <omp.h>
#include <time.h>
#define NRA 500
#define NCA 500
#define NCB 500

int mat_mul() ;
int i , j , k ;
int main(){
    double start , end ;
    start  =  omp_get_wtime() ;
    mat_mul() ;
    end = omp_get_wtime() ; 
    printf("Time taken : \n %lf " , (end - start) );
    return 0;
}

int mat_mul(){
    int mat1[NRA][NCA] , mat2[NCA][NCB] , mat3[NRA][NCB] ;
    //double start , end ;
    //start  =  omp_get_wtime() ;
    #pragma omp parallel private( i , j , k ) shared(mat1 , mat2 , mat3)
    #pragma omp for

    for( i = 0 ; i < NRA ; i++){
        for( j = 0 ; j < NCB ; j++){
            mat1[i][j] = mat2[i][j] = rand() ;
            for ( k = 0 ; k <  NCB ; k++){
                mat3[i][k] += mat1[i][k] * mat2[k][j] ;
            }
        }
    }
    //end = omp_get_wtime() ; 
    printf("REsult : \n");
    for( i = 0 ; i < NRA ; i ++ ) {
        for( j =0 ; j < k ; j ++)
            printf("%lf" , (double)mat3[i][j]);
        printf("\n") ;
    //printf("Time taken : \n %lf " , (end - start) );
    }
    return 0 ;
}

一切都很好(几乎):它编译,执行,甚至终止:-D(并且它实际上提供了加速(比较。

但不幸的是,输出看起来像这样:

[tejas@localhost Documents]$ gcc -pedantic -Wall  -std=c99 -fopenmp  par.c
par.c: In function ‘mat_mul’:
par.c:28:30: warning: implicit declaration of function ‘rand’ [-Wimplicit-function-declaration]
    mat1[i][j] = mat2[i][j] = rand() ;
                              ^~~~
[tejas@localhost Documents]$ ./a.out
REsult :
...
(LOTS of blank spaces later)
...
Time taken : 
0.177506 [tejas@localhost Documents]$ 

我做错了什么? 我正在使用 Fedora 27GCC

提前谢谢您。

最佳答案

这是因为 j < k循环条件:

for( j =0 ; j < k ; j ++)

k的值使用 openmp 时有所不同。你可以将 k 替换为 NCB

关于C openMP 程序的完全空白输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48317503/

相关文章:

c - `"<program> "would like to access files in your Documents folder` 带有 VS Code 和 macOS Catalina 的消息

c - C如何评估这些程序?

c++ - ofStream 错误 : writing to text file?

Python 脚本打开并写入终端

c++ - GCC C/C++ MEX Matlab R2015 Mac OS X(带 OpenMP)无法工作

c - 如何在ansi C中使while循环持续1秒?

c++ - 在 iPhone 上获取蓝牙 MAC 地址

c++ - 为什么输出的是方框里的问号而不是数字?

c++ - OpenMP 线程数问题

c - 对于矩阵初始化,openmp 比串行慢