Caffe源码中math_functions文件分析
Caffe源碼(caffe version:09868ac , date: 2015.08.15)中有一些重要文件,這里介紹下math_functions文件。
1.??????include文件:
(1)、<glog/logging.h>:GLog庫,它是google的一個開源的日志庫,其使用可以參考:http://blog.csdn.net/fengbingchun/article/details/48768039 ;
(2)、<caffe/common.hpp>、<caffe/util/device_alternate.hpp>:這兩個文件的介紹可以參考: http://blog.csdn.net/fengbingchun/article/details/54955236?;
2.??????<caffe/util/mkl_alternate.hpp>文件:
這個文件里包含兩種庫,一個是Intel MKL,一個是OpenBLAS,這里用的是OpenBLAS。如果商用Intel MKL是需要付費的,下面僅對Intel MKL進行簡單介紹。
Intel MKL(Math Kernel Library)即Intel數學核心函數庫,它是一套高度優化和廣泛線程安全的數學例程,專為需要極致性能的科學、工程及金融等領域的應用而設計。核心數學函數包括BLAS、LAPACK、ScaLAPACK、Sparse Solver、快速傅里葉變換、矢量數學及其它函數。它可以為當前及下一代英特爾處理器提供性能優化,包括更出色地與Microsoft Visual Studio、Eclipse和XCode相集成。英特爾MKL支持完全集成英特爾兼容性OpenMP運行時庫,以實現更出色的Windows/Linux跨平臺兼容性。
關于OpenBLAS的介紹可以參考: http://blog.csdn.net/fengbingchun/article/details/55509764 ;
在github/fengbingchun/Caffe_Test中<mkl_alternate.hpp>中走的是OpenBLAS分支。
(1)、定義了一些宏:
DEFINE_VSL_UNARY_FUNC:一元函數,包括Sqr、Exp、Ln、Abs,對應的函數為vsSqr、vsExp、vsLn、vsAbs、vdSqr、vdExp、vdLn、vdAbs,支持float和double類型。
DEFINE_VSL_UNARY_FUNC_WITH_PARAM:帶一個參數的一元函數,包括Powx,對應的函數為vsPowx、vdPowx,支持float和double類型。
DEFINE_VSL_BINARY_FUNC:二元函數,包括Add、Sub、Mul、Div,對應的函數為vsAdd、vsSub、vsMul、vsDiv、vdAdd、vdSub、vdMul、vdDiv,支持float和double類型。
(2)、定義了axpby函數,支持兩種類型,cblas_saxpby、cblas_daxpby,如果設置incX和incY為1(即步長為1),則:Y=alpha*X+beta*Y
mkl_alternate文件測試代碼如下:
int test_caffe_util_mkl_alternate()
{const int N{ 5 };float a[N] {1, 2, 3, 4, 5}, b{ 2 }, alpha{ 0.2f }, beta{0.4f};float y1[N], y2[N], y3[N], y4[N]{6, 7, 8, 9, 10};fprintf(stderr, "test unary function: vsSqr\n");vsSqr(N, a, y1);for (auto ret : y1) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test function unary function with singular parameter: vsPowx\n");vsPowx(N, a, b, y2);for (auto ret : y2) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test binary function: vsAdd\n");vsAdd(N, a, a, y3);for (auto ret : y3) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test axpby function(Y=alpha*X+beta*Y): cblas_saxpby\n");cblas_saxpby(N, alpha, a, 1, beta, y4, 1);for (auto ret : y4) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");return 0;
}
執行結果如下:
3.??????math_functions文件內函數:封裝了一些基礎的數學運算函數
(1)、caffe_cpu_gemm:C=alpha*A*B+beta*C;
(2)、caffe_cpu_gemv:y=alpha*A*x+beta*y;
(3)、caffe_axpy:Y=alpha*X+Y;
(4)、caffe_cpu_axpby:Y=alpha*X+beta*Y;
(5)、caffe_copy:從X中拷貝前N個元素到Y中;
(6)、caffe_set:將X中的前N個元素置為alpha;
(7)、caffe_add_scalar:給Y中的前N個元素分別加上常數alpha;
(8)、caffe_scal:X = alpha*X;
(9)、caffe_sqr/ caffe_exp/caffe_log/caffe_abs:會調用mkl_alternate.hpp中的vsSqr、vsExp、vsLn、vsAbs、vdSqr、vdExp、vdLn、vdAbs函數;
(10)、caffe_add/caffe_sub/caffe_mul/caffe_div:會調用mkl_alternate.hpp中的vsAdd、vsSub、vsMul、vsDiv、vdAdd、vdSub、vdMul、vdDiv函數;
(11)、caffe_powx:會調用mkl_alternate.hpp中的vsPowx和vdPowx函數;
(12)、caffe_rng_rand:返回一個unsignedint類型的隨機數;
(13)、caffe_nextafter:在最大方向上,返回b可以表示的最接近的數值;
(14)、caffe_rng_uniform:產生指定范圍內的均勻分布隨機數;
(15)、caffe_rng_gaussian:產生高斯分布隨機數;
(16)、caffe_rng_bernoulli:產生伯努利分布隨機數;
(17)、caffe_cpu_dot:計算步長為1的內積;
(18)、caffe_cpu_strided_dot:計算指定步長的內積;
(19)、caffe_cpu_hamming_distance:計算x、y之間的海明距離;
(20)、caffe_cpu_asum:計算向量x中前n個元素的絕對值之和;
(21)、caffe_sign:類似于正負號函數,僅返回-1或1;
(22)、caffe_cpu_scale:Y=alpha*X 。
4.????????宏DEFINE_CAFFE_CPU_UNARY_FUNC:一元函數,類似于mkl_alternate.hpp中的宏DEFINE_VSL_UNARY_FUNC,包括:
(1)、caffe_cpu_sign:正負號函數,輸出-1、0、1;
(2)、caffe_cpu_sgnbit:作用類似于std::signbit,static_cast<bool>((std::signbit)(x));x為負數輸出為1,其它輸出為0;
(3)、caffe_cpu_fabs:取絕對值,作用類似于std::fabs。
math_functions文件測試代碼如下:
int test_caffe_util_math_functions()
{float alpha{ 0.5f }, beta{ 0.1f };// h*w: A: 2*3; B: 3*4; C: 2*4float A[2 * 3] {1, 2, 3, 4, 5, 6}, B[3 * 4] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},y1[2 * 4]{1, 2, 3, 4, 5, 6, 7, 8}, x[3]{1, 2, 3}, y2[2]{1, 2},y3[6] {1, 2, 3, 4, 5, 6}, y4[6] {1, 2, 3, 4, 5, 6},y7[6]{1, 2, 3, 4, 5, 6}, y10[6] {1, 2, 3, 4, 5, 6},y11[6] {1, 2, 3, 4, 5, 6}, C[6] {-2, -1, 0, 1, 2, 3}, y19[6] {-10, -10, -10, -10, -10, -10};float y5[6], y6[6], y20[6], y21[6], y22[6];int y12[6] {1, 2, 3, 4, 5, 6};fprintf(stderr, "test math function: caffe_cpu_gemm(C=alpha*A*B+beta*C)\n");// A、B、y1: matrixcaffe::caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, 2, 4, 3, alpha, A, B, beta, y1);for (auto ret : y1) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test math function: caffe_cpu_gemv(y=alpha*A*x+beta*y)\n");// A: matrix; x、y2: vectorcaffe::caffe_cpu_gemv(CblasNoTrans, 2, 3, alpha, A, x, beta, y2);for (auto ret : y2) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test math function: caffe_axpy(Y=alpha*X+Y)\n");caffe::caffe_axpy(6, alpha, A, y3);for (auto ret : y3) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test math function: caffe_cpu_axpby(Y= alpha*X+beta*Y)\n");caffe::caffe_cpu_axpby(6, alpha, A, beta, y4);for (auto ret : y4) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test math function: caffe_copy\n");caffe::caffe_copy(3, A, y5);for (auto ret : y5) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test math function: caffe_set\n");caffe::caffe_set(3, alpha, y6);for (auto ret : y6) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test math function: caffe_scal(X=alpha*X)\n");caffe::caffe_scal(4, alpha, y7);for (auto ret : y7) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test math function: caffe_rng_rand\n");unsigned int y8 = caffe::caffe_rng_rand();fprintf(stderr, "caffe rng rand: %d\n", y8);fprintf(stderr, "test math function: caffe_nextafter\n");float y9 = caffe::caffe_nextafter(alpha);fprintf(stderr, " caffe next after: %f\n", y9);fprintf(stderr, "test math function: caffe_rng_uniform\n");caffe::caffe_rng_uniform(4, -2.f, 2.f, y10);for (auto ret : y10) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test math function: caffe_rng_gaussian\n");caffe::caffe_rng_gaussian(4, -2.f, alpha, y11);for (auto ret : y11) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test math function: caffe_rng_bernoulli\n");caffe::caffe_rng_bernoulli(4, alpha, y12);for (auto ret : y12) {fprintf(stderr, "%d ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test math function: caffe_cpu_dot\n");float y13 = caffe::caffe_cpu_dot(3, A, B);fprintf(stderr, "caffe cpu dot: %f\n", y13);fprintf(stderr, "test math function: caffe_cpu_strided_dot\n");float y14 = caffe::caffe_cpu_strided_dot(2, A, 2, B, 2);fprintf(stderr, "caffe cpu strided dot: %f\n", y14);fprintf(stderr, "test math function: caffe_cpu_hamming_distance\n");int y15 = caffe::caffe_cpu_hamming_distance(4, A, C);fprintf(stderr, "caffe cpu hamming distance: %d\n", y15);fprintf(stderr, "test math function: caffe_cpu_asum\n");float y16 = caffe::caffe_cpu_asum(5, C);fprintf(stderr, "caffe cpu asum: %f\n", y16);fprintf(stderr, "test math function: caffe_sign\n");int8_t y17 = caffe::caffe_sign(-10.0f);int8_t y18 = caffe::caffe_sign(10.0f);fprintf(stderr, "caffe sign: -10.0f: %d, 10.0f: %d\n", y17, y18);fprintf(stderr, "test math function: caffe_cpu_scale\n");caffe::caffe_cpu_scale(5, alpha, C, y19);for (auto ret : y19) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test math function: caffe_cpu_sign\n");caffe::caffe_cpu_sign(5, C, y20);for (auto ret : y20) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test math function: caffe_cpu_sgnbit\n");caffe::caffe_cpu_sgnbit(5, C, y21);for (auto ret : y21) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");fprintf(stderr, "test math function: caffe_cpu_fabs\n");caffe::caffe_cpu_fabs(5, C, y22);for (auto ret : y22) {fprintf(stderr, "%f ", ret);}fprintf(stderr, "\n");return 0;
}
執行結果如下:
GitHub:https://github.com/fengbingchun/Caffe_Test
總結
以上是生活随笔為你收集整理的Caffe源码中math_functions文件分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenBLAS简介及在Windows7
- 下一篇: Caffe源码中syncedmem文件分