program main !*****************************************************************************80 ! !! MAIN is the main program for TASK_DIVISION. ! ! Discussion: ! ! This program simply demonstrates how one might automate the ! assignment 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. ! ! The routine that is called simulates this process. ! ! Modified: ! ! 20 October 2007 ! ! Author: ! ! John Burkardt ! implicit none integer ( kind = 4 ) proc_first integer ( kind = 4 ) proc_last integer ( kind = 4 ) task_number call timestamp ( ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TASK_DIVISION:' write ( *, '(a)' ) ' FORTRAN90 version' write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' Demonstrate how to automate the assignment of' write ( *, '(a)' ) ' T tasks among P processors.' task_number = 23 proc_first = 0 proc_last = 3 call task_divide ( task_number, proc_first, proc_last ) task_number = 17 proc_first = 1 proc_last = 6 call task_divide ( task_number, proc_first, proc_last ) task_number = 17 proc_first = 4 proc_last = 6 call task_divide ( task_number, proc_first, proc_last ) task_number = 5 proc_first = -2 proc_last = 6 call task_divide ( task_number, proc_first, proc_last ) task_number = 5 proc_first = 0 proc_last = 4 call task_divide ( task_number, proc_first, proc_last ) task_number = 5 proc_first = 0 proc_last = 0 call task_divide ( task_number, proc_first, proc_last ) task_number = 1000 proc_first = 1 proc_last = 17 call task_divide ( task_number, proc_first, proc_last ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TASK_DIVISION:' write ( *, '(a)' ) ' Normal end of execution.' write ( *, '(a)' ) ' ' call timestamp ( ) stop end 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. ! ! Modified: ! ! 20 October 2007 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) A, B, the number to be divided, ! and the divisor. ! ! Output, integer ( kind = 4 ) I4_DIV_ROUNDED, the rounded result ! of the division. ! implicit none integer ( kind = 4 ) a integer ( kind = 4 ) a_abs integer ( kind = 4 ) b integer ( kind = 4 ) b_abs integer ( kind = 4 ) c integer ( kind = 4 ) c_abs integer ( kind = 4 ) c_s integer ( kind = 4 ) i4_div_rounded integer ( kind = 4 ) i4_huge integer ( kind = 4 ) i4_sign if ( a == 0 ) then c_abs = i4_huge ( ) c_s = i4_sign ( b ) else a_abs = abs ( a ) b_abs = abs ( b ) c_s = i4_sign ( a ) * i4_sign ( b ) c_abs = a_abs / b_abs if ( ( 2 * c_abs + 1 ) * b_abs < 2 * a_abs ) then c_abs = c_abs + 1 end if end if c = c_s * c_abs i4_div_rounded = c return end function i4_huge ( ) !*****************************************************************************80 ! !! I4_HUGE returns a "huge" I4. ! ! Discussion: ! ! On an IEEE 32 bit machine, I4_HUGE should be 2**31 - 1, and its ! bit pattern should be ! ! 01111111111111111111111111111111 ! ! In this case, its numerical value is 2147483647. ! ! Using the Dec/Compaq/HP Alpha FORTRAN compiler FORT, I could ! use I4_HUGE() and HUGE interchangeably. ! ! However, when using the G95, the values returned by HUGE were ! not equal to 2147483647, apparently, and were causing severe ! and obscure errors in my random number generator, which needs to ! add I4_HUGE to the seed whenever the seed is negative. So I ! am backing away from invoking HUGE, whereas I4_HUGE is under ! my control. ! ! Explanation: because under G95 the default integer type is 64 bits! ! So HUGE ( 1 ) = a very very huge integer indeed, whereas ! I4_HUGE ( ) = the same old 32 bit big value. ! ! An I4 is an integer ( kind = 4 ) value. ! ! Modified: ! ! 26 January 2007 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Output, integer ( kind = 4 ) I4_HUGE, a "huge" I4. ! implicit none integer ( kind = 4 ) i4 integer ( kind = 4 ) i4_huge i4_huge = 2147483647 return end function i4_sign ( x ) !*****************************************************************************80 ! !! I4_SIGN evaluates the sign of an I4. ! ! Discussion: ! ! An I4 is an integer ( kind = 4 ) value. ! ! Modified: ! ! 27 March 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) X, the number whose sign is desired. ! ! Output, integer ( kind = 4 ) I4_SIGN, the sign of X: ! implicit none integer ( kind = 4 ) i4_sign integer ( kind = 4 ) x if ( x < 0 ) then i4_sign = -1 else i4_sign = +1 end if return end subroutine task_divide ( task_number, proc_first, proc_last ) !*****************************************************************************80 ! !! TASK_DIVIDE 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. ! ! 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 ( kind = 4 ) i_hi integer ( kind = 4 ) i_lo integer ( kind = 4 ) i4_div_rounded integer ( kind = 4 ) proc integer ( kind = 4 ) proc_first integer ( kind = 4 ) proc_last integer ( kind = 4 ) proc_number integer ( kind = 4 ) proc_remain integer ( kind = 4 ) task_number integer ( kind = 4 ) task_proc integer ( kind = 4 ) task_remain proc_number = proc_last + 1 - proc_first write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TASK_DIVIDE' write ( *, '(a)' ) ' Divide T tasks among P processors.' write ( *, '(a)' ) ' ' write ( *, '(a,i8)' ) ' P(first) = ', proc_first write ( *, '(a,i8)' ) ' P(last) = ', proc_last write ( *, '(a)' ) ' ' write ( *, '(a,i8)' ) ' T = ', task_number write ( *, '(a,i8)' ) ' P = ', proc_number write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' Processor Tasks First Last' write ( *, '(a)' ) ' ' i_hi = 0 task_remain = task_number proc_remain = proc_number 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 subroutine timestamp ( ) !*****************************************************************************80 ! !! TIMESTAMP prints the current YMDHMS date as a time stamp. ! ! Example: ! ! 31 May 2001 9:45:54.872 AM ! ! Modified: ! ! 06 August 2005 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! None ! implicit none character ( len = 8 ) ampm integer ( kind = 4 ) d integer ( kind = 4 ) h integer ( kind = 4 ) m integer ( kind = 4 ) mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer ( kind = 4 ) n integer ( kind = 4 ) s integer ( kind = 4 ) values(8) integer ( kind = 4 ) y call date_and_time ( values = values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(i2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end