Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- my_rank = Get_rank();
- int flag = 0;
- int current_largest = 0;
- if (my_rank == 0) { // core 0
- while (flag < p); // Busy-wait until all processors finish
- for (int x = 0; x < p; x++) { // for each process
- RECEIVE(current_processor_largest, INT, 100, x) // receive current_processor_largest from all cores
- if (current_largest < current_processor_largest) { // if current_processor_largest is greater than current_largest,
- Lock(&add_m_lock) // lock mutex
- current_largest = current_processor_largest // set current largest to current processor largest
- Unlock(&add_m_lock) // unlock mutex
- }
- }
- printf("Largest Number In Array: %d\n ", current_largest) // print current_largest
- } 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...
- int current_processor_largest = 0;
- for (int x = (ceil(n/p) * (my_rank - 1)); x < (ceil(n/p) * (my_rank)); x++) { // for each index in this process
- if x == n { break; } // break so no index out of bound exception
- int_check_if_largest = a[n] // get int of array index n
- if (current_processor_largest < int_check_if_largest) { // if int_check_if_largest is greater than current_processor_largest,
- current_processor_largest = int_check_if_largest; // set current_processor_largest to int_check_if_largest
- }
- }
- SEND(current_processor_largest, INT, 100, 0) // send current_processor_largest to core 0
- flag++; // increment flag
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement