Reorder.h
Go to the documentation of this file.
1#ifndef SHARK_CORE_IMAGE_REORDER_H
2#define SHARK_CORE_IMAGE_REORDER_H
3
4#include <shark/LinAlg/Base.h>
5#include "CPU/Reorder.h"
6#ifdef SHARK_USE_OPENCL
7#include "OpenCL/Reorder.h"
8#endif
9namespace shark{
10enum class ImageFormat{
11 NHWC = 1234,
12 NWHC = 1324,
13 NCHW = 1423,
14 NCWH = 1432,
15 CHWN = 4231
16};
17
18
19namespace image{
20template<class T, class Device>
22 blas::dense_vector_adaptor<T const, blas::continuous_dense_tag, Device> input,
23 blas::dense_vector_adaptor<T, blas::continuous_dense_tag, Device> output,
24 Shape const& shapeIn,
25 ImageFormat orderIn,
26 ImageFormat orderOut
27){
28 SIZE_CHECK(shapeIn.size() == 4);
29 if(orderIn == orderOut){
30 output = input;
31 return;
32 }
33 int dimsIn[4] = {int(orderIn)/1000, (int(orderIn) / 100) % 10, (int(orderIn) / 10) % 10, int(orderIn) % 10};
34 int dimsOut[4] = {int(orderOut)/1000, (int(orderOut) / 100) % 10, (int(orderOut) / 10) % 10, int(orderOut) % 10};
35
36 //compute permutation from input and output
37 int dimPerm[4] = {0,0,0,0};
38 for(int i = 0; i != 4; ++i){
39 while(dimsIn[dimPerm[i]] != dimsOut[i]) ++dimPerm[i];
40 }
41 //compute dimension strides and sizes
42 //those have to be interpreted in: when changing index i_k in output, which stride does that have in input?
43 std::size_t stride[4]={shapeIn.stride(dimPerm[0]) ,shapeIn.stride(dimPerm[1]),shapeIn.stride(dimPerm[2]), shapeIn.stride(dimPerm[3])};
44 std::size_t size[4]={shapeIn[dimPerm[0]] ,shapeIn[dimPerm[1]],shapeIn[dimPerm[2]], shapeIn[dimPerm[3]]};
45
46 //delegate to device
47 reorder_impl(input,output, size, stride);
48}
49
50}}
51
52#endif