View Javadoc
1   //******************************************************************************
2   //
3   // File:    FixedLongSchedule.java
4   // Package: edu.rit.pj
5   // Unit:    Class edu.rit.pj.FixedLongSchedule
6   //
7   // This Java source file is copyright (C) 2010 by Alan Kaminsky. All rights
8   // reserved. For further information, contact the author, Alan Kaminsky, at
9   // ark@cs.rit.edu.
10  //
11  // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
12  // software; you can redistribute it and/or modify it under the terms of the GNU
13  // General Public License as published by the Free Software Foundation; either
14  // version 3 of the License, or (at your option) any later version.
15  //
16  // PJ is distributed in the hope that it will be useful, but WITHOUT ANY
17  // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18  // A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19  //
20  // Linking this library statically or dynamically with other modules is making a
21  // combined work based on this library. Thus, the terms and conditions of the GNU
22  // General Public License cover the whole combination.
23  //
24  // As a special exception, the copyright holders of this library give you
25  // permission to link this library with independent modules to produce an
26  // executable, regardless of the license terms of these independent modules, and
27  // to copy and distribute the resulting executable under terms of your choice,
28  // provided that you also meet, for each linked independent module, the terms
29  // and conditions of the license of that module. An independent module is a module
30  // which is not derived from or based on this library. If you modify this library,
31  // you may extend this exception to your version of the library, but you are not
32  // obligated to do so. If you do not wish to do so, delete this exception
33  // statement from your version.
34  //
35  // A copy of the GNU General Public License is provided in the file gpl.txt. You
36  // may also obtain a copy of the GNU General Public License on the World Wide
37  // Web at http://www.gnu.org/licenses/gpl.html.
38  //
39  //******************************************************************************
40  package edu.rit.pj;
41  
42  import edu.rit.util.LongRange;
43  
44  /**
45   * Class FixedLongSchedule provides a fixed schedule object. The loop index is
46   * type <code>long</code>. The loop iterations are apportioned among the parallel
47   * team threads once at the beginning of the parallel for loop, with each thread
48   * getting a fixed number of iterations, the same number of iterations for each
49   * thread (plus or minus one).
50   *
51   * @author Alan Kaminsky
52   * @version 27-Jan-2010
53   */
54  class FixedLongSchedule
55          extends LongSchedule {
56  
57  // Hidden data members.
58      // Chunk for each thread.
59      private LongRange[] myChunk;
60  
61  // Exported constructors.
62      /**
63       * Construct a new fixed schedule object.
64       */
65      public FixedLongSchedule() {
66          super();
67      }
68  
69      /**
70       * Construct a new fixed schedule object. This constructor is for use by the
71       * <code>LongSchedule.parse()</code> method.
72       *
73       * @param args Array of argument strings.
74       */
75      public FixedLongSchedule(String[] args) {
76          super();
77          throw new IllegalArgumentException("FixedLongSchedule(): Usage: -Dpj.schedule=fixed");
78      }
79  
80  // Exported operations.
81      /**
82       * Determine if this schedule is a fixed schedule. For a parallel team with
83       * <I>K</I> threads, a fixed schedule partitions the loop index range into
84       * exactly <I>K</I> chunks, one chunk for each thread, each chunk with
85       * predetermined upper and lower bounds.
86       *
87       * @return True if this is a fixed schedule, false otherwise.
88       */
89      public boolean isFixedSchedule() {
90          return true;
91      }
92  
93  // Hidden operations.
94      /**
95       * {@inheritDoc}
96       *
97       * Start generating chunks of iterations for a parallel for loop using this
98       * schedule.
99       * <P>
100      * The <code>start()</code> method is only called by a single thread in the
101      * Parallel Java middleware.
102      */
103     public void start(int K,
104             LongRange theLoopRange) {
105         myChunk = theLoopRange.subranges(K);
106         for (int i = 0; i < K; ++i) {
107             if (myChunk[i].length() == 0) {
108                 myChunk[i] = null;
109             }
110         }
111     }
112 
113     /**
114      * {@inheritDoc}
115      *
116      * Obtain the next chunk of iterations for the given thread index. If there
117      * are more iterations, a range object is returned whose lower bound, upper
118      * bound, and stride specify the chunk of iterations to perform. The
119      * returned range object's stride is the same as that given to the
120      * <code>start()</code> method. The returned range object's lower bound and
121      * upper bound are contained within the range given to the <code>start()</code>
122      * method. If there are no more iterations, null is returned.
123      * <P>
124      * The <code>next()</code> method is called by multiple parallel team threads in
125      * the Parallel Java middleware. The <code>next()</code> method must be multiple
126      * thread safe.
127      */
128     public LongRange next(int theThreadIndex) {
129         LongRange chunk = myChunk[theThreadIndex];
130         myChunk[theThreadIndex] = null;
131         return chunk;
132     }
133 
134 }