1 /* Copyright (c) 2006-2016. The SimGrid Team. All rights reserved. */
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. */
6 package org.simgrid.msg;
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.
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.
19 private long bind = 0;
23 protected String name;
25 private double messageSize;
27 /** Default constructor (all fields to 0 or null) */
37 * Construct a new task with the specified processing amount and amount
40 * @param name Task's name
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.
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.
50 public Task(String name, double flopsAmount, double bytesAmount) {
51 create(name, flopsAmount, bytesAmount);
52 this.messageSize = bytesAmount;
55 * Construct a new parallel task with the specified processing amount and amount for each host
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.
68 public Task(String name, Host[]hosts, double[]flopsAmount, double[]bytesAmount) {
69 parallelCreate(name, hosts, flopsAmount, bytesAmount);
73 * The natively implemented method to create a MSG task.
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
85 private final native void create(String name,
89 * The natively implemented method to create a MSG parallel task.
91 * @param name The name of the parallel task.
92 * @param hosts The list of hosts implied by the parallel task.
93 * @param flopsAmount The total number of operations that have to be performed
95 * @param bytesAmount An array of doubles
98 private final native void parallelCreate(String name,
101 double[]bytesAmount);
103 * * Getters / Setters * *
105 /** Gets the name of the task */
106 public String getName() {
110 /** Gets the sender of the task (or null if not sent yet) */
111 public native Process getSender();
113 /** Gets the source of the task (or null if not sent yet). */
114 public native Host getSource();
116 /** Gets the remaining amount of flops to execute in this task
118 * If it's ongoing, you get the exact amount at the present time. If it's already done, it's 0.
120 public native double getFlopsAmount();
122 * Sets the name of the task
123 * @param name the new task name
125 public native void setName(String name);
127 * This method sets the priority of the computation of the task.
128 * The priority doesn't affect the transfer rate. For example a
129 * priority of 2 will make the task receive two times more cpu than
132 * @param priority The new priority of the task.
134 public native void setPriority(double priority);
136 /** Set the computation amount needed to process the task
138 * Warning if the execution is already started and ongoing, this call does nothing.
139 * @param flopsAmount the amount of computation needed to process the task
141 public native void setFlopsAmount(double flopsAmount);
143 * Set the amount of bytes to exchange the task
145 * Warning if the communication is already started and ongoing, this call does nothing.
146 * @param bytesAmount the size of the task
148 public native void setBytesAmount(double bytesAmount);
150 * * Computation-related * *
153 * Executes a task on the location on which the current process is running.
155 * @throws HostFailureException
156 * @throws TaskCancelledException
158 public native void execute() throws HostFailureException,TaskCancelledException;
160 /** Changes the maximum CPU utilization of a computation task. Unit is flops/s. */
161 public native void setBound(double bound);
163 /** Cancels a task. */
164 public native void cancel();
166 /** Deletes a task once the garbage collector reclaims it */
168 protected void finalize() throws Throwable{
170 bind=0; // to avoid segfaults if the impossible happens yet again making this task surviving its finalize()
172 protected native void nativeFinalize();
174 * * Communication-related * *
177 /** Send the task asynchronously on the specified mailbox,
178 * with no way to retrieve whether the communication succeeded or not
181 public native void dsendBounded(String mailbox, double maxrate);
184 /** Send the task asynchronously on the specified mailbox,
185 * with no way to retrieve whether the communication succeeded or not
188 public native void dsend(String mailbox);
191 * Sends the task on the specified mailbox
193 * @param mailbox where to send the message
194 * @throws TimeoutException
195 * @throws HostFailureException
196 * @throws TransferFailureException
198 public void send(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
203 * Sends the task on the specified mailbox (wait at most \a timeout seconds)
205 * @param mailbox where to send the message
207 * @throws TimeoutException
208 * @throws HostFailureException
209 * @throws TransferFailureException
211 public native void send(String mailbox, double timeout) throws TransferFailureException, HostFailureException, TimeoutException;
213 /** Sends the task on the specified mailbox (capping the sending rate to \a maxrate)
215 * @param mailbox where to send the message
217 * @throws TransferFailureException
218 * @throws HostFailureException
219 * @throws TimeoutException
221 public void sendBounded(String mailbox, double maxrate) throws TransferFailureException, HostFailureException, TimeoutException {
222 sendBounded(mailbox,-1,maxrate);
226 /** Sends the task on the specified mailbox (capping the sending rate to \a maxrate) with a timeout
228 * @param mailbox where to send the message
231 * @throws TransferFailureException
232 * @throws HostFailureException
233 * @throws TimeoutException
235 public native void sendBounded(String mailbox, double timeout, double maxrate) throws TransferFailureException, HostFailureException, TimeoutException;
239 * Sends the task on the mailbox asynchronously
241 public native Comm isend(String mailbox);
244 * Sends the task on the mailbox asynchronously (capping the sending rate to \a maxrate)
246 public native Comm isendBounded(String mailbox, double maxrate);
250 * Starts listening for receiving a task from an asynchronous communication
252 * @return a Comm handler
254 public static native Comm irecv(String mailbox);
256 * Retrieves next task from the mailbox identified by the specified name
262 public static Task receive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
263 return receive(mailbox, -1.0, null);
267 * Retrieves next task on the mailbox identified by the specified name (wait at most \a timeout seconds)
273 public static Task receive(String mailbox, double timeout) throws TransferFailureException, HostFailureException, TimeoutException {
274 return receive(mailbox, timeout, null);
278 * Retrieves next task sent by a given host on the mailbox identified by the specified alias
285 public static Task receive(String mailbox, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
286 return receive(mailbox, -1.0, host);
290 * Retrieves next task sent by a given host on the mailbox identified by the specified alias (wait at most \a timeout seconds)
297 public static native Task receive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException;
300 * Starts listening for receiving a task from an asynchronous communication with a capped rate
302 * @return a Comm handler
304 public static native Comm irecvBounded(String mailbox, double rate);
306 * Retrieves next task from the mailbox identified by the specified name with a capped rate
312 public static Task receiveBounded(String mailbox, double rate) throws TransferFailureException, HostFailureException, TimeoutException {
313 return receiveBounded(mailbox, -1.0, null, rate);
317 * Retrieves next task on the mailbox identified by the specified name (wait at most \a timeout seconds) with a capped rate
323 public static Task receiveBounded(String mailbox, double timeout, double rate) throws TransferFailureException, HostFailureException, TimeoutException {
324 return receiveBounded(mailbox, timeout, null, rate);
328 * Retrieves next task sent by a given host on the mailbox identified by the specified alias with a capped rate
335 public static Task receiveBounded(String mailbox, Host host, double rate) throws TransferFailureException, HostFailureException, TimeoutException {
336 return receiveBounded(mailbox, -1.0, host, rate);
340 * Retrieves next task sent by a given host on the mailbox identified by the specified alias (wait at most \a timeout seconds)
348 public static native Task receiveBounded(String mailbox, double timeout, Host host, double rate) throws TransferFailureException, HostFailureException, TimeoutException;
353 * Tests whether there is a pending communication on the mailbox identified by the specified alias, and who sent it
355 public static native int listenFrom(String mailbox);
357 * Listen whether there is a task waiting (either for a send or a recv) on the mailbox identified by the specified alias
359 public static native boolean listen(String mailbox);
362 * Class initializer, to initialize various JNI stuff
364 public static native void nativeInit();
366 org.simgrid.NativeLib.nativeInit();
370 public double getMessageSize() {
371 return this.messageSize;