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