root / NBody / NBodyGL.py @ 167
Historique | Voir | Annoter | Télécharger (22,91 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 |
|
| 25 |
|
| 26 |
Blob="""
|
| 27 |
__kernel void RungeKutta(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h)
|
| 28 |
{
|
| 29 |
private int gid = get_global_id(0);
|
| 30 |
private MYFLOAT8 clDataGid;
|
| 31 |
|
| 32 |
clDataGid=AtomicRungeKutta(clDataX,clDataV,gid,h);
|
| 33 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 34 |
clDataX[gid]=clDataGid.s0123;
|
| 35 |
clDataV[gid]=clDataGid.s4567;
|
| 36 |
}
|
| 37 |
|
| 38 |
"""
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
BlobOpenCL= """
|
| 101 |
#define TFP32 0
|
| 102 |
#define TFP64 1
|
| 103 |
|
| 104 |
#if TYPE == TFP32
|
| 105 |
#define MYFLOAT4 float4
|
| 106 |
#define MYFLOAT8 float8
|
| 107 |
#define MYFLOAT float
|
| 108 |
#define DISTANCE fast_distance
|
| 109 |
#else
|
| 110 |
#define MYFLOAT4 double4
|
| 111 |
#define MYFLOAT8 double8
|
| 112 |
#define MYFLOAT double
|
| 113 |
#define DISTANCE distance
|
| 114 |
#if defined(cl_khr_fp64) // Khronos extension available?
|
| 115 |
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
|
| 116 |
#endif
|
| 117 |
#endif
|
| 118 |
|
| 119 |
|
| 120 |
#define znew ((zmwc=36969*(zmwc&65535)+(zmwc>>16))<<16)
|
| 121 |
#define wnew ((wmwc=18000*(wmwc&65535)+(wmwc>>16))&65535)
|
| 122 |
#define MWC (znew+wnew)
|
| 123 |
#define SHR3 (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5))
|
| 124 |
#define CONG (jcong=69069*jcong+1234567)
|
| 125 |
#define KISS ((MWC^CONG)+SHR3)
|
| 126 |
|
| 127 |
|
| 128 |
#define MWCfp (MYFLOAT)(MWC * 2.3283064365386963e-10f)
|
| 129 |
#define KISSfp (MYFLOAT)(KISS * 2.3283064365386963e-10f)
|
| 130 |
#define SHR3fp (MYFLOAT)(SHR3 * 2.3283064365386963e-10f)
|
| 131 |
#define CONGfp (MYFLOAT)(CONG * 2.3283064365386963e-10f)
|
| 132 |
|
| 133 |
#define PI (MYFLOAT)3.141592653589793238462643197169399375105820974944592307816406286e0f
|
| 134 |
|
| 135 |
#define SMALL_NUM (MYFLOAT)1.e-9f
|
| 136 |
|
| 137 |
#define LENGTH 1.e0f
|
| 138 |
|
| 139 |
// Create my own Distance implementation: distance buggy on Oland AMD chipset
|
| 140 |
|
| 141 |
MYFLOAT MyDistance(MYFLOAT4 n,MYFLOAT4 m)
|
| 142 |
{
|
| 143 |
private MYFLOAT x2,y2,z2;
|
| 144 |
x2=n.s0-m.s0;
|
| 145 |
x2*=x2;
|
| 146 |
y2=n.s1-m.s1;
|
| 147 |
y2*=y2;
|
| 148 |
z2=n.s2-m.s2;
|
| 149 |
z2*=z2;
|
| 150 |
return(sqrt(x2+y2+z2));
|
| 151 |
}
|
| 152 |
|
| 153 |
MYFLOAT4 Interaction(MYFLOAT4 m,MYFLOAT4 n)
|
| 154 |
{
|
| 155 |
private MYFLOAT r=MyDistance((MYFLOAT4)n,(MYFLOAT4)m);
|
| 156 |
|
| 157 |
return(((MYFLOAT4)n-(MYFLOAT4)m)/(MYFLOAT)(r*r*r));
|
| 158 |
}
|
| 159 |
|
| 160 |
MYFLOAT PairPotential(MYFLOAT4 m,MYFLOAT4 n)
|
| 161 |
{
|
| 162 |
return((MYFLOAT)(-1.e0f)/(DISTANCE(n,m)));
|
| 163 |
|
| 164 |
// return((MYFLOAT)(-1.e0f)/(MyDistance(n,m)));
|
| 165 |
}
|
| 166 |
|
| 167 |
MYFLOAT AtomicPotential(__global MYFLOAT4* clDataX,int gid)
|
| 168 |
{
|
| 169 |
private MYFLOAT potential=(MYFLOAT)0.e0f;
|
| 170 |
private MYFLOAT4 x=clDataX[gid];
|
| 171 |
|
| 172 |
for (int i=0;i<get_global_size(0);i++)
|
| 173 |
{
|
| 174 |
if (gid != i)
|
| 175 |
potential+=PairPotential(x,clDataX[i]);
|
| 176 |
}
|
| 177 |
|
| 178 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 179 |
return(potential);
|
| 180 |
}
|
| 181 |
|
| 182 |
MYFLOAT AtomicPotentialCoM(__global MYFLOAT4* clDataX,__global MYFLOAT4* clCoM,int gid)
|
| 183 |
{
|
| 184 |
return(PairPotential(clDataX[gid],clCoM[0]));
|
| 185 |
}
|
| 186 |
|
| 187 |
// Elements from : http://doswa.com/2009/01/02/fourth-order-runge-kutta-numerical-integration.html
|
| 188 |
|
| 189 |
MYFLOAT8 AtomicRungeKutta(__global MYFLOAT4* clDataInX,__global MYFLOAT4* clDataInV,int gid,MYFLOAT dt)
|
| 190 |
{
|
| 191 |
private MYFLOAT4 a0,v0,x0,a1,v1,x1,a2,v2,x2,a3,v3,x3,a4,v4,x4,xf,vf;
|
| 192 |
MYFLOAT4 DT=dt*(MYFLOAT4)(1.e0f,1.e0f,1.e0f,1.e0f);
|
| 193 |
|
| 194 |
a0=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f);
|
| 195 |
v0=(MYFLOAT4)clDataInV[gid];
|
| 196 |
x0=(MYFLOAT4)clDataInX[gid];
|
| 197 |
int N = get_global_size(0);
|
| 198 |
|
| 199 |
for (private int i=0;i<N;i++)
|
| 200 |
{
|
| 201 |
if (gid != i)
|
| 202 |
a0+=Interaction(x0,clDataInX[i]);
|
| 203 |
}
|
| 204 |
|
| 205 |
a1=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f);
|
| 206 |
v1=a0*dt+v0;
|
| 207 |
x1=v0*dt+x0;
|
| 208 |
for (private int j=0;j<N;j++)
|
| 209 |
{
|
| 210 |
if (gid != j)
|
| 211 |
a1+=Interaction(x1,clDataInX[j]);
|
| 212 |
}
|
| 213 |
|
| 214 |
a2=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f);
|
| 215 |
v2=a1*(MYFLOAT)(dt/2.e0f)+v0;
|
| 216 |
x2=v1*(MYFLOAT)(dt/2.e0f)+x0;
|
| 217 |
for (private int k=0;k<N;k++)
|
| 218 |
{
|
| 219 |
if (gid != k)
|
| 220 |
a2+=Interaction(x2,clDataInX[k]);
|
| 221 |
}
|
| 222 |
|
| 223 |
a3=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f);
|
| 224 |
v3=a2*(MYFLOAT)(dt/2.e0f)+v0;
|
| 225 |
x3=v2*(MYFLOAT)(dt/2.e0f)+x0;
|
| 226 |
for (private int l=0;l<N;l++)
|
| 227 |
{
|
| 228 |
if (gid != l)
|
| 229 |
a3+=Interaction(x3,clDataInX[l]);
|
| 230 |
}
|
| 231 |
|
| 232 |
a4=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f);
|
| 233 |
v4=a3*dt+v0;
|
| 234 |
x4=v3*dt+x0;
|
| 235 |
for (private int m=0;m<N;m++)
|
| 236 |
{
|
| 237 |
if (gid != m)
|
| 238 |
a4+=Interaction(x4,clDataInX[m]);
|
| 239 |
}
|
| 240 |
|
| 241 |
xf=x0+dt*(v1+(MYFLOAT)2.e0f*(v2+v3)+v4)/(MYFLOAT)6.e0f;
|
| 242 |
vf=v0+dt*(a1+(MYFLOAT)2.e0f*(a2+a3)+a4)/(MYFLOAT)6.e0f;
|
| 243 |
|
| 244 |
return((MYFLOAT8)(xf.s0,xf.s1,xf.s2,0.e0f,vf.s0,vf.s1,vf.s2,0.e0f));
|
| 245 |
}
|
| 246 |
|
| 247 |
MYFLOAT8 AtomicHeun(__global MYFLOAT4* clDataInX,__global MYFLOAT4* clDataInV,int gid,MYFLOAT dt)
|
| 248 |
{
|
| 249 |
private MYFLOAT4 x0,v0,a0,x1,v1,a1,xf,vf;
|
| 250 |
MYFLOAT4 Dt=dt*(MYFLOAT4)(1.e0f,1.e0f,1.e0f,1.e0f);
|
| 251 |
|
| 252 |
x0=(MYFLOAT4)clDataInX[gid];
|
| 253 |
v0=(MYFLOAT4)clDataInV[gid];
|
| 254 |
a0=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f);
|
| 255 |
|
| 256 |
for (private int i=0;i<get_global_size(0);i++)
|
| 257 |
{
|
| 258 |
if (gid != i)
|
| 259 |
a0+=Interaction(x0,clDataInX[i]);
|
| 260 |
}
|
| 261 |
|
| 262 |
a1=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f);
|
| 263 |
//v1=v0+dt*a0;
|
| 264 |
//x1=x0+dt*v0;
|
| 265 |
v1=dt*a0+v0;
|
| 266 |
x1=dt*v0+x0;
|
| 267 |
|
| 268 |
for (private int j=0;j<get_global_size(0);j++)
|
| 269 |
{
|
| 270 |
if (gid != j)
|
| 271 |
a1+=Interaction(x1,clDataInX[j]);
|
| 272 |
}
|
| 273 |
|
| 274 |
vf=v0+dt*(a0+a1)/(MYFLOAT)2.e0f;
|
| 275 |
xf=x0+dt*(v0+v1)/(MYFLOAT)2.e0f;
|
| 276 |
|
| 277 |
return((MYFLOAT8)(xf.s0,xf.s1,xf.s2,0.e0f,vf.s0,vf.s1,vf.s2,0.e0f));
|
| 278 |
}
|
| 279 |
|
| 280 |
MYFLOAT8 AtomicImplicitEuler(__global MYFLOAT4* clDataInX,__global MYFLOAT4* clDataInV,int gid,MYFLOAT dt)
|
| 281 |
{
|
| 282 |
MYFLOAT4 x0,v0,a,xf,vf;
|
| 283 |
|
| 284 |
x0=(MYFLOAT4)clDataInX[gid];
|
| 285 |
v0=(MYFLOAT4)clDataInV[gid];
|
| 286 |
a=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f);
|
| 287 |
|
| 288 |
for (private int i=0;i<get_global_size(0);i++)
|
| 289 |
{
|
| 290 |
if (gid != i)
|
| 291 |
a+=Interaction(x0,clDataInX[i]);
|
| 292 |
}
|
| 293 |
|
| 294 |
vf=v0+dt*a;
|
| 295 |
xf=x0+dt*vf;
|
| 296 |
|
| 297 |
return((MYFLOAT8)(xf.s0,xf.s1,xf.s2,0.e0f,vf.s0,vf.s1,vf.s2,0.e0f));
|
| 298 |
}
|
| 299 |
|
| 300 |
MYFLOAT8 AtomicExplicitEuler(__global MYFLOAT4* clDataInX,__global MYFLOAT4* clDataInV,int gid,MYFLOAT dt)
|
| 301 |
{
|
| 302 |
MYFLOAT4 x0,v0,a,xf,vf;
|
| 303 |
|
| 304 |
x0=(MYFLOAT4)clDataInX[gid];
|
| 305 |
v0=(MYFLOAT4)clDataInV[gid];
|
| 306 |
a=(MYFLOAT4)(0.e0f,0.e0f,0.e0f,0.e0f);
|
| 307 |
|
| 308 |
for (private int i=0;i<get_global_size(0);i++)
|
| 309 |
{
|
| 310 |
if (gid != i)
|
| 311 |
a+=Interaction(x0,clDataInX[i]);
|
| 312 |
}
|
| 313 |
|
| 314 |
vf=v0+dt*a;
|
| 315 |
xf=x0+dt*v0;
|
| 316 |
|
| 317 |
return((MYFLOAT8)(xf.s0,xf.s1,xf.s2,0.e0f,vf.s0,vf.s1,vf.s2,0.e0f));
|
| 318 |
}
|
| 319 |
|
| 320 |
__kernel void SplutterPoints(__global MYFLOAT4* clDataX, MYFLOAT box,
|
| 321 |
uint seed_z,uint seed_w)
|
| 322 |
{
|
| 323 |
int gid=get_global_id(0);
|
| 324 |
uint zmwc=seed_z+gid;
|
| 325 |
uint wmwc=seed_w-gid;
|
| 326 |
private MYFLOAT Heat;
|
| 327 |
|
| 328 |
for (int i=0;i<gid;i++)
|
| 329 |
{
|
| 330 |
Heat=MWCfp;
|
| 331 |
}
|
| 332 |
|
| 333 |
// More accurate distribution based on spherical coordonates
|
| 334 |
// Disactivated because of AMD Oland GPU problems
|
| 335 |
// MYFLOAT Radius=Heat*box;
|
| 336 |
// MYFLOAT Theta=acos((float)(-2.e0f*MWCfp+1.0e0f));
|
| 337 |
// MYFLOAT Phi=(MYFLOAT)2.e0f*PI*MWCfp;
|
| 338 |
// MYFLOAT sinTheta=sin((float)Theta);
|
| 339 |
// MYFLOAT PosX=cos((float)Phi)*Radius*sinTheta;
|
| 340 |
// MYFLOAT PosY=sin((float)Phi)*Radius*sinTheta;
|
| 341 |
// MYFLOAT PosZ=cos((float)Theta)*Radius;
|
| 342 |
// clDataX[gid]=(MYFLOAT4)(PosX,PosY,PosZ,0.e0f);
|
| 343 |
|
| 344 |
MYFLOAT4 Position=(MYFLOAT4)((Heat-0.5e0f)*box,(MWCfp-0.5e0f)*box,(MWCfp-0.5e0f)*box,0.e0f);
|
| 345 |
MYFLOAT Length=(MYFLOAT)length((MYFLOAT4)Position);
|
| 346 |
clDataX[gid]=Position/Length*box*MWCfp;
|
| 347 |
|
| 348 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 349 |
}
|
| 350 |
|
| 351 |
__kernel void SplutterStress(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,__global MYFLOAT4* clCoM, MYFLOAT velocity,uint seed_z,uint seed_w)
|
| 352 |
{
|
| 353 |
int gid = get_global_id(0);
|
| 354 |
MYFLOAT N = (MYFLOAT)get_global_size(0);
|
| 355 |
uint zmwc=seed_z+(uint)gid;
|
| 356 |
uint wmwc=seed_w-(uint)gid;
|
| 357 |
MYFLOAT4 SpeedVector;
|
| 358 |
|
| 359 |
if (velocity<SMALL_NUM) {
|
| 360 |
SpeedVector=(MYFLOAT4)normalize(cross(clDataX[gid],clCoM[0]))*sqrt((-AtomicPotential(clDataX,gid)/(MYFLOAT)2.e0f));
|
| 361 |
}
|
| 362 |
else
|
| 363 |
{
|
| 364 |
// cast to float for sin,cos are NEEDED by Mesa FP64 implementation!
|
| 365 |
// Implemention on AMD Oland are probably broken in float
|
| 366 |
|
| 367 |
MYFLOAT theta=acos((float)(1.0e0f-2.e0f*MWCfp));
|
| 368 |
MYFLOAT phi=MWCfp*PI*(MYFLOAT)2.e0f;
|
| 369 |
MYFLOAT sinTheta=sin((float)theta);
|
| 370 |
MYFLOAT sinPhi=sin((float)phi);
|
| 371 |
|
| 372 |
SpeedVector=(MYFLOAT4)((MWCfp-0.5e0f)*velocity,(MWCfp-0.5e0f)*velocity,
|
| 373 |
(MWCfp-0.5e0f)*velocity,0.e0f);
|
| 374 |
}
|
| 375 |
clDataV[gid]=SpeedVector;
|
| 376 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 377 |
}
|
| 378 |
|
| 379 |
__kernel void RungeKutta(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h)
|
| 380 |
{
|
| 381 |
private int gid = get_global_id(0);
|
| 382 |
private MYFLOAT8 clDataGid;
|
| 383 |
|
| 384 |
clDataGid=AtomicRungeKutta(clDataX,clDataV,gid,h);
|
| 385 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 386 |
clDataX[gid]=clDataGid.s0123;
|
| 387 |
clDataV[gid]=clDataGid.s4567;
|
| 388 |
}
|
| 389 |
|
| 390 |
__kernel void Heun(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h)
|
| 391 |
{
|
| 392 |
private int gid = get_global_id(0);
|
| 393 |
private MYFLOAT8 clDataGid;
|
| 394 |
|
| 395 |
clDataGid=AtomicHeun(clDataX,clDataV,gid,h);
|
| 396 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 397 |
clDataX[gid]=clDataGid.s0123;
|
| 398 |
clDataV[gid]=clDataGid.s4567;
|
| 399 |
}
|
| 400 |
|
| 401 |
__kernel void ImplicitEuler(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h)
|
| 402 |
{
|
| 403 |
private int gid = get_global_id(0);
|
| 404 |
private MYFLOAT8 clDataGid;
|
| 405 |
|
| 406 |
clDataGid=AtomicImplicitEuler(clDataX,clDataV,gid,h);
|
| 407 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 408 |
clDataX[gid]=clDataGid.s0123;
|
| 409 |
clDataV[gid]=clDataGid.s4567;
|
| 410 |
}
|
| 411 |
|
| 412 |
__kernel void ExplicitEuler(__global MYFLOAT4* clDataX,__global MYFLOAT4* clDataV,MYFLOAT h)
|
| 413 |
{
|
| 414 |
private int gid = get_global_id(0);
|
| 415 |
private MYFLOAT8 clDataGid;
|
| 416 |
|
| 417 |
clDataGid=AtomicExplicitEuler(clDataX,clDataV,gid,h);
|
| 418 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 419 |
clDataX[gid]=clDataGid.s0123;
|
| 420 |
clDataV[gid]=clDataGid.s4567;
|
| 421 |
}
|
| 422 |
|
| 423 |
__kernel void CoMPotential(__global MYFLOAT4* clDataX,__global MYFLOAT4* clCoM,__global MYFLOAT* clPotential)
|
| 424 |
{
|
| 425 |
int gid = get_global_id(0);
|
| 426 |
|
| 427 |
clPotential[gid]=PairPotential(clDataX[gid],clCoM[0]);
|
| 428 |
}
|
| 429 |
|
| 430 |
__kernel void Potential(__global MYFLOAT4* clDataX,__global MYFLOAT* clPotential)
|
| 431 |
{
|
| 432 |
int gid = get_global_id(0);
|
| 433 |
|
| 434 |
MYFLOAT potential=(MYFLOAT)0.e0f;
|
| 435 |
MYFLOAT4 x=clDataX[gid];
|
| 436 |
|
| 437 |
for (int i=0;i<get_global_size(0);i++)
|
| 438 |
{
|
| 439 |
if (gid != i)
|
| 440 |
potential+=PairPotential(x,clDataX[i]);
|
| 441 |
}
|
| 442 |
|
| 443 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 444 |
clPotential[gid]=potential*(MYFLOAT)5.e-1f;
|
| 445 |
}
|
| 446 |
|
| 447 |
__kernel void CenterOfMass(__global MYFLOAT4* clDataX,__global MYFLOAT4* clCoM,int Size)
|
| 448 |
{
|
| 449 |
MYFLOAT4 CoM=clDataX[0];
|
| 450 |
|
| 451 |
for (int i=1;i<Size;i++)
|
| 452 |
{
|
| 453 |
CoM+=clDataX[i];
|
| 454 |
}
|
| 455 |
|
| 456 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 457 |
clCoM[0]=(MYFLOAT4)(CoM.s0,CoM.s1,CoM.s2,0.e0f)/(MYFLOAT)Size;
|
| 458 |
}
|
| 459 |
|
| 460 |
__kernel void Kinetic(__global MYFLOAT4* clDataV,__global MYFLOAT* clKinetic)
|
| 461 |
{
|
| 462 |
int gid = get_global_id(0);
|
| 463 |
|
| 464 |
barrier(CLK_GLOBAL_MEM_FENCE);
|
| 465 |
MYFLOAT d=(MYFLOAT)length(clDataV[gid]);
|
| 466 |
clKinetic[gid]=(MYFLOAT)5.e-1f*(MYFLOAT)(d*d);
|
| 467 |
}
|
| 468 |
|
| 469 |
"""
|
| 470 |
|
| 471 |
def display(*args): |
| 472 |
|
| 473 |
global MyDataX,MyDataV,clDataX,clDataV,Step,Method,Number,Iterations,Durations,Verbose
|
| 474 |
|
| 475 |
glClearColor(0.0, 0.0, 0.0, 0.0) |
| 476 |
glClear(GL_COLOR_BUFFER_BIT) |
| 477 |
glColor3f(1.0,1.0,1.0) |
| 478 |
|
| 479 |
time_start=time.time() |
| 480 |
if Method=="RungeKutta": |
| 481 |
CLLaunch=MyRoutines.RungeKutta(queue,(Number,1),None,clDataX,clDataV,Step) |
| 482 |
elif Method=="ExplicitEuler": |
| 483 |
CLLaunch=MyRoutines.ExplicitEuler(queue,(Number,1),None,clDataX,clDataV,Step) |
| 484 |
elif Method=="Heun": |
| 485 |
CLLaunch=MyRoutines.Heun(queue,(Number,1),None,clDataX,clDataV,Step) |
| 486 |
else:
|
| 487 |
CLLaunch=MyRoutines.ImplicitEuler(queue,(Number,1),None,clDataX,clDataV,Step) |
| 488 |
CLLaunch.wait() |
| 489 |
Elapsed=time.time()-time_start |
| 490 |
Durations=np.append(Durations,Elapsed) |
| 491 |
if Verbose:
|
| 492 |
print("Duration of #%s iteration: %s" % (Iterations,Elapsed))
|
| 493 |
else:
|
| 494 |
sys.stdout.write('.')
|
| 495 |
sys.stdout.flush() |
| 496 |
cl.enqueue_copy(queue, MyDataX, clDataX) |
| 497 |
# print(MyDataX.reshape(Number,4))
|
| 498 |
MyDataX.reshape(Number,4)[:,3]=1 |
| 499 |
glVertexPointerf(MyDataX.reshape(Number,4))
|
| 500 |
# cl.enqueue_copy(queue, MyDataV, clDataV)
|
| 501 |
# print(MyDataV.reshape(Number,4))
|
| 502 |
# MyDataV.reshape(Number,4)[:,3]=1
|
| 503 |
# glVertexPointerf(MyDataV.reshape(Number,4))
|
| 504 |
glEnableClientState(GL_VERTEX_ARRAY) |
| 505 |
glDrawArrays(GL_POINTS, 0, Number)
|
| 506 |
glDisableClientState(GL_VERTEX_ARRAY) |
| 507 |
glFlush() |
| 508 |
Iterations+=1
|
| 509 |
glutSwapBuffers() |
| 510 |
|
| 511 |
def halt(): |
| 512 |
pass
|
| 513 |
|
| 514 |
def keyboard(k,x,y): |
| 515 |
global ViewRZ,SizeOfBox
|
| 516 |
LC_Z = as_8_bit( 'z' )
|
| 517 |
UC_Z = as_8_bit( 'Z' )
|
| 518 |
Plus = as_8_bit( '+' )
|
| 519 |
Minus = as_8_bit( '-' )
|
| 520 |
|
| 521 |
Zoom=1
|
| 522 |
if k == LC_Z:
|
| 523 |
ViewRZ += 1.0
|
| 524 |
elif k == UC_Z:
|
| 525 |
ViewRZ -= 1.0
|
| 526 |
elif k == Plus:
|
| 527 |
Zoom *= 2.0
|
| 528 |
elif k == Minus:
|
| 529 |
Zoom /= 2.0
|
| 530 |
elif ord(k) == 27: # Escape |
| 531 |
glutLeaveMainLoop() |
| 532 |
return(False) |
| 533 |
else:
|
| 534 |
return
|
| 535 |
glRotatef(ViewRZ, 0.0, 0.0, 1.0) |
| 536 |
glScalef(Zoom,Zoom,Zoom) |
| 537 |
glutPostRedisplay() |
| 538 |
|
| 539 |
def special(k,x,y): |
| 540 |
global ViewRX, ViewRY, ViewRZ
|
| 541 |
|
| 542 |
if k == GLUT_KEY_UP:
|
| 543 |
ViewRX += 1.0
|
| 544 |
elif k == GLUT_KEY_DOWN:
|
| 545 |
ViewRX -= 1.0
|
| 546 |
elif k == GLUT_KEY_LEFT:
|
| 547 |
ViewRY += 1.0
|
| 548 |
elif k == GLUT_KEY_RIGHT:
|
| 549 |
ViewRY -= 1.0
|
| 550 |
else:
|
| 551 |
return
|
| 552 |
glRotatef(ViewRX, 1.0, 0.0, 0.0) |
| 553 |
glRotatef(ViewRY, 0.0, 1.0, 0.0) |
| 554 |
glutPostRedisplay() |
| 555 |
|
| 556 |
def setup_viewport(): |
| 557 |
global SizeOfBox
|
| 558 |
glMatrixMode(GL_PROJECTION) |
| 559 |
glLoadIdentity() |
| 560 |
glOrtho(-SizeOfBox, SizeOfBox, -SizeOfBox, SizeOfBox, -SizeOfBox, SizeOfBox) |
| 561 |
glutPostRedisplay() |
| 562 |
|
| 563 |
def reshape(w, h): |
| 564 |
glViewport(0, 0, w, h) |
| 565 |
setup_viewport() |
| 566 |
|
| 567 |
if __name__=='__main__': |
| 568 |
|
| 569 |
global Number,Step,clDataX,clDataV,MyDataX,MyDataV,Method,SizeOfBox,Iterations,Verbose
|
| 570 |
|
| 571 |
# ValueType
|
| 572 |
ValueType='FP32'
|
| 573 |
class MyFloat(np.float32):pass |
| 574 |
# clType8=cl_array.vec.float8
|
| 575 |
# Set defaults values
|
| 576 |
np.set_printoptions(precision=2)
|
| 577 |
# Id of Device : 1 is for first find !
|
| 578 |
Device=0
|
| 579 |
# Number of bodies is integer
|
| 580 |
Number=2
|
| 581 |
# Size of box
|
| 582 |
SizeOfBall=MyFloat(1.)
|
| 583 |
# Initial velocity of particules
|
| 584 |
Velocity=MyFloat(1.)
|
| 585 |
# Step
|
| 586 |
Step=MyFloat(1./256) |
| 587 |
# Method of integration
|
| 588 |
Method='ImplicitEuler'
|
| 589 |
# InitialRandom
|
| 590 |
InitialRandom=False
|
| 591 |
# RNG Marsaglia Method
|
| 592 |
RNG='MWC'
|
| 593 |
# Viriel Distribution of stress
|
| 594 |
VirielStress=True
|
| 595 |
# Verbose
|
| 596 |
Verbose=False
|
| 597 |
# OpenGL demo
|
| 598 |
OpenGL=False
|
| 599 |
|
| 600 |
HowToUse='%s -h [Help] -r [InitialRandom] -g [OpenGL] -e [VirielStress] -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBall> -v <Velocity> -s <Step> -m <ImplicitEuler|RungeKutta|ExplicitEuler|Heun> -t <FP32|FP64>'
|
| 601 |
|
| 602 |
try:
|
| 603 |
opts, args = getopt.getopt(sys.argv[1:],"rgehd:n:z:v:s:m:t:",["random","viriel","device=","number=","size=","velocity=","step=","method=","valuetype="]) |
| 604 |
except getopt.GetoptError:
|
| 605 |
print(HowToUse % sys.argv[0])
|
| 606 |
sys.exit(2)
|
| 607 |
|
| 608 |
for opt, arg in opts: |
| 609 |
if opt == '-h': |
| 610 |
print(HowToUse % sys.argv[0])
|
| 611 |
|
| 612 |
print("\nInformations about devices detected under OpenCL:")
|
| 613 |
try:
|
| 614 |
Id=0
|
| 615 |
for platform in cl.get_platforms(): |
| 616 |
for device in platform.get_devices(): |
| 617 |
#deviceType=cl.device_type.to_string(device.type)
|
| 618 |
deviceType="xPU"
|
| 619 |
print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip()))
|
| 620 |
Id=Id+1
|
| 621 |
sys.exit() |
| 622 |
except ImportError: |
| 623 |
print("Your platform does not seem to support OpenCL")
|
| 624 |
sys.exit() |
| 625 |
|
| 626 |
elif opt in ("-t", "--valuetype"): |
| 627 |
if arg=='FP64': |
| 628 |
class MyFloat(np.float64): pass |
| 629 |
clType4=cl_array.vec.double4 |
| 630 |
else:
|
| 631 |
class MyFloat(np.float32):pass |
| 632 |
clType4=cl_array.vec.float4 |
| 633 |
ValueType = arg |
| 634 |
elif opt in ("-d", "--device"): |
| 635 |
Device=int(arg)
|
| 636 |
elif opt in ("-m", "--method"): |
| 637 |
Method=arg |
| 638 |
elif opt in ("-n", "--number"): |
| 639 |
Number=int(arg)
|
| 640 |
elif opt in ("-z", "--size"): |
| 641 |
SizeOfBall=MyFloat(arg) |
| 642 |
elif opt in ("-v", "--velocity"): |
| 643 |
Velocity=MyFloat(arg) |
| 644 |
VirielStress=False
|
| 645 |
elif opt in ("-s", "--step"): |
| 646 |
Step=MyFloat(arg) |
| 647 |
elif opt in ("-r", "--random"): |
| 648 |
InitialRandom=True
|
| 649 |
elif opt in ("-c", "--check"): |
| 650 |
CheckEnergies=True
|
| 651 |
elif opt in ("-e", "--viriel"): |
| 652 |
VirielStress=True
|
| 653 |
elif opt in ("-g", "--opengl"): |
| 654 |
OpenGL=True
|
| 655 |
|
| 656 |
SizeOfBall=MyFloat(SizeOfBall*Number/2.)
|
| 657 |
Velocity=MyFloat(Velocity) |
| 658 |
Step=MyFloat(Step) |
| 659 |
|
| 660 |
print("Device choosed : %s" % Device)
|
| 661 |
print("Number of particules : %s" % Number)
|
| 662 |
print("Size of Ball : %s" % SizeOfBall)
|
| 663 |
print("Initial velocity : %s" % Velocity)
|
| 664 |
print("Step of iteration : %s" % Step)
|
| 665 |
print("Method of resolution : %s" % Method)
|
| 666 |
print("Initial Random for RNG Seed : %s" % InitialRandom)
|
| 667 |
print("ValueType is : %s" % ValueType)
|
| 668 |
print("Viriel distribution of stress %s" % VirielStress)
|
| 669 |
|
| 670 |
# Create Numpy array of CL vector with 8 FP32
|
| 671 |
MyCoM = np.zeros(4,dtype=MyFloat)
|
| 672 |
MyDataX = np.zeros(Number*4, dtype=MyFloat)
|
| 673 |
MyDataV = np.zeros(Number*4, dtype=MyFloat)
|
| 674 |
MyPotential = np.zeros(Number, dtype=MyFloat) |
| 675 |
MyKinetic = np.zeros(Number, dtype=MyFloat) |
| 676 |
|
| 677 |
Marsaglia,Computing=DictionariesAPI() |
| 678 |
|
| 679 |
# Scan the OpenCL arrays
|
| 680 |
Id=0
|
| 681 |
HasXPU=False
|
| 682 |
for platform in cl.get_platforms(): |
| 683 |
for device in platform.get_devices(): |
| 684 |
if Id==Device:
|
| 685 |
PlatForm=platform |
| 686 |
XPU=device |
| 687 |
print("CPU/GPU selected: ",device.name.lstrip())
|
| 688 |
print("Platform selected: ",platform.name)
|
| 689 |
HasXPU=True
|
| 690 |
Id+=1
|
| 691 |
|
| 692 |
if HasXPU==False: |
| 693 |
print("No XPU #%i found in all of %i devices, sorry..." % (Device,Id-1)) |
| 694 |
sys.exit() |
| 695 |
|
| 696 |
# Create Context
|
| 697 |
try:
|
| 698 |
ctx = cl.Context([XPU]) |
| 699 |
queue = cl.CommandQueue(ctx,properties=cl.command_queue_properties.PROFILING_ENABLE) |
| 700 |
except:
|
| 701 |
print("Crash during context creation")
|
| 702 |
|
| 703 |
# Build all routines used for the computing
|
| 704 |
|
| 705 |
#BuildOptions="-cl-mad-enable -cl-kernel-arg-info -cl-fast-relaxed-math -cl-std=CL1.2 -DTRNG=%i -DTYPE=%i" % (Marsaglia[RNG],Computing[ValueType])
|
| 706 |
BuildOptions="-cl-mad-enable -cl-fast-relaxed-math -DTRNG=%i -DTYPE=%i" % (Marsaglia[RNG],Computing[ValueType])
|
| 707 |
|
| 708 |
if 'Intel' in PlatForm.name or 'Experimental' in PlatForm.name or 'Clover' in PlatForm.name or 'Portable' in PlatForm.name : |
| 709 |
MyRoutines = cl.Program(ctx, BlobOpenCL).build(options = BuildOptions) |
| 710 |
else:
|
| 711 |
MyRoutines = cl.Program(ctx, BlobOpenCL).build(options = BuildOptions+" -cl-strict-aliasing")
|
| 712 |
|
| 713 |
mf = cl.mem_flags |
| 714 |
# Read/Write approach for buffering
|
| 715 |
# clDataX = cl.Buffer(ctx, mf.READ_WRITE, MyDataX.nbytes)
|
| 716 |
# clDataV = cl.Buffer(ctx, mf.READ_WRITE, MyDataV.nbytes)
|
| 717 |
# clPotential = cl.Buffer(ctx, mf.READ_WRITE, MyPotential.nbytes)
|
| 718 |
# clKinetic = cl.Buffer(ctx, mf.READ_WRITE, MyKinetic.nbytes)
|
| 719 |
# clCoM = cl.Buffer(ctx, mf.READ_WRITE, MyCoM.nbytes)
|
| 720 |
|
| 721 |
# Write/HostPointer approach for buffering
|
| 722 |
clDataX = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyDataX) |
| 723 |
clDataV = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyDataV) |
| 724 |
clPotential = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyPotential) |
| 725 |
clKinetic = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyKinetic) |
| 726 |
clCoM = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyCoM) |
| 727 |
|
| 728 |
print('All particles superimposed.')
|
| 729 |
|
| 730 |
# Set particles to RNG points
|
| 731 |
if InitialRandom:
|
| 732 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clDataX,SizeOfBall,np.uint32(nprnd(2**32)),np.uint32(nprnd(2**32))) |
| 733 |
else:
|
| 734 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clDataX,SizeOfBall,np.uint32(110271),np.uint32(250173)) |
| 735 |
|
| 736 |
print('All particules distributed')
|
| 737 |
|
| 738 |
CLLaunch=MyRoutines.CenterOfMass(queue,(1,1),None,clDataX,clCoM,np.int32(Number)) |
| 739 |
CLLaunch.wait() |
| 740 |
cl.enqueue_copy(queue,MyCoM,clCoM) |
| 741 |
print('\nCenter Of Mass: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2])) |
| 742 |
|
| 743 |
if VirielStress:
|
| 744 |
CLLaunch=MyRoutines.SplutterStress(queue,(Number,1),None,clDataX,clDataV,clCoM,MyFloat(0.),np.uint32(110271),np.uint32(250173)) |
| 745 |
else:
|
| 746 |
CLLaunch=MyRoutines.SplutterStress(queue,(Number,1),None,clDataX,clDataV,clCoM,Velocity,np.uint32(110271),np.uint32(250173)) |
| 747 |
CLLaunch.wait() |
| 748 |
|
| 749 |
CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clDataX,clPotential) |
| 750 |
CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clDataV,clKinetic) |
| 751 |
CLLaunch.wait() |
| 752 |
cl.enqueue_copy(queue,MyPotential,clPotential) |
| 753 |
cl.enqueue_copy(queue,MyKinetic,clKinetic) |
| 754 |
print('Viriel=%s Potential=%s Kinetic=%s\n'% (np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic))) |
| 755 |
|
| 756 |
wall_time_start=time.time() |
| 757 |
|
| 758 |
# Launch OpenGL Loop
|
| 759 |
global SizeOfBox,ViewRX,ViewRY,ViewRZ,Iterations,Durations
|
| 760 |
ViewRX,ViewRY,ViewRZ = 0.,0.,0. |
| 761 |
SizeOfBox=SizeOfBall |
| 762 |
Iterations=0
|
| 763 |
Durations=np.array([],dtype=MyFloat) |
| 764 |
glutInit(sys.argv) |
| 765 |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) |
| 766 |
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION) |
| 767 |
glutSetOption(GLUT_ACTION_GLUTMAINLOOP_RETURNS,GLUT_ACTION_CONTINUE_EXECUTION) |
| 768 |
glutInitWindowSize(512,512) |
| 769 |
glutCreateWindow(b'NBodyGL')
|
| 770 |
setup_viewport() |
| 771 |
glutReshapeFunc(reshape) |
| 772 |
glutDisplayFunc(display) |
| 773 |
glutIdleFunc(display) |
| 774 |
# glutMouseFunc(mouse)
|
| 775 |
glutSpecialFunc(special) |
| 776 |
glutKeyboardFunc(keyboard) |
| 777 |
glutMainLoop() |
| 778 |
|
| 779 |
MyRoutines.CenterOfMass(queue,(1,1),None,clDataX,clCoM,np.int32(Number)) |
| 780 |
CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clDataX,clPotential) |
| 781 |
CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clDataV,clKinetic) |
| 782 |
CLLaunch.wait() |
| 783 |
cl.enqueue_copy(queue,MyCoM,clCoM) |
| 784 |
cl.enqueue_copy(queue,MyPotential,clPotential) |
| 785 |
cl.enqueue_copy(queue,MyKinetic,clKinetic) |
| 786 |
print('\nCenter Of Mass: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2])) |
| 787 |
print('Viriel=%s Potential=%s Kinetic=%s\n'% (np.sum(MyPotential)+2.*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic))) |
| 788 |
|
| 789 |
print("\nDuration stats on device %s with %s iterations :\n\tMean:\t%s\n\tMedian:\t%s\n\tStddev:\t%s\n\tMin:\t%s\n\tMax:\t%s\n\n\tVariability:\t%s\n" % (Device,Iterations,np.mean(Durations),np.median(Durations),np.std(Durations),np.min(Durations),np.max(Durations),np.std(Durations)/np.median(Durations)))
|
| 790 |
|
| 791 |
# Contraction of Square*Size*Hertz: Size*Size/Elapsed
|
| 792 |
Squertz=np.ones(len(Durations))
|
| 793 |
Squertz*=Number*Number |
| 794 |
Squertz/=Durations |
| 795 |
|
| 796 |
print("Squertz in log10 stats on device %s with %s iterations :\n\tMean:\t%s\n\tMedian:\t%s\n\tStddev:\t%s\n\tMin:\t%s\n\tMax:\t%s\n" % (Device,Iterations,np.log10(np.mean(Squertz)),np.log10(np.median(Squertz)),np.log10(np.std(Squertz)),np.log10(np.min(Squertz)),np.log10(np.max(Squertz))))
|
| 797 |
|
| 798 |
clDataX.release() |
| 799 |
clDataV.release() |
| 800 |
clKinetic.release() |
| 801 |
clPotential.release() |
| 802 |
|