Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Distributed
- my_rank = Get_rank();
- if (my_rank == 0) { // core 0
- while (flag < p - 1); // Busy-wait until all other cores finish sending
- m = 0 // Initialize m
- for (int x = 0; x < p; x++) { // for each process
- RECEIVE(current_processor_in_circle_count, INT, 100, x) // receive current_processor_in_circle_count from all other cores
- Lock(&add_m_lock) // lock mutex
- m += current_processor_in_circle_count // add current_processor_in_circle_count to m
- Unlock(&add_m_lock) // unlock mutex
- }
- PI = 4 * m / n // Computate PI
- printf("PI: %d\n ", PI) // print PI
- } else {
- // theoretically n = 100, p = 8
- // my_rank 1 (for x = 0; x < 15; x++)
- // my_rank 2 (for x = 15; x < 30; x++)
- // etc...
- current_processor_in_circle_count = 0
- for (int x = (ceil(n/p) * (my_rank - 1)); x < (ceil(n/p) * (my_rank)); x++) { // for each point in this process
- if x == n { break; } // break when last core finishes x
- new_point = generate_point() // randomly generate point in the square
- is_in_circle = check_if_in_circle(new_point) // check if point is in circle
- current_processor_in_circle_count += 1 //
- flag++ // increment flag
- }
- flag++ // increment flag to signify this process is done
- SEND(current_processor_in_circle_count, INT, 100, 0) // send current_processor_in_circle_count to core 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement