Révision 119 NBody/NBody.py
| NBody.py (revision 119) | ||
|---|---|---|
| 78 | 78 |
a[3]+=Interaction(x[3],clDataIn[i].lo); |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 |
|
|
| 82 | 81 |
xf=x[0]+dt*(v[0]+(float)2.*(v[1]+v[2])+v[3])/(float)6.; |
| 83 | 82 |
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]; |
|
| 83 |
|
|
| 84 |
return((float8)(xf.s0,xf.s1,xf.s2,xf.s3,vf.s0,vf.s1,vf.s2,vf.s3)); |
|
| 85 |
} |
|
| 86 |
|
|
| 87 |
float8 Euler(__global float8* clDataIn,int gid,float dt) |
|
| 88 |
{
|
|
| 89 |
float4 x,v,a,xf,vf; |
|
| 90 |
|
|
| 91 |
x=clDataIn[gid].lo; |
|
| 92 |
v=clDataIn[gid].hi; |
|
| 93 |
a=(0.,0.,0.,0.); |
|
| 94 |
for (int i=0;i<get_global_size(0);i++) |
|
| 95 |
{
|
|
| 96 |
if (gid != i) |
|
| 97 |
a+=Interaction(x,clDataIn[i].lo); |
|
| 98 |
} |
|
| 99 |
|
|
| 100 |
xf=x+dt*v; |
|
| 101 |
vf=v+dt*a; |
|
| 87 | 102 |
|
| 88 | 103 |
return((float8)(xf.s0,xf.s1,xf.s2,xf.s3,vf.s0,vf.s1,vf.s2,vf.s3)); |
| 89 | 104 |
} |
| ... | ... | |
| 103 | 118 |
clData[gid].s6=velocity*cos(theta); |
| 104 | 119 |
} |
| 105 | 120 |
|
| 106 |
__kernel void Evolution(__global float8* clDataOut,__global float8* clDataIn,float h)
|
|
| 121 |
__kernel void Evolution(__global float8* clDataNxt,__global float8* clData,float h)
|
|
| 107 | 122 |
{
|
| 108 | 123 |
int gid = get_global_id(0); |
| 109 | 124 |
|
| 110 |
clDataOut[gid]=RungeKutta(clDataIn,gid,h);
|
|
| 125 |
clDataNxt[gid]=RungeKutta(clData,gid,h);
|
|
| 111 | 126 |
barrier(CLK_GLOBAL_MEM_FENCE); |
| 112 | 127 |
} |
| 113 | 128 |
|
| 114 |
__kernel void Commit(__global float8* clDataOut,__global float8* clDataIn)
|
|
| 129 |
__kernel void Commit(__global float8* clDataNxt,__global float8* clData)
|
|
| 115 | 130 |
{
|
| 116 | 131 |
int gid = get_global_id(0); |
| 117 | 132 |
|
| 118 |
clDataIn[gid]=clDataOut[gid];
|
|
| 133 |
clData[gid]=clDataNxt[gid];
|
|
| 119 | 134 |
barrier(CLK_GLOBAL_MEM_FENCE); |
| 120 | 135 |
} |
| 121 | 136 |
|
| ... | ... | |
| 209 | 224 |
|
| 210 | 225 |
|
| 211 | 226 |
MyRoutines = cl.Program(ctx, BlobOpenCL).build() |
| 227 |
|
|
| 228 |
# Initial forced values for exploration |
|
| 212 | 229 |
# MyData[0][0]=np.float32(-1.) |
| 213 | 230 |
# MyData[0][1]=np.float32(0.) |
| 214 | 231 |
# MyData[0][5]=np.float32(1.) |
| ... | ... | |
| 217 | 234 |
# MyData[1][5]=np.float32(-1.) |
| 218 | 235 |
|
| 219 | 236 |
mf = cl.mem_flags |
| 220 |
# clDataIn = cl.Buffer(ctx, mf.READ_WRITE, MyData.nbytes)
|
|
| 221 |
clDataIn = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyData)
|
|
| 222 |
clDataOut = cl.Buffer(ctx, mf.READ_WRITE, MyData.nbytes)
|
|
| 237 |
clData = cl.Buffer(ctx, mf.READ_WRITE, MyData.nbytes)
|
|
| 238 |
#clData = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyData)
|
|
| 239 |
clDataNxt = cl.Buffer(ctx, mf.READ_WRITE, MyData.nbytes)
|
|
| 223 | 240 |
|
| 224 |
print 'Tous au meme endroit',MyData
|
|
| 241 |
print 'Tous au meme endroit' |
|
| 225 | 242 |
|
| 226 | 243 |
# 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))
|
|
| 244 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clData,np.float32(SizeOfBox),np.float32(Velocity),np.uint32(110271),np.uint32(250173)) |
|
| 228 | 245 |
|
| 229 |
print 'Tous distribues',MyData |
|
| 246 |
print 'Tous distribues' |
|
| 247 |
|
|
| 248 |
cl.enqueue_copy(queue, MyData, clData) |
|
| 249 |
t0=np.array([[MyData[0][0],MyData[0][1],MyData[0][2]]]) |
|
| 250 |
t1=np.array([[MyData[1][0],MyData[1][1],MyData[1][2]]]) |
|
| 251 |
tL=np.array([[MyData[-1][0],MyData[-1][1],MyData[-1][2]]]) |
|
| 230 | 252 |
|
| 231 |
x=np.array([]) |
|
| 232 |
y=np.array([]) |
|
| 233 |
z=np.array([]) |
|
| 234 |
w=np.array([]) |
|
| 235 |
|
|
| 236 | 253 |
time_start=time.time() |
| 237 | 254 |
for i in xrange(Redo): |
| 238 |
CLLaunch=MyRoutines.Evolution(queue,(Number,1),None,clDataOut,clDataIn,np.float32(Step))
|
|
| 255 |
CLLaunch=MyRoutines.Evolution(queue,(Number,1),None,clDataNxt,clData,np.float32(Step))
|
|
| 239 | 256 |
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)
|
|
| 257 |
cl.enqueue_copy(queue, MyData, clDataNxt)
|
|
| 258 |
# print MyData[0]
|
|
| 259 |
t0=np.append(t0,[MyData[0][0],MyData[0][1],MyData[0][2]])
|
|
| 260 |
t1=np.append(t1,[MyData[1][0],MyData[1][1],MyData[1][2]])
|
|
| 261 |
tL=np.append(tL,[MyData[-1][0],MyData[-1][1],MyData[-1][2]])
|
|
| 262 |
CLLaunch=MyRoutines.Commit(queue,(Number,1),None,clDataNxt,clData)
|
|
| 246 | 263 |
CLLaunch.wait() |
| 247 | 264 |
print "\nDuration on %s for each %s" % (Device,(time.time()-time_start)/Redo) |
| 248 | 265 |
|
| 266 |
t0=np.transpose(np.reshape(t0,(Redo+1,3))) |
|
| 267 |
t1=np.transpose(np.reshape(t1,(Redo+1,3))) |
|
| 268 |
tL=np.transpose(np.reshape(tL,(Redo+1,3))) |
|
| 269 |
|
|
| 249 | 270 |
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') |
|
| 271 |
from mpl_toolkits.mplot3d import Axes3D |
|
| 272 |
|
|
| 273 |
fig = plt.figure() |
|
| 274 |
ax = fig.gca(projection='3d') |
|
| 275 |
print tL[2] |
|
| 276 |
ax.scatter(t0[0],t0[1],t0[2], marker='^',color='blue') |
|
| 277 |
ax.scatter(t1[0],t1[1],t1[2], marker='o',color='red') |
|
| 278 |
ax.scatter(tL[0],tL[1],tL[2], marker='D',color='green') |
|
| 279 |
|
|
| 280 |
ax.set_xlabel('X Label')
|
|
| 281 |
ax.set_ylabel('Y Label')
|
|
| 282 |
ax.set_zlabel('Z Label')
|
|
| 283 |
|
|
| 254 | 284 |
plt.show() |
| 255 | 285 |
|
| 256 |
clDataIn.release() |
|
| 257 |
clDataOut.release() |
|
| 286 |
clData.release() |
|
| 287 |
clDataNxt.release() |
|
Formats disponibles : Unified diff