Intel® oneAPI DPC++/C++ Compiler
Talk to fellow users of Intel® oneAPI DPC++/C++ Compiler and companion tools like Intel® oneAPI DPC++ Library, Intel® DPC++ Compatibility Tool, and Intel® Distribution for GDB*

oneapi

Darius_Nicholas
New Contributor I
2,610 Views

I have tried many ways to copy the data from a two-dimensional vector to buffer, but I have failed.How does buffer store a two-dimensional vector?And how do we solve the problem in the diagram?

0 Kudos
1 Solution
VaishnaviV_Intel
Employee
2,455 Views

Hi,

 

Thanks for sharing your code with us.

Please try to declare a separate variable(i.e. z) for b.size() outside parallel_for and replace b.size() in parallel_for with that variable (i.e. z).

Please find the below modified code,

#include <vector>
#include <iostream>
#include <CL/sycl.hpp> // DPC++ header

std::vector<std::vector<double>> filter2d(const std::vector<double>& b, double a, const std::vector<std::vector<double>>& x) {
int rows = x.size();
int cols = x[0].size();
std::vector<std::vector<double>> y(x.size(), std::vector<double>(x[0].size()));
auto z=b.size();

sycl::queue q(sycl::cpu_selector_v);


std::vector<double> flat_y(rows * cols);
std::vector<double> flat_x(rows * cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
flat_x[i * cols + j] = x[i][j];
}
}
sycl::range<1> num_items{static_cast<size_t>(rows * cols)};

sycl::buffer b_buffer(b);
sycl::buffer y_buffer(flat_y.data(),num_items);
sycl::buffer x_buffer(flat_x);
//sycl::buffer<double, 2> x_buffer_1(x.data(), sycl::range<2>(rows, cols));
//sycl::buffer<double, 2> x_buffer((double*)x.data(), num_items);
//sycl::buffer<std::vector<double>, 2> x_buffer_2(x.data(), sycl::range<2>(rows, cols));

q.submit([&](sycl::handler& h) {
sycl::accessor x_acc(x_buffer,h,sycl::read_only);
sycl::accessor b_acc(b_buffer,h,sycl::read_only);

sycl::accessor y_acc(y_buffer, h, sycl::write_only,sycl::no_init);
/*auto b_acc = b_buffer.get_access<sycl::access::mode::read>(h);
auto x_acc = x_buffer.get_access<sycl::access::mode::read>(h);
auto y_acc = y_buffer.get_access<sycl::access::mode::write>(h);*/

h.parallel_for(num_items, [=](sycl::item<1> idx) {
int i = idx / cols;
int j = idx % cols;
y_acc[idx] = 0.0;
for (int k = 0; k < z; k++) {
if (i - k >= 0) {
y_acc[idx] += b_acc[k] * x_acc[(i - k) * cols + j];
}
}
y_acc[idx] /= a;
});
});


q.wait();


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
y[i][j] = flat_y[i * cols + j];
}
}
return y;
}


int main() {
    // Example usage:
    std::vector<double> b = { 1.0, 2.0, 1.0 }; // Some filter coefficients
    double a = 1.0; // Some normalization factor
    std::vector<std::vector<double>> x = {
        { 1.0, 2.0, 3.0 },
        { 4.0, 5.0, 6.0 },
        { 7.0, 8.0, 9.0 }
    }; // Some 2D input data

    std::vector<std::vector<double>> y = filter2d(b, a, x);

    // Print the result
    for (const auto& row : y) {
        for (const auto& val : row) {
            std::cout << val << " ";
        }
        std::cout << "\n";
    }

    return 0;
}

Please let us know if you still face any issues.

 

Thanks & Regards,

Vankudothu Vaishnavi.

 

View solution in original post

0 Kudos
6 Replies
VaishnaviV_Intel
Employee
2,576 Views

Hi,

 

Thanks for posting on Intel communities.

Could you please provide us with the complete code so that we can reproduce your issue at our end?

We have also tried below sample code,

 

#include <iostream>
#include <CL/sycl.hpp>

int matmul() {

    constexpr size_t M = 3, N = 3;
    constexpr size_t X = 2, Y = 2;

    std::vector<std::vector<int>> a = {{1,1,1,1}, {1,1,1,1}, {1,1,1,1}};
    std::vector<std::vector<int>> b = {{1,1,1,1}, {1,1,1,1}, {1,1,1,1}};
    std::vector<std::vector<int>> c = {{0,0,0,0}, {0,0,0,0}, {0,0,0,0}};

    sycl::queue Q;
    sycl::buffer Ab{a};
    sycl::buffer Bb{b};
    sycl::buffer Cb{c};

    Q.submit([&](sycl::handler& h) {
        sycl::accessor Ad{Ab,h};
        sycl::accessor Bd{Bb,h};
        sycl::accessor Cd{Cb,h};

        sycl::range global{M, N};
        sycl::range local{X, Y};

        h.parallel_for(sycl::nd_range{global,local}, [=](sycl::nd_item<2> it) {
            int j = it.get_global_id(0);
            int i = it.get_global_id(1);
            for (int k = 0; k < N; ++k) {
                Cd[j][i] += Ad[j][k] * Bd[k][i];
            }
        });
    }).wait();

    sycl::host_accessor Ch{Cb};
    for(int i = 0 ; i < M ; i++) {
        for(int j = 0 ; j < N ; j++)
            std::cout << Ch[i][j] << " ";
        std::cout << std::endl;
    }

    return 0;
}

int main()
{
    matmul();
    return 0;
}

 

Output:

VaishnaviV_Intel_0-1690452990161.png

 

Thanks & Regards,

Vankudothu Vaishnavi.

 

0 Kudos
Darius_Nicholas
New Contributor I
2,526 Views

This my code !

#include <vector>
#include <iostream>
#include <CL/sycl.hpp>  // DPC++ header

std::vector<std::vector<double>> filter2d(const std::vector<double>& b, double a, const std::vector<std::vector<double>>& x) {
    int rows = x.size();
    int cols = x[0].size();
    std::vector<std::vector<double>> y(x.size(), std::vector<double>(x[0].size()));


    sycl::queue q(sycl::gpu_selector{});


    std::vector<double> flat_y(rows * cols);
    std::vector<double> flat_x(rows * cols);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            flat_x[i * cols + j] = x[i][j];
        }
    }
    sycl::range<1> num_items{rows* cols};

    sycl::buffer b_buffer(b);
    sycl::buffer y_buffer(flat_y.data(),num_items);
    sycl::buffer x_buffer(flat_x);
    //sycl::buffer<double, 2> x_buffer_1(x.data(), sycl::range<2>(rows, cols));  Why is this wrong?
    //sycl::buffer<double, 2> x_buffer((double*)x.data(), num_items);
 

    q.submit([&](sycl::handler& h) {
        sycl::accessor x_acc(x_buffer,h,sycl::read_only);
        sycl::accessor b_acc(b_buffer,h,sycl::read_only);

        sycl::accessor y_acc(y_buffer, h, sycl::write_only,sycl::no_init);
        /*auto b_acc = b_buffer.get_access<sycl::access::mode::read>(h);
        auto x_acc = x_buffer.get_access<sycl::access::mode::read>(h);
        auto y_acc = y_buffer.get_access<sycl::access::mode::write>(h);*/

        h.parallel_for(num_items, [=](sycl::item<1> idx) {
            int i = idx / cols;
            int j = idx % cols;
            y_acc[idx] = 0.0;
            for (int k = 0; k < b.size(); k++) {
                if (i - k >= 0) {
                    y_acc[idx] += b_acc[k] * x_acc[(i - k) * cols + j];
                }
            }
            y_acc[idx] /= a;
        });
    });

   
    q.wait();

 
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            y[i][j] = flat_y[i * cols + j];
        }
    }
    return y;
}
0 Kudos
Darius_Nicholas
New Contributor I
2,528 Views

This is my code!

#include <vector>
#include <iostream>
#include <CL/sycl.hpp> // DPC++ header

std::vector<std::vector<double>> filter2d(const std::vector<double>& b, double a, const std::vector<std::vector<double>>& x) {
int rows = x.size();
int cols = x[0].size();
std::vector<std::vector<double>> y(x.size(), std::vector<double>(x[0].size()));


sycl::queue q(sycl::gpu_selector{});


std::vector<double> flat_y(rows * cols);
std::vector<double> flat_x(rows * cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
flat_x[i * cols + j] = x[i][j];
}
}
sycl::range<1> num_items{rows* cols};

sycl::buffer b_buffer(b);
sycl::buffer y_buffer(flat_y.data(),num_items);
sycl::buffer x_buffer(flat_x);
//sycl::buffer<double, 2> x_buffer_1(x.data(), sycl::range<2>(rows, cols));
//sycl::buffer<double, 2> x_buffer((double*)x.data(), num_items);
//sycl::buffer<std::vector<double>, 2> x_buffer_2(x.data(), sycl::range<2>(rows, cols));

q.submit([&](sycl::handler& h) {
sycl::accessor x_acc(x_buffer,h,sycl::read_only);
sycl::accessor b_acc(b_buffer,h,sycl::read_only);

sycl::accessor y_acc(y_buffer, h, sycl::write_only,sycl::no_init);
/*auto b_acc = b_buffer.get_access<sycl::access::mode::read>(h);
auto x_acc = x_buffer.get_access<sycl::access::mode::read>(h);
auto y_acc = y_buffer.get_access<sycl::access::mode::write>(h);*/

h.parallel_for(num_items, [=](sycl::item<1> idx) {
int i = idx / cols;
int j = idx % cols;
y_acc[idx] = 0.0;
for (int k = 0; k < b.size(); k++) {
if (i - k >= 0) {
y_acc[idx] += b_acc[k] * x_acc[(i - k) * cols + j];
}
}
y_acc[idx] /= a;
});
});


q.wait();


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
y[i][j] = flat_y[i * cols + j];
}
}
return y;
}

0 Kudos
VaishnaviV_Intel
Employee
2,456 Views

Hi,

 

Thanks for sharing your code with us.

Please try to declare a separate variable(i.e. z) for b.size() outside parallel_for and replace b.size() in parallel_for with that variable (i.e. z).

Please find the below modified code,

#include <vector>
#include <iostream>
#include <CL/sycl.hpp> // DPC++ header

std::vector<std::vector<double>> filter2d(const std::vector<double>& b, double a, const std::vector<std::vector<double>>& x) {
int rows = x.size();
int cols = x[0].size();
std::vector<std::vector<double>> y(x.size(), std::vector<double>(x[0].size()));
auto z=b.size();

sycl::queue q(sycl::cpu_selector_v);


std::vector<double> flat_y(rows * cols);
std::vector<double> flat_x(rows * cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
flat_x[i * cols + j] = x[i][j];
}
}
sycl::range<1> num_items{static_cast<size_t>(rows * cols)};

sycl::buffer b_buffer(b);
sycl::buffer y_buffer(flat_y.data(),num_items);
sycl::buffer x_buffer(flat_x);
//sycl::buffer<double, 2> x_buffer_1(x.data(), sycl::range<2>(rows, cols));
//sycl::buffer<double, 2> x_buffer((double*)x.data(), num_items);
//sycl::buffer<std::vector<double>, 2> x_buffer_2(x.data(), sycl::range<2>(rows, cols));

q.submit([&](sycl::handler& h) {
sycl::accessor x_acc(x_buffer,h,sycl::read_only);
sycl::accessor b_acc(b_buffer,h,sycl::read_only);

sycl::accessor y_acc(y_buffer, h, sycl::write_only,sycl::no_init);
/*auto b_acc = b_buffer.get_access<sycl::access::mode::read>(h);
auto x_acc = x_buffer.get_access<sycl::access::mode::read>(h);
auto y_acc = y_buffer.get_access<sycl::access::mode::write>(h);*/

h.parallel_for(num_items, [=](sycl::item<1> idx) {
int i = idx / cols;
int j = idx % cols;
y_acc[idx] = 0.0;
for (int k = 0; k < z; k++) {
if (i - k >= 0) {
y_acc[idx] += b_acc[k] * x_acc[(i - k) * cols + j];
}
}
y_acc[idx] /= a;
});
});


q.wait();


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
y[i][j] = flat_y[i * cols + j];
}
}
return y;
}


int main() {
    // Example usage:
    std::vector<double> b = { 1.0, 2.0, 1.0 }; // Some filter coefficients
    double a = 1.0; // Some normalization factor
    std::vector<std::vector<double>> x = {
        { 1.0, 2.0, 3.0 },
        { 4.0, 5.0, 6.0 },
        { 7.0, 8.0, 9.0 }
    }; // Some 2D input data

    std::vector<std::vector<double>> y = filter2d(b, a, x);

    // Print the result
    for (const auto& row : y) {
        for (const auto& val : row) {
            std::cout << val << " ";
        }
        std::cout << "\n";
    }

    return 0;
}

Please let us know if you still face any issues.

 

Thanks & Regards,

Vankudothu Vaishnavi.

 

0 Kudos
Darius_Nicholas
New Contributor I
2,401 Views

The code has run successfully! Thanks a lot!

0 Kudos
VaishnaviV_Intel
Employee
2,392 Views

Hi,


Thanks for accepting our solution. If you need any additional information, please post a new question as this thread will no longer be monitored by Intel. 


Thanks & Regards,

Vankudothu Vaishnavi


0 Kudos
Reply