blob: 6a75439f3e4e32f47db7cf24e5e0b5fc579da37b [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*!
* Copyright (c) 2016 by Contributors
* \file caffe_blob.cc
* \brief Implementations of SetDataGradToBlob given various device/dimension
* \author Haoran Wang
*/
#include "caffe_blob.h"
namespace mxnet {
namespace op {
namespace caffe {
template<>
void SetDataGradToBlob<mshadow::cpu, float>(caffeMemoryTypes memType,
std::vector<::caffe::Blob<float>*>::iterator blob,
std::vector<TBlob>::const_iterator itr) {
float *data_ptr = reinterpret_cast<float*>((*itr).dptr_);
if (memType == Data)
(*blob)->set_cpu_data(data_ptr);
else
MXCAFFEBLOB(*blob, float)->set_cpu_diff(data_ptr);
}
template<>
void SetDataGradToBlob<mshadow::cpu, double>(caffeMemoryTypes memType,
std::vector<::caffe::Blob<double>*>::iterator blob,
std::vector<TBlob>::const_iterator itr) {
double *data_ptr = reinterpret_cast<double*>((*itr).dptr_);
if (memType == Data)
(*blob)->set_cpu_data(data_ptr);
else
MXCAFFEBLOB(*blob, double)->set_cpu_diff(data_ptr);
}
template<>
void SetDataGradToBlob<mshadow::gpu, float>(caffeMemoryTypes memType,
std::vector<::caffe::Blob<float>*>::iterator blob,
std::vector<TBlob>::const_iterator itr) {
float *data_ptr = reinterpret_cast<float*>((*itr).dptr_);
if (memType == Data)
(*blob)->set_gpu_data(data_ptr);
else
MXCAFFEBLOB(*blob, float)->set_gpu_diff(data_ptr);
}
template<>
void SetDataGradToBlob<mshadow::gpu, double>(caffeMemoryTypes memType,
std::vector<::caffe::Blob<double>*>::iterator blob,
std::vector<TBlob>::const_iterator itr) {
double *data_ptr = reinterpret_cast<double*>((*itr).dptr_);
if (memType == Data)
(*blob)->set_gpu_data(data_ptr);
else
MXCAFFEBLOB(*blob, double)->set_gpu_diff(data_ptr);
}
mxnet::TShape Vector2TShape(const std::vector<int> &vec_int) {
std::vector<mshadow::index_t> vec;
for (uint32_t i = 0; i < vec_int.size(); ++i)
vec.push_back(vec_int[i]);
// 0-dim represents scalar in caffe
if (vec_int.size() == 0)
vec.push_back(1);
return {vec.begin(), vec.end()};
}
std::vector<int> TShape2Vector(const mxnet::TShape &tshape) {
std::vector<int> s;
for (uint32_t i =0 ; i < tshape.ndim(); ++i)
s.push_back(tshape[i]);
return s;
}
} // namespace caffe
} // namespace op
} // namespace mxnet