Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Intel Community
- Software
- Software Development SDKs and Libraries
- Intel® oneAPI Math Kernel Library
- best way to add a value to all elements in a vector

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

madir

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-26-2012
02:03 AM

393 Views

best way to add a value to all elements in a vector

Hi,

Is there a betterway to add a value to all elements in a vector than this:

cblas_saxpy(arrSize, &m_anyvalue, m_pU, 1, m_pX, 1 );

where

m_pU = a vector of ones

m_pX = the vector I want to add m_anyvalue to

Thanks

Link Copied

13 Replies

Sergey_M_Intel2

Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-26-2012
02:29 AM

393 Views

Hello,

cblas_saxpydoesn't lookright routine to use. My recommendation will be:

vsLinearFrac( n, a, b, scalea, shifta, scaleb, shiftb, y )

Description

The v?LinearFrac function performs linear fraction transformation of vectors a by vector b with scalar parameters: scaling multipliers scalea, scaleb and shifting addends shifta, shiftb:

y*=(scaleaa +shifta)/(scalebb+shiftb), i=1,2 n*

Just set scalea=1.0f, scaleb=0.0f, shiftb=1.0f

I hope that helps,

Sergey

madir

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-26-2012
02:54 AM

393 Views

Is the function smart enough to avoid performing the unnecessary computations?

Sergey_M_Intel2

Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-26-2012
03:07 AM

393 Views

Yes, the function should be able to effectively handlesuch subtle cases. Let me know if it works fine.

With one more note. y

Regards,

Sergey

madir

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-26-2012
03:07 AM

393 Views

Oh no, it seems there is no overload for complex input...

Sergey_M_Intel2

Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-26-2012
03:11 AM

393 Views

Correct. Only real single and double precision variant for v?LinearFrac

TimP

Black Belt

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-26-2012
06:28 AM

393 Views

So you may be better off using your compiler (such as Intel or recent gcc).

madir

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-26-2012
09:30 AM

393 Views

mecej4

Black Belt

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-26-2012
10:45 AM

393 Views

In that case, you may want to use mathematical reasoning to see if you can avoid a redundant calculation altogether.

For example, if

madir

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-26-2012
11:05 AM

393 Views

Wouldn't it be nice to add a function to VML that does addition with a constant?

mecej4

Black Belt

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-26-2012
11:17 AM

393 Views

So far, you have stated that you want a routine to add a constant to a vector, and you did not like the ones that were suggested.

If you state details of how the added constant plays a role in subsequent calculations, further suggestions may become possible.

Sergey_M_Intel2

Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-26-2012
09:18 PM

393 Views

Exactly.

for (i=0; i {

x* += c;*

}

Intel C++ compiler should nicely vectorize the loop

Thanks,

Sergey

for (i=0; i

x

}

Intel C++ compiler should nicely vectorize the loop

Thanks,

Sergey

madir

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-27-2012
02:05 AM

393 Views

Yup, nothing like a calculation that never needs to be done:) I don't have any particular problem, but I can imagine that in some cases one can get away with an initialization to a contant instead of an addition. Example:

a = zeros

a = a + b*c

a = a + d

can be changed to

a = d

a = a + b*c

Is there something like a "vector-memset" in MKL?

madir

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-27-2012
02:07 AM

393 Views

Ah - nice! Thanks Sergey!

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

For more complete information about compiler optimizations, see our Optimization Notice.