Logo AND Algorithmique Numérique Distribuée

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