- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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:
Thanks & Regards,
Vankudothu Vaishnavi.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This my code !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The code has run successfully! Thanks a lot!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page