neurophysik / jitcdde

Just-in-time compilation for delay differential equations
Other
56 stars 14 forks source link

runs way slower with different parameters without clear reason #15

Closed wjadvos closed 5 years ago

wjadvos commented 5 years ago

After changing the code from the Kuramoto example to this:

rom jitcdde import jitcdde, y, t
from numpy import pi, arange, random, max
from symengine import sin
import numpy as np
import matplotlib.pyplot as plt
from time import gmtime, strftime
import os
import sympy

n=2
delay=1
np.random.seed(123)

def orderParam(phis):
        R=0
        for phi in phis:

                R+=np.exp(1j*phi)
        return np.abs(R)/len(phis)

def Simulate(c,initphi,forward=True):
    print ("hier komt de ordeparameter van de initiele toestand")
    print (orderParam(initphi))

    omega= random.standard_cauchy(n)

    q = 0.10
    A = random.choice( [1,0], size=(n,n), p=[q,1-q] )

    def kuramotos():
        for i in range(n):
            yield omega[i]+ omega[i]*sum(
                        sin(y(j,t-delay)-y(i))
                        for j in range(n)
                        if A[j,i]
                        )

    I = jitcdde(kuramotos,n=n,verbose=False)
    I.set_integration_parameters(rtol=0,atol=1e-5)

    I.constant_past(initphi, time=0.0 )
    I.integrate_blindly( delay , 0.1 )

    orderparams=[]
    A=[]
    for time in I.t + arange(0,300,0.2):
        #print(*I.integrate(time) % (2*pi))
        print (time)
        A=(I.integrate(time) % (2*pi))
        Rt=orderParam(A)
        orderparams.append(Rt)
    np.savetxt(simulationtitle+"/coupling: "+str(c)+"fw:"+str(forward)+" .txt",orderparams)

    print (c)

    sync=np.average(orderparams[-1000:])
    return A,sync

The code runs way way slower now, but I don't see why at all, it also keeps running that slow when I decrease the accuracy.

Wrzlprmft commented 5 years ago

@wjadvos: When I arbitrarily choose some parameters, your example code runs fast, so it doesn’t help much. My best wild guess is that changing the parameters radically changes the dynamics and the new one is more difficult to integrate for some reason. Can you please provide a minimal example, ideally one which is fast for you with one choice of parameters and slow with another?

wjadvos commented 5 years ago

This is a minimal working example: when I run it one computer it gets stuck and gives an error message related to the initial steps (in integrate blindly), when I run it on another computer it runs, although it spends far more time on the initial steps than on all the rest

from jitcdde import jitcdde, y, t
from numpy import pi, arange, random, max
from symengine import sin
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from time import gmtime, strftime
import os

n =150
omega = random.uniform(-0.5,0.5,n)

print (omega)
print("hier komt de frequentie distributie")
print (omega)
couplings=np.linspace(0,10,5)

q = 0.10
A = random.choice( [1,0], size=(n,n), p=[q,1-q] )
tau = 1.
maxtime=400
timestep=0.2

def Simulate(c,initphi):
    print ("we zitten nu in de volgende koppeling:")
    print (c)   

    def kuramotos():
        for i in range(n):
            yield omega[i] + abs(omega[i])*c*sum(
                        sin(y(j,t-tau)-y(i))
                        for j in range(n)
                        if A[j,i]
                        )
        print ("oproep")

    I = jitcdde(kuramotos,n=n,verbose=False)
    I.set_integration_parameters(rtol=0,atol=1e-4)

    I.constant_past( initphi)
    I.integrate_blindly( max(tau) ,1 )

    print (time)

Simulate(0.4,random.uniform(0,2*pi,size=n))

I don't understand well enough how I should deal with the first steps in the case I use a fixed time delay and in the case I use different time delays. From the documentation it seems that integrate_blindly is meant for the second, but can I also apply it to the first case?

Wrzlprmft commented 5 years ago

when I run it one computer it gets stuck and gives an error message related to the initial steps (in integrate blindly), when I run it on another computer it runs, although it spends far more time on the initial steps than on all the rest

So, the problem is not with different parameters but different machines? In that case, can you please check the versions (jitcdde.__version__) on both machines and try updating the older one, if they differ? Also, what is the error message you get?

As it stands, I have trouble reproducing your problem. Your example code mostly spends time on the compilation (8 s), which is what I would expect from the top of my head. Note though that it is to be expected that integrate_blindly is slower than regular integration to some extent.

As a very wild guess, I would expect a difference in compilation. Does JiTCDDE use reasonable compilers on both systems? Also, could it possibly be that you run into memory problems on the slower machine? Some compilers can easily use a large amount of memory for larger systems (though this usually only starts to become a problem for systems with ten thousands of nodes).

I don't understand well enough how I should deal with the first steps in the case I use a fixed time delay and in the case I use different time delays. From the documentation it seems that integrate_blindly is meant for the second, but can I also apply it to the first case?

Both, step_on_discontinuities and integrate_blindly can handle single and multiple delays in principle. The main practical difference between the two methods is that step_on_discontinuities is faster for a small number of different delays and slightly more robust while integrate_blindly is faster for a large number of delays. However, often it does not matter which one you choose – if this part is sufficiently fast for you and you can successfully integrate afterwards, do not worry about it. Still, you may need to choose the parameters (step or max_step) appropriately.

wjadvos commented 5 years ago

Thanks for the clarifications: how can I asses which compiler jitcdde uses? Is that the same as the cpp compiler?

from matplotlib import pylab
import matplotlib.pyplot as plt
import os 
from time import gmtime, strftime
delay=1.

def Simulate(c,initphi):    

    def kuramotos():
        for i in range(n):
            yield ω [i]+ c*sum(
                        sin(y(j,t-np.pi/5.)-y(i))
                        for j in range(n)
                        if M[j,i]
                        )

    I = jitcdde(kuramotos,n=n,verbose=False)
    I.set_integration_parameters(rtol=0,atol=1e-5)

    I.constant_past( initphi, time=0.0 )
    I.integrate_blindly( delay , 0.1 )

    orderparams=[]
    A=[]
    for time in I.t + arange(0,600,0.2):
        #print(*I.integrate(time) % (2*pi))
        A=(I.integrate(time) % (2*pi))
        Rt=orderParam(A)
        orderparams.append(Rt)
        print (time)

    np.savetxt(simulationtitle+"/ordertimeseries/"+"oderparams"+str(c)+".txt",orderparams)
    print (c)

    sync=np.average(orderparams[-500:])
    return A,sync

n=1000
q=0.10

ω = random.uniform(0,1,n)
M = random.choice( [1,0], size=(n,n), p=[q,1-q] )
Simulate(0.05,np.random.uniform(0,2*pi,n))

This code with 1000 oscillators doesn't work (in the sense of starting to integrate the system) on both the comuters , does it work on your computer?

Wrzlprmft commented 5 years ago

Thanks for the clarifications: how can I asses which compiler jitcdde uses? Is that the same as the cpp compiler?

You can see what compiler is used by running compile_C with the verbose flag. As for controlling the compiler, please see this documentation section.

I will look at your code later.

Wrzlprmft commented 5 years ago

As for your code, a considerable time is spent on compilation because the network is so large. Without any changes, it takes half an hour for me. You can speed it up a little by decreasing the chunk size (as there is little the compiler can optimise here since everything is run through sines first), e.g., via I.compile_C(chunk_size=1). If you have only very short integration times for one system, you may even benefit from turning off compiler optimisation.

Anyway, with such large system, things take some time.

wjadvos commented 5 years ago

Thanks a lot for the clarifications!