I try to write 2D Cross correlation in Sycl and OneAPI. The idea is to write a kind of Map skeleton which wraps OneAPI calls hiding hardware targeting issues through some parameter specifying the kind of target (CPU or GPU/Accelerator).
this is my Map Class:
//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, 0, 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 {
/*change 2D Matrices to the 1D linear arrays,
*
*and operate on them as contiguous blocks */
size_t M = img_row + img_col;
size_t N = filt_row + filt_col;
//size_t O = out_row + out_col;
size_t O_row = out_row;
size_t O_col = out_col;
std::vector<Tin> img_host;
std::vector<Tin> ker_host;
std::vector<Tout> out_gpu;
/* 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_host.data(), M);
sycl::buffer<Tin, 1> ker_buffer(ker_host.data(), N);
sycl::buffer<Tin, 2> out_buffer(out_gpu.data(), sycl::range<2> {
O_row, O_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, h,
sycl::read_only);
sycl::accessor out_accessor(out_buffer, h,
sycl::write_only);
h.parallel_for(sycl::range<2> { O_row, O_col },
[=](sycl::id<2> index) {
int row = index[0];
int col = index[1];
out_accessor[row][col] = f(
slice_matrix(img_accessor, O_row,
O_col, filt_row, filt_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;
}
};
And this is my slice_matrix:
//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;
}
;
The problem is that, in Sycl part inside parallel-for
out_accessor[row][col] = f(
slice_matrix(img_accessor, O_row,
O_col, filt_row, filt_col),
ker_accessor);
});
the program shows me an error which is:
no matching function for call to 'slice_matrix'
I tried to put my slice_matrix inside the Map Class, but nothing changed. Also I thought about limitation of Sycl about " SYCL device code, as defined by this specification, does not support virtual function calls ", so I defined a local copy of slice_matrix, but again I had an error.
I cannot understand how to resolve this error.
You are passing a sycl::accessor type to slice_matrix, but the signature of slice_matrix is:
//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)
So the signature does not match...
You would need a version of slice_matrix that takes an accessor object instead of your vector.