警告:'dot_prod' 可能在此函数中使用未初始化 [-Wmaybe-uninitialized]
warning: ‘dot_prod’ may be used uninitialized in this function [-Wmaybe-uninitialized]
我一直在修改这段代码,但我似乎无法让它工作。当我 运行 它生成的错误文件时,我收到警告
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**
将 dot_prod 的声明更改为:
int dot_prod = 0;
int i;
dot_prod += (numeric expression)
是未定义的行为,如果你还没有初始化 dot_prod
。
您初始化 dot_prod
的方式与初始化任何其他 int
变量的方式相同;通过将其设置为某个 pre-determined 值,例如零。
int dot_prod = 0;
如果没有 初始赋值, dot_prod 可以在声明时包含任何 int
值。您的 C 编译器只需要为您的变量分配一些内存;不需要在那里设置一些合理的初始值。你必须自己做。
我一直在修改这段代码,但我似乎无法让它工作。当我 运行 它生成的错误文件时,我收到警告
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**
将 dot_prod 的声明更改为:
int dot_prod = 0;
int i;
dot_prod += (numeric expression)
是未定义的行为,如果你还没有初始化 dot_prod
。
您初始化 dot_prod
的方式与初始化任何其他 int
变量的方式相同;通过将其设置为某个 pre-determined 值,例如零。
int dot_prod = 0;
如果没有 初始赋值, dot_prod 可以在声明时包含任何 int
值。您的 C 编译器只需要为您的变量分配一些内存;不需要在那里设置一些合理的初始值。你必须自己做。