Search code examples
c++inteldpc++

Terminating code as std::bad_function_call when running the dpcpp code


When i try to run a vector addition dpcpp program. It throws the error as

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

Below is the sample code : `

#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;
}

`

Please help me to resolve this issue. Thanks in advance.

I tried to run a program but it throws an error


Solution

  • Since you already created a handler h for the queue, just change this line

    q.parallel_for(N ,[=](id<1> i)

    to

    h.parallel_for(N ,[=](id<1> i)