Hi Alex,

Great to see more people using Julia for scientific simulations!

Making copies of your offspring is indeed likely to be detrimental to your 
model's performance--especially if (presumably) you wish to repeat the 
simulation many times to obtain accurate estimates of whatever behavior you 
are interested in. However, I don't think that simply changing pointer from 
population individuals to offspring individuals will allow you to obtain 
the best performance since this is likely to lead to issues when you start 
pre-allocating memory to the population arrays that contain your 
individuals. Especially when you repeat the simulation many times over, 
pre-allocating will likely lead to huge performance gains.

Assuming your population size is fixed, I would guess something like the 
following could greatly speed up your simulation while only requiring minor 
adjustments to your program:

function  siminsects(repetitions, populationsize)
  # pre-allocate population arrays
  world = Array(TPop, populationsize)
  newpop = Array(TPop, populationsize)

  # run the simulation in a way that modifies population arrays instead of 
creating new ones for each run
  for i_rep in 1:repetitions
    simonce!(world, newpop)
  end
end

function simonce!(world, newpop, populationsize)
  # now create the world population array by modifying each of the 
individuals in it to avoid memory allocation
  initworld!(world, populationsize)

  # then run the rest of the simulation, while 'creating' the offspring 
population arrays in the same way as the population array

  # when the offspring population has to replace the world population, do 
this by modifying as well
  replacepopulation!(world, newpop, populationsize)
end

function initworld!(world, populationsize)
  for i_individual in 1:populationsize
    world[i_individual] = TInd(whatever_float64_you_want, 
whatever_bool_you_want)
  end
end

function replacepopulation!(world, newpop, populationsize)
  for i_individual in 1:population size
    # in my experience, assigning the various attributes of the types 
directly is usually the fastest way to do this
    world[i_individual].ld = newpop[i_individual].ld
    world[i_individual].disp = newpop[i_individual].disp
  end
end 


I hope this helps.

Best,

Allard


Op maandag 7 september 2015 19:08:50 UTC+2 schreef [email protected]:
>
> Hello everybody,
>
> I'm comparably new to Julia, but not completely new to programming. Yet, 
> I'm a biologist by training, so please excuse potentially dumb questions in 
> advance :)
>
> I am working in evolutionary ecology, programming individual-based 
> simulations. I have now transferred a (very) simple program that simulates 
> insect populations into Julia and am so far happy with its performance and 
> style (I really fell in love with Julia). Yet, I do have a performance 
> problem when it comes to copying a complex object. First of all my basic 
> type structure:
>
> type TInd # an individual
>   ld::Float64
>   disp::Bool
> end
>
> type TPop # a single population
>     ind::Array{TInd,1}
> end
>
> world = TPop[] # just to mention, this is NOT a global variable, but in 
> my main simulation function to create multiple populations
>
> You see that I have a set (world) of populations (TPop), each being 
> defined as arrays of individuals. During reproduction, I create a second 
> Array of individuals, that stores the (mutating) offspring. So far so good. 
> Yet, since I assume discrete generations, after each individual in a 
> population has reproduced, the parental population is to be replaced by the 
> offspring. I have implemented that like this:
>
> newpop = TInd[]
>
> # ... then the new population gets filled with offspring ...
>
> world[p].ind = deepcopy(newpop)
>
> Of course, this solution is working, but it is really slow. And since I do 
> actually not need a copy of the newpop, but just want it to overwrite the 
> original population, I guessed there might probably be a faster and more 
> elegant solution (without complex workarounds, just somehow adjusting the 
> pointers!?)? From what I've seen the model will probably run faster than in 
> C++ as soon as I find the answer :)))
>
> I appreciate any help, thanks a lot in advance!
>
> All the best,
> Alex
>

Reply via email to