[IE CLDNN] Add optional memory access type (#7686)

This commit is contained in:
Roman Lyamin authored and GitHub committed 2021-09-30 10:32:07 +03:00
1 parent fbed7e13a8
commit 17091476d8
18 files changed
+177 -81

No files matched your search

@@ -18,13 +18,19 @@ namespace cldnn {
class engine;
class stream;
enum class mem_lock_type : int32_t {
read,
write,
read_write
};
struct memory {
using ptr = std::shared_ptr<memory>;
using cptr = std::shared_ptr<const memory>;
memory(engine* engine, const layout& layout, allocation_type type, bool reused = false);
virtual ~memory();
virtual void* lock(const stream& stream) = 0;
virtual void* lock(const stream& stream, mem_lock_type type = mem_lock_type::read_write) = 0;
virtual void unlock(const stream& stream) = 0;
virtual event::ptr fill(stream& stream, unsigned char pattern) = 0;
virtual event::ptr fill(stream& stream) = 0;
@@ -83,7 +89,7 @@ struct simple_attached_memory : memory {
simple_attached_memory(const layout& layout, void* pointer)
: memory(nullptr, layout, allocation_type::unknown), _pointer(pointer) {}
void* lock(const stream& /* stream */) override { return _pointer; }
void* lock(const stream& /* stream */, mem_lock_type /* type */) override { return _pointer; }
void unlock(const stream& /* stream */) override {}
event::ptr fill(stream& /* stream */, unsigned char) override { return nullptr; }
event::ptr fill(stream& /* stream */) override { return nullptr; }
@@ -102,9 +108,9 @@ private:
void* _pointer;
};
template <class T>
template <class T, mem_lock_type lock_type = mem_lock_type::read_write>
struct mem_lock {
explicit mem_lock(memory::ptr mem, const stream& stream) : _mem(mem), _stream(stream), _ptr(reinterpret_cast<T*>(_mem->lock(_stream))) {}
explicit mem_lock(memory::ptr mem, const stream& stream) : _mem(mem), _stream(stream), _ptr(reinterpret_cast<T*>(_mem->lock(_stream, lock_type))) {}
~mem_lock() {
_ptr = nullptr;
@@ -18,6 +18,19 @@
namespace cldnn {
namespace ocl {
static int get_cl_map_type(mem_lock_type type) {
switch (type) {
case mem_lock_type::read:
return CL_MAP_READ;
case mem_lock_type::write:
return CL_MAP_WRITE;
case mem_lock_type::read_write:
return CL_MAP_READ | CL_MAP_WRITE;
default:
throw std::runtime_error("Unsupported lock type for cl_memory buffer\n");
}
}
gpu_buffer::gpu_buffer(ocl_engine* engine,
const layout& layout)
: lockable_gpu_mem(), memory(engine, layout, allocation_type::cl_mem, false)
@@ -29,11 +42,11 @@ gpu_buffer::gpu_buffer(ocl_engine* engine,
: lockable_gpu_mem(), memory(engine, new_layout, allocation_type::cl_mem, true)
, _buffer(buffer) {}
void* gpu_buffer::lock(const stream& stream) {
void* gpu_buffer::lock(const stream& stream, mem_lock_type type) {
auto& cl_stream = downcast<const ocl_stream>(stream);
std::lock_guard<std::mutex> locker(_mutex);
if (0 == _lock_count) {
_mapped_ptr = cl_stream.get_cl_queue().enqueueMapBuffer(_buffer, CL_TRUE, CL_MAP_WRITE, 0, size());
_mapped_ptr = cl_stream.get_cl_queue().enqueueMapBuffer(_buffer, CL_TRUE, get_cl_map_type(type), 0, size());
}
_lock_count++;
return _mapped_ptr;
@@ -182,14 +195,14 @@ event::ptr gpu_image2d::fill(stream& stream, unsigned char pattern) {
return ev;
}
void* gpu_image2d::lock(const stream& stream) {
void* gpu_image2d::lock(const stream& stream, mem_lock_type type) {
auto& cl_stream = downcast<const ocl_stream>(stream);
std::lock_guard<std::mutex> locker(_mutex);
if (0 == _lock_count) {
_mapped_ptr = cl_stream.get_cl_queue()
.enqueueMapImage(_buffer,
CL_TRUE,
CL_MAP_WRITE,
get_cl_map_type(type),
{0, 0, 0},
{_width, _height, 1},
&_row_pitch,
@@ -286,7 +299,7 @@ gpu_usm::gpu_usm(ocl_engine* engine, const layout& layout, allocation_type type)
}
}
void* gpu_usm::lock(const stream& stream) {
void* gpu_usm::lock(const stream& stream, mem_lock_type /*type*/) {
assert(get_allocation_type() != allocation_type::usm_device && "Can't lock usm device memory!");
std::lock_guard<std::mutex> locker(_mutex);
if (0 == _lock_count) {
@@ -30,7 +30,7 @@ struct gpu_buffer : public lockable_gpu_mem, public memory {
gpu_buffer(ocl_engine* engine, const layout& new_layout, const cl::Buffer& buffer);
gpu_buffer(ocl_engine* engine, const layout& layout);
void* lock(const stream& stream) override;
void* lock(const stream& stream, mem_lock_type type = mem_lock_type::read_write) override;
void unlock(const stream& stream) override;
event::ptr fill(stream& stream, unsigned char pattern) override;
event::ptr fill(stream& stream) override;
@@ -54,7 +54,7 @@ struct gpu_image2d : public lockable_gpu_mem, public memory {
gpu_image2d(ocl_engine* engine, const layout& new_layout, const cl::Image2D& buffer);
gpu_image2d(ocl_engine* engine, const layout& layout);
void* lock(const stream& stream) override;
void* lock(const stream& stream, mem_lock_type type = mem_lock_type::read_write) override;
void unlock(const stream& stream) override;
event::ptr fill(stream& stream, unsigned char pattern) override;
event::ptr fill(stream& stream) override;
@@ -102,7 +102,7 @@ struct gpu_usm : public lockable_gpu_mem, public memory {
gpu_usm(ocl_engine* engine, const layout& new_layout, const cl::UsmMemory& usm_buffer, allocation_type type);
gpu_usm(ocl_engine* engine, const layout& layout, allocation_type type);
void* lock(const stream& stream) override;
void* lock(const stream& stream, mem_lock_type type = mem_lock_type::read_write) override;
void unlock(const stream& stream) override;
const cl::UsmMemory& get_buffer() const { return _buffer; }
cl::UsmMemory& get_buffer() { return _buffer; }
+2 -2
View File
@@ -25,8 +25,8 @@ memory::ptr attach_or_copy_data(network& network, memory::ptr mem) {
return mem;
memory::ptr result = engine.allocate_memory(mem->get_layout(), false);
mem_lock<char> src(mem, network.get_stream());
mem_lock<char> dst(result, network.get_stream());
mem_lock<char, mem_lock_type::read> src(mem, network.get_stream());
mem_lock<char, mem_lock_type::write> dst(result, network.get_stream());
std::copy(src.begin(), src.end(), dst.begin());
return result;
}
@@ -64,8 +64,8 @@ void shuffle_weights(data_node& node, const std::vector<shuffle_range>& ranges,
auto new_weights_memory = old_weights_memory->get_engine()->allocate_memory(wei_layout, old_weights_memory->get_allocation_type(), need_reset);
auto bytes_per_elem = data_type_traits::size_of(wei_layout.data_type);
mem_lock<uint8_t> old_weights_memory_lock{old_weights_memory, stream};
mem_lock<uint8_t> new_weights_memory_lock{new_weights_memory, stream};
mem_lock<uint8_t, mem_lock_type::read> old_weights_memory_lock{old_weights_memory, stream};
mem_lock<uint8_t, mem_lock_type::write> new_weights_memory_lock{new_weights_memory, stream};
auto old_ptr = old_weights_memory_lock.data();
auto new_ptr = new_weights_memory_lock.data();
for (int32_t ofi = 0; ofi < wei_layout.size.batch[0]; ++ofi) {
@@ -221,11 +221,11 @@ void pre_replace_deconv::run(program& p) {
std::vector<float> weights_vec_float;
if (weights_data_type == data_types::f16) {
mem_lock<half_t> src{ weights_node_ptr->as<data>().get_attached_memory_ptr(), stream };
mem_lock<half_t, mem_lock_type::read> src{ weights_node_ptr->as<data>().get_attached_memory_ptr(), stream };
for (uint32_t i = 0; i < weights_layout.size.count(); i++)
weights_vec_float.push_back(static_cast<float>(src.data()[i]));
} else {
mem_lock<float> src{ weights_node_ptr->as<data>().get_attached_memory_ptr(), stream };
mem_lock<float, mem_lock_type::read> src{ weights_node_ptr->as<data>().get_attached_memory_ptr(), stream };
for (uint32_t i = 0; i < weights_layout.size.count(); i++)
weights_vec_float.push_back(src.data()[i]);
}
@@ -240,10 +240,10 @@ void pre_replace_deconv::run(program& p) {
subpixel_weights);
if (weights_data_type == data_types::f16) {
mem_lock<half_t> dst{ data_to_allocate, stream};
mem_lock<half_t, mem_lock_type::write> dst{ data_to_allocate, stream};
program_helpers::set_weights_values<half_t>(dst.data(), subpixel_weights);
} else if (weights_data_type == data_types::f32) {
mem_lock<float> dst{ data_to_allocate, stream };
mem_lock<float, mem_lock_type::write> dst{ data_to_allocate, stream };
program_helpers::set_weights_values<float>(dst.data(), subpixel_weights);
} else {
throw std::logic_error("Not supported data type.");
@@ -283,10 +283,10 @@ void pre_replace_deconv::run(program& p) {
float bias = 0;
if (bias_data_type == data_types::f16) {
mem_lock<half_t> src{ bias_id_node_ptr->as<data>().get_attached_memory_ptr(), stream };
mem_lock<half_t, mem_lock_type::read> src{ bias_id_node_ptr->as<data>().get_attached_memory_ptr(), stream };
bias = static_cast<float>(src.data()[0]);
} else {
mem_lock<float> src{ bias_id_node_ptr->as<data>().get_attached_memory_ptr(), stream };
mem_lock<float, mem_lock_type::read> src{ bias_id_node_ptr->as<data>().get_attached_memory_ptr(), stream };
bias = src.data()[0];
}
auto pixel_shuffle_prim = std::make_shared<depth_to_space>(deconv_node_id, deconv_id_conv, 2, depth_to_space_mode::blocks_first);
@@ -24,8 +24,8 @@ template<typename T>
bool check_binarization(memory::ptr mem_input_low, memory::ptr mem_input_high, program& p) {
bool is_binarization = true;
const auto& stream = p.get_stream();
mem_lock<T> data_input_low_lock{mem_input_low, stream};
mem_lock<T> data_input_high_lock{mem_input_high, stream};
mem_lock<T, mem_lock_type::read> data_input_low_lock{mem_input_low, stream};
mem_lock<T, mem_lock_type::read> data_input_high_lock{mem_input_high, stream};
auto data_input_low = data_input_low_lock.data();
auto data_input_high = data_input_high_lock.data();
const size_t number_mem_layout_elements = mem_input_high->get_layout().count();
@@ -85,7 +85,7 @@ void prepare_quantization::prepare_scale_shift_opt(program &p, quantize_node& q
std::function<float(size_t)>& get_data) {
switch (memory->get_layout().data_type) {
case data_types::f32: {
std::shared_ptr<mem_lock<float>> data_lock_ptr = std::make_shared<mem_lock<float>>(memory, stream);
std::shared_ptr<mem_lock<float, mem_lock_type::write>> data_lock_ptr = std::make_shared<mem_lock<float, mem_lock_type::write>>(memory, stream);
float* data = data_lock_ptr->data();
set_data = [data] (size_t idx, float value) {
data[idx] = value;
@@ -93,10 +93,11 @@ void prepare_quantization::prepare_scale_shift_opt(program &p, quantize_node& q
get_data = [data] (size_t idx) {
return data[idx];
};
return std::pair<std::shared_ptr<mem_lock<float>>, std::shared_ptr<mem_lock<uint16_t>>>(data_lock_ptr, nullptr);
return std::pair<std::shared_ptr<mem_lock<float, mem_lock_type::write>>,
std::shared_ptr<mem_lock<uint16_t, mem_lock_type::write>>>(data_lock_ptr, nullptr);
}
case data_types::f16: {
std::shared_ptr<mem_lock<uint16_t>> data_lock_ptr = std::make_shared<mem_lock<uint16_t>>(memory, stream);
auto data_lock_ptr = std::make_shared<mem_lock<uint16_t, mem_lock_type::write>>(memory, stream);
uint16_t* data = data_lock_ptr->data();
set_data = [data] (size_t idx, float value) {
data[idx] = float_to_half(value);
@@ -104,7 +105,8 @@ void prepare_quantization::prepare_scale_shift_opt(program &p, quantize_node& q
get_data = [data] (size_t idx) {
return half_to_float(data[idx]);
};
return std::pair<std::shared_ptr<mem_lock<float>>, std::shared_ptr<mem_lock<uint16_t>>>(nullptr, data_lock_ptr);
return std::pair<std::shared_ptr<mem_lock<float, mem_lock_type::write>>,
std::shared_ptr<mem_lock<uint16_t, mem_lock_type::write>>>(nullptr, data_lock_ptr);
}
default:
throw std::runtime_error("prepare_quantization: Unsupported precision of quantize output values");
@@ -378,8 +380,8 @@ void prepare_quantization::prepare_dequantize_merge(program& p, eltwise_node& el
auto mem0 = get_scale_shift_mem(eltwise_dep, i);
auto mem1 = get_scale_shift_mem(eltwise_node, i);
mem_lock<uint8_t> mem0_lock{mem0, stream};
mem_lock<uint8_t> mem1_lock{mem1, stream};
mem_lock<uint8_t, mem_lock_type::read> mem0_lock{mem0, stream};
mem_lock<uint8_t, mem_lock_type::read> mem1_lock{mem1, stream};
auto ptr0 = mem0_lock.data();
auto ptr1 = mem1_lock.data();
@@ -487,40 +489,40 @@ void prepare_quantization::prepare_asymmetric_quantization(program &p, convoluti
const auto& w_dt = wl.data_type;
const auto& azp_dt = azp->get_layout().data_type;
mem_lock<float> comp_lock{compensation, stream};
mem_lock<float, mem_lock_type::write> comp_lock{compensation, stream};
if (w_dt == data_types::u8 && azp_dt == data_types::u8) {
mem_lock<uint8_t> w_lock(w, stream);
mem_lock<uint8_t> azp_lock(azp, stream);
mem_lock<uint8_t, mem_lock_type::read> w_lock(w, stream);
mem_lock<uint8_t, mem_lock_type::read> azp_lock(azp, stream);
if (wzp) {
mem_lock<uint8_t> wzp_lock(wzp, stream);
mem_lock<uint8_t, mem_lock_type::read> wzp_lock(wzp, stream);
fill_compensation_typed(w_lock.data(), azp_lock.data(), wzp_lock.data(), comp_lock.data(), GS, OC, IC, KS);
} else {
fill_compensation_typed(w_lock.data(), azp_lock.data(), static_cast<uint8_t*>(nullptr), comp_lock.data(), GS, OC, IC, KS);
}
} else if (w_dt == data_types::i8 && azp_dt == data_types::u8) {
mem_lock<int8_t> w_lock(w, stream);
mem_lock<uint8_t> azp_lock(azp, stream);
mem_lock<int8_t, mem_lock_type::read> w_lock(w, stream);
mem_lock<uint8_t, mem_lock_type::read> azp_lock(azp, stream);
if (wzp) {
mem_lock<int8_t> wzp_lock(wzp, stream);
mem_lock<int8_t, mem_lock_type::read> wzp_lock(wzp, stream);
fill_compensation_typed(w_lock.data(), azp_lock.data(), wzp_lock.data(), comp_lock.data(), GS, OC, IC, KS);
} else {
fill_compensation_typed(w_lock.data(), azp_lock.data(), static_cast<int8_t*>(nullptr), comp_lock.data(), GS, OC, IC, KS);
}
} else if (w_dt == data_types::i8 && azp_dt == data_types::i8) {
mem_lock<int8_t> w_lock(w, stream);
mem_lock<int8_t> azp_lock(azp, stream);
mem_lock<int8_t, mem_lock_type::read> w_lock(w, stream);
mem_lock<int8_t, mem_lock_type::read> azp_lock(azp, stream);
if (wzp) {
mem_lock<int8_t> wzp_lock(wzp, stream);
mem_lock<int8_t, mem_lock_type::read> wzp_lock(wzp, stream);
fill_compensation_typed(w_lock.data(), azp_lock.data(), wzp_lock.data(), comp_lock.data(), GS, OC, IC, KS);
} else {
fill_compensation_typed(w_lock.data(), azp_lock.data(), static_cast<int8_t*>(nullptr), comp_lock.data(), GS, OC, IC, KS);
}
} else if (w_dt == data_types::u8 && azp_dt == data_types::i8) {
mem_lock<uint8_t> w_lock(w, stream);
mem_lock<int8_t> azp_lock(azp, stream);
mem_lock<uint8_t, mem_lock_type::read> w_lock(w, stream);
mem_lock<int8_t, mem_lock_type::read> azp_lock(azp, stream);
if (wzp) {
mem_lock<uint8_t> wzp_lock(wzp, stream);
mem_lock<uint8_t, mem_lock_type::read> wzp_lock(wzp, stream);
fill_compensation_typed(w_lock.data(), azp_lock.data(), wzp_lock.data(), comp_lock.data(), GS, OC, IC, KS);
} else {
fill_compensation_typed(w_lock.data(), azp_lock.data(), static_cast<uint8_t*>(nullptr), comp_lock.data(), GS, OC, IC, KS);
@@ -579,8 +581,8 @@ void prepare_quantization::prepare_asymmetric_quantization(program &p, convoluti
int s = new_a_zp->get_output_layout().size.feature[0];
auto azp_aligned = p.get_engine().allocate_memory(l);
auto old_ptr = new_a_zp->as<data>().get_attached_memory_ptr();
mem_lock<int8_t> new_data{azp_aligned, stream};
mem_lock<int8_t> old_data{old_ptr, stream};
mem_lock<int8_t, mem_lock_type::write> new_data{azp_aligned, stream};
mem_lock<int8_t, mem_lock_type::read> old_data{old_ptr, stream};
for (int i = 0; i < ifm_aligned; i++) {
new_data.data()[i] = old_data.data()[i % s];
}
@@ -602,8 +604,8 @@ void prepare_quantization::prepare_asymmetric_quantization(program &p, convoluti
int s = new_w_zp->get_output_layout().size.batch[0];
auto wzp_aligned = p.get_engine().allocate_memory(l);
auto old_ptr = new_w_zp->as<data>().get_attached_memory_ptr();
mem_lock<int8_t> new_data{wzp_aligned, stream};
mem_lock<int8_t> old_data{old_ptr, stream};
mem_lock<int8_t, mem_lock_type::write> new_data{wzp_aligned, stream};
mem_lock<int8_t, mem_lock_type::read> old_data{old_ptr, stream};
for (int i = 0; i < ofm_aligned; i++) {
new_data.data()[i] = old_data.data()[i % s];
}
@@ -34,8 +34,8 @@ struct condition_impl : typed_primitive_impl<condition> {
else
memory_to_copy = execute_branch(instance.get_net_false(), instance.result_id(), instance.input_memory_ptr());
// just copy memory
mem_lock<float> inp_ptr{memory_to_copy, instance.get_network().get_stream()};
mem_lock<float> out_ptr{instance.output_memory_ptr(), instance.get_network().get_stream()};
mem_lock<float, mem_lock_type::read> inp_ptr{memory_to_copy, instance.get_network().get_stream()};
mem_lock<float, mem_lock_type::write> out_ptr{instance.output_memory_ptr(), instance.get_network().get_stream()};
std::copy(inp_ptr.begin(), inp_ptr.end(), out_ptr.begin());
ev->set();
return ev;
@@ -71,11 +71,11 @@ private:
Returns boolean flag, which says what branch should be executed.
*/
bool choose_branch_to_exec(condition_inst& instance) const {
mem_lock<float> lock_compare_data{instance.compare_memory_ptr(), instance.get_network().get_stream()};
mem_lock<float, mem_lock_type::read> lock_compare_data{instance.compare_memory_ptr(), instance.get_network().get_stream()};
auto compare_layout = instance.compare_memory().get_layout();
auto compare_ptr = lock_compare_data.begin();
mem_lock<float> lock_input{instance.input_memory_ptr(), instance.get_network().get_stream()};
mem_lock<float, mem_lock_type::read> lock_input{instance.input_memory_ptr(), instance.get_network().get_stream()};
auto input_layout = instance.input_memory().get_layout();
auto input_ptr = lock_input.begin();
@@ -263,7 +263,7 @@ struct detection_output_impl : typed_primitive_impl<detection_output> {
const std::vector<std::vector<std::vector<bounding_box>>>& all_bboxes,
std::vector<std::vector<std::vector<std::pair<float, int>>>>& confidences,
std::vector<std::vector<std::pair<float, std::pair<int, int>>>>& scoreIndexPairs) {
mem_lock<dtype> lock{instance.output_memory_ptr(), stream};
mem_lock<dtype, mem_lock_type::write> lock{instance.output_memory_ptr(), stream};
auto out_ptr = lock.begin();
const auto& args = instance.argument;
@@ -423,7 +423,7 @@ struct detection_output_impl : typed_primitive_impl<detection_output> {
auto input_location = instance.location_memory();
auto location_layout = input_location->get_layout();
const int num_of_images = static_cast<int>(locations.size());
mem_lock<dtype> lock{input_location, stream};
mem_lock<dtype, mem_lock_type::read> lock{input_location, stream};
auto location_data = lock.begin();
assert(num_of_priors * num_loc_classes * PRIOR_BOX_SIZE == input_location->get_layout().size.feature[0]);
@@ -487,7 +487,7 @@ struct detection_output_impl : typed_primitive_impl<detection_output> {
std::vector<std::array<float, PRIOR_BOX_SIZE>>& prior_variances) {
auto input_prior_box = instance.prior_box_memory();
const int num_of_priors = static_cast<int>(prior_bboxes.size()) / images_count;
mem_lock<dtype> lock{input_prior_box, stream};
mem_lock<dtype, mem_lock_type::read> lock{input_prior_box, stream};
for (int i = 0; i < images_count; i++) {
auto prior_box_data =
lock.begin() + i * num_of_priors * prior_info_size * (variance_encoded_in_target ? 1 : 2);
@@ -523,7 +523,7 @@ struct detection_output_impl : typed_primitive_impl<detection_output> {
auto input_confidence = instance.confidence_memory();
const float confidence_threshold = instance.argument.confidence_threshold;
mem_lock<dtype> lock{input_confidence, stream};
mem_lock<dtype, mem_lock_type::read> lock{input_confidence, stream};
auto confidence_data = lock.begin();
assert(num_of_priors * num_classes == input_confidence->get_layout().size.feature[0]);
@@ -615,7 +615,7 @@ struct detection_output_impl : typed_primitive_impl<detection_output> {
const float confidence_threshold = instance.argument.confidence_threshold;
auto confidence_layout = input_confidence->get_layout();
mem_lock<dtype> lock{input_confidence, stream};
mem_lock<dtype, mem_lock_type::read> lock{input_confidence, stream};
auto confidence_data = lock.begin();
assert(num_of_priors * num_classes == confidence_layout.size.feature[0]);
@@ -117,7 +117,7 @@ vector2D<bounding_box> load_boxes_impl(stream& stream, memory::ptr mem, bool cen
auto boxes_num = lay.size.feature[0];
result.resize(batch_size);
mem_lock<T> boxes_lock(mem, stream);
mem_lock<T, mem_lock_type::read> boxes_lock(mem, stream);
auto ptr = boxes_lock.data();
for (int bi = 0; bi < batch_size; ++bi) {
@@ -166,7 +166,7 @@ vector3D<float> load_scores_impl(stream& stream, memory::ptr mem) {
vector3D<float> result(batch_size, vector2D<float>(classes_num));
mem_lock<T> lock(mem, stream);
mem_lock<T, mem_lock_type::read> lock(mem, stream);
auto ptr = lock.data();
for (int bi = 0; bi < batch_size; ++bi) {
@@ -196,7 +196,7 @@ vector3D<float> load_scores(stream& stream, memory::ptr mem) {
template <typename T, typename MemT>
T load_scalar_impl(stream& stream, memory::ptr mem) {
mem_lock<MemT> lock(mem, stream);
mem_lock<MemT, mem_lock_type::read> lock(mem, stream);
auto ptr = lock.data();
return static_cast<T>(ptr[0]);
@@ -219,7 +219,7 @@ T load_scalar(stream& stream, memory::ptr mem) {
template <typename T>
void store_result_impl(stream& stream, memory::ptr mem, const std::vector<result_indices>& result) {
mem_lock<T> lock(mem, stream);
mem_lock<T, mem_lock_type::write> lock(mem, stream);
auto ptr = lock.data();
auto output_size = static_cast<size_t>(mem->get_layout().size.batch[0]);
@@ -273,7 +273,7 @@ void store_first_output(stream& stream, memory::ptr mem, const std::vector<resul
template <typename T>
void store_second_output_impl(stream& stream, memory::ptr mem, const std::vector<result_indices>& result) {
mem_lock<T> lock(mem, stream);
mem_lock<T, mem_lock_type::write> lock(mem, stream);
auto ptr = lock.data();
auto output_size = static_cast<size_t>(mem->get_layout().size.batch[0]);
@@ -310,7 +310,7 @@ void store_second_output(stream& stream, memory::ptr mem, const std::vector<resu
template <typename T>
void store_third_output_impl(stream& stream, memory::ptr mem, const std::vector<result_indices>& result) {
mem_lock<T> lock(mem, stream);
mem_lock<T, mem_lock_type::write> lock(mem, stream);
auto ptr = lock.data();
ptr[0] = static_cast<T>(result.size());
}
@@ -201,7 +201,7 @@ struct proposal_impl : typed_primitive_impl<proposal> {
template <typename dtype>
void read_image_info(stream& stream, proposal_inst& instance, im_info_t& im_info) {
auto image_info = instance.dep_memory_ptr(proposal_inst::image_info_index);
mem_lock<dtype> image_info_ptr{image_info, stream};
mem_lock<dtype, mem_lock_type::read> image_info_ptr{image_info, stream};
const dtype* image_info_mem = image_info_ptr.data();
bool swap_xy = instance.argument.swap_xy;
@@ -284,8 +284,8 @@ struct proposal_impl : typed_primitive_impl<proposal> {
int fm_sz = fm_w * fm_h;
mem_lock<dtype> cls_scores_ptr{cls_scores, stream};
mem_lock<dtype> bbox_pred_ptr{bbox_pred, stream};
mem_lock<dtype, mem_lock_type::read> cls_scores_ptr{cls_scores, stream};
mem_lock<dtype, mem_lock_type::read> bbox_pred_ptr{bbox_pred, stream};
const dtype* cls_scores_mem = cls_scores_ptr.data();
const dtype* bbox_pred_mem = bbox_pred_ptr.data();
@@ -350,7 +350,7 @@ struct proposal_impl : typed_primitive_impl<proposal> {
auto output = instance.output_memory_ptr();
mem_lock<dtype> output_ptr{output, stream};
mem_lock<dtype, mem_lock_type::write> output_ptr{output, stream};
dtype* top_data = output_ptr.data() + n * instance.argument.post_nms_topn * 5;
dtype* top_data_prob = proposal_prob_ptr == nullptr ? nullptr : proposal_prob_ptr + n * instance.argument.post_nms_topn;
@@ -409,10 +409,10 @@ struct proposal_impl : typed_primitive_impl<proposal> {
if (instance.dependencies().size() == 4) {
auto proposal_probabilities = instance.dep_memory_ptr(proposal_inst::proposal_probabilities_out);
if (instance.dep_memory(proposal_inst::cls_scores_index).get_layout().data_type == data_types::f16) {
mem_lock<data_type_to_type<data_types::f16>::type> proposal_prob_ptr{proposal_probabilities, stream};
mem_lock<data_type_to_type<data_types::f16>::type, mem_lock_type::read> proposal_prob_ptr{proposal_probabilities, stream};
execute<data_type_to_type<data_types::f16>::type>(stream, instance, im_info, proposal_prob_ptr.data());
} else {
mem_lock<data_type_to_type<data_types::f32>::type> proposal_prob_ptr{proposal_probabilities, stream};
mem_lock<data_type_to_type<data_types::f32>::type, mem_lock_type::read> proposal_prob_ptr{proposal_probabilities, stream};
execute<data_type_to_type<data_types::f32>::type>(stream, instance, im_info, proposal_prob_ptr.data());
}
} else {
@@ -91,8 +91,8 @@ struct generic_layer_cpu : typed_primitive_impl<generic_layer> {
a->wait();
}
mem_lock<uint8_t> old_pointer(input_mem, stream);
mem_lock<uint8_t> new_pointer(output_mem, stream);
mem_lock<uint8_t, mem_lock_type::read> old_pointer(input_mem, stream);
mem_lock<uint8_t, mem_lock_type::write> new_pointer(output_mem, stream);
const auto& cpu_kernel = *outer.get_primitive()->generic_params.cpuKernel.get();
@@ -21,8 +21,8 @@ cldnn::memory::ptr convert_zp_data_to_s32(const memory::ptr zp_memory) {
zp_s32_layout.data_type = data_types::i32;
auto zp_s32_memory = engine->allocate_memory(zp_s32_layout, false);
mem_lock<T> zp_data(zp_memory, stream);
mem_lock<int32_t> zp_s32_data(zp_s32_memory, stream);
mem_lock<T, mem_lock_type::read> zp_data(zp_memory, stream);
mem_lock<int32_t, mem_lock_type::write> zp_s32_data(zp_s32_memory, stream);
for (size_t i = 0; i < zp_data.size(); i++) {
zp_s32_data.data()[i] = static_cast<int32_t>(zp_data.data()[i]);
}
+2 -2
View File
@@ -48,8 +48,8 @@ void input_layout_inst::set_data(memory::ptr mem) {
if (mem->is_allocated_by(get_network().get_engine())) {
_output = mem;
} else {
mem_lock<char> src(mem, get_network().get_stream());
mem_lock<char> dst(_output, get_network().get_stream());
mem_lock<char, mem_lock_type::read> src(mem, get_network().get_stream());
mem_lock<char, mem_lock_type::write> dst(_output, get_network().get_stream());
std::copy(src.begin(), src.end(), dst.begin());
}
+2 -2
View File
@@ -29,8 +29,8 @@ memory::ptr attach_or_copy_data(network& network, memory::ptr mem, bool reuse) {
}
memory::ptr result = engine.allocate_memory(mem->get_layout(), false);
mem_lock<char> src(mem, stream);
mem_lock<char> dst(result, stream);
mem_lock<char, mem_lock_type::read> src(mem, stream);
mem_lock<char, mem_lock_type::write> dst(result, stream);
std::copy(src.begin(), src.end(), dst.begin());
return result;
+2 -2
View File
@@ -112,7 +112,7 @@ static void dump(memory::ptr mem, stream& stream, std::ofstream& file_stream) {
file_stream << "shape: " << size.to_string() << " ";
file_stream << "(count: " << size.count() << ", original format: " << cldnn::fmt_to_str(mem->get_layout().format) << ")" << std::endl;
mem_lock<T> lock(mem, stream);
mem_lock<T, mem_lock_type::read> lock(mem, stream);
auto mem_ptr = lock.data();
auto x_pitch = get_x_pitch(mem->get_layout());
std::stringstream buffer;
@@ -148,7 +148,7 @@ void dump<uint32_t>(memory::ptr mem, stream& stream, std::ofstream& file_stream)
file_stream << size.spatial[0] << " ";
file_stream << "(" << size.batch[0] * size.feature[0] * size.spatial[1] * size.spatial[0] << ")" << std::endl;
mem_lock<uint32_t> lock(mem, stream);
mem_lock<uint32_t, mem_lock_type::read> lock(mem, stream);
auto mem_ptr = lock.data();
for (cldnn::tensor::value_type b = 0; b < size.batch[0]; ++b) {
+2 -2
View File
@@ -23,8 +23,8 @@ void program_helpers::merge_buffers(engine& engine,
for (size_t i = begin_offset; i < end_offset; i++) {
auto& weights = node.get_dependency(i).as<data>();
mem_lock<char> src{weights.get_attached_memory_ptr(), stream};
mem_lock<char> dst{data_to_allocate, stream};
mem_lock<char, mem_lock_type::read> src{weights.get_attached_memory_ptr(), stream};
mem_lock<char, mem_lock_type::write> dst{data_to_allocate, stream};
std::copy(src.begin(), src.end(), dst.begin() + (i - begin_offset) * src.size());
}
@@ -9,7 +9,6 @@
#include <cldnn/primitives/data.hpp>
#include <cldnn/runtime/device_query.hpp>
#include <ocl/ocl_wrapper.hpp>
using namespace cldnn;
@@ -304,3 +303,79 @@ TEST(cl_mem_check, check_input) {
}
checkStatus(clReleaseMemObject(img), "clReleaseMemObject");
}
TEST(cl_mem_check, check_write_access_type) {
device_query query(engine_types::ocl, runtime_types::ocl);
auto devices = query.get_available_devices();
cldnn::device::ptr device = devices.begin()->second;
for (auto& dev : devices) {
if (dev.second->get_info().dev_type == device_type::discrete_gpu)
device = dev.second;
}
auto engine = engine::create(engine_types::ocl, runtime_types::ocl, device);
auto stream = engine->create_stream();
size_t values_count = 100;
size_t values_bytes_count = values_count * sizeof(float);
std::vector<float> src_buffer(values_count);
std::iota(src_buffer.begin(), src_buffer.end(), 0.0f);
cldnn::layout linear_layout = cldnn::layout(cldnn::data_types::f32, cldnn::format::bfyx, cldnn::tensor(1, 1, int32_t(values_count), 1));
auto cldnn_mem_src = engine->allocate_memory(linear_layout, cldnn::allocation_type::cl_mem);
{
cldnn::mem_lock<float, cldnn::mem_lock_type::write> lock(cldnn_mem_src, *stream);
std::copy(src_buffer.begin(), src_buffer.end(), lock.data());
}
std::vector<float> dst_buffer(values_count);
{
cldnn::mem_lock<float, cldnn::mem_lock_type::read> lock(cldnn_mem_src, *stream);
std::memcpy(dst_buffer.data(), lock.data(), values_bytes_count);
}
bool are_equal = std::equal(src_buffer.begin(), src_buffer.begin() + values_count, dst_buffer.begin());
ASSERT_TRUE(are_equal);
}
TEST(cl_mem_check, check_read_access_type) {
device_query query(engine_types::ocl, runtime_types::ocl);
auto devices = query.get_available_devices();
cldnn::device::ptr device = devices.begin()->second;
for (auto& dev : devices) {
if (dev.second->get_info().dev_type == device_type::discrete_gpu)
device = dev.second;
}
if (device->get_info().dev_type == device_type::integrated_gpu) {
GTEST_SKIP();
}
auto engine = engine::create(engine_types::ocl, runtime_types::ocl, device);
auto stream = engine->create_stream();
size_t values_count = 100;
size_t values_bytes_count = values_count * sizeof(float);
std::vector<float> src_buffer(values_count);
std::iota(src_buffer.begin(), src_buffer.end(), 0.0f);
cldnn::layout linear_layout = cldnn::layout(cldnn::data_types::f32, cldnn::format::bfyx, cldnn::tensor(1, 1, int32_t(values_count), 1));
auto cldnn_mem_src = engine->allocate_memory(linear_layout, cldnn::allocation_type::cl_mem);
{
cldnn::mem_lock<float, cldnn::mem_lock_type::write> lock(cldnn_mem_src, *stream);
std::copy(src_buffer.begin(), src_buffer.end(), lock.data());
}
{
cldnn::mem_lock<float, cldnn::mem_lock_type::read> lock(cldnn_mem_src, *stream);
std::copy(src_buffer.rbegin(), src_buffer.rend(), lock.data());
}
std::vector<float> dst_buffer(values_count);
{
cldnn::mem_lock<float, cldnn::mem_lock_type::read> lock(cldnn_mem_src, *stream);
std::memcpy(dst_buffer.data(), lock.data(), values_bytes_count);
}
bool are_equal = std::equal(src_buffer.begin(), src_buffer.begin() + values_count, dst_buffer.begin());
ASSERT_TRUE(are_equal);
}