Home > Enterprise >  warning: ‘dot_prod’ may be used uninitialized in this function [-Wmaybe-uninitialized]
warning: ‘dot_prod’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Time:05-10

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

CodePudding user response:

Change your declaration of dot_prod to:

int dot_prod = 0;
int i;

CodePudding user response:

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.

  • Related