Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
26dc2b021c7ec8111a5cb492644cd17d17e6d3ea
[simgrid.git] / src / bindings / java / org / simgrid / msg / Task.java
1 /* Copyright (c) 2006-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 package org.simgrid.msg;
8
9 /**
10  * A task is either something to compute somewhere, or something to exchange between two hosts (or both).
11  * It is defined by a computing amount and a message size.
12  *
13  */
14 public class Task {
15         /**
16          * This attribute represents a bind between a java task object and
17          * a native task. Even if this attribute is public you must never
18          * access to it. It is set automatically during the build of the object.
19          */
20         private long bind = 0;
21         /**
22          * Task name
23          */
24         protected String name;
25
26         private double messageSize;
27
28         /** Default constructor (all fields to 0 or null) */
29         public Task() {
30                 create(null, 0, 0);
31                 this.messageSize = 0;
32         }
33
34         /* *              * *
35          * * Constructors * *
36          * *              * */
37         /**
38          * Construct a new task with the specified processing amount and amount
39          * of data needed.
40          *
41          * @param name  Task's name
42          *
43          * @param flopsAmount   A value of the processing amount (in flop) needed to process the task. 
44          *                              If 0, then it cannot be executed with the execute() method.
45          *                              This value has to be ≥ 0.
46          *
47          * @param bytesAmount           A value of amount of data (in bytes) needed to transfert this task.
48          *                              If 0, then it cannot be transfered with the get() and put() methods.
49          *                              This value has to be ≥ 0.
50          */ 
51         public Task(String name, double flopsAmount, double bytesAmount) {
52                 create(name, flopsAmount, bytesAmount);
53                 this.messageSize = bytesAmount;
54         }
55         /**
56          * Construct a new parallel task with the specified processing amount and amount for each host
57          * implied.
58          *
59          * @param name          The name of the parallel task.
60          * @param hosts         The list of hosts implied by the parallel task.
61          * @param flopsAmount   The amount of operations to be performed by each host of hosts. 
62          *                      flopsAmount[i] is the total number of operations that have to be 
63          *                      performed on hosts[i].
64          * @param bytesAmount   A matrix describing the amount of data to exchange between hosts. The 
65          *                      length of this array must be hosts.length * hosts.length. It is actually 
66          *                      used as a matrix with the lines being the source and the columns being 
67          *                      the destination of the communications.
68          */ 
69         public Task(String name, Host[]hosts, double[]flopsAmount, double[]bytesAmount) {
70                 parallelCreate(name, hosts, flopsAmount, bytesAmount);
71         }
72
73         /**
74          * The natively implemented method to create a MSG task.
75          *
76          * @param name            The name of the task.
77          * @param flopsAmount    A value of the processing amount (in flop) needed 
78          *                        to process the task. If 0, then it cannot be executed
79          *                        with the execute() method. This value has to be >= 0.
80          * @param bytesAmount        A value of amount of data (in bytes) needed to transfert 
81          *                        this task. If 0, then it cannot be transfered this task. 
82          *                        If 0, then it cannot be transfered with the get() and put() 
83          *                        methods. This value has to be >= 0.
84          * @exception             IllegalArgumentException if compute duration <0 or message size <0
85          */
86         private final native void create(String name,
87                         double flopsAmount,
88                         double bytesAmount)
89                                         throws IllegalArgumentException;                
90         /**
91          * The natively implemented method to create a MSG parallel task.
92          *
93          * @param name                The name of the parallel task.
94          * @param hosts               The list of hosts implied by the parallel task.
95          * @param flopsAmount         The total number of operations that have to be performed
96          *                            on the hosts.
97          * @param bytesAmount        An array of doubles
98          *
99          */
100         private final native void parallelCreate(String name,
101                         Host[]hosts,
102                         double[]flopsAmount,
103                         double[]bytesAmount)
104                                         throws NullPointerException, IllegalArgumentException;
105         /* *                   * *
106          * * Getters / Setters * *
107          * *                   * */
108         /** Gets the name of the task */
109         public String getName() {
110                 return name;
111         }
112
113         /** Gets the sender of the task (or null if not sent yet) */
114         public native Process getSender();
115
116         /** Gets the source of the task (or null if not sent yet). */
117         public native Host getSource();   
118
119         /** Gets the remaining amount of flops to execute in this task
120          * 
121          * If it's ongoing, you get the exact amount at the present time. If it's already done, it's 0.
122          */
123         public native double getFlopsAmount();
124         /**
125          * Sets the name of the task
126          * @param name the new task name
127          */
128         public native void setName(String name);
129         /**
130          * This method sets the priority of the computation of the task.
131          * The priority doesn't affect the transfer rate. For example a
132          * priority of 2 will make the task receive two times more cpu than
133          * the other ones.
134          *
135          * @param priority      The new priority of the task.
136          */ 
137         public native void setPriority(double priority);
138
139         /** Set the computation amount needed to process the task
140          * 
141          * Warning if the execution is already started and ongoing, this call does nothing.
142          * @param flopsAmount the amount of computation needed to process the task
143          */
144         public native void setFlopsAmount(double flopsAmount);
145         /**
146          * Set the amount of bytes to exchange the task
147          * 
148          * Warning if the communication is already started and ongoing, this call does nothing.
149          * @param bytesAmount the size of the task
150          */
151         public native void setBytesAmount(double bytesAmount);
152         /* *                     * *
153          * * Computation-related * *
154          * *                     * */
155         /**
156          * Executes a task on the location on which the current process is running.
157          *
158          * @throws HostFailureException
159          * @throws TaskCancelledException
160          */
161         public native void execute() throws HostFailureException,TaskCancelledException;
162
163         /** Changes the maximum CPU utilization of a computation task. Unit is flops/s. */
164         public native void setBound(double bound); 
165
166         /** Cancels a task. */ 
167         public native void cancel();
168
169         /** Deletes a task once the garbage collector reclaims it */
170         protected void finalize() throws Throwable{
171                 nativeFinalize();
172                 bind=0; // to avoid segfaults if the impossible happens yet again making this task surviving its finalize()
173         }
174         protected native void nativeFinalize();
175         /* *                       * *
176          * * Communication-related * *
177          * *                       * */
178
179         /** Send the task asynchronously on the specified mailbox, 
180          *  with no way to retrieve whether the communication succeeded or not
181          * 
182          */
183         public native void dsendBounded(String mailbox, double maxrate);
184
185
186         /** Send the task asynchronously on the specified mailbox, 
187          *  with no way to retrieve whether the communication succeeded or not
188          * 
189          */
190         public native void dsend(String mailbox);
191
192         /**
193          * Sends the task on the specified mailbox 
194          *
195          * @param mailbox where to send the message
196          * @throws TimeoutException
197          * @throws HostFailureException 
198          * @throws TransferFailureException 
199          */
200         public void send(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException, NativeException {
201                 send(mailbox, -1);
202         } 
203
204         /**
205          * Sends the task on the specified mailbox (wait at most \a timeout seconds)
206          *
207          * @param mailbox where to send the message
208          * @param timeout
209          * @exception  NativeException if the retrieval fails.
210          * @throws TimeoutException 
211          * @throws HostFailureException 
212          * @throws TransferFailureException 
213          */
214         public native void send(String mailbox, double timeout) throws TransferFailureException, HostFailureException, TimeoutException, NativeException;
215
216         /** Sends the task on the specified mailbox (capping the sending rate to \a maxrate) 
217          *
218          * @param mailbox where to send the message
219          * @param maxrate 
220          * @throws TransferFailureException
221          * @throws HostFailureException
222          * @throws TimeoutException
223          */
224         public void sendBounded(String mailbox, double maxrate) throws TransferFailureException, HostFailureException, TimeoutException {
225                 sendBounded(mailbox,-1,maxrate);
226         }
227
228
229         /** Sends the task on the specified mailbox (capping the sending rate to \a maxrate) with a timeout
230          *
231          * @param mailbox where to send the message
232          * @param timeout
233          * @param maxrate 
234          * @throws TransferFailureException
235          * @throws HostFailureException
236          * @throws TimeoutException
237          */
238         public native void sendBounded(String mailbox, double timeout, double maxrate) throws TransferFailureException, HostFailureException, TimeoutException;
239
240
241         /**
242          * Sends the task on the mailbox asynchronously
243          */
244         public native Comm isend(String mailbox);
245
246         /**
247          * Sends the task on the mailbox asynchronously (capping the sending rate to \a maxrate)
248          */
249         public native Comm isendBounded(String mailbox, double maxrate);
250
251
252         /**
253          * Starts listening for receiving a task from an asynchronous communication
254          * @param mailbox
255          * @return a Comm handler
256          */
257         public static native Comm irecv(String mailbox);
258         /**
259          * Retrieves next task from the mailbox identified by the specified name
260          *
261          * @param mailbox
262          * @return a Task
263          */
264
265         public static Task receive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
266                 return receive(mailbox, -1.0, null);
267         }
268
269         /**
270          * Retrieves next task on the mailbox identified by the specified name (wait at most \a timeout seconds)
271          *
272          * @param mailbox
273          * @param timeout
274          * @return a Task
275          */
276         public static Task receive(String mailbox, double timeout) throws  TransferFailureException, HostFailureException, TimeoutException {
277                 return receive(mailbox, timeout, null);
278         }
279
280         /**
281          * Retrieves next task sent by a given host on the mailbox identified by the specified alias 
282          *
283          * @param mailbox
284          * @param host
285          * @return a Task
286          */
287
288         public static Task receive(String mailbox, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
289                 return receive(mailbox, -1.0, host);
290         }
291
292         /**
293          * Retrieves next task sent by a given host on the mailbox identified by the specified alias (wait at most \a timeout seconds)
294          *
295          * @param mailbox
296          * @param timeout 
297          * @param host
298          * @return a Task
299          */
300         public static native Task receive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException;
301
302         /**
303          * Starts listening for receiving a task from an asynchronous communication with a capped rate
304          * @param mailbox
305          * @return a Comm handler
306          */
307         public static native Comm irecvBounded(String mailbox, double rate);
308         /**
309          * Retrieves next task from the mailbox identified by the specified name with a capped rate
310          *
311          * @param mailbox
312          * @return a Task
313          */
314
315         public static Task receiveBounded(String mailbox, double rate) throws TransferFailureException, HostFailureException, TimeoutException {
316                 return receiveBounded(mailbox, -1.0, null, rate);
317         }
318
319         /**
320          * Retrieves next task on the mailbox identified by the specified name (wait at most \a timeout seconds) with a capped rate
321          *
322          * @param mailbox
323          * @param timeout
324          * @return a Task
325          */
326         public static Task receiveBounded(String mailbox, double timeout, double rate) throws  TransferFailureException, HostFailureException, TimeoutException {
327                 return receiveBounded(mailbox, timeout, null, rate);
328         }
329
330         /**
331          * Retrieves next task sent by a given host on the mailbox identified by the specified alias with a capped rate
332          *
333          * @param mailbox
334          * @param host
335          * @return a Task
336          */
337
338         public static Task receiveBounded(String mailbox, Host host, double rate) throws TransferFailureException, HostFailureException, TimeoutException {
339                 return receiveBounded(mailbox, -1.0, host, rate);
340         }
341
342         /**
343          * Retrieves next task sent by a given host on the mailbox identified by the specified alias (wait at most \a timeout seconds)
344          * with a capped rate
345          *
346          * @param mailbox
347          * @param timeout 
348          * @param host
349          * @return a Task
350          */
351         public static native Task receiveBounded(String mailbox, double timeout, Host host, double rate) throws TransferFailureException, HostFailureException, TimeoutException;
352
353
354
355         /**
356          * Tests whether there is a pending communication on the mailbox identified by the specified alias, and who sent it
357          */
358         public static native int listenFrom(String mailbox);
359         /**
360          * Listen whether there is a task waiting (either for a send or a recv) on the mailbox identified by the specified alias
361          */
362         public static native boolean listen(String mailbox);
363
364         /**
365          * Class initializer, to initialize various JNI stuff
366          */
367         public static native void nativeInit();
368         static {
369                 org.simgrid.NativeLib.nativeInit();
370                 nativeInit();
371         }
372
373         public double getMessageSize() {
374                 return this.messageSize;
375         }
376 }