Add and test non-in-place scale math functions for CPU and GPU

This commit is contained in:
Kai Li 2014-02-25 20:26:55 +08:00
Родитель b458b41d68
Коммит b1f6eb0b91
3 изменённых файлов: 60 добавлений и 0 удалений

Просмотреть файл

@ -157,6 +157,12 @@ DEFINE_CAFFE_CPU_UNARY_FUNC(fabs, y[i] = std::fabs(x[i]));
template <typename Dtype>
void caffe_gpu_fabs(const int n, const Dtype* x, Dtype* y);
template <typename Dtype>
void caffe_cpu_scale(const int n, const Dtype alpha, const Dtype *x, Dtype* y);
template <typename Dtype>
void caffe_gpu_scale(const int n, const Dtype alpha, const Dtype *x, Dtype* y);
} // namespace caffe

Просмотреть файл

@ -140,4 +140,30 @@ TYPED_TEST(MathFunctionsTest, TestFabsGPU){
}
}
TYPED_TEST(MathFunctionsTest, TestScaleCPU){
int n = this->blob_bottom_->count();
TypeParam alpha = this->blob_bottom_->cpu_diff()[rand() %
this->blob_bottom_->count()];
caffe_cpu_scale<TypeParam>(n, alpha, this->blob_bottom_->cpu_data(),
this->blob_bottom_->mutable_cpu_diff());
const TypeParam* scaled = this->blob_bottom_->cpu_diff();
const TypeParam* x = this->blob_bottom_->cpu_data();
for (int i = 0; i < n; ++i) {
CHECK_EQ(scaled[i], x[i] * alpha);
}
}
TYPED_TEST(MathFunctionsTest, TestScaleGPU){
int n = this->blob_bottom_->count();
TypeParam alpha = this->blob_bottom_->cpu_diff()[rand() %
this->blob_bottom_->count()];
caffe_gpu_scale<TypeParam>(n, alpha, this->blob_bottom_->gpu_data(),
this->blob_bottom_->mutable_gpu_diff());
const TypeParam* scaled = this->blob_bottom_->cpu_diff();
const TypeParam* x = this->blob_bottom_->cpu_data();
for (int i = 0; i < n; ++i) {
CHECK_EQ(scaled[i], x[i] * alpha);
}
}
}

Просмотреть файл

@ -413,4 +413,32 @@ void caffe_gpu_asum<double>(const int n, const double* x, double* y) {
INSTANTIATE_CAFFE_CPU_UNARY_FUNC(sign);
INSTANTIATE_CAFFE_CPU_UNARY_FUNC(fabs);
template <>
void caffe_cpu_scale<float>(const int n, const float alpha, const float *x,
float* y) {
cblas_scopy(n, x, 1, y, 1);
cblas_sscal(n, alpha, y, 1);
}
template <>
void caffe_cpu_scale<double>(const int n, const double alpha, const double *x,
double* y) {
cblas_dcopy(n, x, 1, y, 1);
cblas_dscal(n, alpha, y, 1);
}
template <>
void caffe_gpu_scale<float>(const int n, const float alpha, const float *x,
float* y) {
CUBLAS_CHECK(cublasScopy(Caffe::cublas_handle(), n, x, 1, y, 1));
CUBLAS_CHECK(cublasSscal(Caffe::cublas_handle(), n, &alpha, y, 1));
}
template <>
void caffe_gpu_scale<double>(const int n, const double alpha, const double *x,
double* y) {
CUBLAS_CHECK(cublasDcopy(Caffe::cublas_handle(), n, x, 1, y, 1));
CUBLAS_CHECK(cublasDscal(Caffe::cublas_handle(), n, &alpha, y, 1));
}
} // namespace caffe