View Javadoc
1   //******************************************************************************
2   //
3   // File:    CommRequest.java
4   // Package: edu.rit.pj
5   // Unit:    Class edu.rit.pj.CommRequest
6   //
7   // This Java source file is copyright (C) 2007 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 java.io.IOException;
43  
44  import edu.rit.mp.IORequest;
45  
46  /**
47   * Class CommRequest provides an object for doing a non-blocking message passing
48   * operation in a PJ cluster parallel program. A non-blocking message passing
49   * operation in class {@linkplain Comm} immediately returns a CommRequest object
50   * while performing the message passing operation in a separate thread. This
51   * allows the calling thread to do other work while the message passing
52   * operation is in progress. When the calling thread needs to wait for the
53   * message passing operation to finish, the calling thread calls the CommRequest
54   * object's <code>waitForFinish()</code> method.
55   *
56   * @author Alan Kaminsky
57   * @version 18-Sep-2007
58   */
59  public class CommRequest {
60  
61  // Hidden data members.
62      IORequest mySendRequest;
63      IORequest myRecvRequest;
64  
65  // Exported constructors.
66      /**
67       * Construct a new CommRequest object.
68       */
69      public CommRequest() {
70      }
71  
72  // Exported operations.
73      /**
74       * Determine if the message passing operation associated with this
75       * CommRequest object has finished.
76       * <P>
77       * <I>Note:</I> If the <code>isFinished()</code> method is called on a
78       * newly-created CommRequest object that has not been passed to or returned
79       * from a communicator's non-blocking send or receive method, the
80       * <code>isFinished()</code> method immediately returns true.
81       *
82       * @return False if the message passing operation has not finished, true if
83       * the message passing operation has finished successfully.
84       * @exception IOException Thrown if the message passing operation has
85       * finished and an I/O error occurred.
86       * @throws java.io.IOException if any.
87       */
88      public boolean isFinished()
89              throws IOException {
90          return (mySendRequest == null || mySendRequest.isFinished())
91                  && (myRecvRequest == null || myRecvRequest.isFinished());
92      }
93  
94      /**
95       * Wait for the message passing operation associated with this CommRequest
96       * object to finish. If the message passing operation involved a receive, a
97       * {@linkplain CommStatus} object is returned giving the results of the
98       * receive, otherwise null is returned.
99       * <P>
100      * For a receive operation, the returned status object gives the actual rank
101      * of the process that sent the message, the actual message tag that was
102      * received, and the actual number of data items in the message. If the
103      * actual number of data items in the message is less than the length of the
104      * receive buffer, nothing is stored into the extra data items at the end of
105      * the buffer. If the actual number of data items in the message is greater
106      * than the length of the receive buffer, the extra data items at the end of
107      * the message are discarded.
108      * <P>
109      * <I>Note:</I> If the <code>waitForFinish()</code> method is called on a
110      * newly-created CommRequest object that has not been passed to or returned
111      * from a communicator's non-blocking send or receive method, the
112      * <code>waitForFinish()</code> method immediately returns null.
113      *
114      * @return Status object for a receive operation, otherwise null.
115      * @exception IOException Thrown if an I/O error occurred.
116      * @throws java.io.IOException if any.
117      */
118     public CommStatus waitForFinish()
119             throws IOException {
120         if (mySendRequest != null) {
121             mySendRequest.waitForFinish();
122         }
123         if (myRecvRequest != null) {
124             edu.rit.mp.Status status = myRecvRequest.waitForFinish();
125             return new CommStatus(Comm.getFarRank(status.channel),
126                     status.tag,
127                     status.length);
128         } else {
129             return null;
130         }
131     }
132 
133 }