      SUBROUTINE SETASKS (NTASK,NPROC, ITAB, NKPASS, NCPU)
C
C     * AUGUST 19/97 - B.DENIS
C     
C     * GIVEN THE NUMBER OF TASKS (NTASK) TO BE DONE AND THE NUMBER OF
C     * PROCESSOR (NPROC) MADE AVAILABLE BY THE USER THIS ROUTINE 
C     * SETS HOW THE MACROTASKED LOOP WILL BE DONE. IF NPROC IS NOT BIG
C     * ENOUGH TO DO ALL THE LOOP IN ONE PASS, THE LOOP WILL BREAK
C     * AND DONE IN NKPASS PASSES. THE NUMBER OF PROC NEEDED FOR EACH 
C     * PASS IS PUT IN THE TABLE ITAB.
C
C     * INPUT
C     * -----
C     * NTASK   : NUMBER OF TASK TO BE DONE IN A MACROTASKED LOOP
C     * NPROC   : NUMBER OF PROC MADE AVAILABLE BY THE USER.
C
C     * OUTPUT 
C     * ------
C     * ITAB    : TABLE CONTANING THE NUMBER OF PROC FOR EACH PASS.
C     * NKPASS  : NUMBER OF PASSES NEEDED TO COMPLETE THE WHOLE LOOP.
C     * NCPU    : MAX NUMBER OF PROCESSOR THAT WILL BE NEEDED
C
C     * FIRST FIND IF NPROC IS A INTEGRAL DIVISOR OF NTASK.
C     * THE RESULT (NKPASS) REPRESENTS THE NUMBER OF PTFORK
C     * PASS NEEDED TO DO ALL THE NTASK.

      IMPLICIT REAL (A-H,O-Z),
     +INTEGER (I-N)

      INTEGER ITAB(1)
C     
      IMOD=MOD(NTASK,NPROC)

      IF (IMOD.EQ.0) THEN
C
C        * FIND THE NUMBER OF PTFORK PASSES NEEDED.
C     
         NKPASS  = NTASK/NPROC
C     
C        * FIND THE NUMBER OF PROC BY PTFORK PASS.
C
         NCPU    = NTASK/NKPASS
C     
C        * PUT THE NUMBER OF TASK THE NKPASS PTFORK PASS IN ITAB.
C     
         DO 100 I=1,NKPASS
            ITAB(I)=NCPU
  100    CONTINUE
C     
      ELSE
C     
C        * IN THE CASE THAT NPROC IS NOT A INTEGRAL DIVISOR OF NTASK
C        * (THE DIVISION NTASK/NPROC HAS A REST) ONE MORE PTFROK PASS 
C        * WILL BE NEEDED. THE NUMBER OF TASK BY PTFROK PASS IS COMPUTED 
C        * AND PUT IN THE ARRAY ITAB.
C     
         NKPASS= NTASK/NPROC + 1
C     
C        * PUT INITIAL NUMBER OF TASK BY PTFORK IN ITAB.
C     
         DO 200 I=1,NKPASS-1
            ITAB(I)=NPROC
  200    CONTINUE 
         ITAB(NKPASS)=IMOD
C     
C        * REARRANGE THE NUMBER OF TASK AMONG THE PTFORK PASSES 
C        * TO MINIMIZED THE NUMBER OF PROC USED IN EACH PASS AND
C        * TO BALANCE THE LOAD AMONG THE PTFORK PASSES.
C     
         DO 300 K=1,NPROC
         DO 300 I=NKPASS,1,-1
         DO 300 J=NKPASS-1,1,-1
            IF ((ITAB(J)-ITAB(I)).GT.1) THEN
                ITAB(I)=ITAB(I)+1
                ITAB(J)=ITAB(J)-1
            ENDIF
  300    CONTINUE
      ENDIF
            
C     * FIND THE MAX OF PROC REQUIRED AMONG OF THE PTFORK PASSES.
            
      NCPU=1
      DO 400 J=1, NKPASS
         IMAXPRO=ITAB(J)
         IF (IMAXPRO.GT.NCPU) NCPU=IMAXPRO
  400 CONTINUE
C     
      RETURN
      END
