Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4be7046fad74287cb02708ec9feb90fc258d223b
[simgrid.git] / src / bindings / java / org / simgrid / msg / Task.java
1 /* Copyright (c) 2006-2014. 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         static private Long idCpt = 0L;
29
30         private Long id;
31
32         /** Default constructor (all fields to 0 or null) */
33         public Task() {
34                 create(null, 0, 0);
35                 this.messageSize = 0;
36                 setId(idCpt);
37                 idCpt++;
38         }
39
40         /* *              * *
41          * * Constructors * *
42          * *              * */
43         /**
44          * Construct an new task with the specified processing amount and amount
45          * of data needed.
46          *
47          * @param name  Task's name
48          *
49          * @param computeDuration       A value of the processing amount (in flop) needed to process the task. 
50          *                              If 0, then it cannot be executed with the execute() method.
51          *                              This value has to be >= 0.
52          *
53          * @param messageSize           A value of amount of data (in bytes) needed to transfert this task.
54          *                              If 0, then it cannot be transfered with the get() and put() methods.
55          *                              This value has to be >= 0.
56          */ 
57         public Task(String name, double computeDuration, double messageSize) {
58                 create(name, computeDuration, messageSize);
59                 this.messageSize = messageSize;
60                 setId(idCpt);
61                 idCpt++;
62         }
63         /**
64          * Construct an new parallel task with the specified processing amount and amount for each host
65          * implied.
66          *
67          * @param name          The name of the parallel task.
68          * @param hosts         The list of hosts implied by the parallel task.
69          * @param computeDurations      The amount of operations to be performed by each host of \a hosts.
70          * @param messageSizes  A matrix describing the amount of data to exchange between hosts.
71          */ 
72         public Task(String name, Host[]hosts, double[]computeDurations, double[]messageSizes) {
73                 parallelCreate(name, hosts, computeDurations, messageSizes);
74         }
75         
76         /**
77          * The natively implemented method to create a MSG task.
78          *
79          * @param name            The name of th task.
80          * @param computeDuration    A value of the processing amount (in flop) needed 
81          *                        to process the task. If 0, then it cannot be executed
82          *                        with the execute() method. This value has to be >= 0.
83          * @param messageSize        A value of amount of data (in bytes) needed to transfert 
84          *                        this task. If 0, then it cannot be transfered this task. 
85          *                        If 0, then it cannot be transfered with the get() and put() 
86          *                        methods. This value has to be >= 0.
87          * @exception             IllegalArgumentException if compute duration <0 or message size <0
88          */
89         private final native void create(String name,
90                         double computeDuration,
91                         double messageSize)
92         throws IllegalArgumentException;                
93         /**
94          * The natively implemented method to create a MSG parallel task.
95          *
96          * @param name                The name of the parallel task.
97          * @param hosts                The list of hosts implied by the parallel task.
98          * @param computeDurations    The total number of operations that have to be performed
99          *                            on the hosts.
100          * @param messageSizes        An array of doubles
101          *
102          */
103         private final native void parallelCreate(String name,
104                         Host[]hosts,
105                         double[]computeDurations,
106                         double[]messageSizes)
107         throws NullPointerException, IllegalArgumentException;
108         /* *                   * *
109          * * Getters / Setters * *
110          * *                   * */
111     /** 
112      * Gets the name of a task
113      */
114         public String getName() {
115                 return name;
116         }
117         /**
118          * Gets the sender of the task 
119          * Returns null if the task hasn't been sent yet
120          */
121         public native Process getSender();
122         /** Gets the source of the task.
123          * Returns null if the task hasn't been sent yet.
124      */
125         public native Host getSource();   
126         /** Gets the computing amount of the task
127      * FIXME: Cache it !
128      */
129         public native double getComputeDuration();
130         /** Gets the remaining computation of the task
131      */
132         public native double getRemainingDuration();
133         /**
134          * Sets the name of the task
135          * @param name the new task name.c
136          */
137         public native void setName(String name);
138         /**
139          * This method sets the priority of the computation of the task.
140          * The priority doesn't affect the transfer rate. For example a
141          * priority of 2 will make the task receive two times more cpu than
142          * the other ones.
143          *
144          * @param priority      The new priority of the task.
145          */ 
146         public native void setPriority(double priority);
147         /**
148          * Set the computation amount needed to process the task
149          * @param computationAmount the amount of computation needed to process the task
150          */
151         public native void setComputeDuration(double computationAmount);
152         /**
153          * Set the data size of the task
154          * @param dataSize the size of the task
155          */
156         public native void setDataSize(double dataSize);
157         /* *                     * *
158          * * Computation-related * *
159          * *                     * */
160         /**
161          * Executes a task on the location on which the process is running.
162          *
163      *
164      * @throws HostFailureException
165      * @throws TaskCancelledException
166      */
167         public native void execute() throws HostFailureException,TaskCancelledException;
168         /**
169          * Bound a computation to a certain load
170          *
171          */
172         public native void setBound(double load); 
173         /**
174          * Cancels a task.
175          *
176          */ 
177         public native void cancel();
178         /** Deletes a task.
179          *
180          * @exception                   NativeException if the destruction failed.
181          */ 
182         protected void finalize() throws NativeException {
183                 destroy();
184         }
185         /**
186          * The natively implemented method to destroy a MSG task.
187          */
188         protected native void destroy();
189         /* *                       * *
190          * * Communication-related * *
191          * *                       * */
192
193         /** Send the task asynchronously on the mailbox identified by the specified name, 
194          *  with no way to retrieve whether the communication succeeded or not
195          * 
196          */
197         public native void dsendBounded(String mailbox, double maxrate);
198
199
200         /** Send the task asynchronously on the mailbox identified by the specified name, 
201          *  with no way to retrieve whether the communication succeeded or not
202          * 
203          */
204         public native void dsend(String mailbox);
205         
206         /**
207          * Sends the task on the mailbox identified by the specified name 
208          *
209      * @param mailbox
210      * @throws TimeoutException
211          * @throws HostFailureException 
212          * @throws TransferFailureException 
213          */
214         public void send(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
215                 send(mailbox, -1);
216         } 
217
218         /**
219          * Sends the task on the mailbox identified by the specified name (wait at most \a timeout seconds)
220          *
221      * @param mailbox
222      * @param timeout
223      * @exception  NativeException if the retrieval fails.
224          * @throws TimeoutException 
225          * @throws HostFailureException 
226          * @throws TransferFailureException 
227          */
228         public native void send(String mailbox, double timeout) throws TransferFailureException, HostFailureException, TimeoutException;
229         /**
230          * Sends the task on the mailbox identified by the specified alias  (capping the sending rate to \a maxrate) 
231          *
232      * @param alias
233      * @param maxrate 
234      * @throws TransferFailureException
235      * @throws HostFailureException
236      * @throws TimeoutException
237          */
238         public void sendBounded(String alias, double maxrate) throws TransferFailureException, HostFailureException, TimeoutException {
239               sendBounded(alias,-1,maxrate);
240         }
241
242
243 /**
244          * Sends the task on the mailbox identified by the specified alias  (capping the sending rate to \a maxrate) with a timeout
245          *
246      * @param alias
247      * @param timeout
248      * @param maxrate 
249      * @throws TransferFailureException
250      * @throws HostFailureException
251      * @throws TimeoutException
252          */
253         public native void sendBounded(String alias, double timeout, double maxrate) throws TransferFailureException, HostFailureException, TimeoutException;
254
255
256         /**
257          * Sends the task on the mailbox asynchronously
258          */
259         public native Comm isend(String mailbox);
260
261         /**
262          * Sends the task on the mailbox asynchronously (capping the sending rate to \a maxrate)
263          */
264         public native Comm isendBounded(String mailbox, double maxrate);
265         
266
267         /**
268          * Starts listening for receiving a task from an asynchronous communication
269          * @param mailbox
270          */
271         public static native Comm irecv(String mailbox);
272         /**
273          * Retrieves next task from the mailbox identified by the specified name
274          *
275      * @param mailbox
276          */
277
278         public static Task receive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
279                 return receive(mailbox, -1.0, null);
280         }
281
282         /**
283          * Retrieves next task on the mailbox identified by the specified name (wait at most \a timeout seconds)
284          *
285      * @param mailbox
286      * @param timeout
287          */
288         public static Task receive(String mailbox, double timeout) throws  TransferFailureException, HostFailureException, TimeoutException {
289                 return receive(mailbox, timeout, null);
290         }
291
292         /**
293          * Retrieves next task sent by a given host on the mailbox identified by the specified alias 
294          *
295      * @param mailbox
296      * @param host
297          */
298
299         public static Task receive(String mailbox, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
300                 return receive(mailbox, -1.0, host);
301         }
302
303         /**
304          * Retrieves next task sent by a given host on the mailbox identified by the specified alias (wait at most \a timeout seconds)
305          *
306      * @param mailbox
307      * @param timeout 
308      * @param host
309          */
310         public native static Task receive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException;
311
312         /**
313          * Starts listening for receiving a task from an asynchronous communication with a capped rate
314          * @param mailbox
315          */
316         public static native Comm irecvBounded(String mailbox, double rate);
317         /**
318          * Retrieves next task from the mailbox identified by the specified name with a capped rate
319          *
320      * @param mailbox
321          */
322
323         public static Task receiveBounded(String mailbox, double rate) throws TransferFailureException, HostFailureException, TimeoutException {
324                 return receiveBounded(mailbox, -1.0, null, rate);
325         }
326
327         /**
328          * Retrieves next task on the mailbox identified by the specified name (wait at most \a timeout seconds) with a capped rate
329          *
330      * @param mailbox
331      * @param timeout
332          */
333         public static Task receiveBounded(String mailbox, double timeout, double rate) throws  TransferFailureException, HostFailureException, TimeoutException {
334                 return receiveBounded(mailbox, timeout, null, rate);
335         }
336
337         /**
338          * Retrieves next task sent by a given host on the mailbox identified by the specified alias with a capped rate
339          *
340      * @param mailbox
341      * @param host
342          */
343
344         public static Task receiveBounded(String mailbox, Host host, double rate) throws TransferFailureException, HostFailureException, TimeoutException {
345                 return receiveBounded(mailbox, -1.0, host, rate);
346         }
347
348         /**
349          * Retrieves next task sent by a given host on the mailbox identified by the specified alias (wait at most \a timeout seconds)
350          * with a capped rate
351          *
352      * @param mailbox
353      * @param timeout 
354      * @param host
355          */
356         public native static Task receiveBounded(String mailbox, double timeout, Host host, double rate) throws TransferFailureException, HostFailureException, TimeoutException;
357
358         
359         
360         /**
361          * Tests whether there is a pending communication on the mailbox identified by the specified alias, and who sent it
362      */
363         public native static int listenFrom(String mailbox);
364         /**
365          * Listen whether there is a waiting task on the mailbox identified by the specified alias
366      */
367         public native static boolean listen(String mailbox);
368
369         /**
370          * 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.
371      */
372         public native static int listenFromHost(String alias, Host host);
373         
374         /**
375          * Class initializer, to initialize various JNI stuff
376          */
377         public static native void nativeInit();
378         static {
379                 Msg.nativeInit();
380                 nativeInit();
381         }
382
383         public double getMessageSize() {
384                 return this.messageSize;
385         }
386
387         public Long getId() {
388                 return id;
389         }
390
391         public void setId(Long id) {
392                 this.id = id;
393         }
394 }