last day (15 days later) » 

3:41 PM
0
Q: Monte Carlo simulation runs significantly slower than sequential

Amirreza A.I'm new to the concept of concurrent and parallel programing in general. I'm trying to calculate Pi using Monte Carlo method in C. Here is my source code: #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> int main(void) { long points; long m = 0; double coord...

 
Access to coordinates, m, distance is a bottleneck since each thread need access to the same memory location.
 
Note: rand is not threadsafe.
 
Note: you can avoid calling sqrt, and fasten both codes.
 
@Jean-BaptisteYunès -- those are thread local variables. The shared variable is in the rand() function.
 
@Jean-BaptisteYunès those variables will be private to each thread so that is not a problem
@Amirreza A the code that I have posted has speedups of 4 for 4 cores do you mind testing in your machine ?
 
3:41 PM
@dreamcrash With all due respect, if your claim were correct ( obtaining a 4x speedup ), Dr. Gene AMDAHL's arguments would have gone ( which they never can ). No matter how short the initial (principally SERIAL, non-PARALLEL-isable) processing setup efforts take, these can never get avoided and the speedup can never reach 4.00 x. The higher the loop-count, the smaller the SERIAL fraction will remain and still bearing all the add-on overhead costs makes the limit of 4.00 x speedup principally not achievable :o) The Math ...
 
@user3666197 You can speedups with more than 4x for a 4 cores, with an improving on cache locality. If you have less miss in the parallel version than in the sequential.
@user3666197 the actually speedup was 3.96x, and I can provide it very easily if you want.
@user3666197 "These can never get avoided and the speedup can never reach 4.00 x." you clearly never worked in HPC before
 
@dreamcrash - well, this is not an argument. HPC practitioners often present a SUPER-LINEAR speedup figures, yet these never get a level playfield - as the "baseline" for the comparison has never been run at a very same playfield as the "claimed" super-linear instruction-mix. Not my fault, yet if one starts to compare apples-to-oranges, may we live with claims that oranges are super-linear spead-up apples ( because having been grown in other biotop ) -- grow the oranges in the exactly the same eco-system -- ( i.e.same mem-I/O bottlenecks (non-free channels due to NUMA-colocated ) )
 
This question was about the overheads, I was the only one point out the overheads besides the obvious rand. None have provided a solution actually using openMP. I compare the same sequential version with correspond parallel version. For some reason you start to pick on myself with comments like :"Proposing timed-sections is left to @dreamcrash for having a level plainfield for re-testing with meaningful comparisons:" I have better to do. Obviously, if you optimized the seq. code as much as possible you will likely have less speedups on the parallel version. Did I claim otherwise?
 
If one inspects the assembly code for the claimed super-linear speedups, soon you realise that original apples can never get fair compared with oranges, the less with oranges-on-transformed-code-on-steroids. ( Code works faster, no doubts about that, yet the code is, for many reasons, way different than was the pure-[SERIAL] version one started with, so shan't be promoted as a fair baseline for such super-linearity skewed comparisons --- or, provide the same footprint of non-blocking, non-cross-QPI / NUMA-equalised L1data access, and there we go to compare a "same" code under fair conditions )
 
@user3666197 "way different than was the pure-[SERIAL] version one started with?" I used the same code with 4 threads and without any threads. One could claim that it is unfair for the sequential version because one could try to optimized further with SIMD instructions and so on.
@user3666197 I added some results testing against you optimized version. So your optimized sequential version compared with my non optimized parallel version. The speedup was 2.27 for 4 cores.
 
3:41 PM
woh I did'nt saw that variables are local. Shame on me! Sorry for the stupid comment.
 
@Jean-BaptisteYunès no need to say sorry, you were just trying to help, everyone makes mistakes :)
@Amirreza A. Do you mind testing the parallel version?
@user3666197 Btw you have a race conditions in your code, namely coordinates[0] = rand_r( &aThreadSpecificSEED_x ); and coordinates[1] = rand_r( &aThreadSpecificSEED_y ); since double coordinates[2]; is shared among threads.
 
 
1 hour later…
4:50 PM
@dreamcrash No, not at all, here's the results:

Sequential: 19.298s
2 threads: 9.733s 1.98x
4 threads: 4.899s 3.93x
8 threads: 2.738s 7.04x

My machine has 4 cores and 8 threads and I'm running mint 20 with gcc 9.3.0
 
5:10 PM
Nice speeds up, this is with the first or second version? The second version contains some of the optimizations that @user3666197 did, which made the sequential code faster, I added the parallelism there you probably will even get a better execution time
 

last day (15 days later) »