Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add setComputeDuration in Task
[simgrid.git] / src / jmsg_task.c
1 /* Functions related to the java task instances.                            */
2
3 /* Copyright (c) 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7   * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "jmsg.h"
10
11 #include "smx_context_java.h"
12
13 #include "jmsg_host.h"
14 #include "jmsg_task.h"
15
16 #include "jxbt_utilities.h"
17
18 #include <msg/msg.h>
19
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
21
22 static jmethodID jtask_method_Comm_constructor;
23
24 static jfieldID jtask_field_Task_bind;
25 static jfieldID jtask_field_Task_name;
26 static jfieldID jtask_field_Comm_bind;
27 static jfieldID jtask_field_Comm_taskBind;
28 static jfieldID jtask_field_Comm_receiving;
29
30 void jtask_bind(jobject jtask, m_task_t task, JNIEnv * env)
31 {
32   (*env)->SetLongField(env, jtask, jtask_field_Task_bind, (jlong) (long) (task));
33 }
34
35 m_task_t jtask_to_native_task(jobject jtask, JNIEnv * env)
36 {
37   return (m_task_t) (long) (*env)->GetLongField(env, jtask, jtask_field_Task_bind);
38 }
39
40 jboolean jtask_is_valid(jobject jtask, JNIEnv * env)
41 {
42   return (*env)->GetLongField(env, jtask, jtask_field_Task_bind) ? JNI_TRUE : JNI_FALSE;
43 }
44
45 JNIEXPORT void JNICALL
46 Java_org_simgrid_msg_Task_nativeInit(JNIEnv *env, jclass cls) {
47         jclass jtask_class_Comm = (*env)->FindClass(env, "org/simgrid/msg/Comm");
48         jclass jtask_class_Task = (*env)->FindClass(env, "org/simgrid/msg/Task");
49
50         jtask_method_Comm_constructor = (*env)->GetMethodID(env, jtask_class_Comm, "<init>", "()V");
51         //FIXME: Don't use jxbt_get_sfield directly, it is slower.
52         jtask_field_Task_bind = jxbt_get_sfield(env, "org/simgrid/msg/Task", "bind", "J");
53         jtask_field_Task_name = jxbt_get_jfield(env, jtask_class_Task, "name", "Ljava/lang/String;");
54         jtask_field_Comm_bind = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "bind", "J");
55         jtask_field_Comm_taskBind = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "taskBind", "J");
56         jtask_field_Comm_receiving = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "receiving", "Z");
57         if (!jtask_field_Task_bind || !jtask_class_Task || !jtask_field_Comm_bind || !jtask_field_Comm_taskBind ||
58                   !jtask_field_Comm_receiving || !jtask_method_Comm_constructor) {
59                         jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
60                   }
61 }
62
63 JNIEXPORT void JNICALL
64 Java_org_simgrid_msg_Task_create(JNIEnv * env,
65                                       jobject jtask, jstring jname,
66                                       jdouble jcomputeDuration,
67                                       jdouble jmessageSize)
68 {
69   m_task_t task;                /* the native task to create                            */
70   const char *name = NULL;      /* the name of the task                                 */
71
72   if (jcomputeDuration < 0) {
73     jxbt_throw_illegal(env,
74                        bprintf
75                        ("Task ComputeDuration (%f) cannot be negative",
76                         (double) jcomputeDuration));
77     return;
78   }
79
80   if (jmessageSize < 0) {
81     jxbt_throw_illegal(env,
82                        bprintf("Task MessageSize (%f) cannot be negative",
83                                (double) jmessageSize));
84     return;
85   }
86
87   if (jname) {
88     /* get the C string from the java string */
89     name = (*env)->GetStringUTFChars(env, jname, 0);
90   }
91
92
93   /* create the task */
94   task =
95       MSG_task_create(name, (double) jcomputeDuration,
96                       (double) jmessageSize, NULL);
97   if (jname)
98     (*env)->ReleaseStringUTFChars(env, jname, name);
99   /* sets the task name */
100   (*env)->SetObjectField(env, jtask, jtask_field_Task_name, jname);
101   /* bind & store the task */
102   jtask_bind(jtask, task, env);
103   MSG_task_set_data(task, jtask);
104 }
105
106 JNIEXPORT void JNICALL
107 Java_org_simgrid_msg_Task_parallelCreate(JNIEnv * env,
108                                                jobject jtask,
109                                                jstring jname,
110                                                jobjectArray jhosts,
111                                                jdoubleArray
112                                                jcomputeDurations_arg,
113                                                jdoubleArray
114                                                jmessageSizes_arg) {
115
116   m_task_t task;                /* the native parallel task to create           */
117   const char *name;             /* the name of the task                         */
118   int host_count;
119   m_host_t *hosts;
120   double *computeDurations;
121   double *messageSizes;
122   jdouble *jcomputeDurations;
123   jdouble *jmessageSizes;
124
125   jobject jhost;
126   int index;
127
128
129   if (!jcomputeDurations_arg) {
130     jxbt_throw_null(env,
131                     xbt_strdup
132                     ("Parallel task compute durations cannot be null"));
133     return;
134   }
135
136   if (!jmessageSizes_arg) {
137     jxbt_throw_null(env,
138                     xbt_strdup
139                     ("Parallel task message sizes cannot be null"));
140     return;
141   }
142
143   if (!jname) {
144     jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
145     return;
146   }
147
148   host_count = (int) (*env)->GetArrayLength(env, jhosts);
149
150
151   hosts = xbt_new0(m_host_t, host_count);
152   computeDurations = xbt_new0(double, host_count);
153   messageSizes = xbt_new0(double, host_count * host_count);
154
155   jcomputeDurations =
156       (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
157   jmessageSizes =
158       (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
159
160   for (index = 0; index < host_count; index++) {
161     jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
162     hosts[index] = jhost_get_native(env, jhost);
163     computeDurations[index] = jcomputeDurations[index];
164   }
165   for (index = 0; index < host_count * host_count; index++) {
166     messageSizes[index] = jmessageSizes[index];
167   }
168
169   (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
170                                      jcomputeDurations, 0);
171   (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
172                                      0);
173
174
175   /* get the C string from the java string */
176   name = (*env)->GetStringUTFChars(env, jname, 0);
177
178   task =
179       MSG_parallel_task_create(name, host_count, hosts, computeDurations,
180                                messageSizes, NULL);
181
182   (*env)->ReleaseStringUTFChars(env, jname, name);
183   /* sets the task name */
184   (*env)->SetObjectField(env, jtask, jtask_field_Task_name, jname);
185   /* associate the java task object and the native task */
186   jtask_bind(jtask, task, env);
187
188   MSG_task_set_data(task, (void *) jtask);
189
190   if (!MSG_task_get_data(task))
191     jxbt_throw_jni(env, "global ref allocation failed");
192 }
193
194 JNIEXPORT void JNICALL
195 Java_org_simgrid_msg_Task_cancel(JNIEnv * env,
196                                       jobject jtask)
197 {
198   m_task_t ptask = jtask_to_native_task(jtask, env);
199
200   if (!ptask) {
201     jxbt_throw_notbound(env, "task", jtask);
202     return;
203   }
204
205   MSG_error_t rv = MSG_task_cancel(ptask);
206
207   jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
208                  bprintf("unexpected error , please report this bug"));
209 }
210
211 JNIEXPORT void JNICALL
212 Java_org_simgrid_msg_Task_execute(JNIEnv * env,
213                                        jobject jtask)
214 {
215   m_task_t task = jtask_to_native_task(jtask, env);
216
217   if (!task) {
218     jxbt_throw_notbound(env, "task", jtask);
219     return;
220   }
221
222   MSG_error_t rv = MSG_task_execute(task);
223
224   jxbt_check_res("MSG_task_execute()", rv,
225                  MSG_HOST_FAILURE | MSG_TASK_CANCELED,
226                  bprintf("while executing task %s",
227                          MSG_task_get_name(task)));
228 }
229
230 JNIEXPORT jstring JNICALL
231 Java_org_simgrid_msg_Task_getName(JNIEnv * env,
232                                        jobject jtask) {
233   m_task_t task = jtask_to_native_task(jtask, env);
234
235   if (!task) {
236     jxbt_throw_notbound(env, "task", jtask);
237     return NULL;
238   }
239
240   return (*env)->NewStringUTF(env, MSG_task_get_name(task));
241 }
242
243 JNIEXPORT jobject JNICALL
244 Java_org_simgrid_msg_Task_getSender(JNIEnv * env,
245                                          jobject jtask) {
246   m_process_t process;
247
248   m_task_t task = jtask_to_native_task(jtask, env);
249
250   if (!task) {
251     jxbt_throw_notbound(env, "task", jtask);
252     return NULL;
253   }
254
255   process = MSG_task_get_sender(task);
256   if (process == NULL) {
257         return NULL;
258   }
259   return (jobject) native_to_java_process(process);
260 }
261
262 JNIEXPORT jobject JNICALL
263 Java_org_simgrid_msg_Task_getSource(JNIEnv * env,
264                                          jobject jtask)
265 {
266   m_host_t host;
267   m_task_t task = jtask_to_native_task(jtask, env);
268
269   if (!task) {
270     jxbt_throw_notbound(env, "task", jtask);
271     return NULL;
272   }
273
274   host = MSG_task_get_source(task);
275   if (host == NULL) {
276         return NULL;
277   }
278   if (!MSG_host_get_data(host)) {
279     jxbt_throw_jni(env, "MSG_task_get_source() failed");
280     return NULL;
281   }
282
283   return (jobject) MSG_host_get_data(host);
284 }
285
286 JNIEXPORT jdouble JNICALL
287 Java_org_simgrid_msg_Task_getComputeDuration(JNIEnv * env,
288                                                   jobject jtask)
289 {
290   m_task_t ptask = jtask_to_native_task(jtask, env);
291
292   if (!ptask) {
293     jxbt_throw_notbound(env, "task", jtask);
294     return -1;
295   }
296   return (jdouble) MSG_task_get_compute_duration(ptask);
297 }
298
299 JNIEXPORT jdouble JNICALL
300 Java_org_simgrid_msg_Task_getRemainingDuration(JNIEnv * env, jobject jtask)
301 {
302   m_task_t ptask = jtask_to_native_task(jtask, env);
303
304   if (!ptask) {
305     jxbt_throw_notbound(env, "task", jtask);
306     return -1;
307   }
308   return (jdouble) MSG_task_get_remaining_computation(ptask);
309 }
310
311 JNIEXPORT void JNICALL
312 Java_org_simgrid_msg_Task_setPriority(JNIEnv * env,
313                                            jobject jtask, jdouble priority)
314 {
315   m_task_t task = jtask_to_native_task(jtask, env);
316
317   if (!task) {
318     jxbt_throw_notbound(env, "task", jtask);
319     return;
320   }
321   MSG_task_set_priority(task, (double) priority);
322 }
323 JNIEXPORT void JNICALL
324 Java_org_simgrid_msg_Task_setComputeDuration
325                 (JNIEnv *env, jobject jtask, jdouble computationAmount) {
326         m_task_t task = jtask_to_native_task(jtask, env);
327
328         if (!task) {
329     jxbt_throw_notbound(env, "task", jtask);
330     return;
331         }
332         MSG_task_set_compute_duration(task, (double) computationAmount);
333 }
334 JNIEXPORT void JNICALL
335 Java_org_simgrid_msg_Task_send(JNIEnv * env,jobject jtask,
336                                     jstring jalias,
337                                     jdouble jtimeout)
338 {
339   MSG_error_t rv;
340   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
341
342   m_task_t task = jtask_to_native_task(jtask, env);
343
344
345   if (!task) {
346     (*env)->ReleaseStringUTFChars(env, jalias, alias);
347     jxbt_throw_notbound(env, "task", jtask);
348     return;
349   }
350
351   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
352   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
353   rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
354
355   (*env)->ReleaseStringUTFChars(env, jalias, alias);
356
357   jxbt_check_res("MSG_task_send_with_timeout()", rv,
358                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
359                  bprintf("while sending task %s to mailbox %s",
360                          MSG_task_get_name(task), alias));
361 }
362
363 JNIEXPORT void JNICALL
364 Java_org_simgrid_msg_Task_sendBounded(JNIEnv * env, jobject jtask,
365                                            jstring jalias,
366                                            jdouble jmaxRate)
367 {
368   m_task_t task = jtask_to_native_task(jtask, env);
369   MSG_error_t rv;
370   const char *alias;
371
372   if (!task) {
373     jxbt_throw_notbound(env, "task", jtask);
374     return;
375   }
376
377   alias = (*env)->GetStringUTFChars(env, jalias, 0);
378
379   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
380   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
381   rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
382
383   (*env)->ReleaseStringUTFChars(env, jalias, alias);
384
385   jxbt_check_res("MSG_task_send_bounded()", rv,
386                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
387                  bprintf
388                  ("while sending task %s to mailbox %s with max rate %f",
389                   MSG_task_get_name(task), alias, (double) jmaxRate));
390
391 }
392
393
394 JNIEXPORT jobject JNICALL
395 Java_org_simgrid_msg_Task_receive(JNIEnv * env, jclass cls,
396                                        jstring jalias, jdouble jtimeout,
397                                        jobject jhost)
398 {
399   MSG_error_t rv;
400   m_task_t task = NULL;
401   m_host_t host = NULL;
402   jobject jtask_global, jtask_local;
403   const char *alias;
404
405   if (jhost) {
406     host = jhost_get_native(env, jhost);
407
408     if (!host) {
409       jxbt_throw_notbound(env, "host", jhost);
410       return NULL;
411     }
412   }
413
414   alias = (*env)->GetStringUTFChars(env, jalias, 0);
415
416   rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
417   if (rv != MSG_OK) {
418         switch (rv) {
419                 case MSG_TIMEOUT:
420                         jxbt_throw_time_out_failure(env,NULL);
421                 break;
422                 case MSG_TRANSFER_FAILURE:
423                         jxbt_throw_transfer_failure(env,NULL);
424                 break;
425                 case MSG_HOST_FAILURE:
426                         jxbt_throw_host_failure(env,NULL);
427                 break;
428                 default:
429                         jxbt_throw_native(env,bprintf("receive failed"));
430         }
431         return NULL;
432   }
433   jtask_global = MSG_task_get_data(task);
434
435   /* Convert the global ref into a local ref so that the JVM can free the stuff */
436   jtask_local = (*env)->NewLocalRef(env, jtask_global);
437   (*env)->DeleteGlobalRef(env, jtask_global);
438   MSG_task_set_data(task, NULL);
439
440   (*env)->ReleaseStringUTFChars(env, jalias, alias);
441
442   jxbt_check_res("MSG_task_receive_ext()", rv,
443                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
444                  bprintf("while receiving from mailbox %s", alias));
445
446   return (jobject) jtask_local;
447 }
448
449
450 JNIEXPORT jobject JNICALL
451 Java_org_simgrid_msg_Task_irecv(JNIEnv * env, jclass cls, jstring jmailbox) {
452         msg_comm_t comm;
453         const char *mailbox;
454         jclass comm_class;
455         //pointer to store the task object pointer.
456         m_task_t *task = xbt_new(m_task_t,1);
457         *task = NULL;
458         /* There should be a cache here */
459         comm_class = (*env)->FindClass(env, "org/simgrid/msg/Comm");
460
461         if (!comm_class) {
462                 jxbt_throw_native(env,bprintf("fieldID or methodID or class not found."));
463                 return NULL;
464         }
465
466         jobject jcomm = (*env)->NewObject(env, comm_class, jtask_method_Comm_constructor);
467         if (!jcomm) {
468                 jxbt_throw_native(env,bprintf("Can't create a Comm object."));
469                 return NULL;
470         }
471
472         mailbox = (*env)->GetStringUTFChars(env, jmailbox, 0);
473
474         comm = MSG_task_irecv(task,mailbox);
475
476         (*env)->SetLongField(env, jcomm, jtask_field_Comm_bind, (jlong) (long)(comm));
477         (*env)->SetLongField(env, jcomm, jtask_field_Comm_taskBind, (jlong) (long)(task));
478         (*env)->SetBooleanField(env, jcomm, jtask_field_Comm_receiving, JNI_TRUE);
479
480         (*env)->ReleaseStringUTFChars(env, jmailbox, mailbox);
481
482         return jcomm;
483 }
484
485 JNIEXPORT jobject JNICALL
486 Java_org_simgrid_msg_Task_isend(JNIEnv *env, jobject jtask, jstring jmailbox) {
487         jclass comm_class;
488
489         const char *mailbox;
490
491         m_task_t task;
492
493         jobject jcomm;
494         msg_comm_t comm;
495
496         comm_class = (*env)->FindClass(env, "org/simgrid/msg/Comm");
497
498         if (!comm_class) return NULL;
499
500         jcomm = (*env)->NewObject(env, comm_class, jtask_method_Comm_constructor);
501         mailbox = (*env)->GetStringUTFChars(env, jmailbox, 0);
502
503         task = jtask_to_native_task(jtask, env);
504
505         if (!task) {
506     (*env)->ReleaseStringUTFChars(env, jmailbox, mailbox);
507     (*env)->DeleteLocalRef(env, jcomm);
508     jxbt_throw_notbound(env, "task", jtask);
509                 return NULL;
510         }
511
512   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
513         comm = MSG_task_isend(task,mailbox);
514
515         (*env)->SetLongField(env, jcomm, jtask_field_Comm_bind, (jlong) (long)(comm));
516         (*env)->SetLongField(env, jcomm, jtask_field_Comm_taskBind, (jlong) (long)(NULL));
517         (*env)->SetBooleanField(env, jcomm, jtask_field_Comm_receiving, JNI_FALSE);
518
519         (*env)->ReleaseStringUTFChars(env, jmailbox, mailbox);
520
521         return jcomm;
522 }
523
524 static void msg_task_cancel_on_failed_dsend(void*t) {
525         m_task_t task = t;
526         JNIEnv *env =get_current_thread_env();
527         jobject jtask_global = MSG_task_get_data(task);
528
529         /* Destroy the global ref so that the JVM can free the stuff */
530         (*env)->DeleteGlobalRef(env, jtask_global);
531         MSG_task_set_data(task, NULL);
532         MSG_task_destroy(task);
533 }
534
535 JNIEXPORT void JNICALL
536 Java_org_simgrid_msg_Task_dsend(JNIEnv * env, jobject jtask,
537                                 jstring jalias) {
538
539   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
540
541   m_task_t task = jtask_to_native_task(jtask, env);
542
543
544   if (!task) {
545     (*env)->ReleaseStringUTFChars(env, jalias, alias);
546     jxbt_throw_notbound(env, "task", jtask);
547     return;
548   }
549
550   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
551   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
552   MSG_task_dsend(task, alias, msg_task_cancel_on_failed_dsend);
553
554   (*env)->ReleaseStringUTFChars(env, jalias, alias);
555 }
556
557 JNIEXPORT jboolean JNICALL
558 Java_org_simgrid_msg_Task_listen(JNIEnv * env, jclass cls,
559                                                                                                            jstring jalias) {
560
561   const char *alias;
562   int rv;
563
564   alias = (*env)->GetStringUTFChars(env, jalias, 0);
565
566   rv = MSG_task_listen(alias);
567
568   (*env)->ReleaseStringUTFChars(env, jalias, alias);
569
570   return (jboolean) rv;
571 }
572
573 JNIEXPORT jint JNICALL
574 Java_org_simgrid_msg_Task_listenFromHost(JNIEnv * env, jclass cls,
575                                                                                                                                            jstring jalias,
576                                                                                                                                            jobject jhost) {
577   int rv;
578   const char *alias;
579
580   m_host_t host = jhost_get_native(env, jhost);
581
582   if (!host) {
583     jxbt_throw_notbound(env, "host", jhost);
584     return -1;
585   }
586   alias = (*env)->GetStringUTFChars(env, jalias, 0);
587
588   rv = MSG_task_listen_from_host(alias, host);
589
590   (*env)->ReleaseStringUTFChars(env, jalias, alias);
591
592   return (jint) rv;
593 }
594
595
596 JNIEXPORT jint JNICALL
597 Java_org_simgrid_msg_Task_listenFrom(JNIEnv * env, jclass cls,
598                                                         jstring jalias) {
599
600   int rv;
601   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
602
603   rv = MSG_task_listen_from(alias);
604
605   (*env)->ReleaseStringUTFChars(env, jalias, alias);
606
607   return (jint) rv;
608 }