Advertisement
Ambamore

Untitled

Feb 11th, 2022
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. my_rank = Get_rank();
  2.  
  3. int flag = 0;
  4.  
  5. int current_largest = 0;
  6.  
  7. if (my_rank == 0) { // core 0
  8. while (flag < p); // Busy-wait until all processors finish
  9.  
  10. for (int x = 0; x < p; x++) { // for each process
  11. RECEIVE(current_processor_largest, INT, 100, x) // receive current_processor_largest from all cores
  12. if (current_largest < current_processor_largest) { // if current_processor_largest is greater than current_largest,
  13. Lock(&add_m_lock) // lock mutex
  14. current_largest = current_processor_largest // set current largest to current processor largest
  15. Unlock(&add_m_lock) // unlock mutex
  16. }
  17. }
  18.  
  19. printf("Largest Number In Array: %d\n ", current_largest) // print current_largest
  20.  
  21. } else {
  22. // theoretically n = 100, p = 8
  23. // my_rank 1 (for x = 0; x < 15; x++)
  24. // my_rank 2 (for x = 15; x < 30; x++)
  25. // etc...
  26.  
  27. int current_processor_largest = 0;
  28. for (int x = (ceil(n/p) * (my_rank - 1)); x < (ceil(n/p) * (my_rank)); x++) { // for each index in this process
  29. if x == n { break; } // break so no index out of bound exception
  30.  
  31. int_check_if_largest = a[n] // get int of array index n
  32. if (current_processor_largest < int_check_if_largest) { // if int_check_if_largest is greater than current_processor_largest,
  33. current_processor_largest = int_check_if_largest; // set current_processor_largest to int_check_if_largest
  34. }
  35. }
  36. SEND(current_processor_largest, INT, 100, 0) // send current_processor_largest to core 0
  37. flag++; // increment flag
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement