Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add dsend and simulatedSleep to the binding, and add an example
[simgrid.git] / org / simgrid / msg / Task.java
1 /*
2  * simgrid.msg.Task.java        1.00 07/05/01
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier           
5  * All right reserved. 
6  *
7  * This program is free software; you can redistribute 
8  * it and/or modify it under the terms of the license 
9  *(GNU LGPL) which comes with this package. 
10  */
11
12 package org.simgrid.msg;
13
14 /**
15  * A task is either something to compute somewhere, or something to exchange between two hosts (or both).
16  * It is defined by a computing amount and a message size.
17  *
18  */
19 public class Task {
20         /**
21          * This attribute represents a bind between a java task object and
22          * a native task. Even if this attribute is public you must never
23          * access to it. It is set automatically during the build of the object.
24          */
25         public long bind = 0;
26
27
28         /** Default constructor (all fields to 0 or null) */
29         public Task() {
30                 MsgNative.taskCreate(this, null, 0, 0);
31         }
32         /* *              * *
33          * * Constructors * *
34          * *              * */
35         /**
36          * Construct an new task with the specified processing amount and amount
37          * of data needed.
38          *
39          * @param name  Task's name
40          *
41          * @param computeDuration       A value of the processing amount (in flop) needed to process the task. 
42          *                              If 0, then it cannot be executed with the execute() method.
43          *                              This value has to be >= 0.
44          *
45          * @param messageSize           A value of amount of data (in bytes) needed to transfert this task.
46          *                              If 0, then it cannot be transfered with the get() and put() methods.
47          *                              This value has to be >= 0.
48          */ 
49         public Task(String name, double computeDuration, double messageSize) {
50                 MsgNative.taskCreate(this, name, computeDuration, messageSize);
51         }
52         /**
53          * Construct an new parallel task with the specified processing amount and amount for each host
54          * implied.
55          *
56          * @param name          The name of the parallel task.
57          * @param hosts         The list of hosts implied by the parallel task.
58          * @param computeDurations      The amount of operations to be performed by each host of \a hosts.
59          * @param messageSizes  A matrix describing the amount of data to exchange between hosts.
60          */ 
61         public Task(String name, Host[]hosts, double[]computeDurations, double[]messageSizes) {
62                 MsgNative.parallelTaskCreate(this, name, hosts, computeDurations, messageSizes);
63         }
64         /* *                   * *
65          * * Getters / Setters * *
66          * *                   * */
67     /** Gets the name of a task
68      * @return
69      */
70         public String getName() {
71                 Process.ifInterruptedStop();
72                 return MsgNative.taskGetName(this);
73         }
74         /** Gets the sender of the task */ 
75         Process getSender() {
76                 Process.ifInterruptedStop();
77                 return MsgNative.taskGetSender(this);
78         }
79     /** Gets the source of the task
80      * @return
81      */
82         public Host getSource()  {
83                 Process.ifInterruptedStop();
84                 return MsgNative.taskGetSource(this);
85         }
86     /** Gets the computing amount of the task
87      * @return
88      */
89         public double getComputeDuration() {
90                 Process.ifInterruptedStop();
91                 return MsgNative.taskGetComputeDuration(this);
92         }
93     /** Gets the remaining computation of the task
94      * @return
95      */
96         public double getRemainingDuration() {
97                 Process.ifInterruptedStop();
98                 return MsgNative.taskGetRemainingDuration(this);
99         }
100         /**
101          * This method sets the priority of the computation of the task.
102          * The priority doesn't affect the transfert rate. For example a
103          * priority of 2 will make the task receive two times more cpu than
104          * the other ones.
105          *
106          * @param priority      The new priority of the task.
107          */ 
108         public void setPriority(double priority) {
109                 Process.ifInterruptedStop();
110                 MsgNative.taskSetPriority(this, priority);
111         }
112         /* *                       * *
113          * * Communication-related * *
114          * *                       * */
115
116
117         /* *                     * *
118          * * Computation-related * *
119          * *                     * */
120         /**
121          * Executes a task on the location on which the process is running.
122          *
123      *
124      * @throws HostFailureException
125      * @throws TaskCancelledException
126      */
127         public void execute() throws HostFailureException,TaskCancelledException {
128                 Process.ifInterruptedStop();
129                 MsgNative.taskExecute(this);
130         }
131         /**
132          * Cancels a task.
133          *
134          */ 
135         public void cancel()  {
136                 Process.ifInterruptedStop();
137                 MsgNative.taskCancel(this);
138         }
139         /** Deletes a task.
140          *
141          * @exception                   NativeException if the destruction failed.
142          */ 
143         protected void finalize() throws NativeException {
144                 Process.ifInterruptedStop();
145                 if (this.bind != 0)
146                         MsgNative.taskDestroy(this);
147         }
148
149         /** Send the task asynchronously on the mailbox identified by the specified name, 
150          *  with no way to retrieve whether the communication succeeded or not
151          * 
152          */
153         public void dsend(String mailbox) {
154                 MsgNative.taskDSend(mailbox, this);
155         } 
156         
157         /**
158          * Sends the task on the mailbox identified by the specified name 
159          *
160      * @param mailbox
161      * @throws TimeoutException
162          * @throws HostFailureException 
163          * @throws TransferFailureException 
164          */
165         public void send(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
166                 Process.ifInterruptedStop();
167                 MsgNative.taskSend(mailbox, this, -1);
168         } 
169
170         /**
171          * Sends the task on the mailbox identified by the specified name (wait at most \a timeout seconds)
172          *
173      * @param mailbox
174      * @param timeout
175      * @exception  NativeException if the retrieval fails.
176          * @throws TimeoutException 
177          * @throws HostFailureException 
178          * @throws TransferFailureException 
179          */
180         public void send(String mailbox, double timeout) throws NativeException, TransferFailureException, HostFailureException, TimeoutException {
181                 Process.ifInterruptedStop();
182                 MsgNative.taskSend(mailbox, this, timeout);
183         } 
184
185         /**
186          * Sends the task on the mailbox identified by the specified alias  (capping the sending rate to \a maxrate) 
187          *
188      * @param alias
189      * @param maxrate 
190      * @throws TransferFailureException
191      * @throws HostFailureException
192      * @throws TimeoutException
193          */
194         public void sendBounded(String alias, double maxrate) throws TransferFailureException, HostFailureException, TimeoutException {
195                 Process.ifInterruptedStop();
196                 MsgNative.taskSendBounded(alias, this, maxrate);
197         } 
198
199         /**
200          * Retrieves next task from the mailbox identified by the specified name
201          *
202      * @param mailbox
203      * @return
204      * @throws TransferFailureException
205      * @throws HostFailureException
206      * @throws TimeoutException
207          */
208
209         public static Task receive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
210                 Process.ifInterruptedStop();
211                 return MsgNative.taskReceive(mailbox, -1.0, null);
212         }
213
214         /**
215          * Retrieves next task on the mailbox identified by the specified name (wait at most \a timeout seconds)
216          *
217      * @param mailbox
218      * @param timeout
219      * @return 
220      * @throws TransferFailureException
221      * @throws HostFailureException
222      * @throws TimeoutException
223          */
224         public static Task receive(String mailbox, double timeout) throws  TransferFailureException, HostFailureException, TimeoutException {
225                 Process.ifInterruptedStop();
226                 return MsgNative.taskReceive(mailbox, timeout, null);
227         }
228
229         /**
230          * Retrieves next task sent by a given host on the mailbox identified by the specified alias 
231          *
232      * @param mailbox
233      * @param host
234      * @return
235      * @throws TransferFailureException
236      * @throws TimeoutException
237      * @throws HostFailureException
238          */
239
240         public static Task receive(String mailbox, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
241                 Process.ifInterruptedStop();
242                 return MsgNative.taskReceive(mailbox, -1.0, host);
243         }
244
245         /**
246          * Retrieves next task sent by a given host on the mailbox identified by the specified alias (wait at most \a timeout seconds)
247          *
248      * @param mailbox
249      * @param timeout 
250      * @param host
251      * @return 
252      * @throws TransferFailureException
253      * @throws HostFailureException
254      * @throws TimeoutException
255          */
256         public static Task receive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
257                 Process.ifInterruptedStop();
258                 return MsgNative.taskReceive(mailbox, timeout, host);
259         }
260
261         /**
262          * Tests whether there is a pending communication on the mailbox identified by the specified alias, and who sent it
263          *
264      *
265      * @param mailbox
266      * @return
267      */
268         public static int listenFrom(String mailbox)  {
269                 Process.ifInterruptedStop();
270                 return MsgNative.taskListenFrom(mailbox);
271         }
272         /**
273          * Listen whether there is a waiting task on the mailbox identified by the specified alias
274          *
275      *
276      * @param mailbox
277      * @return
278      */
279         public static boolean listen(String mailbox)   {
280                 Process.ifInterruptedStop();
281                 return MsgNative.taskListen(mailbox);
282         }
283
284         /**
285          * Counts the number of tasks waiting to be received on the \a mailbox identified by the specified alia and sended by the specified \a host.
286          *
287      *
288      * @param alias
289      * @param host
290      * @return
291      */
292         public static int listenFromHost(String alias, Host host)   {
293                 Process.ifInterruptedStop();
294                 return MsgNative.taskListenFromHost(alias, host);
295         }
296 }