Révision 118 NBody/NBody.py

NBody.py (revision 118)
33 33

  
34 34
#define SMALL_NUM 0.000000001
35 35

  
36
float Gravity(float4 m,float4 n)
36
float4 Interaction(float4 m,float4 n)
37 37
{
38
    return((float)(1./pow(distance(m,n),2)));
38
    return((n-m)/(float)pow(distance(n,m),2));
39 39
}
40 40

  
41
float8 RungeKutta(__global float8* clDataIn,int gid,float h)
41
float8 RungeKutta(__global float8* clDataIn,int gid,float dt)
42 42
{
43
    float4 c[4],d[4],ct,dt;
43
    float4 x[4],v[4],a[4],xf,vf;
44 44

  
45
    c[0]=h*clDataIn[gid].hi;
46
    c[1]=h*(clDataIn[gid].hi+c[0]/(float)2.);
47
    c[2]=h*(clDataIn[gid].hi+c[1]/(float)2.);
48
    c[3]=h*(clDataIn[gid].hi+c[2]);
49

  
50
    d[0]=(0.,0.,0.,0.);
45
    x[0]=clDataIn[gid].lo;
46
    v[0]=clDataIn[gid].hi;
47
    a[0]=(0.,0.,0.,0.);
51 48
    for (int i=0;i<get_global_size(0);i++)
52 49
    {
53 50
        if (gid != i)
54
        d[0]+=Gravity(clDataIn[gid].lo,clDataIn[i].lo);
51
        a[0]+=Interaction(x[0],clDataIn[i].lo);
55 52
    }
56
    d[0]*=h;
57
    d[1]=(0.,0.,0.,0.);
53
        
54
    x[1]=x[0]+v[0]*(float)0.5*dt;
55
    v[1]=v[0]+a[0]*(float)0.5*dt;
56
    a[1]=(0.,0.,0.,0.);
58 57
    for (int i=0;i<get_global_size(0);i++)
59 58
    {
60 59
        if (gid != i)
61
        d[1]+=Gravity(clDataIn[gid].lo+d[0]/(float)2.,clDataIn[i].lo);
60
        a[1]+=Interaction(x[1],clDataIn[i].lo);
62 61
    }
63
    d[1]*=h;
64
    d[2]=(0.,0.,0.,0.);
62

  
63
    x[2]=x[0]+v[1]*(float)0.5*dt;
64
    v[2]=v[0]+a[1]*(float)0.5*dt;
65
    a[2]=(0.,0.,0.,0.);
65 66
    for (int i=0;i<get_global_size(0);i++)
66 67
    {
67 68
        if (gid != i)
68
        d[2]+=Gravity(clDataIn[gid].lo+d[1]/(float)2.,clDataIn[i].lo);
69
        a[2]+=Interaction(x[2],clDataIn[i].lo);
69 70
    }
70
    d[2]*=h;
71
    d[3]=(0.,0.,0.,0.);
71
    
72
    x[3]=x[0]+v[2]*dt;
73
    v[3]=v[0]+a[2]*dt;
74
    a[3]=(0.,0.,0.,0.);
72 75
    for (int i=0;i<get_global_size(0);i++)
73 76
    {
74 77
        if (gid != i)
75
        d[3]+=Gravity(clDataIn[gid].lo+d[2],clDataIn[i].lo);
78
        a[3]+=Interaction(x[3],clDataIn[i].lo);
76 79
    }
77
    d[3]*=h;
78 80
    
79
//    ct=clDataIn[gid].lo+(c[0]+(float)2.*c[1]+(float)2.*c[2]+c[3])/(float)6.;
80
//    dt=clDataIn[gid].hi+(d[0]+(float)2.*d[1]+(float)2.*d[2]+d[3])/(float)6.;
81
    if (gid != 0)
82
        ct=(float4)distance(clDataIn[gid],clDataIn[0]);
83
    else
84
        ct=(float4)3.14159;
85
    dt=(float4)0;
86 81
    
87
    return((float8)(ct.s0,ct.s1,ct.s2,ct.s3,dt.s0,dt.s1,dt.s2,dt.s3));
82
    xf=x[0]+dt*(v[0]+(float)2.*(v[1]+v[2])+v[3])/(float)6.;
83
    vf=v[0]+dt*(a[0]+(float)2.*(a[1]+a[2])+a[3])/(float)6.;
84
    
85
//    xf=x[0]+dt*v[0];
86
//    vf=v[0]+dt*a[0];
87
 
88
    return((float8)(xf.s0,xf.s1,xf.s2,xf.s3,vf.s0,vf.s1,vf.s2,vf.s3));
88 89
}
89 90

  
90 91
__kernel void SplutterPoints(__global float8* clData, float box, float velocity,
......
102 103
    clData[gid].s6=velocity*cos(theta);
103 104
}
104 105

  
105
__kernel void Evolution(__global float8* clDataOut,__global float8* clDataIn, float h)
106
__kernel void Evolution(__global float8* clDataOut,__global float8* clDataIn,float h)
106 107
{
107 108
    int gid = get_global_id(0);
108 109
    
......
123 124
if __name__=='__main__':
124 125
    
125 126
    # Set defaults values
126
  
127
    np.set_printoptions(precision=2)  
127 128
    # Id of Device : 1 is for first find !
128 129
    Device=2
129 130
    # Iterations is integer
130
    Number=2
131
    Number=10
131 132
    # Size of box
132
    SizeOfBox=1000.
133
    SizeOfBox=1.
133 134
    # Initial velocity of particules
134
    Velocity=10.
135
    Velocity=1.
135 136
    # Redo the last process
136
    Redo=1
137
    Redo=10
137 138
    # Step
138
    Step=1.
139
    Step=0.1
139 140

  
140 141
    HowToUse='%s -d <DeviceId> -n <NumberOfSegments> -z <SizeOfBox> -v <Velocity> -s <Step>'
141 142

  
142 143
    try:
143
        opts, args = getopt.getopt(sys.argv[1:],"hd:n:z:v:r:",["device=","number=","size=","velocity=","redo=","step=1"])
144
        opts, args = getopt.getopt(sys.argv[1:],"hd:n:z:v:r:s:",["device=","number=","size=","velocity=","redo=","step="])
144 145
    except getopt.GetoptError:
145 146
        print HowToUse % sys.argv[0]
146 147
        sys.exit(2)
......
208 209
   
209 210

  
210 211
    MyRoutines = cl.Program(ctx, BlobOpenCL).build()
211
    MyData[1][0]=1.
212
#    MyData[0][0]=np.float32(-1.)
213
#    MyData[0][1]=np.float32(0.)
214
#    MyData[0][5]=np.float32(1.)
215
#    MyData[1][0]=np.float32(1.)
216
#    MyData[1][1]=np.float32(0.)
217
#    MyData[1][5]=np.float32(-1.)
212 218

  
213 219
    mf = cl.mem_flags
214 220
    # clDataIn = cl.Buffer(ctx, mf.READ_WRITE, MyData.nbytes)
......
217 223

  
218 224
    print 'Tous au meme endroit',MyData
219 225

  
220
    # MyRoutines.SplutterPoints(queue,(Number,1),None,clDataIn,np.float32(SizeOfBox),np.float32(Velocity),np.uint32(nprnd(2**32)),np.uint32(nprnd(2**32)))
226
#    MyRoutines.SplutterPoints(queue,(Number,1),None,clDataIn,np.float32(SizeOfBox),np.float32(Velocity),np.uint32(nprnd(2**32)),np.uint32(nprnd(2**32)))
227
    MyRoutines.SplutterPoints(queue,(Number,1),None,clDataIn,np.float32(SizeOfBox),np.float32(Velocity),np.uint32(110271),np.uint32(250173))
221 228

  
222 229
    print 'Tous distribues',MyData
223 230

  
224
    CLLaunch=MyRoutines.Evolution(queue,(Number,1),None,clDataOut,clDataIn,np.float32(Step))
225
    CLLaunch.wait()
226
    cl.enqueue_copy(queue, MyData, clDataOut)
227
    print 'Tous calcules',MyData
228
    CLLaunch=MyRoutines.Commit(queue,(Number,1),None,clDataOut,clDataIn)
229
    CLLaunch.wait()
231
    x=np.array([])
232
    y=np.array([])
233
    z=np.array([])
234
    w=np.array([])
230 235

  
231 236
    time_start=time.time()
232 237
    for i in xrange(Redo):
233
        #CLLaunch=MyRoutines.ShortestDistance(queue, (Number,Number), None, clData, clDistance)
234
        sys.stdout.write('.')
235
        #CLLaunch.wait()
238
        CLLaunch=MyRoutines.Evolution(queue,(Number,1),None,clDataOut,clDataIn,np.float32(Step))
239
        CLLaunch.wait()
240
        cl.enqueue_copy(queue, MyData, clDataOut)
241
        x=np.append(x,MyData[0][0])
242
        y=np.append(y,MyData[0][1])
243
        z=np.append(z,MyData[1][0])
244
        w=np.append(w,MyData[1][1])
245
        CLLaunch=MyRoutines.Commit(queue,(Number,1),None,clDataOut,clDataIn)
246
        CLLaunch.wait()
236 247
    print "\nDuration on %s for each %s" % (Device,(time.time()-time_start)/Redo)
237

  
248
    
249
    import matplotlib.pyplot as plt
250
    print x,y
251
    print z,w
252
    plt.plot(x,y,marker='^')
253
    plt.plot(z,w,marker='o')
254
    plt.show()
255
    
238 256
    clDataIn.release()
239 257
    clDataOut.release()

Formats disponibles : Unified diff