Intel® High Level Design
Support for Intel® High Level Synthesis Compiler, DSP Builder, OneAPI for Intel® FPGAs, Intel® FPGA SDK for OpenCL™
661 Discussions

Weird behavior of dpc++ code after running it on FPGA device

amaltaha
New Contributor I
558 Views

Hello,

I am using DPC++ to accelerate knn algorithm on FPGA device. The following code is the code I wrote for the euclidean distance. The problem is that the fpga_emulation works very well with no problems while running it on fpga hardware (Intel Arria 10 OneAPI)  gives -nan for all values in the resulting buffer, which means something got wrong in the parallel_for lioop. But I can't find anything wrong about it and the emulation worked.

I am using Intel Devcloud platform.  

  1. std::vector<double> distance_calculation_FPGA(queue& q, const std::vector<std::vector<double>>& dataset, const std::vector<double>& curr_test) {
        std::cout<<"convert 2D to 1D"<<std::endl;
        std::vector<double>linear_dataset;
        for (int i = 0; i < dataset.size(); ++i) {
            for (int j = 0; j < dataset[i].size(); ++j) {
                linear_dataset.push_back(dataset[i][j]);
            }
        }
        std::cout<<"buffering"<<std::endl;
          range<1> num_items{dataset.size()};
        std::vector<double>res;
        //std::cout << "im in" << std::endl;
    
        res.resize(dataset.size());
        buffer dataset_buf(linear_dataset);
        buffer curr_test_buf(curr_test);
        buffer res_buf(res.data(), num_items);
        
        std::cout<<"submit a job"<<std::endl;
        auto start = std::chrono::high_resolution_clock::now();
        {
        q.submit([&](handler& h) {
            accessor a(dataset_buf, h, read_only);
            accessor b(curr_test_buf, h, read_only);
    
            accessor dif(res_buf, h, write_only, no_init);
            h.parallel_for(num_items, [=](auto i) {
                    for (int j = 0; j < 5; ++j) {
                        dif[i] += (b[j] - a[i * 5 + j]) * (b[j] - a[i * 5 + j]);  
                    }
               // out << "i : " << i << " i[0]: " << i[0] << " b: " << b[0] << cl::sycl::endl;
                });
            }).wait();
        }
        auto finish = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double> elapsed = finish - start;
        std::cout << "Elapsed time: " << elapsed.count() << " s\n";
        /*
            for (int i = 0; i < dataset.size(); ++i) {
                double dis = 0;
                for (int j = 0; j < dataset[i].size(); ++j) {
                    dis += (curr_test[j] - dataset[i][j]) * (curr_test[j] - dataset[i][j]);
                }
                res.push_back(dis);
            }
            */
        return res;
    }

results with fpga_emulation: ./knn.fpga_emu

amaltaha_1-1652982724733.png

 

results for fpga hardware: ./knn.fpga

amaltaha_0-1652982589911.png

 

Thank you so much!

0 Kudos
1 Solution
amaltaha
New Contributor I
519 Views

Hello Aik Eu,

Thank you for your reply, I removed the no_init from the accessor and is now working correctly as expected.

this line:
accessor dif(res_buf, h, write_only, no_init);

became this:
accessor dif(res_buf, h, write_only);

 

Thank you!

View solution in original post

0 Kudos
3 Replies
aikeu
Employee
528 Views

Hi amaltaha,


May I know which reference example that you build to run your project?

I not sure the project you used may not considered some of the changes required for the hardware nodes to run. Therefore you might get different results when running on hardware as compared to emulation.

I think will be better to leverage on a working reference example project to build your own project.


Thanks.

Regards,

Aik Eu


0 Kudos
amaltaha
New Contributor I
520 Views

Hello Aik Eu,

Thank you for your reply, I removed the no_init from the accessor and is now working correctly as expected.

this line:
accessor dif(res_buf, h, write_only, no_init);

became this:
accessor dif(res_buf, h, write_only);

 

Thank you!

0 Kudos
aikeu
Employee
501 Views

Hi amaltaha,


Good to hear that is working now.

I will close this thread for now.


Thanks.

Regards,

Aik Eu


0 Kudos
Reply