Commit 9657baec authored by Chao Liu's avatar Chao Liu
Browse files

initial direct conv correct run

parent dfa02139
......@@ -7,21 +7,35 @@
#include "direct_convolution.cuh"
template <class T>
struct Generator
struct GeneratorConstant
{
T value = 0;
template <class... Is>
T operator()(Is... is)
{
#if 0
return value;
#else
}
};
template <class T>
struct GeneratorTensor
{
template <class... Is>
T operator()(Is... is)
{
#if 0
std::initializer_list<std::size_t> ls = {static_cast<std::size_t>(is)...};
return std::accumulate(ls.begin(), ls.end(), std::size_t(0));
#else
assert(sizeof...(Is) > 0);
std::initializer_list<std::size_t> ids = {static_cast<std::size_t>(is)...};
std::vector<std::size_t> lens(sizeof...(Is), 100);
std::vector<std::size_t> strides(sizeof...(Is), 1);
std::partial_sum(lens.rbegin(), lens.rbegin() + (sizeof...(Is) - 1), strides.rbegin() + 1);
return std::inner_product(ids.begin(), ids.end(), strides.begin(), std::size_t(0)) + 1;
#endif
}
};
template <typename T>
......@@ -57,18 +71,22 @@ void host_convolution(const Tensor<T>& in,
}
template <class T>
void device_convolution(Tensor<T>& in, Tensor<T>& wei, Tensor<T>& out)
void device_convolution(const Tensor<T>& in, const Tensor<T>& wei, Tensor<T>& out)
{
DeviceTensorDescriptor<4> in_desc_device(in.mDesc);
DeviceTensorDescriptor<4> wei_desc_device(wei.mDesc);
DeviceTensorDescriptor<4> out_desc_device(out.mDesc);
printf("__func__: in_desc_device: %u %u %u %u\n",
printf("__func__: in_desc_device: {%u %u %u %u}, {%u %u %u %u}\n",
in_desc_device.GetLength(0),
in_desc_device.GetLength(1),
in_desc_device.GetLength(2),
in_desc_device.GetLength(3));
in_desc_device.GetLength(3),
in_desc_device.GetStride(0),
in_desc_device.GetStride(1),
in_desc_device.GetStride(2),
in_desc_device.GetStride(3));
std::size_t data_sz = sizeof(T);
DeviceMem in_device_buf(data_sz * in.mDesc.GetElementSpace());
......@@ -77,11 +95,7 @@ void device_convolution(Tensor<T>& in, Tensor<T>& wei, Tensor<T>& out)
int num_thread = std::thread::hardware_concurrency();
#if 1
in.GenerateTensorValue(Generator<float>{1}, num_thread);
wei.GenerateTensorValue(Generator<float>{1}, num_thread);
#endif
out.GenerateTensorValue(Generator<float>{0}, num_thread);
out.GenerateTensorValue(GeneratorConstant<float>{0}, num_thread);
in_device_buf.ToDevice(in.mData.data());
wei_device_buf.ToDevice(wei.mData.data());
......@@ -89,7 +103,7 @@ void device_convolution(Tensor<T>& in, Tensor<T>& wei, Tensor<T>& out)
dim3 block_dim(64, 1, 1);
dim3 grid_dim(1, 1, 1);
gridwise_convolution<T, 3, 3, 4, 4, 2, 2, 1, 1, 32, 32, 1>
gridwise_convolution<T, 3, 3, 4, 4, 2, 2, 1, 1, 8, 8, 1>
<<<grid_dim, block_dim>>>(in_desc_device,
static_cast<T*>(in_device_buf.GetDeviceBuffer()),
wei_desc_device,
......@@ -97,6 +111,7 @@ void device_convolution(Tensor<T>& in, Tensor<T>& wei, Tensor<T>& out)
out_desc_device,
static_cast<T*>(out_device_buf.GetDeviceBuffer()));
checkCudaErrors(cudaGetLastError());
out_device_buf.FromDevice(out.mData.data());
}
......@@ -125,16 +140,23 @@ int main()
std::cout << __func__ << ": num_thread " << num_thread << std::endl;
in.GenerateTensorValue(Generator<float>{1}, num_thread);
wei.GenerateTensorValue(Generator<float>{1}, num_thread);
in.GenerateTensorValue(GeneratorTensor<float>{}, num_thread);
wei.GenerateTensorValue(GeneratorTensor<float>{}, num_thread);
//host_convolution(in, wei, out_host, num_thread);
host_convolution(in, wei, out_host, num_thread);
device_convolution(in, wei, out_device);
std::cout << __func__ << ": done" << std::endl;
LogRange(std::cout, in.mData, ",") << std::endl;
LogRange(std::cout, wei.mData, ",") << std::endl;
//LogRange(std::cout, out_host.mData, ",") << std::endl;
LogRange(std::cout << __func__ << "in : ", in.mData, ",") << std::endl;
LogRange(std::cout << __func__ << "wei: ", wei.mData, ",") << std::endl;
LogRange(std::cout, out_host.mData, ",") << std::endl;
LogRange(std::cout, out_device.mData, ",") << std::endl;
float error = 0;
for(int i = 0; i < out_host.mData.size(); ++i)
{
error += std::abs(out_host.mData[i] - out_device.mData[i]);
}
std::cout << "error: " << error << std::endl;
}
......@@ -17,15 +17,14 @@ struct DeviceTensorDescriptor
__host__ __device__ unsigned GetLength(unsigned i) const { return mpLengths[i]; }
__host__ __device__ unsigned long GetStride(unsigned i) const { return mpStrides[i]; }
__host__ __device__ unsigned GetStride(unsigned i) const { return mpStrides[i]; }
// this is ugly
__host__ __device__ unsigned long
Get1dIndex(unsigned n, unsigned c, unsigned h, unsigned w) const
__host__ __device__ unsigned Get1dIndex(unsigned n, unsigned c, unsigned h, unsigned w) const
{
return n * mpStrides[0] + c * mpStrides[1] + h * mpStrides[2] + w * mpStrides[3];
}
unsigned mpLengths[NDim];
unsigned long mpStrides[NDim];
unsigned mpStrides[NDim];
};
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment