Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Only try to load winpthread on windows
[simgrid.git] / src / bindings / java / surf.i
1 /* Copyright (c) 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 /* File : example.i */
8 %module(directors="1") Surf
9
10 %include "arrays_java.i"
11 %include "std_string.i"
12 %include "surfdoc.i"
13
14 %pragma(java) jniclassimports=%{
15 import org.simgrid.NativeLib;
16 %}
17 %pragma(java) jniclasscode=%{
18   static {
19     if (System.getProperty("os.name").toLowerCase().startsWith("win"))
20         NativeLib.nativeInit("winpthread");
21     NativeLib.nativeInit("simgrid");
22     NativeLib.nativeInit("surf-java");
23     Runtime.getRuntime().addShutdownHook(
24       new Thread() {
25         public void run() {
26           Thread.currentThread().setName( "Destroyer" );
27           Surf.clean();
28         }
29       }
30     );
31   }
32 %}
33
34 %{
35 #include "src/surf/surf_interface.hpp"
36 #include "src/surf/cpu_interface.hpp"
37 #include "src/surf/network_interface.hpp"
38 #include "src/surf/trace_mgr_private.h"
39 #include "src/bindings/java/surf_swig.hpp"
40 #include "src/xbt/dict_private.h"
41
42 typedef struct lmm_constraint *lmm_constraint_t;
43 typedef xbt_dynar_t DoubleDynar;
44 %}
45
46 %wrapper %{
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_getAction(JNIEnv *env, jclass cls, jlong jarg1) {
52   Action * action = (Action *)jarg1;
53   jobject res;
54   CpuAction *cpu_action = dynamic_cast<CpuAction*>(action);
55   if (cpu_action) {
56     SwigDirector_CpuAction *dir_cpu_action = dynamic_cast<SwigDirector_CpuAction*>(cpu_action);
57     if (dir_cpu_action) {
58       res = dir_cpu_action->swig_get_self(env);\
59     } else {
60       jclass clss = env->FindClass("org/simgrid/surf/CpuAction");\
61       jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
62       res = env->NewObject(clss, constru);\
63       res = env->NewGlobalRef(res);\
64     }
65   } else {
66     jclass clss = env->FindClass("org/simgrid/surf/Action");\
67     jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
68     res = env->NewObject(clss, constru);\
69     res = env->NewGlobalRef(res);\
70   }
71   return res;
72 }
73
74 #define GETDIRECTOR(NAME) \
75 JNIEXPORT jobject JNICALL Java_org_simgrid_surf_SurfJNI_get## NAME ## Director(JNIEnv *env, jclass cls, jlong jarg1)\
76 {\
77   NAME * arg1 = (NAME*)jarg1;\
78   SwigDirector_ ##NAME *director = dynamic_cast<SwigDirector_ ##NAME *>(arg1);\
79   jobject res;\
80   if (director) {\
81     res = director->swig_get_self(env);\
82   } else {\
83     jclass clss = env->FindClass("org/simgrid/surf/NAME");\
84     jmethodID constru = env->GetMethodID(clss, "<init>", "()V");\
85     res = env->NewObject(clss, constru);\
86     res = env->NewGlobalRef(res);\
87   }\
88   return res;\
89 }
90
91 GETDIRECTOR(CpuModel)
92 GETDIRECTOR(Cpu)
93 GETDIRECTOR(CpuAction)
94
95 #ifdef __cplusplus
96 }
97 #endif
98
99 %}
100
101 %typemap(freearg) char* name {
102 }
103
104 /* Handle xbt_dynar_t of Link */
105 JAVA_ARRAYSOFCLASSES(Action);
106 %apply Action[] {ActionArrayPtr};
107 %typemap(jstype) ActionArrayPtr "Action[]"
108 %typemap(javain) ActionArrayPtr "Action.cArrayUnwrap($javainput)"
109 %typemap(javaout) ActionArrayPtr {
110   long[] cArray = $jnicall;
111   Action[] arrayWrapper = new Action[cArray.length];
112   for (int i=0; i<cArray.length; i++)
113     arrayWrapper[i] = (Action) Surf.getAction(cArray[i]);
114   return arrayWrapper;
115   //   return Action.cArrayWrap($jnicall, $owner);
116 }
117 %typemap(out) ActionArrayPtr {
118   long l = 0;
119   for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
120     l++;
121   }
122   $result = jenv->NewLongArray(l);
123   jlong *elts = jenv->GetLongArrayElements($result, NULL);
124   l = 0;
125   for(ActionList::iterator it($1->begin()), itend($1->end()); it != itend ; ++it) {
126     elts[l++] = (jlong)static_cast<Action*>(&*it);
127   }
128   jenv->ReleaseLongArrayElements($result, elts, 0);
129 }
130
131 class ActionList {
132 public:
133   //void push_front(Action &action);
134   //void push_back(Action &action);
135 %extend {
136   ActionArrayPtr getArray(){
137     return $self;
138   }
139 }
140 };
141
142 /* Handle xbt_dynar_t of Link */
143 JAVA_ARRAYSOFCLASSES(Link);
144 %apply Link[] {LinkDynar};
145 %typemap(jstype) LinkDynar "Link[]"
146 %typemap(javain) LinkDynar "Link.cArrayUnwrap($javainput)"
147 %typemap(javaout) LinkDynar {
148      return Link.cArrayWrap($jnicall, $owner);
149 }
150 %typemap(out) LinkDynar {
151   long l = xbt_dynar_length($1);
152   $result = jenv->NewLongArray(l);
153   unsigned i;
154   Link *link;
155   jlong *elts = jenv->GetLongArrayElements($result, NULL);
156   xbt_dynar_foreach($1, i, link) {
157     elts[i] = (jlong)link;
158   }
159   jenv->ReleaseLongArrayElements($result, elts, 0);
160   xbt_dynar_free(&$1);
161 }
162
163 %nodefault DoubleDynar;
164 %typemap(jni) DoubleDynar "jdoubleArray"
165 %rename(DoubleDynar) Double[];
166 %typemap(jtype) DoubleDynar "double[]"
167 %typemap(jstype) DoubleDynar "double[]"
168 %typemap(out) DoubleDynar {
169   long l = xbt_dynar_length($1);
170   $result = jenv->NewDoubleArray(l);
171   double *lout = (double *)xbt_dynar_to_array($1);
172   jenv->SetDoubleArrayRegion($result, 0, l, (jdouble*)lout);
173   free(:);
174 }
175 %typemap(javadirectorin) DoubleDynar "$jniinput"
176 %typemap(directorin,descriptor="[D") DoubleDynar %{
177   long l = xbt_dynar_length($1);
178   $input = jenv->NewDoubleArray(l);
179   double *lout = (double *)xbt_dynar_to_array($1);
180   jenv->SetDoubleArrayRegion($input, 0, l, (jdouble*)lout);
181   free(lout);
182 %}
183 %typemap(javain) DoubleDynar "$javainput"
184 %typemap(javaout) DoubleDynar {return  $jnicall}
185
186 /* Allow to subclass Plugin and send java object to C++ code */
187 %feature("director") Plugin;
188
189 %native(getAction) jobject getAction(jlong jarg1);
190 %native(getCpuModelDirector) jobject getCpuModelDirector(jlong jarg1);
191 %typemap(javaout) CpuModel * {
192   long cPtr = $jnicall;
193   return (CpuModel)Surf.getCpuModelDirector(cPtr);
194 }
195 %native(getCpuDirector) jobject getCpuDirector(jlong jarg1);
196 %typemap(javaout) Cpu * {
197   long cPtr = $jnicall;
198   return (Cpu)Surf.getCpuDirector(cPtr);
199 }
200 %native(getCpuActionDirector) jobject getCpuActionDirector(jlong jarg1);
201 %typemap(javaout) CpuAction * {
202   long cPtr = $jnicall;
203   return (CpuAction)Surf.getCpuDirector(cPtr);
204 }
205
206 %include "src/bindings/java/surf_swig.hpp"
207
208 %rename tmgr_trace TmgrTrace;
209 %nodefaultctor tmgr_trace;
210 struct tmgr_trace {
211   //enum e_trace_type type;
212   /*union {
213     struct {
214       xbt_dynar_t event_list;
215     } s_list;
216     struct {
217       probabilist_event_generator_t event_generator[2];
218       int is_state_trace;
219       int next_event;
220     } s_probabilist;
221   };*/
222   %extend {
223   }
224 };
225
226 %rename tmgr_trace_event TmgrTraceEvent;
227 %nodefaultctor tmgr_trace_event;
228 struct tmgr_trace_event {
229   //tmgr_trace_t trace;
230   //unsigned int idx;
231   //void *model;
232   //int free_me;
233   %extend {
234     unsigned int getIdx() {return 0;}//$self->idx;}
235   }
236 };
237
238 %nodefaultctor Model;
239 class Model {
240 public:
241   Model();
242   
243   virtual double shareResources(double now);
244   virtual double shareResourcesLazy(double now);
245   virtual double shareResourcesFull(double now);
246
247   virtual void updateActionsState(double now, double delta);
248   virtual void updateActionsStateLazy(double now, double delta);
249   virtual void updateActionsStateFull(double now, double delta);
250
251   virtual ActionList *getRunningActionSet();
252
253   virtual void addTraces()=0;
254 };
255
256 %feature("director") CpuModel;
257 class CpuModel : public Model {
258 public:
259   CpuModel();
260   virtual ~CpuModel();
261   virtual Cpu *createCpu(const char *name, DoubleDynar power_peak,
262                               int pstate, double power_scale,
263                               tmgr_trace *power_trace, int core,
264                               e_surf_resource_state_t state_initial,
265                               tmgr_trace *state_trace,
266                               s_xbt_dict *cpu_properties)=0;
267 };
268
269 class Resource {
270 public:
271   Resource();
272   const char *getName();
273   virtual bool isUsed()=0;
274   Model *getModel();
275
276   virtual e_surf_resource_state_t getState();
277   lmm_constraint *getConstraint();
278   s_xbt_dict *getProperties();
279   virtual void updateState(tmgr_trace_event *event_type, double value, double date)=0;
280 };
281
282 %feature("director") Cpu;
283 class Cpu : public Resource {
284 public:
285   Cpu(Model *model, const char *name, s_xbt_dict *props,
286     lmm_constraint *constraint, int core, double powerPeak, double powerScale);
287   Cpu(Model *model, const char *name, s_xbt_dict *props,
288     int core, double powerPeak, double powerScale);
289   virtual ~Cpu();
290   virtual double getCurrentPowerPeak();
291   virtual CpuAction *execute(double size)=0;
292   virtual CpuAction *sleep(double duration)=0;
293   virtual int getCore();
294   virtual double getSpeed(double load);
295   virtual double getAvailableSpeed();
296   virtual double getPowerPeakAt(int pstate_index)=0;
297   virtual int getNbPstates()=0;
298   virtual void setPstate(int pstate_index)=0;
299   virtual int  getPstate()=0;
300   void setState(e_surf_resource_state_t state);
301 };
302
303 class Link : public Resource {
304 public:
305   Link();
306   ~Link();
307   double getBandwidth();
308   void updateBandwidth(double value, double date=surf_get_clock());
309   double getLatency();
310   void updateLatency(double value, double date=surf_get_clock());
311 };
312
313 %nodefaultctor Action;
314 class Action {
315 public:
316   Action(Model *model, double cost, bool failed);
317   virtual ~Action();
318   Model *getModel();
319   lmm_variable *getVariable();
320   e_surf_action_state_t getState();
321   bool isSuspended();
322   double getBound();
323   void setBound(double bound);
324   void updateRemains(double delta);
325   virtual double getRemains();
326   virtual void setPriority(double priority);
327   virtual void setState(e_surf_action_state_t state);
328 };
329
330 %nodefaultctor CpuAction;
331 %feature("director") CpuAction;
332 class CpuAction : public Action {
333 public:
334 CpuAction(Model *model, double cost, bool failed);
335 %extend {
336   Cpu *getCpu() {return getActionCpu($self);}
337 }
338 };
339
340 %nodefaultctor NetworkAction;
341 class NetworkAction : public Action {
342 public:
343 %extend {
344   double getLatency() {return $self->m_latency;}
345 }
346 };
347
348
349 %nodefaultctor RoutingEdge;
350 class RoutingEdge {
351 public:
352   virtual char *getName()=0;
353 };
354
355 %rename lmm_constraint LmmConstraint;
356 struct lmm_constraint {
357 %extend {
358   double getUsage() {return lmm_constraint_get_usage($self);}
359 }
360 };
361
362 %rename lmm_variable LmmVariable;
363 struct lmm_variable {
364 %extend {
365   double getValue() {return lmm_variable_getvalue($self);}
366 }
367 };
368
369 %rename s_xbt_dict XbtDict;
370 struct s_xbt_dict {
371 %extend {
372   char *getValue(char *key) {return (char*)xbt_dict_get_or_null($self, key);}
373 }
374 };
375
376 %rename e_surf_action_state_t ActionState;
377 typedef enum {
378   SURF_ACTION_READY = 0,        /**< Ready        */
379   SURF_ACTION_RUNNING,          /**< Running      */
380   SURF_ACTION_FAILED,           /**< Task Failure */
381   SURF_ACTION_DONE,             /**< Completed    */
382   SURF_ACTION_TO_FREE,          /**< Action to free in next cleanup */
383   SURF_ACTION_NOT_IN_THE_SYSTEM
384                                 /**< Not in the system anymore. Why did you ask ? */
385 } e_surf_action_state_t;
386
387 %rename e_surf_resource_state_t ResourceState;
388 typedef enum {
389   SURF_RESOURCE_ON = 1,                   /**< Up & ready        */
390   SURF_RESOURCE_OFF = 0                   /**< Down & broken     */
391 } e_surf_resource_state_t;