Logo AND Algorithmique Numérique Distribuée

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