Révision 121 NBody/NBody.py

NBody.py (revision 121)
40 40

  
41 41
// Elements from : http://doswa.com/2009/01/02/fourth-order-runge-kutta-numerical-integration.html
42 42

  
43
float8 RungeKutta(__global float8* clDataIn,int gid,float dt)
43
float8 AtomicRungeKutta(__global float8* clDataIn,int gid,float dt)
44 44
{
45
    float4 x0=clDataIn[gid].lo;
46
    float4 v0=clDataIn[gid].hi;
47
    float4 a0=(0.,0.,0.,0.);
48
    for (int i=0;i<get_global_size(0);i++)
49
    {
50
        if (gid != i)
51
        a0+=Interaction(x0,clDataIn[i].lo);
52
    }
53
        
54
    float4 x1=x0+v0*(float)0.5*dt;
55
    float4 v1=v0+a0*(float)0.5*dt;
56
    float4 a1=(0.,0.,0.,0.);
57
    for (int i=0;i<get_global_size(0);i++)
58
    {
59
        if (gid != i)
60
        a1+=Interaction(x1,clDataIn[i].lo);
61
    }
62

  
63
    float4 x2=x0+v1*(float)0.5*dt;
64
    float4 v2=v0+a1*(float)0.5*dt;
65
    float4 a2=(0.,0.,0.,0.);
66
    for (int i=0;i<get_global_size(0);i++)
67
    {
68
        if (gid != i)
69
        a2+=Interaction(x2,clDataIn[i].lo);
70
    }
71
    
72
    float4 x3=x0+v2*dt;
73
    float4 v3=v0+a2*dt;
74
    float4 a3=(0.,0.,0.,0.);
75
    for (int i=0;i<get_global_size(0);i++)
76
    {
77
        if (gid != i)
78
        a3+=Interaction(x3,clDataIn[i].lo);
79
    }
80
    
81
    float4 xf=x0+dt*(v0+(float)2.*(v1+v2)+v3)/(float)6.;
82
    float4 vf=v0+dt*(a0+(float)2.*(a1+a2)+a3)/(float)6.;
83
     
84
    return((float8)(xf.s0,xf.s1,xf.s2,xf.s3,vf.s0,vf.s1,vf.s2,vf.s3));
85
}
86

  
87
// Elements from : http://doswa.com/2009/01/02/fourth-order-runge-kutta-numerical-integration.html
88

  
89
float8 AtomicRungeKutta2(__global float8* clDataIn,int gid,float dt)
90
{
45 91
    float4 x[4],v[4],a[4],xf,vf;
46 92

  
47 93
    x[0]=clDataIn[gid].lo;
......
86 132
    return((float8)(xf.s0,xf.s1,xf.s2,xf.s3,vf.s0,vf.s1,vf.s2,vf.s3));
87 133
}
88 134

  
89
float8 Euler(__global float8* clDataIn,int gid,float dt)
135
float8 AtomicEuler(__global float8* clDataIn,int gid,float dt)
90 136
{
91 137
    float4 x,v,a,xf,vf;
92 138

  
......
120 166
    clData[gid].s6=velocity*cos(theta);
121 167
}
122 168

  
123
__kernel void Evolution(__global float8* clDataNxt,__global float8* clData,float h)
169
__kernel void RungeKutta(__global float8* clData,float h)
124 170
{
125 171
    int gid = get_global_id(0);
126 172
    
127
    clDataNxt[gid]=RungeKutta(clData,gid,h);
173
    float8 clDataGid=AtomicRungeKutta(clData,gid,h);
128 174
    barrier(CLK_GLOBAL_MEM_FENCE);
175
    clData[gid]=clDataGid;
129 176
}
130 177

  
131
__kernel void Commit(__global float8* clDataNxt,__global float8* clData)
178
__kernel void Euler(__global float8* clData,float h)
132 179
{
133 180
    int gid = get_global_id(0);
134 181
    
135
    clData[gid]=clDataNxt[gid];
182
    float8 clDataGid=AtomicEuler(clData,gid,h);
136 183
    barrier(CLK_GLOBAL_MEM_FENCE);
184
    clData[gid]=clDataGid;
137 185
}
138

  
139 186
"""
140 187

  
141 188
if __name__=='__main__':
......
154 201
    Iterations=1000
155 202
    # Step
156 203
    Step=0.1
204
    # Method of integration
205
    Method='RungeKutta'
157 206

  
158
    HowToUse='%s -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBox> -v <Velocity> -s <Step> -i <Iterations>'
207
    HowToUse='%s -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBox> -v <Velocity> -s <Step> -i <Iterations> -m <RungeKutta|Euler>'
159 208

  
160 209
    try:
161
        opts, args = getopt.getopt(sys.argv[1:],"hd:n:z:v:i:s:",["device=","number=","size=","velocity=","iterations=","step="])
210
        opts, args = getopt.getopt(sys.argv[1:],"hd:n:z:v:i:s:m:",["device=","number=","size=","velocity=","iterations=","step=","method="])
162 211
    except getopt.GetoptError:
163 212
        print HowToUse % sys.argv[0]
164 213
        sys.exit(2)
......
182 231

  
183 232
        elif opt in ("-d", "--device"):
184 233
            Device=int(arg)
234
        elif opt in ("-m", "--method"):
235
            Method=arg
185 236
        elif opt in ("-n", "--number"):
186 237
            Number=int(arg)
187 238
        elif opt in ("-z", "--size"):
......
199 250
    print "Initial velocity % s" % Velocity
200 251
    print "Number of iterations % s" % Iterations
201 252
    print "Step of iteration % s" % Step
253
    print "Method of resolution % s" % Method
202 254
    
203 255
    MyData = np.zeros(Number, dtype=cl_array.vec.float8)
204 256

  
......
238 290
    mf = cl.mem_flags
239 291
    clData = cl.Buffer(ctx, mf.READ_WRITE, MyData.nbytes)
240 292
    #clData = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyData)
241
    clDataNxt = cl.Buffer(ctx, mf.READ_WRITE, MyData.nbytes)
242 293

  
243 294
    print 'Tous au meme endroit'
244 295

  
......
254 305

  
255 306
    time_start=time.time()
256 307
    for i in xrange(Iterations):
257
        CLLaunch=MyRoutines.Evolution(queue,(Number,1),None,clDataNxt,clData,np.float32(Step))
308
        if Method=="RungeKutta":
309
            CLLaunch=MyRoutines.RungeKutta(queue,(Number,1),None,clData,np.float32(Step))
310
        else:
311
            CLLaunch=MyRoutines.Euler(queue,(Number,1),None,clData,np.float32(Step))
258 312
        CLLaunch.wait()
259
        cl.enqueue_copy(queue, MyData, clDataNxt)
313
        cl.enqueue_copy(queue, MyData, clData)
260 314
        t0=np.append(t0,[MyData[0][0],MyData[0][1],MyData[0][2]])
261 315
        t1=np.append(t1,[MyData[1][0],MyData[1][1],MyData[1][2]])
262 316
        tL=np.append(tL,[MyData[-1][0],MyData[-1][1],MyData[-1][2]])
263
        CLLaunch=MyRoutines.Commit(queue,(Number,1),None,clDataNxt,clData)
264
        CLLaunch.wait()
265 317
    print "\nDuration on %s for each %s" % (Device,(time.time()-time_start)/Iterations)
266 318
    
267 319
    t0=np.transpose(np.reshape(t0,(Iterations+1,3)))
......
284 336
    plt.show()
285 337
    
286 338
    clData.release()
287
    clDataNxt.release()

Formats disponibles : Unified diff