Advertisement
Ambamore

Untitled

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