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);
}
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);
}
链接已复制
2 回复数
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.