root / NBody / NBodyGL.py @ 164
Historique | Voir | Annoter | Télécharger (21,6 ko)
| 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,0.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 |
int gid=get_global_id(0);
|
| 240 |
uint z=seed_z+gid;
|
| 241 |
uint w=seed_w+gid;
|
| 242 |
|
| 243 |
for (int i=0;i<gid;i++)
|
| 244 |
{
|
| 245 |
private MYFLOAT heat=MWCfp;
|
| 246 |
}
|
| 247 |
|
| 248 |
// Distribute in sphere
|
| 249 |
MYFLOAT radius=MWCfp*box;
|
| 250 |
MYFLOAT phi=MWCfp*PI*(MYFLOAT)2.e0f;
|
| 251 |
MYFLOAT theta=acos(1.0e0f-2.e0f*MWCfp);
|
| 252 |
// cast to float for sin,cos are NEEDED by Mesa FP64 implementation!
|
| 253 |
MYFLOAT sinTheta=sin((float)theta);
|
| 254 |
clDataX[gid].s0=radius*sinTheta*cos((float)phi)/2.e0f;
|
| 255 |
clDataX[gid].s1=radius*sinTheta*sin((float)phi)/2.e0f;
|
| 256 |
clDataX[gid].s2=radius*cos((float)theta)/2.e0f;
|
| 257 |
clDataX[gid].s3=(MYFLOAT)0.e0f;
|
| 258 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 259 |
|
| 260 |
// Distribute in box
|
| 261 |
// MYFLOAT x0=box*(MYFLOAT)(MWCfp-0.5);
|
| 262 |
// MYFLOAT y0=box*(MYFLOAT)(MWCfp-0.5);
|
| 263 |
// MYFLOAT z0=box*(MYFLOAT)(MWCfp-0.5);
|
| 264 |
// clDataX[gid].s0123 = (MYFLOAT4) (x0,y0,z0,0.e0f);
|
| 265 |
}
|
| 266 |
|
| 267 |
__kernel void SplutterStress(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,__global MYFLOAT4* clCoM, MYFLOAT velocity,uint seed_z,uint seed_w)
|
| 268 |
{
|
| 269 |
int gid = get_global_id(0);
|
| 270 |
MYFLOAT N = (MYFLOAT)get_global_size(0);
|
| 271 |
uint z=seed_z+(uint)gid;
|
| 272 |
uint w=seed_w-(uint)gid;
|
| 273 |
|
| 274 |
if (velocity<SMALL_NUM) {
|
| 275 |
MYFLOAT4 SpeedVector=(MYFLOAT4)normalize(cross(clDataX[gid],clCoM[0]))*sqrt(-AtomicPotential(clDataX,gid)/(MYFLOAT)2.e0f);
|
| 276 |
clDataV[gid]=SpeedVector;
|
| 277 |
}
|
| 278 |
else
|
| 279 |
{
|
| 280 |
// cast to float for sin,cos are NEEDED by Mesa FP64 implementation!
|
| 281 |
MYFLOAT theta=MWCfp*PI;
|
| 282 |
MYFLOAT phi=MWCfp*PI*(MYFLOAT)2.e0f;
|
| 283 |
MYFLOAT sinTheta=sin((float)theta);
|
| 284 |
|
| 285 |
clDataV[gid].s0=velocity*sinTheta*cos((float)phi);
|
| 286 |
clDataV[gid].s1=velocity*sinTheta*sin((float)phi);
|
| 287 |
clDataV[gid].s2=velocity*cos((float)theta);
|
| 288 |
clDataV[gid].s3=(MYFLOAT)0.e0f;
|
| 289 |
}
|
| 290 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 291 |
}
|
| 292 |
|
| 293 |
__kernel void RungeKutta(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h)
|
| 294 |
{
|
| 295 |
int gid = get_global_id(0);
|
| 296 |
|
| 297 |
MYFLOAT8 clDataGid=AtomicRungeKutta(clDataX,clDataV,gid,h);
|
| 298 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 299 |
clDataX[gid]=clDataGid.lo;
|
| 300 |
clDataV[gid]=clDataGid.hi;
|
| 301 |
}
|
| 302 |
|
| 303 |
__kernel void ImplicitEuler(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h)
|
| 304 |
{
|
| 305 |
int gid = get_global_id(0);
|
| 306 |
|
| 307 |
MYFLOAT8 clDataGid=AtomicImplicitEuler(clDataX,clDataV,gid,h);
|
| 308 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 309 |
clDataX[gid]=clDataGid.lo;
|
| 310 |
clDataV[gid]=clDataGid.hi;
|
| 311 |
}
|
| 312 |
|
| 313 |
__kernel void Heun(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h)
|
| 314 |
{
|
| 315 |
int gid = get_global_id(0);
|
| 316 |
|
| 317 |
MYFLOAT8 clDataGid=AtomicHeun(clDataX,clDataV,gid,h);
|
| 318 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 319 |
clDataX[gid]=clDataGid.lo;
|
| 320 |
clDataV[gid]=clDataGid.hi;
|
| 321 |
}
|
| 322 |
|
| 323 |
__kernel void ExplicitEuler(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h)
|
| 324 |
{
|
| 325 |
int gid = get_global_id(0);
|
| 326 |
|
| 327 |
MYFLOAT8 clDataGid=AtomicExplicitEuler(clDataX,clDataV,gid,h);
|
| 328 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 329 |
clDataX[gid]=clDataGid.lo;
|
| 330 |
clDataV[gid]=clDataGid.hi;
|
| 331 |
}
|
| 332 |
|
| 333 |
__kernel void CoMPotential(__global MYFLOAT4* clDataX,__global MYFLOAT4* clCoM,__global MYFLOAT* clPotential)
|
| 334 |
{
|
| 335 |
int gid = get_global_id(0);
|
| 336 |
|
| 337 |
clPotential[gid]=PairPotential(clDataX[gid],clCoM[0]);
|
| 338 |
}
|
| 339 |
|
| 340 |
__kernel void Potential(__global MYFLOAT4* clDataX,__global MYFLOAT* clPotential)
|
| 341 |
{
|
| 342 |
int gid = get_global_id(0);
|
| 343 |
|
| 344 |
MYFLOAT potential=(MYFLOAT)0.e0f;
|
| 345 |
MYFLOAT4 x=clDataX[gid];
|
| 346 |
|
| 347 |
for (int i=0;i<get_global_size(0);i++)
|
| 348 |
{
|
| 349 |
if (gid != i)
|
| 350 |
potential+=PairPotential(x,clDataX[i]);
|
| 351 |
}
|
| 352 |
|
| 353 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 354 |
clPotential[gid]=potential*(MYFLOAT)5.e-1f;
|
| 355 |
}
|
| 356 |
|
| 357 |
__kernel void CenterOfMass(__global MYFLOAT4* clDataX,__global MYFLOAT4* clCoM,int Size)
|
| 358 |
{
|
| 359 |
MYFLOAT4 CoM=clDataX[0];
|
| 360 |
|
| 361 |
for (int i=1;i<Size;i++)
|
| 362 |
{
|
| 363 |
CoM+=clDataX[i];
|
| 364 |
}
|
| 365 |
|
| 366 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 367 |
clCoM[0]=(MYFLOAT4)(CoM.s0,CoM.s1,CoM.s2,1.e0f)/(MYFLOAT)Size;
|
| 368 |
}
|
| 369 |
|
| 370 |
__kernel void Kinetic(__global MYFLOAT4* clDataV,__global MYFLOAT* clKinetic)
|
| 371 |
{
|
| 372 |
int gid = get_global_id(0);
|
| 373 |
|
| 374 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 375 |
MYFLOAT d=(MYFLOAT)length(clDataV[gid]);
|
| 376 |
clKinetic[gid]=(MYFLOAT)5.e-1f*(MYFLOAT)(d*d);
|
| 377 |
}
|
| 378 |
"""
|
| 379 |
|
| 380 |
def display(*args): |
| 381 |
|
| 382 |
global MyDataX,MyDataV,clDataX,clDataV,Step,Method,Number,Iterations,Durations,Verbose
|
| 383 |
|
| 384 |
glClearColor(0.0, 0.0, 0.0, 0.0) |
| 385 |
glClear(GL_COLOR_BUFFER_BIT) |
| 386 |
glColor3f(1.0,1.0,1.0) |
| 387 |
|
| 388 |
time_start=time.time() |
| 389 |
if Method=="RungeKutta": |
| 390 |
CLLaunch=MyRoutines.RungeKutta(queue,(Number,1),None,clDataX,clDataV,Step) |
| 391 |
elif Method=="ExplicitEuler": |
| 392 |
CLLaunch=MyRoutines.ExplicitEuler(queue,(Number,1),None,clDataX,clDataV,Step) |
| 393 |
elif Method=="Heun": |
| 394 |
CLLaunch=MyRoutines.Heun(queue,(Number,1),None,clDataX,clDataV,Step) |
| 395 |
else:
|
| 396 |
CLLaunch=MyRoutines.ImplicitEuler(queue,(Number,1),None,clDataX,clDataV,Step) |
| 397 |
CLLaunch.wait() |
| 398 |
Elapsed=time.time()-time_start |
| 399 |
Durations=np.append(Durations,Elapsed) |
| 400 |
if Verbose:
|
| 401 |
print("Duration of #%s iteration: %s" % (Iterations,Elapsed))
|
| 402 |
else:
|
| 403 |
sys.stdout.write('.')
|
| 404 |
sys.stdout.flush() |
| 405 |
cl.enqueue_copy(queue, MyDataX, clDataX) |
| 406 |
# print(MyDataX.reshape(Number,4))
|
| 407 |
MyDataX.reshape(Number,4)[:,3]=1 |
| 408 |
glVertexPointerf(MyDataX.reshape(Number,4))
|
| 409 |
# cl.enqueue_copy(queue, MyDataV, clDataV)
|
| 410 |
# print(MyDataV.reshape(Number,4))
|
| 411 |
# MyDataV.reshape(Number,4)[:,3]=1
|
| 412 |
# glVertexPointerf(MyDataV.reshape(Number,4))
|
| 413 |
glEnableClientState(GL_VERTEX_ARRAY) |
| 414 |
glDrawArrays(GL_POINTS, 0, Number)
|
| 415 |
glDisableClientState(GL_VERTEX_ARRAY) |
| 416 |
glFlush() |
| 417 |
Iterations+=1
|
| 418 |
glutSwapBuffers() |
| 419 |
|
| 420 |
def halt(): |
| 421 |
pass
|
| 422 |
|
| 423 |
def keyboard(k,x,y): |
| 424 |
global ViewRZ,SizeOfBox
|
| 425 |
LC_Z = as_8_bit( 'z' )
|
| 426 |
UC_Z = as_8_bit( 'Z' )
|
| 427 |
Plus = as_8_bit( '+' )
|
| 428 |
Minus = as_8_bit( '-' )
|
| 429 |
|
| 430 |
Zoom=1
|
| 431 |
if k == LC_Z:
|
| 432 |
ViewRZ += 1.0
|
| 433 |
elif k == UC_Z:
|
| 434 |
ViewRZ -= 1.0
|
| 435 |
elif k == Plus:
|
| 436 |
Zoom *= 2.0
|
| 437 |
elif k == Minus:
|
| 438 |
Zoom /= 2.0
|
| 439 |
elif ord(k) == 27: # Escape |
| 440 |
glutLeaveMainLoop() |
| 441 |
return(False) |
| 442 |
else:
|
| 443 |
return
|
| 444 |
glRotatef(ViewRZ, 0.0, 0.0, 1.0) |
| 445 |
glScalef(Zoom,Zoom,Zoom) |
| 446 |
glutPostRedisplay() |
| 447 |
|
| 448 |
def special(k,x,y): |
| 449 |
global ViewRX, ViewRY, ViewRZ
|
| 450 |
|
| 451 |
if k == GLUT_KEY_UP:
|
| 452 |
ViewRX += 1.0
|
| 453 |
elif k == GLUT_KEY_DOWN:
|
| 454 |
ViewRX -= 1.0
|
| 455 |
elif k == GLUT_KEY_LEFT:
|
| 456 |
ViewRY += 1.0
|
| 457 |
elif k == GLUT_KEY_RIGHT:
|
| 458 |
ViewRY -= 1.0
|
| 459 |
else:
|
| 460 |
return
|
| 461 |
glRotatef(ViewRX, 1.0, 0.0, 0.0) |
| 462 |
glRotatef(ViewRY, 0.0, 1.0, 0.0) |
| 463 |
glutPostRedisplay() |
| 464 |
|
| 465 |
def setup_viewport(): |
| 466 |
global SizeOfBox
|
| 467 |
glMatrixMode(GL_PROJECTION) |
| 468 |
glLoadIdentity() |
| 469 |
glOrtho(-SizeOfBox, SizeOfBox, -SizeOfBox, SizeOfBox, -SizeOfBox, SizeOfBox) |
| 470 |
glutPostRedisplay() |
| 471 |
|
| 472 |
def reshape(w, h): |
| 473 |
glViewport(0, 0, w, h) |
| 474 |
setup_viewport() |
| 475 |
|
| 476 |
if __name__=='__main__': |
| 477 |
|
| 478 |
global Number,Step,clDataX,clDataV,MyDataX,MyDataV,Method,SizeOfBox,Iterations,Verbose
|
| 479 |
|
| 480 |
# ValueType
|
| 481 |
ValueType='FP32'
|
| 482 |
class MyFloat(np.float32):pass |
| 483 |
# clType8=cl_array.vec.float8
|
| 484 |
# Set defaults values
|
| 485 |
np.set_printoptions(precision=2)
|
| 486 |
# Id of Device : 1 is for first find !
|
| 487 |
Device=0
|
| 488 |
# Number of bodies is integer
|
| 489 |
Number=2
|
| 490 |
# Size of box
|
| 491 |
SizeOfBall=MyFloat(1.)
|
| 492 |
# Initial velocity of particules
|
| 493 |
Velocity=MyFloat(1.)
|
| 494 |
# Step
|
| 495 |
Step=MyFloat(1./256) |
| 496 |
# Method of integration
|
| 497 |
Method='ImplicitEuler'
|
| 498 |
# InitialRandom
|
| 499 |
InitialRandom=False
|
| 500 |
# RNG Marsaglia Method
|
| 501 |
RNG='MWC'
|
| 502 |
# Viriel Distribution of stress
|
| 503 |
VirielStress=True
|
| 504 |
# Verbose
|
| 505 |
Verbose=False
|
| 506 |
|
| 507 |
HowToUse='%s -h [Help] -r [InitialRandom] -e [VirielStress] -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBall> -v <Velocity> -s <Step> -m <ImplicitEuler|RungeKutta|ExplicitEuler|Heun> -t <FP32|FP64>'
|
| 508 |
|
| 509 |
try:
|
| 510 |
opts, args = getopt.getopt(sys.argv[1:],"rehd:n:z:v:s:m:t:",["random","viriel","device=","number=","size=","velocity=","step=","method=","valuetype="]) |
| 511 |
except getopt.GetoptError:
|
| 512 |
print(HowToUse % sys.argv[0])
|
| 513 |
sys.exit(2)
|
| 514 |
|
| 515 |
for opt, arg in opts: |
| 516 |
if opt == '-h': |
| 517 |
print(HowToUse % sys.argv[0])
|
| 518 |
|
| 519 |
print("\nInformations about devices detected under OpenCL:")
|
| 520 |
try:
|
| 521 |
Id=0
|
| 522 |
for platform in cl.get_platforms(): |
| 523 |
for device in platform.get_devices(): |
| 524 |
#deviceType=cl.device_type.to_string(device.type)
|
| 525 |
deviceType="xPU"
|
| 526 |
print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip()))
|
| 527 |
Id=Id+1
|
| 528 |
sys.exit() |
| 529 |
except ImportError: |
| 530 |
print("Your platform does not seem to support OpenCL")
|
| 531 |
sys.exit() |
| 532 |
|
| 533 |
elif opt in ("-t", "--valuetype"): |
| 534 |
if arg=='FP64': |
| 535 |
class MyFloat(np.float64): pass |
| 536 |
clType4=cl_array.vec.double4 |
| 537 |
else:
|
| 538 |
class MyFloat(np.float32):pass |
| 539 |
clType4=cl_array.vec.float4 |
| 540 |
ValueType = arg |
| 541 |
elif opt in ("-d", "--device"): |
| 542 |
Device=int(arg)
|
| 543 |
elif opt in ("-m", "--method"): |
| 544 |
Method=arg |
| 545 |
elif opt in ("-n", "--number"): |
| 546 |
Number=int(arg)
|
| 547 |
elif opt in ("-z", "--size"): |
| 548 |
SizeOfBall=MyFloat(arg) |
| 549 |
elif opt in ("-v", "--velocity"): |
| 550 |
Velocity=MyFloat(arg) |
| 551 |
VirielStress=False
|
| 552 |
elif opt in ("-s", "--step"): |
| 553 |
Step=MyFloat(arg) |
| 554 |
elif opt in ("-r", "--random"): |
| 555 |
InitialRandom=True
|
| 556 |
elif opt in ("-c", "--check"): |
| 557 |
CheckEnergies=True
|
| 558 |
elif opt in ("-e", "--viriel"): |
| 559 |
VirielStress=True
|
| 560 |
|
| 561 |
SizeOfBall=MyFloat(SizeOfBall*Number) |
| 562 |
Velocity=MyFloat(Velocity) |
| 563 |
Step=MyFloat(Step) |
| 564 |
|
| 565 |
print("Device choosed : %s" % Device)
|
| 566 |
print("Number of particules : %s" % Number)
|
| 567 |
print("Size of Ball : %s" % SizeOfBall)
|
| 568 |
print("Initial velocity : %s" % Velocity)
|
| 569 |
print("Step of iteration : %s" % Step)
|
| 570 |
print("Method of resolution : %s" % Method)
|
| 571 |
print("Initial Random for RNG Seed : %s" % InitialRandom)
|
| 572 |
print("ValueType is : %s" % ValueType)
|
| 573 |
print("Viriel distribution of stress %s" % VirielStress)
|
| 574 |
|
| 575 |
# Create Numpy array of CL vector with 8 FP32
|
| 576 |
MyCoM = np.zeros(4,dtype=MyFloat)
|
| 577 |
MyDataX = np.zeros(Number*4, dtype=MyFloat)
|
| 578 |
MyDataV = np.zeros(Number*4, dtype=MyFloat)
|
| 579 |
MyPotential = np.zeros(Number, dtype=MyFloat) |
| 580 |
MyKinetic = np.zeros(Number, dtype=MyFloat) |
| 581 |
|
| 582 |
Marsaglia,Computing=DictionariesAPI() |
| 583 |
|
| 584 |
# Scan the OpenCL arrays
|
| 585 |
Id=0
|
| 586 |
HasXPU=False
|
| 587 |
for platform in cl.get_platforms(): |
| 588 |
for device in platform.get_devices(): |
| 589 |
if Id==Device:
|
| 590 |
PlatForm=platform |
| 591 |
XPU=device |
| 592 |
print("CPU/GPU selected: ",device.name.lstrip())
|
| 593 |
print("Platform selected: ",platform.name)
|
| 594 |
HasXPU=True
|
| 595 |
Id+=1
|
| 596 |
|
| 597 |
if HasXPU==False: |
| 598 |
print("No XPU #%i found in all of %i devices, sorry..." % (Device,Id-1)) |
| 599 |
sys.exit() |
| 600 |
|
| 601 |
# Create Context
|
| 602 |
try:
|
| 603 |
ctx = cl.Context([XPU]) |
| 604 |
queue = cl.CommandQueue(ctx,properties=cl.command_queue_properties.PROFILING_ENABLE) |
| 605 |
except:
|
| 606 |
print("Crash during context creation")
|
| 607 |
|
| 608 |
# print(Marsaglia[RNG],Computing[ValueType])
|
| 609 |
# Build all routines used for the computing
|
| 610 |
BuildOptions="-cl-mad-enable -cl-kernel-arg-info -cl-fast-relaxed-math -cl-std=CL1.2 -DTRNG=%i -DTYPE=%i" % (Marsaglia[RNG],Computing[ValueType])
|
| 611 |
|
| 612 |
if 'Intel' in PlatForm.name or 'Experimental' in PlatForm.name or 'Clover' in PlatForm.name or 'Portable' in PlatForm.name : |
| 613 |
MyRoutines = cl.Program(ctx, BlobOpenCL).build(options = BuildOptions) |
| 614 |
else:
|
| 615 |
MyRoutines = cl.Program(ctx, BlobOpenCL).build(options = BuildOptions+" -cl-strict-aliasing")
|
| 616 |
|
| 617 |
mf = cl.mem_flags |
| 618 |
# Read/Write approach for buffering
|
| 619 |
# clDataX = cl.Buffer(ctx, mf.READ_WRITE, MyDataX.nbytes)
|
| 620 |
# clDataV = cl.Buffer(ctx, mf.READ_WRITE, MyDataV.nbytes)
|
| 621 |
# clPotential = cl.Buffer(ctx, mf.READ_WRITE, MyPotential.nbytes)
|
| 622 |
# clKinetic = cl.Buffer(ctx, mf.READ_WRITE, MyKinetic.nbytes)
|
| 623 |
# clCoM = cl.Buffer(ctx, mf.READ_WRITE, MyCoM.nbytes)
|
| 624 |
|
| 625 |
# Write/HostPoniter approach for buffering
|
| 626 |
clDataX = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyDataX) |
| 627 |
clDataV = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyDataV) |
| 628 |
clPotential = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyPotential) |
| 629 |
clKinetic = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyKinetic) |
| 630 |
clCoM = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyCoM) |
| 631 |
|
| 632 |
print('All particles superimposed.')
|
| 633 |
|
| 634 |
# Set particles to RNG points
|
| 635 |
if InitialRandom:
|
| 636 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clDataX,SizeOfBall,np.uint32(nprnd(2**32)),np.uint32(nprnd(2**32))) |
| 637 |
else:
|
| 638 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clDataX,SizeOfBall,np.uint32(110271),np.uint32(250173)) |
| 639 |
|
| 640 |
print('All particules distributed')
|
| 641 |
|
| 642 |
CLLaunch=MyRoutines.CenterOfMass(queue,(1,1),None,clDataX,clCoM,np.int32(Number)) |
| 643 |
CLLaunch.wait() |
| 644 |
cl.enqueue_copy(queue,MyCoM,clCoM) |
| 645 |
print('\nCenter Of Mass: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2])) |
| 646 |
|
| 647 |
if VirielStress:
|
| 648 |
CLLaunch=MyRoutines.SplutterStress(queue,(Number,1),None,clDataX,clDataV,clCoM,MyFloat(0.),np.uint32(110271),np.uint32(250173)) |
| 649 |
else:
|
| 650 |
CLLaunch=MyRoutines.SplutterStress(queue,(Number,1),None,clDataX,clDataV,clCoM,Velocity,np.uint32(110271),np.uint32(250173)) |
| 651 |
CLLaunch.wait() |
| 652 |
|
| 653 |
CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clDataX,clPotential) |
| 654 |
CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clDataV,clKinetic) |
| 655 |
CLLaunch.wait() |
| 656 |
cl.enqueue_copy(queue,MyPotential,clPotential) |
| 657 |
cl.enqueue_copy(queue,MyKinetic,clKinetic) |
| 658 |
print('Viriel=%s Potential=%s Kinetic=%s\n'% (np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic))) |
| 659 |
|
| 660 |
wall_time_start=time.time() |
| 661 |
|
| 662 |
# Launch OpenGL Loop
|
| 663 |
global SizeOfBox,ViewRX,ViewRY,ViewRZ,Iterations,Durations
|
| 664 |
ViewRX,ViewRY,ViewRZ = 0.,0.,0. |
| 665 |
SizeOfBox=SizeOfBall |
| 666 |
Iterations=0
|
| 667 |
Durations=np.array([],dtype=MyFloat) |
| 668 |
glutInit(sys.argv) |
| 669 |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) |
| 670 |
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION) |
| 671 |
glutSetOption(GLUT_ACTION_GLUTMAINLOOP_RETURNS,GLUT_ACTION_CONTINUE_EXECUTION) |
| 672 |
glutInitWindowSize(512,512) |
| 673 |
glutCreateWindow(b'NBodyGL')
|
| 674 |
setup_viewport() |
| 675 |
glutReshapeFunc(reshape) |
| 676 |
glutDisplayFunc(display) |
| 677 |
glutIdleFunc(display) |
| 678 |
# glutMouseFunc(mouse)
|
| 679 |
glutSpecialFunc(special) |
| 680 |
glutKeyboardFunc(keyboard) |
| 681 |
glutMainLoop() |
| 682 |
|
| 683 |
MyRoutines.CenterOfMass(queue,(1,1),None,clDataX,clCoM,np.int32(Number)) |
| 684 |
CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clDataX,clPotential) |
| 685 |
CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clDataV,clKinetic) |
| 686 |
CLLaunch.wait() |
| 687 |
cl.enqueue_copy(queue,MyCoM,clCoM) |
| 688 |
cl.enqueue_copy(queue,MyPotential,clPotential) |
| 689 |
cl.enqueue_copy(queue,MyKinetic,clKinetic) |
| 690 |
print('\nCenter Of Mass: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2])) |
| 691 |
print('Viriel=%s Potential=%s Kinetic=%s\n'% (np.sum(MyPotential)+2.*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic))) |
| 692 |
|
| 693 |
print("\nDuration stats on device %s :\n\tMean:\t%s\n\tMedian:\t%s\n\tStddev:\t%s\n\tMin:\t%s\n\tMax:\t%s\n" % (Device,np.mean(Durations),np.median(Durations),np.std(Durations),np.min(Durations),np.max(Durations)))
|
| 694 |
|
| 695 |
# Contraction of Square*Size*Hertz: Size*Size/Tile
|
| 696 |
Squertz=np.ones(len(Durations))
|
| 697 |
Squertz*=Number*Number |
| 698 |
Squertz/=Durations |
| 699 |
|
| 700 |
print("\nSquertz stats on device %s :\n\tMean:\t%s\n\tMedian:\t%s\n\tStddev:\t%s\n\tMin:\t%s\n\tMax:\t%s\n" % (Device,np.mean(Squertz),np.median(Squertz),np.std(Squertz),np.min(Squertz),np.max(Squertz)))
|
| 701 |
|
| 702 |
clDataX.release() |
| 703 |
clDataV.release() |
| 704 |
clKinetic.release() |
| 705 |
clPotential.release() |
| 706 |
|