Révision 161
| NBody/NBodyGL.py (revision 161) | ||
|---|---|---|
| 1 |
#!/usr/bin/env python3 |
|
| 2 |
# -*- coding: utf-8 -*- |
|
| 3 |
""" |
|
| 4 |
Demonstrateur OpenCL d'interaction NCorps |
|
| 5 |
|
|
| 6 |
Emmanuel QUEMENER <emmanuel.quemener@ens-lyon.fr> CeCILLv2 |
|
| 7 |
""" |
|
| 8 |
import getopt |
|
| 9 |
import sys |
|
| 10 |
import time |
|
| 11 |
import numpy as np |
|
| 12 |
import pyopencl as cl |
|
| 13 |
import pyopencl.array as cl_array |
|
| 14 |
from numpy.random import randint as nprnd |
|
| 15 |
import string, sys |
|
| 16 |
from OpenGL.GL import * |
|
| 17 |
from OpenGL.GLUT import * |
|
| 18 |
|
|
| 19 |
def DictionariesAPI(): |
|
| 20 |
Marsaglia={'CONG':0,'SHR3':1,'MWC':2,'KISS':3}
|
|
| 21 |
Computing={'FP32':0,'FP64':1}
|
|
| 22 |
return(Marsaglia,Computing) |
|
| 23 |
|
|
| 24 |
BlobOpenCL= """ |
|
| 25 |
#define znew ((z=36969*(z&65535)+(z>>16))<<16) |
|
| 26 |
#define wnew ((w=18000*(w&65535)+(w>>16))&65535) |
|
| 27 |
#define MWC (znew+wnew) |
|
| 28 |
#define SHR3 (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5)) |
|
| 29 |
#define CONG (jcong=69069*jcong+1234567) |
|
| 30 |
#define KISS ((MWC^CONG)+SHR3) |
|
| 31 |
|
|
| 32 |
#define TFP32 0 |
|
| 33 |
#define TFP64 1 |
|
| 34 |
|
|
| 35 |
#define MWCfp MWC * 2.3283064365386963e-10f |
|
| 36 |
#define KISSfp KISS * 2.3283064365386963e-10f |
|
| 37 |
#define SHR3fp SHR3 * 2.3283064365386963e-10f |
|
| 38 |
#define CONGfp CONG * 2.3283064365386963e-10f |
|
| 39 |
|
|
| 40 |
#define PI 3.141592653589793238462643197169399375105820974944592307816406286e0f |
|
| 41 |
|
|
| 42 |
#define SMALL_NUM 1.e-9f |
|
| 43 |
|
|
| 44 |
#define LENGTH 1.e0f |
|
| 45 |
|
|
| 46 |
#if TYPE == TFP32 |
|
| 47 |
#define MYFLOAT4 float4 |
|
| 48 |
#define MYFLOAT8 float8 |
|
| 49 |
#define MYFLOAT float |
|
| 50 |
#define DISTANCE fast_distance |
|
| 51 |
#else |
|
| 52 |
#if defined(cl_khr_fp64) // Khronos extension available? |
|
| 53 |
#pragma OPENCL EXTENSION cl_khr_fp64 : enable |
|
| 54 |
#define DOUBLE_SUPPORT_AVAILABLE |
|
| 55 |
#elif defined(cl_amd_fp64) // AMD extension available? |
|
| 56 |
#pragma OPENCL EXTENSION cl_amd_fp64 : enable |
|
| 57 |
#define DOUBLE_SUPPORT_AVAILABLE |
|
| 58 |
#endif |
|
| 59 |
#define MYFLOAT4 double4 |
|
| 60 |
#define MYFLOAT8 double8 |
|
| 61 |
#define MYFLOAT double |
|
| 62 |
#define DISTANCE distance |
|
| 63 |
#endif |
|
| 64 |
|
|
| 65 |
|
|
| 66 |
MYFLOAT4 Interaction(MYFLOAT4 m,MYFLOAT4 n) |
|
| 67 |
{
|
|
| 68 |
private MYFLOAT r=DISTANCE(n,m); |
|
| 69 |
|
|
| 70 |
return((n-m)/(MYFLOAT)(r*r*r)); |
|
| 71 |
} |
|
| 72 |
|
|
| 73 |
MYFLOAT4 InteractionCore(MYFLOAT4 m,MYFLOAT4 n) |
|
| 74 |
{
|
|
| 75 |
private MYFLOAT core=(MYFLOAT)1.e5f; |
|
| 76 |
private MYFLOAT r=DISTANCE(n,m); |
|
| 77 |
private MYFLOAT d=r*r+core*core; |
|
| 78 |
|
|
| 79 |
return(core*(n-m)/(MYFLOAT)(d*d)); |
|
| 80 |
} |
|
| 81 |
|
|
| 82 |
MYFLOAT PairPotential(MYFLOAT4 m,MYFLOAT4 n) |
|
| 83 |
{
|
|
| 84 |
return((MYFLOAT)(-1.e0f)/(DISTANCE(n,m))); |
|
| 85 |
} |
|
| 86 |
|
|
| 87 |
MYFLOAT AtomicPotential(__global MYFLOAT4* clDataX,int gid) |
|
| 88 |
{
|
|
| 89 |
private MYFLOAT potential=(MYFLOAT)0.e0f; |
|
| 90 |
private MYFLOAT4 x=clDataX[gid]; |
|
| 91 |
|
|
| 92 |
for (int i=0;i<get_global_size(0);i++) |
|
| 93 |
{
|
|
| 94 |
if (gid != i) |
|
| 95 |
potential+=PairPotential(x,clDataX[i]); |
|
| 96 |
} |
|
| 97 |
|
|
| 98 |
barrier(CLK_GLOBAL_MEM_FENCE); |
|
| 99 |
return(potential); |
|
| 100 |
} |
|
| 101 |
|
|
| 102 |
MYFLOAT AtomicPotentialCoM(__global MYFLOAT4* clDataX,__global MYFLOAT4* clCoM,int gid) |
|
| 103 |
{
|
|
| 104 |
return(PairPotential(clDataX[gid],clCoM[0])); |
|
| 105 |
} |
|
| 106 |
|
|
| 107 |
MYFLOAT8 AtomicRungeKutta(__global MYFLOAT4* clDataInX,__global MYFLOAT4* clDataInV,int gid,MYFLOAT dt) |
|
| 108 |
{
|
|
| 109 |
private MYFLOAT4 a0,v0,x0,a1,v1,x1,a2,v2,x2,a3,v3,x3,a4,v4,x4,xf,vf; |
|
| 110 |
|
|
| 111 |
a0=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f); |
|
| 112 |
v0=(MYFLOAT4)clDataInV[gid]; |
|
| 113 |
x0=(MYFLOAT4)clDataInX[gid]; |
|
| 114 |
int N = get_global_size(0); |
|
| 115 |
|
|
| 116 |
for (int i=0;i<N;i++) |
|
| 117 |
{
|
|
| 118 |
if (gid != i) |
|
| 119 |
a0+=Interaction(x0,clDataInX[i]); |
|
| 120 |
} |
|
| 121 |
|
|
| 122 |
a1=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f); |
|
| 123 |
v1=v0+a0*dt; |
|
| 124 |
x1=x0+v0*dt; |
|
| 125 |
for (int i=0;i<N;i++) |
|
| 126 |
{
|
|
| 127 |
if (gid != i) |
|
| 128 |
a1+=Interaction(x1,clDataInX[i]); |
|
| 129 |
} |
|
| 130 |
|
|
| 131 |
a2=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f); |
|
| 132 |
v2=v0+a1*dt*(MYFLOAT)5.e-1f; |
|
| 133 |
x2=x0+v1*dt*(MYFLOAT)5.e-1f; |
|
| 134 |
for (int i=0;i<N;i++) |
|
| 135 |
{
|
|
| 136 |
if (gid != i) |
|
| 137 |
a2+=Interaction(x2,clDataInX[i]); |
|
| 138 |
} |
|
| 139 |
|
|
| 140 |
a3=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f); |
|
| 141 |
v3=v0+a2*dt*(MYFLOAT)5.e-1f; |
|
| 142 |
x3=x0+v2*dt*(MYFLOAT)5.e-1f; |
|
| 143 |
for (int i=0;i<N;i++) |
|
| 144 |
{
|
|
| 145 |
if (gid != i) |
|
| 146 |
a3+=Interaction(x3,clDataInX[i]); |
|
| 147 |
} |
|
| 148 |
|
|
| 149 |
a4=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f); |
|
| 150 |
v4=v0+a3*dt; |
|
| 151 |
x4=x0+v3*dt; |
|
| 152 |
for (int i=0;i<N;i++) |
|
| 153 |
{
|
|
| 154 |
if (gid != i) |
|
| 155 |
a4+=Interaction(x4,clDataInX[i]); |
|
| 156 |
} |
|
| 157 |
|
|
| 158 |
xf=x0+dt*(v1+(MYFLOAT)2.e0f*(v2+v3)+v4)/(MYFLOAT)6.e0f; |
|
| 159 |
vf=v0+dt*(a1+(MYFLOAT)2.e0f*(a2+a3)+a4)/(MYFLOAT)6.e0f; |
|
| 160 |
|
|
| 161 |
return((MYFLOAT8)(xf.s0,xf.s1,xf.s2,1.e0f,vf.s0,vf.s1,vf.s2,1.e0f)); |
|
| 162 |
} |
|
| 163 |
|
|
| 164 |
// Elements from : http://doswa.com/2009/01/02/fourth-order-runge-kutta-numerical-integration.html |
|
| 165 |
|
|
| 166 |
MYFLOAT8 AtomicHeun(__global MYFLOAT4* clDataInX,__global MYFLOAT4* clDataInV,int gid,MYFLOAT dt) |
|
| 167 |
{
|
|
| 168 |
private MYFLOAT4 x,v,a,xi,vi,ai,xf,vf; |
|
| 169 |
|
|
| 170 |
x=(MYFLOAT4)clDataInX[gid]; |
|
| 171 |
v=(MYFLOAT4)clDataInV[gid]; |
|
| 172 |
a=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f); |
|
| 173 |
|
|
| 174 |
for (int i=0;i<get_global_size(0);i++) |
|
| 175 |
{
|
|
| 176 |
if (gid != i) |
|
| 177 |
a+=Interaction(x,clDataInX[i]); |
|
| 178 |
} |
|
| 179 |
|
|
| 180 |
vi=v+dt*a; |
|
| 181 |
xi=x+dt*vi; |
|
| 182 |
ai=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f); |
|
| 183 |
|
|
| 184 |
for (int i=0;i<get_global_size(0);i++) |
|
| 185 |
{
|
|
| 186 |
if (gid != i) |
|
| 187 |
ai+=Interaction(xi,clDataInX[i]); |
|
| 188 |
} |
|
| 189 |
|
|
| 190 |
vf=v+dt*(a+ai)/(MYFLOAT)2.e0f; |
|
| 191 |
xf=x+dt*(v+vi)/(MYFLOAT)2.e0f; |
|
| 192 |
|
|
| 193 |
return((MYFLOAT8)(xf.s0,xf.s1,xf.s2,1.e0f,vf.s0,vf.s1,vf.s2,0.e0f)); |
|
| 194 |
} |
|
| 195 |
|
|
| 196 |
MYFLOAT8 AtomicImplicitEuler(__global MYFLOAT4* clDataInX,__global MYFLOAT4* clDataInV,int gid,MYFLOAT dt) |
|
| 197 |
{
|
|
| 198 |
private MYFLOAT4 x,v,a,xf,vf; |
|
| 199 |
|
|
| 200 |
x=(MYFLOAT4)clDataInX[gid]; |
|
| 201 |
v=(MYFLOAT4)clDataInV[gid]; |
|
| 202 |
a=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f); |
|
| 203 |
|
|
| 204 |
for (int i=0;i<get_global_size(0);i++) |
|
| 205 |
{
|
|
| 206 |
if (gid != i) |
|
| 207 |
a+=Interaction(x,clDataInX[i]); |
|
| 208 |
} |
|
| 209 |
|
|
| 210 |
vf=v+dt*a; |
|
| 211 |
xf=x+dt*vf; |
|
| 212 |
|
|
| 213 |
return((MYFLOAT8)(xf.s0,xf.s1,xf.s2,1.e0f,vf.s0,vf.s1,vf.s2,0.e0f)); |
|
| 214 |
} |
|
| 215 |
|
|
| 216 |
MYFLOAT8 AtomicExplicitEuler(__global MYFLOAT4* clDataInX,__global MYFLOAT4* clDataInV,int gid,MYFLOAT dt) |
|
| 217 |
{
|
|
| 218 |
MYFLOAT4 x,v,a,xf,vf; |
|
| 219 |
|
|
| 220 |
x=(MYFLOAT4)clDataInX[gid]; |
|
| 221 |
v=(MYFLOAT4)clDataInV[gid]; |
|
| 222 |
a=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f); |
|
| 223 |
|
|
| 224 |
for (int i=0;i<get_global_size(0);i++) |
|
| 225 |
{
|
|
| 226 |
if (gid != i) |
|
| 227 |
a+=Interaction(x,clDataInX[i]); |
|
| 228 |
} |
|
| 229 |
|
|
| 230 |
vf=v+dt*a; |
|
| 231 |
xf=x+dt*v; |
|
| 232 |
|
|
| 233 |
return((MYFLOAT8)(xf.s0,xf.s1,xf.s2,1.e0f,vf.s0,vf.s1,vf.s2,0.e0f)); |
|
| 234 |
} |
|
| 235 |
|
|
| 236 |
__kernel void SplutterPoints(__global MYFLOAT4* clDataX, MYFLOAT box, |
|
| 237 |
uint seed_z,uint seed_w) |
|
| 238 |
{
|
|
| 239 |
uint gid=(uint)get_global_id(0); |
|
| 240 |
uint z=(seed_z+gid)%gid; |
|
| 241 |
uint w=seed_w+gid; |
|
| 242 |
|
|
| 243 |
// cast to float for sin,cos are NEEDED by Mesa FP64 implementation! |
|
| 244 |
MYFLOAT radius=MWCfp*box; |
|
| 245 |
MYFLOAT theta=MWCfp*PI; |
|
| 246 |
MYFLOAT phi=MWCfp*PI*(MYFLOAT)2.e0f; |
|
| 247 |
MYFLOAT sinTheta=sin((float)theta); |
|
| 248 |
|
|
| 249 |
clDataX[gid].s0=radius*sinTheta*cos((float)phi)/2.e0f; |
|
| 250 |
clDataX[gid].s1=radius*sinTheta*sin((float)phi)/2.e0f; |
|
| 251 |
clDataX[gid].s2=radius*cos((float)theta)/2.e0f; |
|
| 252 |
clDataX[gid].s3=(MYFLOAT)1.e0f; |
|
| 253 |
barrier(CLK_GLOBAL_MEM_FENCE); |
|
| 254 |
|
|
| 255 |
//MYFLOAT x0=box*(MYFLOAT)(MWCfp-0.5); |
|
| 256 |
//MYFLOAT y0=box*(MYFLOAT)(MWCfp-0.5); |
|
| 257 |
//MYFLOAT z0=box*(MYFLOAT)(MWCfp-0.5); |
|
| 258 |
//clDataX[gid].s0123 = (MYFLOAT4) (x0,y0,z0,1.e0f); |
|
| 259 |
} |
|
| 260 |
|
|
| 261 |
__kernel void SplutterStress(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,__global MYFLOAT4* clCoM, MYFLOAT velocity,uint seed_z,uint seed_w) |
|
| 262 |
{
|
|
| 263 |
int gid = get_global_id(0); |
|
| 264 |
MYFLOAT N = (MYFLOAT)get_global_size(0); |
|
| 265 |
uint z=seed_z+(uint)gid; |
|
| 266 |
uint w=seed_w-(uint)gid; |
|
| 267 |
|
|
| 268 |
if (velocity<SMALL_NUM) {
|
|
| 269 |
MYFLOAT4 SpeedVector=(MYFLOAT4)normalize(cross(clDataX[gid],clCoM[0]))*sqrt(-AtomicPotential(clDataX,gid)/(MYFLOAT)2.e0f); |
|
| 270 |
clDataV[gid]=SpeedVector; |
|
| 271 |
} |
|
| 272 |
else |
|
| 273 |
{
|
|
| 274 |
// cast to float for sin,cos are NEEDED by Mesa FP64 implementation! |
|
| 275 |
MYFLOAT theta=MWCfp*PI; |
|
| 276 |
MYFLOAT phi=MWCfp*PI*(MYFLOAT)2.e0f; |
|
| 277 |
MYFLOAT sinTheta=sin((float)theta); |
|
| 278 |
|
|
| 279 |
clDataV[gid].s0=velocity*sinTheta*cos((float)phi); |
|
| 280 |
clDataV[gid].s1=velocity*sinTheta*sin((float)phi); |
|
| 281 |
clDataV[gid].s2=velocity*cos((float)theta); |
|
| 282 |
clDataV[gid].s3=(MYFLOAT)1.e0f; |
|
| 283 |
} |
|
| 284 |
barrier(CLK_GLOBAL_MEM_FENCE); |
|
| 285 |
} |
|
| 286 |
|
|
| 287 |
__kernel void RungeKutta(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h) |
|
| 288 |
{
|
|
| 289 |
int gid = get_global_id(0); |
|
| 290 |
|
|
| 291 |
MYFLOAT8 clDataGid=AtomicRungeKutta(clDataX,clDataV,gid,h); |
|
| 292 |
barrier(CLK_GLOBAL_MEM_FENCE); |
|
| 293 |
clDataX[gid]=clDataGid.lo; |
|
| 294 |
clDataV[gid]=clDataGid.hi; |
|
| 295 |
} |
|
| 296 |
|
|
| 297 |
__kernel void ImplicitEuler(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h) |
|
| 298 |
{
|
|
| 299 |
int gid = get_global_id(0); |
|
| 300 |
|
|
| 301 |
MYFLOAT8 clDataGid=AtomicImplicitEuler(clDataX,clDataV,gid,h); |
|
| 302 |
barrier(CLK_GLOBAL_MEM_FENCE); |
|
| 303 |
clDataX[gid]=clDataGid.lo; |
|
| 304 |
clDataV[gid]=clDataGid.hi; |
|
| 305 |
} |
|
| 306 |
|
|
| 307 |
__kernel void Heun(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h) |
|
| 308 |
{
|
|
| 309 |
int gid = get_global_id(0); |
|
| 310 |
|
|
| 311 |
MYFLOAT8 clDataGid=AtomicHeun(clDataX,clDataV,gid,h); |
|
| 312 |
barrier(CLK_GLOBAL_MEM_FENCE); |
|
| 313 |
clDataX[gid]=clDataGid.lo; |
|
| 314 |
clDataV[gid]=clDataGid.hi; |
|
| 315 |
} |
|
| 316 |
|
|
| 317 |
__kernel void ExplicitEuler(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h) |
|
| 318 |
{
|
|
| 319 |
int gid = get_global_id(0); |
|
| 320 |
|
|
| 321 |
MYFLOAT8 clDataGid=AtomicExplicitEuler(clDataX,clDataV,gid,h); |
|
| 322 |
barrier(CLK_GLOBAL_MEM_FENCE); |
|
| 323 |
clDataX[gid]=clDataGid.lo; |
|
| 324 |
clDataV[gid]=clDataGid.hi; |
|
| 325 |
} |
|
| 326 |
|
|
| 327 |
__kernel void CoMPotential(__global MYFLOAT4* clDataX,__global MYFLOAT4* clCoM,__global MYFLOAT* clPotential) |
|
| 328 |
{
|
|
| 329 |
int gid = get_global_id(0); |
|
| 330 |
|
|
| 331 |
clPotential[gid]=PairPotential(clDataX[gid],clCoM[0]); |
|
| 332 |
} |
|
| 333 |
|
|
| 334 |
__kernel void Potential(__global MYFLOAT4* clDataX,__global MYFLOAT* clPotential) |
|
| 335 |
{
|
|
| 336 |
int gid = get_global_id(0); |
|
| 337 |
|
|
| 338 |
MYFLOAT potential=(MYFLOAT)0.e0f; |
|
| 339 |
MYFLOAT4 x=clDataX[gid]; |
|
| 340 |
|
|
| 341 |
for (int i=0;i<get_global_size(0);i++) |
|
| 342 |
{
|
|
| 343 |
if (gid != i) |
|
| 344 |
potential+=PairPotential(x,clDataX[i]); |
|
| 345 |
} |
|
| 346 |
|
|
| 347 |
barrier(CLK_GLOBAL_MEM_FENCE); |
|
| 348 |
clPotential[gid]=potential*(MYFLOAT)5.e-1f; |
|
| 349 |
} |
|
| 350 |
|
|
| 351 |
__kernel void CenterOfMass(__global MYFLOAT4* clDataX,__global MYFLOAT4* clCoM,int Size) |
|
| 352 |
{
|
|
| 353 |
MYFLOAT4 CoM=clDataX[0]; |
|
| 354 |
|
|
| 355 |
for (int i=1;i<Size;i++) |
|
| 356 |
{
|
|
| 357 |
CoM+=clDataX[i]; |
|
| 358 |
} |
|
| 359 |
|
|
| 360 |
barrier(CLK_GLOBAL_MEM_FENCE); |
|
| 361 |
clCoM[0]=(MYFLOAT4)(CoM.s0,CoM.s1,CoM.s2,1.e0f)/(MYFLOAT)Size; |
|
| 362 |
} |
|
| 363 |
|
|
| 364 |
__kernel void Kinetic(__global MYFLOAT4* clDataV,__global MYFLOAT* clKinetic) |
|
| 365 |
{
|
|
| 366 |
int gid = get_global_id(0); |
|
| 367 |
|
|
| 368 |
barrier(CLK_GLOBAL_MEM_FENCE); |
|
| 369 |
MYFLOAT d=(MYFLOAT)length(clDataV[gid]); |
|
| 370 |
clKinetic[gid]=(MYFLOAT)5.e-1f*(MYFLOAT)(d*d); |
|
| 371 |
} |
|
| 372 |
""" |
|
| 373 |
|
|
| 374 |
def display(*args): |
|
| 375 |
|
|
| 376 |
global MyDataX,clDataX,clDataV,Step,Method,Number |
|
| 377 |
|
|
| 378 |
glClearColor(0.0, 0.0, 0.0, 0.0) |
|
| 379 |
glClear(GL_COLOR_BUFFER_BIT) |
|
| 380 |
glColor3f(1.0,1.0,0.0) |
|
| 381 |
|
|
| 382 |
time_start=time.time() |
|
| 383 |
if Method=="RungeKutta": |
|
| 384 |
CLLaunch=MyRoutines.RungeKutta(queue,(Number,1),None,clDataX,clDataV,Step) |
|
| 385 |
elif Method=="ExplicitEuler": |
|
| 386 |
CLLaunch=MyRoutines.ExplicitEuler(queue,(Number,1),None,clDataX,clDataV,Step) |
|
| 387 |
elif Method=="Heun": |
|
| 388 |
CLLaunch=MyRoutines.Heun(queue,(Number,1),None,clDataX,clDataV,Step) |
|
| 389 |
else: |
|
| 390 |
CLLaunch=MyRoutines.ImplicitEuler(queue,(Number,1),None,clDataX,clDataV,Step) |
|
| 391 |
CLLaunch.wait() |
|
| 392 |
print("Duration of 1 iteration %s" % (time.time()-time_start))
|
|
| 393 |
|
|
| 394 |
cl.enqueue_copy(queue, MyDataX, clDataX) |
|
| 395 |
print(MyDataX.reshape(Number,4)) |
|
| 396 |
glVertexPointerf(MyDataX.reshape(Number,4)) |
|
| 397 |
glEnableClientState(GL_VERTEX_ARRAY) |
|
| 398 |
glDrawArrays(GL_POINTS, 0, Number) |
|
| 399 |
glDisableClientState(GL_VERTEX_ARRAY) |
|
| 400 |
glFlush() |
|
| 401 |
glutSwapBuffers() |
|
| 402 |
|
|
| 403 |
def halt(): |
|
| 404 |
pass |
|
| 405 |
|
|
| 406 |
def keyboard(*args): |
|
| 407 |
return() |
|
| 408 |
|
|
| 409 |
def mouse(button, state, x, y): |
|
| 410 |
global angle, delta_angle, halted |
|
| 411 |
if button == GLUT_LEFT_BUTTON: |
|
| 412 |
angle = angle + delta_angle |
|
| 413 |
elif button == GLUT_RIGHT_BUTTON: |
|
| 414 |
angle = angle - delta_angle |
|
| 415 |
elif button == GLUT_MIDDLE_BUTTON and state == GLUT_DOWN: |
|
| 416 |
if halted: |
|
| 417 |
glutIdleFunc(display) |
|
| 418 |
halted = 0 |
|
| 419 |
else: |
|
| 420 |
glutIdleFunc(halt) |
|
| 421 |
halted = 1 |
|
| 422 |
|
|
| 423 |
def setup_viewport(): |
|
| 424 |
global SizeOfBox |
|
| 425 |
glMatrixMode(GL_PROJECTION) |
|
| 426 |
glLoadIdentity() |
|
| 427 |
glOrtho(-SizeOfBox, SizeOfBox, -SizeOfBox, SizeOfBox, -SizeOfBox, SizeOfBox) |
|
| 428 |
|
|
| 429 |
def reshape(w, h): |
|
| 430 |
glViewport(0, 0, w, h) |
|
| 431 |
setup_viewport() |
|
| 432 |
|
|
| 433 |
def main(): |
|
| 434 |
|
|
| 435 |
glutInit(sys.argv) |
|
| 436 |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) |
|
| 437 |
glutInitWindowSize(400,400) |
|
| 438 |
glutCreateWindow(b'NBodyGL') |
|
| 439 |
setup_viewport() |
|
| 440 |
# glutReshapeFunc(reshape) |
|
| 441 |
glutDisplayFunc(display) |
|
| 442 |
glutIdleFunc(display) |
|
| 443 |
# glutMouseFunc(mouse) |
|
| 444 |
glutKeyboardFunc(keyboard) |
|
| 445 |
glutMainLoop() |
|
| 446 |
|
|
| 447 |
if __name__=='__main__': |
|
| 448 |
|
|
| 449 |
global Number,Step,clDataX,clDataV,MyDataX,MyDataV,Method,SizeOfBox |
|
| 450 |
|
|
| 451 |
# ValueType |
|
| 452 |
ValueType='FP32' |
|
| 453 |
class MyFloat(np.float32):pass |
|
| 454 |
# clType8=cl_array.vec.float8 |
|
| 455 |
clType4=cl_array.vec.float4 |
|
| 456 |
# Set defaults values |
|
| 457 |
np.set_printoptions(precision=2) |
|
| 458 |
# Id of Device : 1 is for first find ! |
|
| 459 |
Device=0 |
|
| 460 |
# Iterations is integer |
|
| 461 |
Number=2 |
|
| 462 |
# Size of box |
|
| 463 |
SizeOfBox=MyFloat(1.) |
|
| 464 |
# Initial velocity of particules |
|
| 465 |
Velocity=MyFloat(1.) |
|
| 466 |
# Redo the last process |
|
| 467 |
Iterations=int(np.pi*1024) |
|
| 468 |
# Step |
|
| 469 |
Step=MyFloat(1./256) |
|
| 470 |
# Method of integration |
|
| 471 |
Method='ImplicitEuler' |
|
| 472 |
# InitialRandom |
|
| 473 |
InitialRandom=False |
|
| 474 |
# RNG Marsaglia Method |
|
| 475 |
RNG='MWC' |
|
| 476 |
# CheckEnergies |
|
| 477 |
CheckEnergies=False |
|
| 478 |
# Display samples in 3D |
|
| 479 |
GraphSamples=False |
|
| 480 |
# Viriel Distribution of stress |
|
| 481 |
VirielStress=True |
|
| 482 |
|
|
| 483 |
HowToUse='%s -h [Help] -r [InitialRandom] -e [VirielStress] -g [GraphSamples] -c [CheckEnergies] -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBox> -v <Velocity> -s <Step> -i <Iterations> -m <ImplicitEuler|RungeKutta|ExplicitEuler|Heun> -t <FP32|FP64>' |
|
| 484 |
|
|
| 485 |
try: |
|
| 486 |
opts, args = getopt.getopt(sys.argv[1:],"rehgcd:n:z:v:i:s:m:t:",["random","viriel","graph","check","device=","number=","size=","velocity=","iterations=","step=","method=","valuetype="]) |
|
| 487 |
except getopt.GetoptError: |
|
| 488 |
print(HowToUse % sys.argv[0]) |
|
| 489 |
sys.exit(2) |
|
| 490 |
|
|
| 491 |
for opt, arg in opts: |
|
| 492 |
if opt == '-h': |
|
| 493 |
print(HowToUse % sys.argv[0]) |
|
| 494 |
|
|
| 495 |
print("\nInformations about devices detected under OpenCL:")
|
|
| 496 |
try: |
|
| 497 |
Id=0 |
|
| 498 |
for platform in cl.get_platforms(): |
|
| 499 |
for device in platform.get_devices(): |
|
| 500 |
#deviceType=cl.device_type.to_string(device.type) |
|
| 501 |
deviceType="xPU" |
|
| 502 |
print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip()))
|
|
| 503 |
Id=Id+1 |
|
| 504 |
sys.exit() |
|
| 505 |
except ImportError: |
|
| 506 |
print("Your platform does not seem to support OpenCL")
|
|
| 507 |
sys.exit() |
|
| 508 |
|
|
| 509 |
elif opt in ("-t", "--valuetype"):
|
|
| 510 |
if arg=='FP64': |
|
| 511 |
class MyFloat(np.float64): pass |
|
| 512 |
clType4=cl_array.vec.double4 |
|
| 513 |
else: |
|
| 514 |
class MyFloat(np.float32):pass |
|
| 515 |
clType4=cl_array.vec.float4 |
|
| 516 |
ValueType = arg |
|
| 517 |
elif opt in ("-d", "--device"):
|
|
| 518 |
Device=int(arg) |
|
| 519 |
elif opt in ("-m", "--method"):
|
|
| 520 |
Method=arg |
|
| 521 |
elif opt in ("-n", "--number"):
|
|
| 522 |
Number=int(arg) |
|
| 523 |
elif opt in ("-z", "--size"):
|
|
| 524 |
SizeOfBox=MyFloat(arg) |
|
| 525 |
elif opt in ("-v", "--velocity"):
|
|
| 526 |
Velocity=MyFloat(arg) |
|
| 527 |
VirielStress=False |
|
| 528 |
elif opt in ("-s", "--step"):
|
|
| 529 |
Step=MyFloat(arg) |
|
| 530 |
elif opt in ("-i", "--iterations"):
|
|
| 531 |
Iterations=int(arg) |
|
| 532 |
elif opt in ("-r", "--random"):
|
|
| 533 |
InitialRandom=True |
|
| 534 |
elif opt in ("-c", "--check"):
|
|
| 535 |
CheckEnergies=True |
|
| 536 |
elif opt in ("-g", "--graph"):
|
|
| 537 |
GraphSamples=True |
|
| 538 |
elif opt in ("-e", "--viriel"):
|
|
| 539 |
VirielStress=True |
|
| 540 |
|
|
| 541 |
SizeOfBox=MyFloat(SizeOfBox*Number) |
|
| 542 |
Velocity=MyFloat(Velocity) |
|
| 543 |
Step=MyFloat(Step) |
|
| 544 |
|
|
| 545 |
print("Device choosed : %s" % Device)
|
|
| 546 |
print("Number of particules : %s" % Number)
|
|
| 547 |
print("Size of Box : %s" % SizeOfBox)
|
|
| 548 |
print("Initial velocity : %s" % Velocity)
|
|
| 549 |
print("Number of iterations : %s" % Iterations)
|
|
| 550 |
print("Step of iteration : %s" % Step)
|
|
| 551 |
print("Method of resolution : %s" % Method)
|
|
| 552 |
print("Initial Random for RNG Seed : %s" % InitialRandom)
|
|
| 553 |
print("Check for Energies : %s" % CheckEnergies)
|
|
| 554 |
print("Graph for Samples : %s" % GraphSamples)
|
|
| 555 |
print("ValueType is : %s" % ValueType)
|
|
| 556 |
print("Viriel distribution of stress %s" % VirielStress)
|
|
| 557 |
|
|
| 558 |
# Create Numpy array of CL vector with 8 FP32 |
|
| 559 |
MyCoM = np.zeros(4,dtype=MyFloat) |
|
| 560 |
MyDataX = np.zeros(Number*4, dtype=MyFloat) |
|
| 561 |
MyDataV = np.zeros(Number*4, dtype=MyFloat) |
|
| 562 |
# MyCoM = np.zeros(1,dtype=clType4) |
|
| 563 |
# MyDataX = np.zeros(Number, dtype=clType4) |
|
| 564 |
# MyDataV = np.zeros(Number, dtype=clType4) |
|
| 565 |
MyPotential = np.zeros(Number, dtype=MyFloat) |
|
| 566 |
MyKinetic = np.zeros(Number, dtype=MyFloat) |
|
| 567 |
|
|
| 568 |
Marsaglia,Computing=DictionariesAPI() |
|
| 569 |
|
|
| 570 |
# Scan the OpenCL arrays |
|
| 571 |
Id=0 |
|
| 572 |
HasXPU=False |
|
| 573 |
for platform in cl.get_platforms(): |
|
| 574 |
for device in platform.get_devices(): |
|
| 575 |
if Id==Device: |
|
| 576 |
PlatForm=platform |
|
| 577 |
XPU=device |
|
| 578 |
print("CPU/GPU selected: ",device.name.lstrip())
|
|
| 579 |
print("Platform selected: ",platform.name)
|
|
| 580 |
HasXPU=True |
|
| 581 |
Id+=1 |
|
| 582 |
|
|
| 583 |
if HasXPU==False: |
|
| 584 |
print("No XPU #%i found in all of %i devices, sorry..." % (Device,Id-1))
|
|
| 585 |
sys.exit() |
|
| 586 |
|
|
| 587 |
# Create Context |
|
| 588 |
try: |
|
| 589 |
ctx = cl.Context([XPU]) |
|
| 590 |
queue = cl.CommandQueue(ctx,properties=cl.command_queue_properties.PROFILING_ENABLE) |
|
| 591 |
except: |
|
| 592 |
print("Crash during context creation")
|
|
| 593 |
|
|
| 594 |
print(Marsaglia[RNG],Computing[ValueType]) |
|
| 595 |
# Build all routines used for the computing |
|
| 596 |
#BuildOptions="-DTRNG=%i -DTYPE=%i" % (Marsaglia[RNG],Computing[ValueType]) |
|
| 597 |
#BuildOptions="-cl-mad-enable -cl-fast-relaxed-math -DTRNG=%i -DTYPE=%i" % (Marsaglia[RNG],Computing[ValueType]) |
|
| 598 |
BuildOptions="-cl-mad-enable -cl-kernel-arg-info -cl-fast-relaxed-math -cl-std=CL1.2 -DTRNG=%i -DTYPE=%i" % (Marsaglia[RNG],Computing[ValueType]) |
|
| 599 |
|
|
| 600 |
if 'Intel' in PlatForm.name or 'Clover' in PlatForm.name or 'Portable' in PlatForm.name : |
|
| 601 |
MyRoutines = cl.Program(ctx, BlobOpenCL).build(options = BuildOptions) |
|
| 602 |
else: |
|
| 603 |
MyRoutines = cl.Program(ctx, BlobOpenCL).build(options = BuildOptions+" -cl-strict-aliasing") |
|
| 604 |
|
|
| 605 |
mf = cl.mem_flags |
|
| 606 |
# clDataX = cl.Buffer(ctx, mf.READ_WRITE, MyDataX.nbytes) |
|
| 607 |
# clDataV = cl.Buffer(ctx, mf.READ_WRITE, MyDataV.nbytes) |
|
| 608 |
# clPotential = cl.Buffer(ctx, mf.READ_WRITE, MyPotential.nbytes) |
|
| 609 |
# clKinetic = cl.Buffer(ctx, mf.READ_WRITE, MyKinetic.nbytes) |
|
| 610 |
# clCoM = cl.Buffer(ctx, mf.READ_WRITE, MyCoM.nbytes) |
|
| 611 |
|
|
| 612 |
clDataX = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyDataX) |
|
| 613 |
clDataV = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyDataV) |
|
| 614 |
clPotential = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyPotential) |
|
| 615 |
clKinetic = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyKinetic) |
|
| 616 |
clCoM = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyCoM) |
|
| 617 |
|
|
| 618 |
print('All particles superimposed.')
|
|
| 619 |
|
|
| 620 |
print(SizeOfBox.dtype) |
|
| 621 |
|
|
| 622 |
# Set particles to RNG points |
|
| 623 |
if InitialRandom: |
|
| 624 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clDataX,SizeOfBox,np.uint32(nprnd(2**32)),np.uint32(nprnd(2**32))) |
|
| 625 |
else: |
|
| 626 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clDataX,SizeOfBox,np.uint32(110271),np.uint32(250173)) |
|
| 627 |
|
|
| 628 |
print('All particules distributed')
|
|
| 629 |
|
|
| 630 |
CLLaunch=MyRoutines.CenterOfMass(queue,(1,1),None,clDataX,clCoM,np.int32(Number)) |
|
| 631 |
CLLaunch.wait() |
|
| 632 |
cl.enqueue_copy(queue,MyCoM,clCoM) |
|
| 633 |
print('Center Of Mass: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2]))
|
|
| 634 |
|
|
| 635 |
if VirielStress: |
|
| 636 |
CLLaunch=MyRoutines.SplutterStress(queue,(Number,1),None,clDataX,clDataV,clCoM,MyFloat(0.),np.uint32(110271),np.uint32(250173)) |
|
| 637 |
else: |
|
| 638 |
CLLaunch=MyRoutines.SplutterStress(queue,(Number,1),None,clDataX,clDataV,clCoM,Velocity,np.uint32(110271),np.uint32(250173)) |
|
| 639 |
CLLaunch.wait() |
|
| 640 |
|
|
| 641 |
if GraphSamples: |
|
| 642 |
cl.enqueue_copy(queue, MyDataX, clDataX) |
|
| 643 |
# t0=np.array([[MyDataX[0][0],MyDataX[0][1],MyDataX[0][2]]]) |
|
| 644 |
# t1=np.array([[MyDataX[1][0],MyDataX[1][1],MyDataX[1][2]]]) |
|
| 645 |
# tL=np.array([[MyDataX[-1][0],MyDataX[-1][1],MyDataX[-1][2]]]) |
|
| 646 |
|
|
| 647 |
CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clDataX,clPotential) |
|
| 648 |
CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clDataV,clKinetic) |
|
| 649 |
CLLaunch.wait() |
|
| 650 |
cl.enqueue_copy(queue,MyPotential,clPotential) |
|
| 651 |
cl.enqueue_copy(queue,MyKinetic,clKinetic) |
|
| 652 |
print('Viriel=%s Potential=%s Kinetic=%s'% (np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
|
|
| 653 |
|
|
| 654 |
if GraphSamples: |
|
| 655 |
cl.enqueue_copy(queue, MyDataX, clDataX) |
|
| 656 |
# t0=np.array([[MyDataX[0][0],MyDataX[0][1],MyDataX[0][2]]]) |
|
| 657 |
# t1=np.array([[MyDataX[1][0],MyDataX[1][1],MyDataX[1][2]]]) |
|
| 658 |
# tL=np.array([[MyDataX[-1][0],MyDataX[-1][1],MyDataX[-1][2]]]) |
|
| 659 |
|
|
| 660 |
time_start=time.time() |
|
| 661 |
print("Use the mouse buttons to control some of the dots.")
|
|
| 662 |
print("Hit any key to quit.")
|
|
| 663 |
main() |
|
| 664 |
print("\nDuration on %s for each %s\n" % (Device,(time.time()-time_start)/Iterations))
|
|
| 665 |
|
|
| 666 |
MyRoutines.CenterOfMass(queue,(1,1),None,clDataX,clCoM,np.int32(Number)) |
|
| 667 |
CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clDataX,clPotential) |
|
| 668 |
CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clDataV,clKinetic) |
|
| 669 |
CLLaunch.wait() |
|
| 670 |
cl.enqueue_copy(queue,MyCoM,clCoM) |
|
| 671 |
cl.enqueue_copy(queue,MyPotential,clPotential) |
|
| 672 |
cl.enqueue_copy(queue,MyKinetic,clKinetic) |
|
| 673 |
print('Center Of Mass: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2]))
|
|
| 674 |
print('Viriel=%s Potential=%s Kinetic=%s'% (np.sum(MyPotential)+2.*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
|
|
| 675 |
|
|
| 676 |
if GraphSamples: |
|
| 677 |
t0=np.transpose(np.reshape(t0,(Iterations+1,3))) |
|
| 678 |
t1=np.transpose(np.reshape(t1,(Iterations+1,3))) |
|
| 679 |
tL=np.transpose(np.reshape(tL,(Iterations+1,3))) |
|
| 680 |
|
|
| 681 |
import matplotlib.pyplot as plt |
|
| 682 |
from mpl_toolkits.mplot3d import Axes3D |
|
| 683 |
|
|
| 684 |
fig = plt.figure() |
|
| 685 |
ax = fig.gca(projection='3d') |
|
| 686 |
ax.scatter(t0[0],t0[1],t0[2], marker='^',color='blue') |
|
| 687 |
ax.scatter(t1[0],t1[1],t1[2], marker='o',color='red') |
|
| 688 |
ax.scatter(tL[0],tL[1],tL[2], marker='D',color='green') |
|
| 689 |
|
|
| 690 |
ax.set_xlabel('X Label')
|
|
| 691 |
ax.set_ylabel('Y Label')
|
|
| 692 |
ax.set_zlabel('Z Label')
|
|
| 693 |
|
|
| 694 |
plt.show() |
|
| 695 |
|
|
| 696 |
clDataX.release() |
|
| 697 |
clDataV.release() |
|
| 698 |
clKinetic.release() |
|
| 699 |
clPotential.release() |
|
| 700 |
|
|
Formats disponibles : Unified diff