Advertisement
Ambamore

Untitled

Feb 12th, 2022
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. // Shared
  2. my_rank = Get_rank();
  3.  
  4. m = 0
  5.  
  6. if (my_rank == 0) { // core 0
  7. while (flag < p - 1); // Busy-wait until all other cores finish sending
  8.  
  9. PI = 4 * m / n // Computate PI
  10. printf("PI: %d\n ", PI) // print PI
  11.  
  12. } else {
  13. // theoretically n = 100, p = 8
  14. // my_rank 1 (for x = 0; x < 15; x++)
  15. // my_rank 2 (for x = 15; x < 30; x++)
  16. // etc...
  17. for (int x = (ceil(n/p) * (my_rank - 1)); x < (ceil(n/p) * (my_rank)); x++) { // for each point in this process
  18. if x == n { break; } // break when last core finishes x
  19. new_point = generate_point() // randomly generate point in the square
  20. is_in_circle = check_if_in_circle(new_point) // check if point is in circle
  21.  
  22. if (is_in_circle) { // if is in circle
  23. Lock(&add_m_lock) // lock mutex
  24. m += 1 // add to m
  25. Unlock(&add_m_lock) // unlock mutex
  26. }
  27. flag++ // increment flag
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement