警告:‘ dot_prod’可以在此功能[-wmaybe-initialization]中使用非专业化。

发布于 2025-01-27 08:58:11 字数 2889 浏览 3 评论 0原文

我一直在修改此代码,似乎无法使其正常工作。当我运行错误文件时,会生成后,我会得到警告

dotp.c:39:11: warning: ‘dot_prod’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   39 |   #pragma omp parallel for reduction(+: dot_prod) num_threads(num_td)
      |           ^~~

,我不明白我如何使dot_prod概念化或是否可以解决错误,任何人都可以帮助我修复它吗?太感谢了。

#include <omp.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>


int main(int argc, char **argv){
        
    int exe_mode, num_td, vec_size;
    
    
    sscanf(argv[0], "%d", &exe_mode);
    sscanf(argv[1], "%d", &num_td);
    sscanf(argv[2], "%d", &vec_size);

    srand(1);

    int a[vec_size];
    int b[vec_size];
    int dot_prod,i;
    
    double start = omp_get_wtime();
    
    //initializing the vectors
    for( i = 0; i < vec_size; i++){ 
        a[i] = (int) (rand() % vec_size - vec_size/2);
        b[i] = (int) (rand() % vec_size - vec_size/2);
    }
    
    //Sequential execution
    if (exe_mode == 1){
        for( i = 0; i<vec_size;i++){
            dot_prod += a[i] * b[i];
        }
    }   
    //Parallel execution
    if (exe_mode == 2){
        #pragma omp parallel for reduction(+: dot_prod) num_threads(num_td)
        for(i=0; i<vec_size;i++){
            dot_prod += a[i] * b[i];
        }
    }
    //Parallel execution with vectorization
    if (exe_mode == 3){
        #pragma omp simd reduction(+: dot_prod)
        for( i=0; i<vec_size;i++){
            dot_prod += a[i] * b[i];
        }
    double runtime = omp_get_wtime()-start;
    printf("%.4e\t%i\n", runtime ,dot_prod);    
    }
    
    
    
    return 0;
}

/**void dotproduct(int exe_mode, int num_td, int vec_size){
    
    srand(1);
    
    int a[vec_size];
    int b[vec_size];
    int dot_prod;
    
    //initializing the vectors
    for(int i = 0; i < vec_size; i++){ 
        a[i] = (int) (rand() % vec_size - vec_size/2);
        b[i] = (int) (rand() % vec_size - vec_size/2);
    }
    
    //Sequential execution
    if (exe_mode == 1){
        for(int i=0; i<vec_size;i++){
            dot_prod += a[i] * b[i];
        }
    }   
    //Parallel execution
    if (exe_mode == 2){
        #pragma omp parallel for num_threads(num_td)
        for(int i=0; i<vec_size;i++){
            dot_prod += a[i] * b[i];
        }
    }
    //Parallel execution with vectorization
    if (exe_mode == 3){
        #pragma omp parallel for simd num_threads(num_td)
        for(int i=0; i<vec_size;i++){
            dot_prod += a[i] * b[i];
        }
    double runtime = omp_get_wtime();   
    printf("%.4e\t%i\n", runtime ,dot_prod);    
    }
}**/**strong text**

I have been tinkering with this code and I cannot seem to make it work. when I run the error file it generates I get the warning

dotp.c:39:11: warning: ‘dot_prod’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   39 |   #pragma omp parallel for reduction(+: dot_prod) num_threads(num_td)
      |           ^~~

I do not understand how I can initalize dot_prod or if that will fix the error, could anyone help me fix it? thank you so much.

#include <omp.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>


int main(int argc, char **argv){
        
    int exe_mode, num_td, vec_size;
    
    
    sscanf(argv[0], "%d", &exe_mode);
    sscanf(argv[1], "%d", &num_td);
    sscanf(argv[2], "%d", &vec_size);

    srand(1);

    int a[vec_size];
    int b[vec_size];
    int dot_prod,i;
    
    double start = omp_get_wtime();
    
    //initializing the vectors
    for( i = 0; i < vec_size; i++){ 
        a[i] = (int) (rand() % vec_size - vec_size/2);
        b[i] = (int) (rand() % vec_size - vec_size/2);
    }
    
    //Sequential execution
    if (exe_mode == 1){
        for( i = 0; i<vec_size;i++){
            dot_prod += a[i] * b[i];
        }
    }   
    //Parallel execution
    if (exe_mode == 2){
        #pragma omp parallel for reduction(+: dot_prod) num_threads(num_td)
        for(i=0; i<vec_size;i++){
            dot_prod += a[i] * b[i];
        }
    }
    //Parallel execution with vectorization
    if (exe_mode == 3){
        #pragma omp simd reduction(+: dot_prod)
        for( i=0; i<vec_size;i++){
            dot_prod += a[i] * b[i];
        }
    double runtime = omp_get_wtime()-start;
    printf("%.4e\t%i\n", runtime ,dot_prod);    
    }
    
    
    
    return 0;
}

/**void dotproduct(int exe_mode, int num_td, int vec_size){
    
    srand(1);
    
    int a[vec_size];
    int b[vec_size];
    int dot_prod;
    
    //initializing the vectors
    for(int i = 0; i < vec_size; i++){ 
        a[i] = (int) (rand() % vec_size - vec_size/2);
        b[i] = (int) (rand() % vec_size - vec_size/2);
    }
    
    //Sequential execution
    if (exe_mode == 1){
        for(int i=0; i<vec_size;i++){
            dot_prod += a[i] * b[i];
        }
    }   
    //Parallel execution
    if (exe_mode == 2){
        #pragma omp parallel for num_threads(num_td)
        for(int i=0; i<vec_size;i++){
            dot_prod += a[i] * b[i];
        }
    }
    //Parallel execution with vectorization
    if (exe_mode == 3){
        #pragma omp parallel for simd num_threads(num_td)
        for(int i=0; i<vec_size;i++){
            dot_prod += a[i] * b[i];
        }
    double runtime = omp_get_wtime();   
    printf("%.4e\t%i\n", runtime ,dot_prod);    
    }
}**/**strong text**

console output when performing a CAT on the error file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抹茶夏天i‖ 2025-02-03 08:58:11

将您的dot_prod声明更改为:

int dot_prod = 0;
int i;

Change your declaration of dot_prod to:

int dot_prod = 0;
int i;
葬﹪忆之殇 2025-02-03 08:58:11

dot_prod +=(数字表达式)是未定义的行为,如果您没有初始化dot_prod

您初始化dot_prod以初始化任何其他int varible的方式相同;通过将其设置为一些预定的值,例如零。

int dot_prod = 0;

没有该初始分配, dot_prod可以在声明时包含任何int值。您的C编译器仅需要为您的变量分配一些内存;不需要在此处放置一些明智的初始值。你必须自己做。

dot_prod += (numeric expression) is undefined behavior if you have not initialized dot_prod.

You initialize dot_prod the same way you initialize any other int variable; by setting it to some pre-determined value, like zero.

int dot_prod = 0;

Without that initial assignment, dot_prod could contain any int value when it is declared. Your C compiler is only required to allocate some memory for your variable; it's not required to put some sensible initial value there. You have to do that yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文