Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

Intializing an array

janzz2k
Beginner
396 Views
Hi,
Suppose I have an array of doubles created using malloc. Is there an efficient (vectorized + optimised) way to set some or all its elements to a specific value, say 1.0? I want to avoid using a for loop to do this element by element. In other words, is there a more optimal way to do the following:


int main()
{
int n=1000;
double *x=(double *) malloc( n*sizeof(double));

int p=20, q=40;
for (int i=p; i < q; ++ i) x=1.0;

return(0);
}
0 Kudos
2 Replies
TimP
Honored Contributor III
396 Views

Compilation of your code by an auto-vectorizing compiler may give excellent results. Your loop is probably not long enough for icc #pragma vector nontemporal tobe useful. If you have taken care to get a 16-byte aligned allocation (should be the default on 64-bit OS), icc #pragma vector aligned should speed it up.

You probably spend more time in malloc() than in the initialization, for such a short loop.

If you don't want to use an auto-vectorizing compiler, you should be able to show an advantage from compiling with SSE intrinsics. It seems like overkill on a simple example like this.

0 Kudos
janzz2k
Beginner
396 Views
I used the value n=1000 just as an example. In my actual code, it may be quite large, such as 1 million or 1 billion.

0 Kudos
Reply