#include <vector>

#include "caffe/layer.hpp"
#include "caffe/util/math_functions.hpp"
#include "caffe/vision_layers.hpp"

namespace caffe {



template <typename Dtype>
void DeConcatLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* top) {
  const Dtype* bottom_data = bottom[0]->gpu_data();
  if (deconcat_dim_== 0) {
    int offset_num = 0;
    for (int i=0;i<top->size(); ++i) {
    	Dtype* top_data = (*top)[i]->mutable_gpu_data();
    	int num_elem = (*top)[i]->count();
    	caffe_copy(num_elem, bottom_data+bottom[0]->offset(offset_num), top_data);
    	offset_num += (*top)[i]->num();
    }
  }
}

template <typename Dtype>
void DeConcatLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, vector<Blob<Dtype>*>* bottom) {
  //const Dtype* top_diff = top[0]->gpu_diff();
  Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff();
  if (deconcat_dim_ == 0) {
    int offset_num = 0;
    for (int i = 0; i < top.size(); ++i) {
    	Blob<Dtype>* blob = top[i];
    	const Dtype* top_diff = blob->gpu_diff();
    	caffe_copy(blob->count(), top_diff,
    	           bottom_diff + (*bottom)[0]->offset(offset_num));
    	offset_num += blob->num();
    }
  }
}

INSTANTIATE_CLASS(DeConcatLayer);

}  // namespace caffe
