如何使out_dotp.err停止引起错误

发布于 2025-01-27 14:06:25 字数 1587 浏览 2 评论 0原文

我需要帮助,我已经弄乱了这个代码了几个小时,它应该以矛盾的方式执行点产品操作,但我无法让它输出任何结果,而我所得到的只是令人难以置信的痛苦和此错误消息。谁能通过解决这个错误的可能解决来启发我可怜的折磨灵魂?我将永远感谢。

#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 = 0;
    int 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;
}

在此处输入图像描述

I need help, I have been messing with this code for hours , it is supposed to perform the dot product operation in a paralel manner yet I cannot get it to output any result and all I get is excruciating pain and this error message. could anyone enlighten my poor tortured soul with a possible fix for this error? I will be eternally thankful.

#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 = 0;
    int 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;
}

enter image description here

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

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

发布评论

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

评论(1

甚是思念 2025-02-03 14:06:25

因为您没有检查引起SSCAN的第一个参数引起segfault的参数计数。我通过添加多个检查来修复这些零件:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include <vector>
#include <iostream>
int main(int argc, char **argv){
        
    int exe_mode, num_td, vec_size;
    std::cout<<exe_mode<<std::endl;
    
    if(argc >= 1) // when no command-line argument given, segfaults
        sscanf(argv[0], "%d", &exe_mode);
    
    if(argc >= 2) 
        sscanf(argv[1], "%d", &num_td);

    if(argc >= 3)
        sscanf(argv[2], "%d", &vec_size);

    // just in case too much memory allocated by a typo
    if(vec_size > 10000)
        vec_size = 10000;

    // godbolt.org gives only 2 threads
    if(num_td > 2)
        num_td = 2;

    if(num_td <= 0)
        num_td = 1;

    // arbitrary initialization size
    if(vec_size <= 0)
        vec_size = num_td*10;

    if(exe_mode <= 0)
        exe_mode = 1;

    if(exe_mode > 3)
        exe_mode = 3;
    
    srand(1);

    // no simple arrays because no compile-time known vec_size
    std::vector<int> a(vec_size);
    std::vector<int> b(vec_size);

    int dot_prod = 0;
    int 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;
}

Because you did not check argument count which caused segfault on the sscan's first parameter. I fixed those parts by adding multiple checks:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include <vector>
#include <iostream>
int main(int argc, char **argv){
        
    int exe_mode, num_td, vec_size;
    std::cout<<exe_mode<<std::endl;
    
    if(argc >= 1) // when no command-line argument given, segfaults
        sscanf(argv[0], "%d", &exe_mode);
    
    if(argc >= 2) 
        sscanf(argv[1], "%d", &num_td);

    if(argc >= 3)
        sscanf(argv[2], "%d", &vec_size);

    // just in case too much memory allocated by a typo
    if(vec_size > 10000)
        vec_size = 10000;

    // godbolt.org gives only 2 threads
    if(num_td > 2)
        num_td = 2;

    if(num_td <= 0)
        num_td = 1;

    // arbitrary initialization size
    if(vec_size <= 0)
        vec_size = num_td*10;

    if(exe_mode <= 0)
        exe_mode = 1;

    if(exe_mode > 3)
        exe_mode = 3;
    
    srand(1);

    // no simple arrays because no compile-time known vec_size
    std::vector<int> a(vec_size);
    std::vector<int> b(vec_size);

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