- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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);
}
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Reply
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