Cblas notes

syntax

func cblas_sgemv(_ __Order: CBLAS_ORDER, 
               _ __TransA: CBLAS_TRANSPOSE, 
               _ __M: Int32, 
               _ __N: Int32, 
               _ __alpha: Float, 
               _ __A: UnsafePointer<Float>!, 
               _ __lda: Int32, 
               _ __X: UnsafePointer<Float>!, 
               _ __incX: Int32, 
               _ __beta: Float, 
               _ __Y: UnsafeMutablePointer<Float>!, 
               _ __incY: Int32)
void MyMultiply(cv::Mat &A, cv::Mat &x, cv::Mat &y) {
    const int M = A.rows;
    const int N = A.cols;
    float alpha = 1.0;
    float beta = 1.0;
    int incx = 1;
    int incy = 1;
    cblas_sgemv(CblasRowMajor, CblasTrans, M, N, alpha, A.ptr<float>(), N, x.ptr<float>(), incx, beta, y.ptr<float>(), incy);
    cv::transpose(y,y); // data is stored in row major.
}

Note: Because OpenCV stores elements row-wise, I passed CblasTrans parameter in cblas_sgemv.

Also, for lda, I passed N instead of M (lda: The size of the first dimention of matrix A; if you are passing a matrix A[m][n], the value should be m.) beause I did CblasTrans.

Don't forget .ptr<float>() for all the cv::Mat objects

Make sure number of elements for y is matching with the result of the matrix multiplication. Otherwise not enough elements calculated or 0's filled with excess. Also, if the result dimension is 4x1, y should be transposed after cblas_sgemv.

Still unclear about CblasRowMajor.

results matching ""

    No results matching ""