Discussion:
parallel implementation of summing a vector
(too old to reply)
pomdps
2009-06-24 08:46:04 UTC
Permalink
Dear All,

In my program, I have to summate a vector in a for loop like the
following:

.....
double sum = 0;
for(int i=0; i< 100; i++)
sum += vector[i];
.....


Is it possible to implement this for loop in a parallel way?

Thanks for your help!
Georg Bisseling
2009-06-24 15:38:35 UTC
Permalink
Post by pomdps
Dear All,
In my program, I have to summate a vector in a for loop like the
.....
double sum = 0;
for(int i=0; i< 100; i++)
sum += vector[i];
.....
Is it possible to implement this for loop in a parallel way?
Thanks for your help!
This is a standard reduction example in any OpenMP tutorial or book.

It is very likely that you will not find this in an MPI example,
if so then as a bad example.

The reason is that sending the operands is more time consuming
in a distributed memory context than calculating the sum locally.
For OpenMP and therefore shared memory the operands do not have to
be moved around so parallelization of this task makes sense.

BTW.: for a simple reduction of such short vectors it is most
probably the fastest to try no parallelization at all.
--
This signature intentionally left almost blank.
http://www.this-page-intentionally-left-blank.org/
Loading...