зеркало из https://github.com/microsoft/caffe.git
Add and test sum of absolute values math functions for CPU and GPU
This commit is contained in:
Родитель
699b557c75
Коммит
910f3128c7
|
@ -112,6 +112,13 @@ void caffe_gpu_dot(const int n, const Dtype* x, const Dtype* y, Dtype* out);
|
|||
template <typename Dtype>
|
||||
int caffe_hamming_distance(const int n, const Dtype* x, const Dtype* y);
|
||||
|
||||
// Returns the sum of the absolute values of the elements of vector x
|
||||
template <typename Dtype>
|
||||
Dtype caffe_cpu_asum(const int n, const Dtype* x);
|
||||
|
||||
template <typename Dtype>
|
||||
void caffe_gpu_asum(const int n, const Dtype* x, Dtype* y);
|
||||
|
||||
} // namespace caffe
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2014 kloudkl@github
|
||||
|
||||
#include <stdint.h> // for uint32_t & uint64_t
|
||||
#include <cmath> // for std::fabs
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "caffe/blob.hpp"
|
||||
|
@ -74,4 +75,27 @@ TYPED_TEST(MathFunctionsTest, TestHammingDistance) {
|
|||
caffe_hamming_distance<TypeParam>(n, x, y));
|
||||
}
|
||||
|
||||
TYPED_TEST(MathFunctionsTest, TestAsumCPU){
|
||||
int n = this->blob_bottom_->count();
|
||||
const TypeParam* x = this->blob_bottom_->cpu_data();
|
||||
TypeParam std_asum = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
std_asum += std::fabs(x[i]);
|
||||
}
|
||||
TypeParam cpu_asum = caffe_cpu_asum<TypeParam>(n, x);
|
||||
CHECK_LT((cpu_asum - std_asum) / std_asum, 1e-2);
|
||||
}
|
||||
|
||||
TYPED_TEST(MathFunctionsTest, TestAsumGPU){
|
||||
int n = this->blob_bottom_->count();
|
||||
const TypeParam* x = this->blob_bottom_->cpu_data();
|
||||
TypeParam std_asum = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
std_asum += std::fabs(x[i]);
|
||||
}
|
||||
TypeParam gpu_asum;
|
||||
caffe_gpu_asum<TypeParam>(n, this->blob_bottom_->gpu_data(), &gpu_asum);
|
||||
CHECK_LT((gpu_asum - std_asum) / std_asum, 1e-2);
|
||||
}
|
||||
|
||||
} // namespace caffe
|
||||
|
|
|
@ -390,4 +390,24 @@ int caffe_hamming_distance<double>(const int n, const double* x,
|
|||
return dist;
|
||||
}
|
||||
|
||||
template <>
|
||||
float caffe_cpu_asum<float>(const int n, const float* x) {
|
||||
return cblas_sasum(n, x, 1);
|
||||
}
|
||||
|
||||
template <>
|
||||
double caffe_cpu_asum<double>(const int n, const double* x) {
|
||||
return cblas_dasum(n, x, 1);
|
||||
}
|
||||
|
||||
template <>
|
||||
void caffe_gpu_asum<float>(const int n, const float* x, float* y) {
|
||||
CUBLAS_CHECK(cublasSasum(Caffe::cublas_handle(), n, x, 1, y));
|
||||
}
|
||||
|
||||
template <>
|
||||
void caffe_gpu_asum<double>(const int n, const double* x, double* y) {
|
||||
CUBLAS_CHECK(cublasDasum(Caffe::cublas_handle(), n, x, 1, y));
|
||||
}
|
||||
|
||||
} // namespace caffe
|
||||
|
|
Загрузка…
Ссылка в новой задаче