警告:‘ dot_prod’可以在此功能[-wmaybe-initialization]中使用非专业化。
我一直在修改此代码,似乎无法使其正常工作。当我运行错误文件时,会生成后,我会得到警告
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**
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的dot_prod声明更改为:
Change your declaration of dot_prod to:
dot_prod +=(数字表达式)
是未定义的行为,如果您没有初始化dot_prod
。您初始化
dot_prod
以初始化任何其他int
varible的方式相同;通过将其设置为一些预定的值,例如零。没有该初始分配, dot_prod可以在声明时包含任何
int
值。您的C编译器仅需要为您的变量分配一些内存;不需要在此处放置一些明智的初始值。你必须自己做。dot_prod += (numeric expression)
is undefined behavior if you have not initializeddot_prod
.You initialize
dot_prod
the same way you initialize any otherint
variable; by setting it to some pre-determined value, like zero.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.