I have a code that is calculating coulomb interactions F = q1*q2/(c*r^2) it effectively looks like this.
I currently have this parallelized as
but this could also be written as
These two methods effectively perform the function, making sure force is updated atomically. Which of these two methods is better? Which one would be faster and make the most efficient use of all cores? Note the actual code is question is using __m128d vectors so it's not quite as simple as replacing += with an atomic operation.
I have a second snippet of code that looks like
is it better to write this as
Code:
for (size_t i = 0, e = numParticles - 1; i < e; i++) {
for (size_t j = i + 1; j < numParticles; j++) {
... Calculate magnetude of force ...
force[i] += forceMagnetude;
force[j] -= forceMagnetude;
}
}
Code:
dispatch_apply(numParticles - 1, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) ^(size_t i) {
for (size_t j = i + 1; j < numParticles; j++) {
... Calculate magnetude of force ...
dispatch_semaphore_wait(sema[i], DISPATCH_TIME_FOREVER);
force[i] += forceMagnetude;
dispatch_semaphore_signal(sema[i]);
dispatch_semaphore_wait(sema[j], DISPATCH_TIME_FOREVER);
force[j] -= forceMagnetude;
dispatch_semaphore_signal(sema[j]);
}
});
Code:
dispatch_apply(numParticles - 1, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) ^(size_t i) {
for (size_t j = i + 1; j < numParticles; j++) {
... Calculate magnetude of force ...
dispatch_sync(serial_queue[i], ^{
force[i] += forceMagnetude;
});
dispatch_sync(serial_queue[j], ^{
force[j] -= forceMagnetude;
});
}
});
I have a second snippet of code that looks like
Code:
dispatch_apply(numParticles, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) ^(size_t i) {
...do work...
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
non_threadsafe_function();
dispatch_semaphore_signal(sema);
...do more work...
});
Code:
dispatch_apply(numParticles, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) ^(size_t i) {
...do work...
dispatch_sync(serial_queue, ^{
non_threadsafe_function();
})
...do more work...
});