function i4_div_rounded ( a, b ) !*****************************************************************************80 ! !! i4_div_rounded() computes the rounded result of I4 division. ! ! Discussion: ! ! This routine computes C = A / B, where A, B and C are integers ! and C is the closest integer value to the exact real result. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 23 October 2011 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer A, the number to be divided. ! ! Input, integer B, the divisor, or the number of parts. ! ! Output, integer I4_DIV_ROUNDED, the rounded result ! of the division. ! implicit none integer a integer a_abs integer b integer b_abs integer i4_div_rounded integer, parameter :: i4_huge = 2147483647 integer value if ( a == 0 .and. b == 0 ) then value = i4_huge else if ( a == 0 ) then value = 0 else if ( b == 0 ) then if ( a < 0 ) then value = - i4_huge else value = + i4_huge end if else a_abs = abs ( a ) b_abs = abs ( b ) value = a_abs / b_abs ! ! Round the value. ! if ( ( 2 * value + 1 ) * b_abs < 2 * a_abs ) then value = value + 1 end if ! ! Set the sign. ! if ( ( a < 0 .and. 0 < b ) .or. ( 0 < a .and. b < 0 ) ) then value = - value end if end if i4_div_rounded = value return end subroutine task_division ( task_number, proc_first, proc_last ) !*****************************************************************************80 ! !! task_division() divides tasks among processors. ! ! Discussion: ! ! This routine assigns each of T tasks to P processors, assuming that ! the assignment is to be beforehand. ! ! In that case, we just want to make sure that we assign each task ! to a processor, that we assign about the same number of tasks ! to each processor, and that we assign each processor a contiguous ! range of tasks, say tasks I_LO to I_HI. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 20 October 2007 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer TASK_NUMBER, the number of tasks. ! ! Input, integer PROC_FIRST, PROC_LAST, the first and last processors. ! implicit none integer i_hi integer i_lo integer i4_div_rounded integer p integer proc integer proc_first integer proc_last integer proc_remain integer task_number integer task_proc integer task_remain p = proc_last + 1 - proc_first write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TASK_DIVISION' write ( *, '(a)' ) ' Divide T tasks among P processors.' write ( *, '(a)' ) ' ' write ( *, '(a,i8)' ) ' Number of tasks T = ', task_number write ( *, '(a,i8)' ) ' Number of processors P = ', p write ( *, '(a)' ) ' ' write ( *, '(a,i8)' ) ' P_FIRST = ', proc_first write ( *, '(a,i8)' ) ' P_LAST = ', proc_last write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' Number of First Last' write ( *, '(a)' ) ' Processor Tasks Task Task' write ( *, '(a)' ) ' ' i_hi = 0 task_remain = task_number proc_remain = p do proc = proc_first, proc_last task_proc = i4_div_rounded ( task_remain, proc_remain ) proc_remain = proc_remain - 1 task_remain = task_remain - task_proc i_lo = i_hi + 1 i_hi = i_hi + task_proc write ( *, '(2x,i8,2x,i8,2x,i8,2x,i8)' ) proc, task_proc, i_lo, i_hi end do return end