• No se han encontrado resultados

CAPÍTULO 3. CASO PRÁCTICO: DEFINICIÓN

3.3. Caso Práctico

3.3.4. Propuesta de alternativas

1 float3 goal = make_float3( 0.1f,0.2f,0.3f );

2 for (int i=0; i < params->agent_count; ++i) {

3 float3 *mypos = (float3*)(h_positions + i*3);

4 float3 *myvel = (float3*)(h_velocities + i*3);

5

6 *myvel = *myvel + (goal - *mypos) * 0.001;

7 *mypos = *mypos + *myvel * 0.1;

8 }

LISTING5.7: C++ code used for comparison against the MOL code in Listing5.8. The code uses sometypedefs from CUDA.

1 mol

2 defvar goal ={0.1,0.2,0.3}

3 movetowardsgoalby0.001

4 pos = pos + 0.1∗vel

5 end

6

LISTING 5.8: Custom DSL code used to compare against the GNU C++ code in Listing5.7.

5.4. EXAMPLES AND SELECTED RESULTS 117

parallel computing is not always trivially easy to accomplish. It would be interesting to explore the role that recent paradigms such asMSPplay in automatic parallel code generation. It is known that expertise can be embedded as function generators usingMSP, and it appears that parallelism can also be. Also interesting to note is that the MOL compiler is itself unaware that there is automatic vectorisation of thevectorstructures. It is very encouraging to observe automatically generated code which performs well in terms of objective and wall-clock performance. This is demonstrated in Chapter6, where different parallelisation strategies automatically generate suitable code for

GPUs.

In the next section, some examples of MOL syntax are given. This will serve to showcase the major features of this language.

5.4

Examples and Selected Results

Two examples of the lattice-oriented MOL language are given in this section, and also a more brief example of the spatial version. First, Conway’s Game of Life [75] is given in MOL and Lua-Terra in its entirety, followed by a variant of the Predator-prey model [104,257]. Then, a flocking model reminiscent of Boids [240] is given and discussed.

5.4.1

Conway’s Game of Life

(I, p.15)

A sample program written in MOL for Conway’s Game of Life, including some extensions written in Lua-Terra is shown in Listing5.9. Lines 67–82 contain the specialDSLsyntax. Some additional code is provided as Lua-Terra extensions, which make it possible to extend code using the lexical environment at parse time. Much of the functionality presented as extensions could easily be moved into a separate library for future use. In the spirit of the advantages thatMSPafford, this also allows an opportunity to write self-specialising code for future models. The code as shown is placed in a Terra script file (extension.t). Lines 1–67 in fact contain valid Lua code, where any constructs beginning with the keywordterraare first converted to first class Lua values.

Extensions to MOL are written in the form of a Lua function which constructs a Terra expression. This is later spliced into the Terra code generated by the MOL compiler. The MOL compiler passes the lexical environment with all its symbols by reference through the argumentenv. Examples of these are on lines 3, 19 and 38. It is also possible to write Terra functions and use these directly in MOL code, operating on variables and globals in scope. The macro-style MOL extensions take the form of a function, which accesses local and global variables by name. TheenvLua table contains all parsed symbols, including ones that are defined by the compiler: wolattice,

rolattice,me,agent count,score,posx,posy,posz,up,down, left,right,back,front,neighbour,nx,nyandnz. Currently, MOL supports the bare minimum of program control constructs. As seen in Listing5.9, if-then constructs are supported, along with local variables and constant global variables.

The frame compute event function discussed earlier was used to gather data on the Game of Life simulation in Listing5.9. Some metrics are computed over this simulation, including live/dead fractions as well as like-like (LL) bond fractions (shown in Figure5.3). In this case, there are only two cell states (unlike Q-state generalisations of the Game of Life [94]), therefore it can be expected that LL-bond fractions would loosely correlate with the cell live/dead fractions. Nevertheless these are still provided to serve as demonstration. As can be seen, the model reaches equilibrium around time step275.

Quantitative measures are not to be ignored during the development of a modelling platform. Packages such as NetLogo [279] demonstrate this to some extent, using real-time graphing facilities to aid understanding of various

118 5. DOMAIN SPECIFIC LANGUAGE FOR ABM

1 −−not shown: reset function.

2

3 function birth (env)

4 local wolattice

5 local posx= env:localenv ()[ ”posx”]

6 local posy= env:localenv ()[ ”posy”]

7 local posz= env:localenv ()[ ”posz”]

8 wolattice = env:localenv ()[ ” wolattice ” ]

9

10 local lbirth =

11 terra(wolattice: &int , x: int , y: int , z: int )

12 wolattice[

13 [ library . scalar index periodic (x,y,z )]

14 ] = 1

15 end

16 return ‘ lbirth (wolattice, posx,posy,posz)

17 end

18

19 function death(env)

20 local wolattice

21 local posx= env:localenv ()[ ”posx”]

22 local posy= env:localenv ()[ ”posy”]

23 local posz= env:localenv ()[ ”posz”]

24

25 wolattice = env:localenv ()[ ” wolattice ” ]

26

27 local ldeath =

28 terra(wolattice: &int ,x: int ,y: int ,z: int )

29 wolattice[

30 [ library . scalar index periodic (x,y,z )]

31 ] = 0

32 end

33 return ‘ ldeath(wolattice, posx,posy,posz)

34 end

35

36 −−function to compute number of

37 −−adjacent live cells

38 function get neighbour count(env)

39 local rolattice

40 local posx= env:localenv ()[ ”posx”]

41 local posy= env:localenv ()[ ”posy”]

42 local posz= env:localenv ()[ ”posz”]

43 rolattice = env:localenv ()[ ” rolattice ” ]

44

45 −−opportunity to specialise for

46 −−2D or 3D here without effort

47 local getcount =

48 terra(rolattice: &int , x: int , y: int , z: int ): int

49 var c: int = 0

50 for a=−1,2do

51 for b=−1,2do

52 if not (a == 0andb == 0)then

53 if rolattice[

54 [ library . scalar index periodic (‘ x+a,‘y+b,‘z )]

55 ] ˜= 0then 56 c = c + 1 57 end 58 end 59 end 60 end 61 return c 62 end

63 return ‘ getcount(rolattice,posx,posy,posz)

64 end

65

66 −−MOL DSL specific code

67 agent =mol

68 defvar adjacent live cells = get neighbour count

69 if me== 0then

70 if adjacent live cells == 3then

71 birth −−this cell will become live

72 end

73 else

74 if adjacent live cells >= 4then

75 death−−this cell will die

76 else

77 if adjacent live cells <= 1then

78 death−−this cell will die

79 end

80 end

81 end

82 end

LISTING5.9: Conway’s Game of Life written in MOL with some Lua-Terra extensions.

5.4. EXAMPLES AND SELECTED RESULTS 119

Documento similar