#include <vector>

#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/filler.hpp"
#include "caffe/layer.hpp"
#include "caffe/util/math_functions.hpp"
#include "caffe/vision_layers.hpp"

namespace caffe {

template <typename Dtype>
void InnerProductLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
    vector<Blob<Dtype>*>* top) {
  //const Dtype* bottom_data = bottom[0]->gpu_data();
  //Dtype* top_data = (*top)[0]->mutable_gpu_data();
  if (this->layer_param_.has_unuse()) {
  		//bool layer_unuse = this->layer_param_.unuse();
  		if (this->layer_param_.unuse()) {return;}
  	}
  const Dtype* weight = this->blobs_[0]->gpu_data();
  for (int i=0;i<bottom.size();i++) {
		const Dtype* bottom_data = bottom[i]->gpu_data();
		Dtype* top_data = (*top)[i]->mutable_gpu_data();
		M_ = bottom[i]->num();
		caffe_gpu_gemm<Dtype>(CblasNoTrans, CblasTrans, M_, N_, K_, (Dtype)1.,
		      bottom_data, weight, (Dtype)0., top_data);
		if (bias_term_) {
		    caffe_gpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, M_, N_, 1, (Dtype)1.,
		        bias_multiplier_.gpu_data(),
		        this->blobs_[1]->gpu_data(), (Dtype)1., top_data);
		}
  if (this->layer_param_.has_recording()) {
  if (this->layer_param_.recording()) {
    std::ofstream file;
    file.open("./loss_ip.txt", ios::out|ios::app);
      for (int k = 0; k < bottom[i]->num(); ++k)
      {
    	  file << k << "\t";
    	  file << (*top)[i]->cpu_data()[k] << "\n";
      }
      file.close();
  }
  }
  }

  /*caffe_gpu_gemm<Dtype>(CblasNoTrans, CblasTrans, M_, N_, K_, (Dtype)1.,
      bottom_data, weight, (Dtype)0., top_data);
  if (bias_term_) {
    caffe_gpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, M_, N_, 1, (Dtype)1.,
        bias_multiplier_.gpu_data(),
        this->blobs_[1]->gpu_data(), (Dtype)1., top_data);
  }*/
  /*if (this->layer_param_.has_recording()) {
  if (this->layer_param_.recording()) {
    std::ofstream file;
    file.open("./loss_ip.txt", ios::out|ios::app);
      for (int i = 0; i < bottom[0]->num(); ++i)
      {
    	  file << i << "\t";
    	  file << (*top)[0]->cpu_data()[i] << "\n";
      }
      file.close();
  }
  }*/
}

template <typename Dtype>
void InnerProductLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down,
    vector<Blob<Dtype>*>* bottom) {
    if (this->layer_param_.has_unuse()) {
  		//bool layer_unuse = this->layer_param_.unuse();
  		if (this->layer_param_.unuse()) {return;}
  	}
  if (this->param_propagate_down_[0]) {
	  for (int i=0;i<top.size();i++) {
		  const Dtype* top_diff = top[i]->gpu_diff();
		  const Dtype* bottom_data = (*bottom)[i]->gpu_data();
		  M_ = (*bottom)[i]->num();
		  if (i==0) {
			  caffe_gpu_gemm<Dtype>(CblasTrans, CblasNoTrans, N_, K_, M_, (Dtype)1.,
			  		          top_diff, bottom_data, (Dtype)0., this->blobs_[0]->mutable_gpu_diff());
		  } else {
			  caffe_gpu_gemm<Dtype>(CblasTrans, CblasNoTrans, N_, K_, M_, (Dtype)1.,
			  		          top_diff, bottom_data, (Dtype)1., this->blobs_[0]->mutable_gpu_diff());
		  }
	  }
    //const Dtype* top_diff = top[0]->gpu_diff();
    //const Dtype* bottom_data = (*bottom)[0]->gpu_data();
    // Gradient with respect to weight
    /*caffe_gpu_gemm<Dtype>(CblasTrans, CblasNoTrans, N_, K_, M_, (Dtype)1.,
        top_diff, bottom_data, (Dtype)0., this->blobs_[0]->mutable_gpu_diff());*/
  }
  if (bias_term_ && this->param_propagate_down_[1]) {
	  for (int i=0;i<top.size();i++) {
		  const Dtype* top_diff = top[i]->gpu_diff();
		  M_ = (*bottom)[i]->num();
		  if (i==0) {
			  caffe_gpu_gemv<Dtype>(CblasTrans, M_, N_, (Dtype)1., top_diff,
			  		          bias_multiplier_.gpu_data(), (Dtype)0.,
			  		          this->blobs_[1]->mutable_gpu_diff());
		  } else {
			  caffe_gpu_gemv<Dtype>(CblasTrans, M_, N_, (Dtype)1., top_diff,
			  		          bias_multiplier_.gpu_data(), (Dtype)1.,
			  		          this->blobs_[1]->mutable_gpu_diff());
		  }
	  }
    //const Dtype* top_diff = top[0]->gpu_diff();
    // Gradient with respect to bias
    /*caffe_gpu_gemv<Dtype>(CblasTrans, M_, N_, (Dtype)1., top_diff,
        bias_multiplier_.gpu_data(), (Dtype)0.,
        this->blobs_[1]->mutable_gpu_diff());*/
  }
  if (propagate_down[0]) {
	  for (int i=0;i<top.size();i++) {
		  const Dtype* top_diff = top[i]->gpu_diff();
		  M_ = (*bottom)[i]->num();
		  caffe_gpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, M_, K_, N_, (Dtype)1.,
			  		          top_diff, this->blobs_[0]->gpu_data(), (Dtype)0.,
			  		          (*bottom)[i]->mutable_gpu_diff());
	  }
    //const Dtype* top_diff = top[0]->gpu_diff();
    // Gradient with respect to bottom data
    /*caffe_gpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, M_, K_, N_, (Dtype)1.,
        top_diff, this->blobs_[0]->gpu_data(), (Dtype)0.,
        (*bottom)[0]->mutable_gpu_diff());*/
  }
}

INSTANTIATE_CLASS(InnerProductLayer);

}  // namespace caffe
