#include <vector>

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

namespace caffe {

template <typename Dtype>
void MinusLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* top) {
        Dtype* top_data = (*top)[0]->mutable_gpu_data();
  int count = bottom[0]->count();
  caffe_gpu_sub(
      count,
      bottom[0]->gpu_data(),  // a
      bottom[1]->gpu_data(),  // b
      top_data);  // a_i-b_i
}

template <typename Dtype>
void MinusLayer<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* bottom1_diff = (*bottom)[0]->mutable_gpu_diff();
  Dtype* bottom2_diff = (*bottom)[1]->mutable_gpu_diff();
  int count = (*bottom)[0]->count();
  caffe_gpu_scale(count, Dtype(1.0), top_diff, bottom1_diff);
  caffe_gpu_scale(count, Dtype(-1.0), top_diff, bottom2_diff);
}

INSTANTIATE_CLASS(MinusLayer);

}  // namespace caffe
