如何解决Sycl 部分+ DPCPP 中的互相关和误差
我尝试编写互相关函数。 在我的程序中,我编写了一个 Map 框架,它通过一些指定目标类型(cpu 或 GPU/加速器)的参数来包装 OneAPI 调用以隐藏硬件定位问题。 问题是,在 Sycl 部分,程序通过了一些错误而我无法解决它们。 我的代码:
<!-- language: c++ -->
//DeFinition of function which apply filter on matrices
template<class T>
T applyFilter(std::vector<std::vector<T>> f,std::vector<std::vector<T>> g) {
int n_rows = f.size();
int n_cols = f[0].size();
double sum = 0;
for (int i = 0; i < n_rows; i++) {
for (int j = 0; j < n_cols; j++) {
sum += f[i][j] * g[i][j];
}
}
return sum;
}
;
//function which print a specific part of my matrix
template<class T>
void print_matrix(std::vector<std::vector<T>> matrix) {
int m = matrix.size();
int n = matrix[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
std::cout << matrix[i][j] << ' ';
}
std::cout << "\n";
}
}
//Function which Slice a specific part of my matricx
template<class T>
std::vector<std::vector<T>> slice_matrix(std::vector<std::vector<T>> mat,int i,int j,int r,int c) {
std::vector<std::vector<T>> out(r,std::vector<T>(c,0));
for (int k = 0; k < r; k++) {
std::vector<T> temp(mat[i + k].begin() + j,mat[i + k].begin() + j + c);
out[k] = temp;
}
return out;
}
//Start to produce for my Matrix random numbers
template<class T>
void rand_fill_row(std::vector<T> &row) {
std::generate(row.begin(),row.end(),[]() {
return rand() % 100;
});
}
//A function that for each cell of my matrix execute to fill it with random numbers
template<class T>
void rand_fill_matrix(std::vector<std::vector<T>> &mat) {
for_each(mat.begin(),mat.end(),rand_fill_row<T>);
}
//DeFinition of Map Skeleton
template<class Tin,class Tout,class Function>
class Map {
private:
Function fun;
public:
Map() {
}
Map(Function f) :
fun(f) {
}
//Overriding () operator
std::vector<std::vector<Tout>> operator()(bool use_tbb,std::vector<std::vector<Tin>> &img,std::vector<std::vector<Tin>> &ker) {
int img_row = img.size();
int img_col = img[0].size();
int filt_row = ker.size();
int filt_col = ker[0].size();
int out_row = img_row - filt_row;
int out_col = img_col - filt_col;
std::vector<std::vector<Tout>> out;
if (use_tbb) {
uTimer *timer = new uTimer("Executing Code On cpu");
tbb::parallel_for(
tbb::blocked_range2d<int,int>(0,out_row,out_col),[&](tbb::blocked_range2d<int,int> &t) {
for (int n = t.rows().begin(); n < t.rows().end();
++n) {
for (int m = t.cols().begin(); m < t.cols().end();
++m) {
out[n][m] = fun(
slice_matrix(img,n,m,filt_row,filt_col),ker);
}
}
});
timer->~uTimer();
return out;
} else {
/* A 2D std::vector<std::vector<T>>
* does not have elements stored contiguously in the memory.
* Thus I define a vector<T> and operate on them as contiguous blocks.*/
//Define Buffer for
sycl::buffer<Tin,1> img_buffer(img.data(),img.size());
sycl::buffer<Tin,1> ker_buffer(ker.data(),ker.size());
sycl::buffer<Tin,2> out_buffer(out.data(),{ out_row,out_col });
//Profiling GPU
// Initialize property list with profiling information
sycl::property_list propList {
sycl::property::queue::enable_profiling() };
// Build the command queue (constructed to handle event profling)
sycl::queue gpuQueue = cl::sycl::queue(sycl::gpu_selector(),propList);
// print out the device information used for the kernel code
std::cout << "Device: "
<< gpuQueue.get_device().get_info<sycl::info::device::name>()
<< std::endl;
std::cout << "Compute Units: "
<< gpuQueue.get_device().get_info<
sycl::info::device::max_compute_units>()
<< std::endl;
auto start_overall = std::chrono::system_clock::Now();
auto event = gpuQueue.submit(
[&](sycl::handler &h) {
//local copy of fun
auto f = fun;
sycl::accessor img_accessor(img_buffer,h,sycl::read_only);
sycl::accessor ker_accessor(ker_buffer,sycl::read_only);
sycl::accessor out_accessor(out_buffer,sycl::write_only);
h.parallel_for(sycl::range<2> { out_row,out_col },[=](sycl::id<2> index) {
int row = index[0];
int col = index[1];
out_accessor[row][col] = f(slice_matrix(img_accessor,row,col,ker_accessor);
});
});
event.wait();
auto end_overall = std::chrono::system_clock::Now();
cl_ulong submit_time = event.template get_profiling_info<
cl::sycl::info::event_profiling::command_submit>();
cl_ulong start_time = event.template get_profiling_info<
cl::sycl::info::event_profiling::command_start>();
cl_ulong end_time = event.template get_profiling_info<
cl::sycl::info::event_profiling::command_end>();
auto submission_time = (start_time - submit_time) / 1000000.0f;
std::cout << "Submit Time: " << submission_time << " ms"
<< std::endl;
auto execution_time = (end_time - start_time) / 1000000.0f;
std::cout << "Execution Time: " << execution_time << " ms"
<< std::endl;
auto execution_overall = std::chrono::duration_cast<
std::chrono::milliseconds>(end_overall - start_overall);
std::cout << "Overall Execution Time: " << execution_overall.count()
<< " ms" << std::endl;
}
;
return out;
}
};
//The main part
template<class Tin,class Function>
Map<Tin,Tout,Function> make_map(Function f) {
return Map<Tin,Function>(f);
}
int main(int argc,char *argv[]) {
std::cout << "The Exutable File! " << argv[0] << std::endl;
std::cout << "The Device Is! " << argv[1] << std::endl;
std::cout << "The Fist Vector Size! " << argv[2] << std::endl;
std::cout << "The Second Vector Size! " << argv[3] << std::endl;
//The Device
std::string device = argv[1];
// Image's row count
int m = std::stoi(argv[2]);
// Image's col count
int n = std::stoi(argv[3]);
std::vector<std::vector<double>> img(m,std::vector<double>(n,0));
// Filter's row count
int k = std::stoi(argv[4]);
// Filter's row count
int l = std::stoi(argv[5]);
std::vector<std::vector<double>> ker(k,std::vector<double>(l,0));
//std::vector<std::vector<T>> out(r,0));
rand_fill_matrix(img);
rand_fill_matrix(ker);
/*Error is : no matching function for call to 'make_map'*/
<!-- language: lang-js -->
auto m1 = make_map<double,double>(applyFilter);
<!-- language: lang-js -->
std::vector<std::vector<double>> r = m1(true,img,ker);
//print the result
//for (auto &e : r) {
//std::cout << e << " ";
//}
return 0;
}
错误是:
没有匹配的构造函数用于初始化 'sycl::buffer
//Define Buffer for
sycl::buffer<Tin,1> img_buffer(&img[0],img.size());
sycl::buffer<Tin,1> ker_buffer(&ker[0],ker.size());
sycl::buffer<Tin,sycl::range<2>{ out_row,out_col });
================================================ ========
non-constant-expression cannot be narrowed from type 'int' to 'size_t' (aka 'unsigned long') in initializer list [-Wc++11-narrowing]
h.parallel_for(sycl::range<2> { out_row,[=](sycl::id<2> index) {
int row = index[0];
int col = index[1];
================================================ /p>
Invalid arguments '
Candidates are:
std::vector<std::vector<#0,std::allocator<#0>>,std::allocator<std::vector<#0,std::allocator<#0>>>> slice_matrix(std::vector<std::vector<#0,std::allocator<#0>>>>,int,int)
'
out_accessor[row][col] = f(slice_matrix(img_accessor,ker_accessor); }); });
================================================ ====
no matching function for call to 'make_map'
auto m1 = make_map<double,double>(applyFilter);
解决方法
通过匹配类型修复第一个错误 - 只需将 img_row、img_col、filt_row、filt_col、out)row 和 out_col 的声明更改为 size_t 而不是 int。
对于第二个错误 - 编译器是否也发出了有关问题的提示?我不得不根据你的 snipets 做出一些假设,但我最终得到了:
错误:没有匹配的函数调用'make_map' 注意:候选 模板被忽略:无法推断模板参数“函数”
这告诉我,我们需要添加的不仅仅是 Tin 和 Tout (
自动 m1 =
make_map
但这在我的代码模型中不太正确。 你应该尝试这样的事情。
如果您仍有问题 - 请提供完整的代码示例,我们可以尝试编译。 如果您解决了问题 - 请在此处发回您的发现,以便我们一起学习。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。