/*
All modification made by Intel Corporation: © 2016 Intel Corporation

All contributions by the University of California:
Copyright (c) 2014, 2015, The Regents of the University of California (Regents)
All rights reserved.

All other contributions:
Copyright (c) 2014, 2015, the respective contributors
All rights reserved.
For the list of contributors go to https://github.com/BVLC/caffe/blob/master/CONTRIBUTORS.md


Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of Intel Corporation nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "caffe/common.hpp"
#include "caffe/syncedmem.hpp"
#include "caffe/util/math_functions.hpp"

namespace caffe {
SyncedMemory::~SyncedMemory() {
  if (cpu_ptr_ && own_cpu_data_) {
    CaffeFreeHost(cpu_ptr_, cpu_malloc_use_cuda_);
  }

#ifndef CPU_ONLY
  if (gpu_ptr_ && own_gpu_data_) {
    int initial_device;
    cudaGetDevice(&initial_device);
    if (gpu_device_ != -1) {
      CUDA_CHECK(cudaSetDevice(gpu_device_));
    }
    CUDA_CHECK(cudaFree(gpu_ptr_));
    cudaSetDevice(initial_device);
  }
#endif  // CPU_ONLY
#ifdef CO_SIM
  if (cpu_ptr_cosim_){
    CaffeFreeHost(cpu_ptr_cosim_, cpu_malloc_use_cuda_);
  }
#endif
}

inline void SyncedMemory::to_cpu() {
  switch (head_) {
  case UNINITIALIZED:
    CaffeMallocHost(&cpu_ptr_, size_, &cpu_malloc_use_cuda_);
    caffe_memset(size_, 0, cpu_ptr_);
    head_ = HEAD_AT_CPU;
    own_cpu_data_ = true;
    break;
  case HEAD_AT_GPU:
#ifndef CPU_ONLY
    if (cpu_ptr_ == NULL) {
      CaffeMallocHost(&cpu_ptr_, size_, &cpu_malloc_use_cuda_);
      own_cpu_data_ = true;
    }
    caffe_gpu_memcpy(size_, gpu_ptr_, cpu_ptr_);
    head_ = SYNCED;
#else
    NO_GPU;
#endif
    break;
  case HEAD_AT_PRV:
    if (cpu_ptr_ == NULL) {
      CaffeMallocHost(&cpu_ptr_, size_, &cpu_malloc_use_cuda_);
      own_cpu_data_ = true;
    }
    CHECK(prv_descriptor_.get());
    prv_descriptor_->convert_from_prv(cpu_ptr_);
    prv_descriptor_->on_to_cpu();
    head_ = SYNCED_PRV;
    break;
  case SYNCED_PRV:
  case HEAD_AT_CPU:
  case SYNCED:
    break;
  }
}

#ifdef CO_SIM
//Only invoke when getting data from mkldnn engine by COSIM.
inline void SyncedMemory::to_cpu_cosim(){
  switch (head_) {
  case UNINITIALIZED:
    if (cpu_ptr_cosim_ == NULL) {
      CaffeMallocHost(&cpu_ptr_cosim_, size_, &cpu_malloc_use_cuda_);
      caffe_memset(size_, 0, cpu_ptr_cosim_);
    }
    break;
  case HEAD_AT_GPU:
    assert(!"Do not support GPU.");
    break;
  case HEAD_AT_PRV:
    if (cpu_ptr_cosim_ == NULL) {
      CaffeMallocHost(&cpu_ptr_cosim_, size_, &cpu_malloc_use_cuda_);
    }
    CHECK(prv_descriptor_.get());
    prv_descriptor_->convert_from_prv_cosim(cpu_ptr_cosim_);
    prv_descriptor_->on_to_cpu();
    break;
  case SYNCED_PRV:
  case HEAD_AT_CPU:
  case SYNCED:
    if (cpu_ptr_cosim_ == NULL) {
      CaffeMallocHost(&cpu_ptr_cosim_, size_, &cpu_malloc_use_cuda_);
    }
    memcpy(cpu_ptr_cosim_, cpu_ptr_, size_);
    break;
  }
}

const void* SyncedMemory::cpu_data_cosim() {
  boost::mutex::scoped_lock lock(mtx);
  to_cpu_cosim();
  return (const void*)cpu_ptr_cosim_;

}
#endif

inline void SyncedMemory::to_gpu() {
#ifndef CPU_ONLY
  switch (head_) {
  case UNINITIALIZED:
    CUDA_CHECK(cudaGetDevice(&gpu_device_));
    CUDA_CHECK(cudaMalloc(&gpu_ptr_, size_));
    caffe_gpu_memset(size_, 0, gpu_ptr_);
    head_ = HEAD_AT_GPU;
    own_gpu_data_ = true;
    break;
  case HEAD_AT_PRV:
    to_cpu();
  case HEAD_AT_CPU:
    if (gpu_ptr_ == NULL) {
      CUDA_CHECK(cudaGetDevice(&gpu_device_));
      CUDA_CHECK(cudaMalloc(&gpu_ptr_, size_));
      own_gpu_data_ = true;
    }
    caffe_gpu_memcpy(size_, cpu_ptr_, gpu_ptr_);
    head_ = SYNCED;
    break;
  case HEAD_AT_GPU:
  case SYNCED:
    break;
  }
#else
  NO_GPU;
#endif
}

const void* SyncedMemory::cpu_data() {
  boost::mutex::scoped_lock lock(mtx);
  to_cpu();
  return (const void*)cpu_ptr_;
}

void SyncedMemory::set_cpu_data(void* data) {
  boost::mutex::scoped_lock lock(mtx);
  CHECK(data);
  if (own_cpu_data_) {
    CaffeFreeHost(cpu_ptr_, cpu_malloc_use_cuda_);
  }
  cpu_ptr_ = data;
  head_ = HEAD_AT_CPU;
  own_cpu_data_ = false;
}

const void* SyncedMemory::gpu_data() {
  boost::mutex::scoped_lock lock(mtx);
#ifndef CPU_ONLY
  to_gpu();
  return (const void*)gpu_ptr_;
#else
  NO_GPU;
  return NULL;
#endif
}

void SyncedMemory::set_gpu_data(void* data) {
  boost::mutex::scoped_lock lock(mtx);
#ifndef CPU_ONLY
  CHECK(data);
  if (own_gpu_data_) {
    int initial_device;
    cudaGetDevice(&initial_device);
    if (gpu_device_ != -1) {
      CUDA_CHECK(cudaSetDevice(gpu_device_));
    }
    CUDA_CHECK(cudaFree(gpu_ptr_));
    cudaSetDevice(initial_device);
  }
  gpu_ptr_ = data;
  head_ = HEAD_AT_GPU;
  own_gpu_data_ = false;
#else
  NO_GPU;
#endif
}

void* SyncedMemory::mutable_cpu_data() {
  boost::mutex::scoped_lock lock(mtx);
  to_cpu();
  head_ = HEAD_AT_CPU;
  return cpu_ptr_;
}

void* SyncedMemory::mutable_gpu_data() {
  boost::mutex::scoped_lock lock(mtx);
#ifndef CPU_ONLY
  to_gpu();
  head_ = HEAD_AT_GPU;
  return gpu_ptr_;
#else
  NO_GPU;
  return NULL;
#endif
}

#ifndef CPU_ONLY
void SyncedMemory::async_gpu_push(const cudaStream_t& stream) {
  boost::mutex::scoped_lock lock(mtx);
  CHECK(head_ == HEAD_AT_CPU);
  if (gpu_ptr_ == NULL) {
    CUDA_CHECK(cudaGetDevice(&gpu_device_));
    CUDA_CHECK(cudaMalloc(&gpu_ptr_, size_));
    own_gpu_data_ = true;
  }
  const cudaMemcpyKind put = cudaMemcpyHostToDevice;
  CUDA_CHECK(cudaMemcpyAsync(gpu_ptr_, cpu_ptr_, size_, put, stream));
  // Assume caller will synchronize on the stream before use
  head_ = SYNCED;
}
#endif

void SyncedMemory::set_prv_descriptor(shared_ptr<PrvMemDescr> descriptor,
        bool same_data) {
  // If it wasn't synced before, it won't be now.
  if (descriptor == NULL) {
    if (head_ != UNINITIALIZED)
      head_ = HEAD_AT_CPU;
  } else {
    if ((head_ != HEAD_AT_PRV) && same_data)
      head_ = SYNCED_PRV;
    else
      head_ = HEAD_AT_PRV;
  }

  prv_descriptor_ = descriptor;
}

const void* SyncedMemory::prv_data() {
  if ((head_ != HEAD_AT_PRV) &&
     (head_ != SYNCED_PRV)) {
    return NULL;
  }

  CHECK(prv_descriptor_.get());
  return (const void* ) prv_descriptor_->prv_ptr();
}

void* SyncedMemory::mutable_prv_data() {
  CHECK(prv_descriptor_.get());
  if (head_ == HEAD_AT_CPU) {
    prv_descriptor_->convert_to_prv(cpu_ptr_);
  }
  head_ = HEAD_AT_PRV;
  return prv_descriptor_->prv_ptr();
}

void SyncedMemory::swap(shared_ptr<SyncedMemory> other) {
  std::swap(other->cpu_ptr_, this->cpu_ptr_);
  std::swap(other->head_, this->head_);
  std::swap(other->own_cpu_data_, this->own_cpu_data_);
  std::swap(other->own_prv_data_, this->own_prv_data_);
  std::swap(other->prv_descriptor_, this->prv_descriptor_);
}
}  // namespace caffe
