mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Replace memset with fill to work for non-trivial scalars.
For custom scalars, zero is not necessarily represented by a zeroed-out memory block (e.g. gnu MPFR). We therefore cannot rely on `memset` if we want to fill a matrix or tensor with zeroes. Instead, we should rely on `fill`, which for trivial types does end up getting converted to a `memset` under-the-hood (at least with gcc/clang). Requires adding a `fill(begin, end, v)` to `TensorDevice`. Replaced all potentially bad instances of memset with fill. Fixes #2245.
This commit is contained in:
committed by
Rasmus Munk Larsen
parent
e9c9a3130b
commit
1e6c6c1576
@@ -25,10 +25,8 @@ static void test_1d()
|
||||
vec1(4) = 23; vec2(4) = 4;
|
||||
vec1(5) = 42; vec2(5) = 5;
|
||||
|
||||
int col_major[6];
|
||||
int row_major[6];
|
||||
memset(col_major, 0, 6*sizeof(int));
|
||||
memset(row_major, 0, 6*sizeof(int));
|
||||
int col_major[6] = {0};
|
||||
int row_major[6] = {0};
|
||||
TensorMap<Tensor<int, 1> > vec3(col_major, 6);
|
||||
TensorMap<Tensor<int, 1, RowMajor> > vec4(row_major, 6);
|
||||
|
||||
@@ -88,10 +86,8 @@ static void test_2d()
|
||||
mat2(1,1) = 4;
|
||||
mat2(1,2) = 5;
|
||||
|
||||
int col_major[6];
|
||||
int row_major[6];
|
||||
memset(col_major, 0, 6*sizeof(int));
|
||||
memset(row_major, 0, 6*sizeof(int));
|
||||
int col_major[6] = {0};
|
||||
int row_major[6] = {0};
|
||||
TensorMap<Tensor<int, 2> > mat3(row_major, 2, 3);
|
||||
TensorMap<Tensor<int, 2, RowMajor> > mat4(col_major, 2, 3);
|
||||
|
||||
@@ -148,10 +144,8 @@ static void test_3d()
|
||||
}
|
||||
}
|
||||
|
||||
int col_major[2*3*7];
|
||||
int row_major[2*3*7];
|
||||
memset(col_major, 0, 2*3*7*sizeof(int));
|
||||
memset(row_major, 0, 2*3*7*sizeof(int));
|
||||
int col_major[2*3*7] = {0};
|
||||
int row_major[2*3*7] = {0};
|
||||
TensorMap<Tensor<int, 3> > mat3(col_major, 2, 3, 7);
|
||||
TensorMap<Tensor<int, 3, RowMajor> > mat4(row_major, 2, 3, 7);
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#define EIGEN_USE_GPU
|
||||
|
||||
#include "main.h"
|
||||
#include "OffByOneScalar.h"
|
||||
#include <unsupported/Eigen/CXX11/Tensor>
|
||||
|
||||
#include <unsupported/Eigen/CXX11/src/Tensor/TensorGpuHipCudaDefines.h>
|
||||
@@ -175,6 +176,44 @@ void test_3d_convolution(Context* context)
|
||||
context->out().slice(indices, sizes).device(context->device()) = context->in1().convolve(context->kernel3d(), dims);
|
||||
}
|
||||
|
||||
// Helper method to synchronize device.
|
||||
template<typename Device>
|
||||
void synchronize(Device& device) { /*nothing*/ }
|
||||
template<>
|
||||
void synchronize(Eigen::GpuDevice& device) {
|
||||
device.synchronize();
|
||||
}
|
||||
|
||||
template <typename DataType, typename TensorDevice>
|
||||
void test_device_memory(const TensorDevice& device) {
|
||||
int count = 100;
|
||||
Eigen::array<int, 1> tensorRange = {{count}};
|
||||
Eigen::Tensor<DataType, 1> host(tensorRange);
|
||||
Eigen::Tensor<DataType, 1> expected(tensorRange);
|
||||
DataType* device_data = static_cast<DataType*>(device.allocate(count * sizeof(DataType)));
|
||||
|
||||
// memset
|
||||
const char byte_value = static_cast<char>(0xAB);
|
||||
device.memset(device_data, byte_value, count * sizeof(DataType));
|
||||
device.memcpyDeviceToHost(host.data(), device_data, count * sizeof(DataType));
|
||||
synchronize(device);
|
||||
memset(expected.data(), byte_value, count * sizeof(DataType));
|
||||
for (size_t i=0; i<count; i++) {
|
||||
VERIFY_IS_EQUAL(host(i), expected(i));
|
||||
}
|
||||
|
||||
// fill
|
||||
DataType fill_value = DataType(7);
|
||||
std::fill_n(expected.data(), count, fill_value);
|
||||
device.fill(device_data, device_data + count, fill_value);
|
||||
device.memcpyDeviceToHost(host.data(), device_data, count * sizeof(DataType));
|
||||
synchronize(device);
|
||||
for (int i=0; i<count; i++) {
|
||||
VERIFY_IS_EQUAL(host(i), expected(i));
|
||||
}
|
||||
|
||||
device.deallocate(device_data);
|
||||
}
|
||||
|
||||
void test_cpu() {
|
||||
Eigen::Tensor<float, 3> in1(40,50,70);
|
||||
@@ -266,6 +305,9 @@ void test_cpu() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test_device_memory<float>(context.device());
|
||||
test_device_memory<OffByOneScalar<int>>(context.device());
|
||||
}
|
||||
|
||||
void test_gpu() {
|
||||
@@ -386,6 +428,8 @@ void test_gpu() {
|
||||
|
||||
#endif
|
||||
|
||||
test_device_memory<float>(context.device());
|
||||
test_device_memory<OffByOneScalar<int>>(context.device());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,26 +18,36 @@
|
||||
#define EIGEN_USE_SYCL
|
||||
|
||||
#include "main.h"
|
||||
#include "OffByOneScalar.h"
|
||||
#include <unsupported/Eigen/CXX11/Tensor>
|
||||
#include <stdint.h>
|
||||
#include <iostream>
|
||||
|
||||
template <typename DataType, int DataLayout, typename IndexType>
|
||||
void test_device_memory(const Eigen::SyclDevice &sycl_device) {
|
||||
std::cout << "Running on : "
|
||||
<< sycl_device.sycl_queue().get_device(). template get_info<cl::sycl::info::device::name>()
|
||||
<<std::endl;
|
||||
IndexType sizeDim1 = 100;
|
||||
array<IndexType, 1> tensorRange = {{sizeDim1}};
|
||||
Tensor<DataType, 1, DataLayout,IndexType> in(tensorRange);
|
||||
Tensor<DataType, 1, DataLayout,IndexType> in1(tensorRange);
|
||||
memset(in1.data(), 1, in1.size() * sizeof(DataType));
|
||||
DataType* gpu_in_data = static_cast<DataType*>(sycl_device.allocate(in.size()*sizeof(DataType)));
|
||||
|
||||
// memset
|
||||
memset(in1.data(), 1, in1.size() * sizeof(DataType));
|
||||
sycl_device.memset(gpu_in_data, 1, in.size()*sizeof(DataType));
|
||||
sycl_device.memcpyDeviceToHost(in.data(), gpu_in_data, in.size()*sizeof(DataType));
|
||||
for (IndexType i=0; i<in.size(); i++) {
|
||||
VERIFY_IS_EQUAL(in(i), in1(i));
|
||||
}
|
||||
|
||||
// fill
|
||||
DataType value = DataType(7);
|
||||
std::fill_n(in1.data(), in1.size(), value);
|
||||
sycl_device.fill(gpu_in_data, gpu_in_data + in.size(), value);
|
||||
sycl_device.memcpyDeviceToHost(in.data(), gpu_in_data, in.size()*sizeof(DataType));
|
||||
for (IndexType i=0; i<in.size(); i++) {
|
||||
VERIFY_IS_EQUAL(in(i), in1(i));
|
||||
}
|
||||
|
||||
sycl_device.deallocate(gpu_in_data);
|
||||
}
|
||||
|
||||
@@ -73,5 +83,6 @@ template<typename DataType> void sycl_device_test_per_device(const cl::sycl::dev
|
||||
EIGEN_DECLARE_TEST(cxx11_tensor_device_sycl) {
|
||||
for (const auto& device :Eigen::get_sycl_supported_devices()) {
|
||||
CALL_SUBTEST(sycl_device_test_per_device<float>(device));
|
||||
CALL_SUBTEST(sycl_device_test_per_device<OffByOneScalar<int>>(device));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user