You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行DPC++向量加法程序触发std::bad_function_call错误求助

DPC++向量加法程序std::bad_function_call错误修复

错误信息

运行程序时抛出以下异常:

terminate called after throwing an instance of 'std::bad_function_call'
  what():  bad_function_call
Aborted (core dumped)

原问题代码

#include<CL/sycl.hpp>
#include<iostream>
#include<chrono>
using namespace sycl;
int main()
{
    constexpr int N=10;
    int i;
    std::array<int,N> a;
    std::array<int,N> b;
    std::array<int, N> c;
    auto start = std::chrono::high_resolution_clock::now();
    for(i=0;i<N;i++)
    {
        a[i]=i;
        b[i]=N-i;
    }
    buffer a_buffer(a);
    buffer b_buffer(b);
    buffer c_buffer(c);
    queue q;
    q.submit([&] (handler &h){
        accessor acc_a(a_buffer,h);
        accessor acc_b(b_buffer,h);
        accessor acc_c(c_buffer,h);
        q.parallel_for(N ,[=](id<1> i) {
            
        });
        });
    q.wait();
     auto stop = std::chrono::high_resolution_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop-start);
        std::cout<<duration.count();
    host_accessor h_a(c_buffer ,read_only);
    for(i=0;i<N;i++)
    {
    std::cout<<c[i]<<std::endl;
    }
    return 0;
}

错误分析与修复步骤

  • 核心错误:错误调用parallel_for
    在q.submit的handler lambda中,必须通过handler对象h提交内核,而非直接使用队列q。原代码中q.parallel_for的写法会触发内部函数调用异常,应改为h.parallel_for。

  • 补充内核逻辑
    原parallel_for的lambda是空的,未实现向量加法核心逻辑,需添加:acc_c[i] = acc_a[i] + acc_b[i];。

  • 修正结果读取方式
    原代码直接打印栈上未被设备修改的c数组,应通过host_accessor读取设备计算后的结果,即打印h_a[i]而非c[i]。

  • 调整计时范围
    原计时包含主机数组初始化和后续打印时间,需将计时范围调整为仅包裹设备计算部分,保证计时准确。

修复后的完整代码

#include<CL/sycl.hpp>
#include<iostream>
#include<chrono>
using namespace sycl;

int main()
{
    constexpr int N=10;
    int i;
    std::array<int,N> a;
    std::array<int,N> b;
    std::array<int, N> c;

    // 主机端初始化数组
    for(i=0;i<N;i++)
    {
        a[i]=i;
        b[i]=N-i;
    }

    buffer a_buffer(a);
    buffer b_buffer(b);
    buffer c_buffer(c);
    queue q;

    // 开始计时设备计算部分
    auto start = std::chrono::high_resolution_clock::now();

    q.submit([&] (handler &h){
        accessor acc_a(a_buffer, h, read_only);
        accessor acc_b(b_buffer, h, read_only);
        accessor acc_c(c_buffer, h, write_only);
        // 通过handler调用parallel_for,补充向量加法逻辑
        h.parallel_for(N ,[=](id<1> i) {
            acc_c[i] = acc_a[i] + acc_b[i];
        });
    });
    q.wait();

    // 结束计时
    auto stop = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop-start);
    std::cout << "计算耗时:" << duration.count() << "微秒" << std::endl;

    // 通过host_accessor读取设备结果
    host_accessor h_a(c_buffer, read_only);
    std::cout << "向量加法结果:" << std::endl;
    for(i=0;i<N;i++)
    {
        std::cout << h_a[i] << std::endl;
    }

    return 0;
}

内容的提问来源于stack exchange,提问作者HemanthCH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 04:05:40