How to add scalar to vector?

Open discussion regarding features, bugs, issues, vendors, etc.

How to add scalar to vector?

Postby Matt Phillips » Thu Apr 04, 2013 5:47 pm

I've asked this on SO,http://stackoverflow.com/questions/14051064/add-scalar-to-vector-in-blas-cublas-cuda but the answer I got--use axpy with a unit vector scaled by the number I want to add--seems inefficient, requiring unnecessary memory allocation and multiplication. So I wanted to check here and see if there is a better way. I'm also curious about the rationale for leaving out such an obvious and essential operation, if anybody has some insight there.
Matt Phillips
 
Posts: 5
Joined: Thu Apr 04, 2013 12:37 am

Re: How to add scalar to vector?

Postby Julien Langou » Thu Apr 04, 2013 6:07 pm

Code: Select all
CALL DAXPY( N, 1.0D+00 , X, INCX, ALPHA, 0 )

where ALPHA is a DOUBLE PRECISION number.

So the "trick" is to use a ZERO increment for your SCALAR.

A good implementation of DAXPY should have an IF statement to avoid the multiplication by 1.0D+00.

For CPU, a compiler should be able to optimize this kind of loop very efficiently. (This is a bandwidth bound problem so you will not go faster than the bandwidth from your main memory to your CPU.) So writing your own kernel makes sense.
For GPU, yes, if you do not want to mess up with programming on GPU and simply use a GPU BLAS, DAXPY should do the work.

Cheers,
Julien.
Julien Langou
 
Posts: 727
Joined: Thu Dec 09, 2004 12:32 pm
Location: Denver, CO, USA

Re: How to add scalar to vector?

Postby Matt Phillips » Thu Apr 04, 2013 6:28 pm

Thank you, that is exactly the kind of answer I am looking for. However it is slower than doing the addition 'by hand' in a for-loop, to my surprise. However this is using the CBLAS implementation that comes with the GSL, which I did not manually 'tune' in any way. My implementation could very well be sub-optimal, I haven't gotten to that stage yet. :)
Matt Phillips
 
Posts: 5
Joined: Thu Apr 04, 2013 12:37 am

Re: How to add scalar to vector?

Postby sven » Sun Apr 07, 2013 4:28 pm

If you were using Fortran, then you could just use the Fortran array statement
Code: Select all
x = alpha + x
!!

A minor comment on Julien's code, strictly speaking alpha should be declared as at least a one element array for the call to DAXPY to be legal Fortran (and I presume C as well).
sven
 
Posts: 144
Joined: Wed Dec 22, 2004 4:28 am

Re: How to add scalar to vector?

Postby Matt Phillips » Sun Apr 07, 2013 6:35 pm

Hi, thanks for the tip about Fortran, I did not know that it had vector operations built in like that, that's very good to know. I'm in fact coding in C; I don't have the test code in front of me but I think passing the address of a
Code: Select all
const double
element as ALPHA worked without compiler warning/error/unexpected behavior.
Matt Phillips
 
Posts: 5
Joined: Thu Apr 04, 2013 12:37 am


Return to User Discussion

Who is online

Users browsing this forum: No registered users and 2 guests