I need to sum all the elements of a matrix in caffe,
But as I noticed, the caffe wrapper of the cblas functions ('math_functions.hpp'
& 'math_functions.cpp'
) is using cblas_sasum
function as caffe_cpu_asum
that computes the sum of the absolute values of elements in a vector.
Since I'm a newbie in cblas, I tried to find a suitable function to get rid of absolute there, but it seems that there is no function with that property in cblas.
Any suggestion?
Summation of all the elements of an array is simple enough to be implemented by a single for-loop. You only need to use proper compile options to vectorise it with SIMD instructions.
For Blob in caffe, you could use
.cpu_data()
to get the raw pointer of the array and then use for-loop.There is a way to do so using cblas functions, though it is a bit of an awkward way.
What you need to do is to define an "all 1" vector, and then do a dot product between this vector and your matrix, the result is the sum.
Let
myBlob
be a caffe Blob whose elements you want to sum:This trick is used in the implementation of
"Reduction"
layer.To make this answer both GPU compliant, one need to allocate a
Blob
formult_data
and not astd::vector
(because you need it'spgu_data()
):For GPU, (in a
'.cu'
source file):