cblasopenblascblas

Blas dgemv slower than manual implementation


I'm trying to use BLAS in my c programm to improve the speed of a matrix/vector product.

Manually i had this code :

for (j = 0; j < ann->hidden; ++j) {

        double delta = 0;
        //h is known before 
        for (k = 0; k < (h == ann->hidden_layers-1 ? ann->outputs : ann->hidden); ++k) {
            const double forward_delta = dd[k];
            const int windex = k * (ann->hidden + 1) + (j + 1);
            const double forward_weight = ww[windex];
            delta += forward_delta * forward_weight;
        }
        *d = *o * (1.0-*o) * delta;
        ++d; ++o;
    }
}

So i tried to replace this double for by the blas function cblas_dgemv and it look like it :

int n = h == ann->hidden_layers-1 ? ann->outputs : ann->hidden ;
    int m = ann->hidden ;
    double *delta = calloc(m,sizeof(double));
    cblas_dgemv(CblasColMajor,CblasNoTrans,m,n,1,&ww[1],m,dd,1,0.0,delta,1);
    for(j=0 ;  j < ann->hidden; ++j) {
      *d = *o * (1.0-*o)*delta[j];
      ++d; ++o;

    }
    free(delta);
    }

The output value are good.

The problem is that my implementation with BLAS is way slower than the "manual" one...

I don't know if it's because i don't use the most optimized function for this calculation or did i do something wrong ?


Solution

  • Ok the point is that my blas implementation is faster for big matrix but the overhead of blas slow down the things for little matrix !