Révision 74
| Pi/GPU/Pi-GPU.py (revision 74) | ||
|---|---|---|
| 92 | 92 |
#define SHR3fp SHR3 * 2.328306435454494e-10f |
| 93 | 93 |
#define CONGfp CONG * 2.328306435454494e-10f |
| 94 | 94 |
|
| 95 |
__global__ void MainLoopBlocks(ulong *s,ulong iterations,uint seed_w,uint seed_z)
|
|
| 95 |
__device__ ulong MainLoop(ulong iterations,uint seed_w,uint seed_z,size_t work)
|
|
| 96 | 96 |
{
|
| 97 |
uint z=seed_z/(blockIdx.x+1); |
|
| 98 |
uint w=seed_w/(blockIdx.x+1); |
|
| 99 | 97 |
|
| 100 |
ulong total=0; |
|
| 98 |
#if TRNG == TCONG |
|
| 99 |
uint jcong=seed_z+work; |
|
| 100 |
#elif TRNG == TSHR3 |
|
| 101 |
uint jsr=seed_w+work; |
|
| 102 |
#elif TRNG == TMWC |
|
| 103 |
uint z=seed_z+work; |
|
| 104 |
uint w=seed_w+work; |
|
| 105 |
#elif TRNG == TKISS |
|
| 106 |
uint jcong=seed_z+work; |
|
| 107 |
uint jsr=seed_w+work; |
|
| 108 |
uint z=seed_z-work; |
|
| 109 |
uint w=seed_w-work; |
|
| 110 |
#endif |
|
| 101 | 111 |
|
| 102 |
for (ulong i=0;i<iterations;i++) {
|
|
| 103 |
|
|
| 104 |
float x=MWCfp ; |
|
| 105 |
float y=MWCfp ; |
|
| 106 |
|
|
| 107 |
// Matching test |
|
| 108 |
ulong inside=((x*x+y*y) < 1.0f) ? 1:0; |
|
| 109 |
total+=inside; |
|
| 110 |
|
|
| 111 |
} |
|
| 112 |
|
|
| 113 |
s[blockIdx.x]=total; |
|
| 114 |
__syncthreads(); |
|
| 115 |
|
|
| 116 |
} |
|
| 117 |
|
|
| 118 |
__global__ void MainLoopThreads(ulong *s,ulong iterations,uint seed_w,uint seed_z) |
|
| 119 |
{
|
|
| 120 |
uint z=seed_z+threadIdx.x; |
|
| 121 |
uint w=seed_w+threadIdx.x; |
|
| 122 |
|
|
| 123 | 112 |
ulong total=0; |
| 124 | 113 |
|
| 125 | 114 |
for (ulong i=0;i<iterations;i++) {
|
| 126 | 115 |
|
| 127 |
float x=MWCfp ; |
|
| 128 |
float y=MWCfp ; |
|
| 116 |
#if TYPE == TINT32 |
|
| 117 |
#define THEONE 1073741824 |
|
| 118 |
#if TRNG == TCONG |
|
| 119 |
uint x=CONG>>17 ; |
|
| 120 |
uint y=CONG>>17 ; |
|
| 121 |
#elif TRNG == TSHR3 |
|
| 122 |
uint x=SHR3>>17 ; |
|
| 123 |
uint y=SHR3>>17 ; |
|
| 124 |
#elif TRNG == TMWC |
|
| 125 |
uint x=MWC>>17 ; |
|
| 126 |
uint y=MWC>>17 ; |
|
| 127 |
#elif TRNG == TKISS |
|
| 128 |
uint x=KISS>>17 ; |
|
| 129 |
uint y=KISS>>17 ; |
|
| 130 |
#endif |
|
| 131 |
#elif TYPE == TINT64 |
|
| 132 |
#define THEONE 4611686018427387904 |
|
| 133 |
#if TRNG == TCONG |
|
| 134 |
ulong x=(ulong)(CONG>>1) ; |
|
| 135 |
ulong y=(ulong)(CONG>>1) ; |
|
| 136 |
#elif TRNG == TSHR3 |
|
| 137 |
ulong x=(ulong)(SHR3>>1) ; |
|
| 138 |
ulong y=(ulong)(SHR3>>1) ; |
|
| 139 |
#elif TRNG == TMWC |
|
| 140 |
ulong x=(ulong)(MWC>>1) ; |
|
| 141 |
ulong y=(ulong)(MWC>>1) ; |
|
| 142 |
#elif TRNG == TKISS |
|
| 143 |
ulong x=(ulong)(KISS>>1) ; |
|
| 144 |
ulong y=(ulong)(KISS>>1) ; |
|
| 145 |
#endif |
|
| 146 |
#elif TYPE == TFP32 |
|
| 147 |
#define THEONE 1.0f |
|
| 148 |
#if TRNG == TCONG |
|
| 149 |
float x=CONGfp ; |
|
| 150 |
float y=CONGfp ; |
|
| 151 |
#elif TRNG == TSHR3 |
|
| 152 |
float x=SHR3fp ; |
|
| 153 |
float y=SHR3fp ; |
|
| 154 |
#elif TRNG == TMWC |
|
| 155 |
float x=MWCfp ; |
|
| 156 |
float y=MWCfp ; |
|
| 157 |
#elif TRNG == TKISS |
|
| 158 |
float x=KISSfp ; |
|
| 159 |
float y=KISSfp ; |
|
| 160 |
#endif |
|
| 161 |
#elif TYPE == TFP64 |
|
| 162 |
#define THEONE 1.0f |
|
| 163 |
#if TRNG == TCONG |
|
| 164 |
double x=(double)CONGfp ; |
|
| 165 |
double y=(double)CONGfp ; |
|
| 166 |
#elif TRNG == TSHR3 |
|
| 167 |
double x=(double)SHR3fp ; |
|
| 168 |
double y=(double)SHR3fp ; |
|
| 169 |
#elif TRNG == TMWC |
|
| 170 |
double x=(double)MWCfp ; |
|
| 171 |
double y=(double)MWCfp ; |
|
| 172 |
#elif TRNG == TKISS |
|
| 173 |
double x=(double)KISSfp ; |
|
| 174 |
double y=(double)KISSfp ; |
|
| 175 |
#endif |
|
| 176 |
#endif |
|
| 129 | 177 |
|
| 130 |
// Matching test |
|
| 131 |
ulong inside=((x*x+y*y) < 1.0f) ? 1:0; |
|
| 178 |
ulong inside=((x*x+y*y) <= THEONE) ? 1:0; |
|
| 132 | 179 |
total+=inside; |
| 133 |
|
|
| 134 | 180 |
} |
| 135 | 181 |
|
| 136 |
s[threadIdx.x]=total; |
|
| 137 |
__syncthreads(); |
|
| 138 |
|
|
| 182 |
return(total); |
|
| 139 | 183 |
} |
| 140 | 184 |
|
| 141 |
__global__ void MainLoopHybrid(ulong *s,ulong iterations,uint seed_w,uint seed_z)
|
|
| 185 |
__global__ void MainLoopBlocks(ulong *s,ulong iterations,uint seed_w,uint seed_z)
|
|
| 142 | 186 |
{
|
| 143 |
uint z=seed_z+(blockDim.x*blockIdx.x+threadIdx.x+1); |
|
| 144 |
uint w=seed_w+(blockDim.x*blockIdx.x+threadIdx.x+1); |
|
| 145 |
|
|
| 146 |
ulong total=0; |
|
| 147 |
|
|
| 148 |
for (ulong i=0;i<iterations;i++) {
|
|
| 149 |
|
|
| 150 |
float x=MWCfp ; |
|
| 151 |
float y=MWCfp ; |
|
| 152 |
|
|
| 153 |
// Matching test |
|
| 154 |
ulong inside=((x*x+y*y) < 1.0f) ? 1:0; |
|
| 155 |
total+=inside; |
|
| 156 |
|
|
| 157 |
} |
|
| 158 |
|
|
| 159 |
s[blockDim.x*blockIdx.x+threadIdx.x]=total; |
|
| 160 |
__syncthreads(); |
|
| 161 |
|
|
| 162 |
} |
|
| 163 |
|
|
| 164 |
__global__ void MainLoopBlocks64(ulong *s,ulong iterations,uint seed_w,uint seed_z) |
|
| 165 |
{
|
|
| 166 |
uint z=seed_z+blockIdx.x; |
|
| 167 |
uint w=seed_w+blockIdx.x; |
|
| 168 |
|
|
| 169 |
ulong total=0; |
|
| 170 |
|
|
| 171 |
for (ulong i=0;i<iterations;i++) {
|
|
| 172 |
|
|
| 173 |
double x=(double)MWCfp ; |
|
| 174 |
double y=(double)MWCfp ; |
|
| 175 |
|
|
| 176 |
// Matching test |
|
| 177 |
ulong inside=((x*x+y*y) < 1.0f) ? 1:0; |
|
| 178 |
total+=inside; |
|
| 179 |
|
|
| 180 |
} |
|
| 181 |
|
|
| 187 |
ulong total=MainLoop(iterations,seed_z,seed_w,blockIdx.x); |
|
| 182 | 188 |
s[blockIdx.x]=total; |
| 183 | 189 |
__syncthreads(); |
| 184 | 190 |
|
| 185 | 191 |
} |
| 186 | 192 |
|
| 187 |
__global__ void MainLoopThreads64(ulong *s,ulong iterations,uint seed_w,uint seed_z)
|
|
| 193 |
__global__ void MainLoopThreads(ulong *s,ulong iterations,uint seed_w,uint seed_z) |
|
| 188 | 194 |
{
|
| 189 |
uint z=seed_z+threadIdx.x; |
|
| 190 |
uint w=seed_w+threadIdx.x; |
|
| 191 |
|
|
| 192 |
ulong total=0; |
|
| 193 |
|
|
| 194 |
for (ulong i=0;i<iterations;i++) {
|
|
| 195 |
|
|
| 196 |
double x=(double)MWCfp ; |
|
| 197 |
double y=(double)MWCfp ; |
|
| 198 |
|
|
| 199 |
// Matching test |
|
| 200 |
ulong inside=((x*x+y*y) < 1.0f) ? 1:0; |
|
| 201 |
total+=inside; |
|
| 202 |
|
|
| 203 |
} |
|
| 204 |
|
|
| 195 |
ulong total=MainLoop(iterations,seed_z,seed_w,threadIdx.x); |
|
| 205 | 196 |
s[threadIdx.x]=total; |
| 206 | 197 |
__syncthreads(); |
| 207 | 198 |
|
| 208 | 199 |
} |
| 209 | 200 |
|
| 210 |
__global__ void MainLoopHybrid64(ulong *s,ulong iterations,uint seed_w,uint seed_z)
|
|
| 201 |
__global__ void MainLoopHybrid(ulong *s,ulong iterations,uint seed_w,uint seed_z) |
|
| 211 | 202 |
{
|
| 212 |
uint z=seed_z+blockDim.x*blockIdx.x+threadIdx.x; |
|
| 213 |
uint w=seed_w+blockDim.x*blockIdx.x+threadIdx.x; |
|
| 214 |
|
|
| 215 |
ulong total=0; |
|
| 216 |
|
|
| 217 |
for (ulong i=0;i<iterations;i++) {
|
|
| 218 |
|
|
| 219 |
double x=(double)MWCfp ; |
|
| 220 |
double y=(double)MWCfp ; |
|
| 221 |
|
|
| 222 |
// Matching test |
|
| 223 |
ulong inside=((x*x+y*y) < 1.0f) ? 1:0; |
|
| 224 |
total+=inside; |
|
| 225 |
|
|
| 226 |
} |
|
| 227 |
|
|
| 203 |
ulong total=MainLoop(iterations,seed_z,seed_w,blockDim.x*blockIdx.x+threadIdx.x); |
|
| 228 | 204 |
s[blockDim.x*blockIdx.x+threadIdx.x]=total; |
| 229 | 205 |
__syncthreads(); |
| 230 |
|
|
| 231 | 206 |
} |
| 207 |
|
|
| 232 | 208 |
""" |
| 233 | 209 |
|
| 234 | 210 |
KERNEL_CODE_OPENCL=""" |
| ... | ... | |
| 371 | 347 |
|
| 372 | 348 |
""" |
| 373 | 349 |
|
| 374 |
def MetropolisCuda(circle,iterations,steps,jobs,ParaStyle,DoublePrecision):
|
|
| 350 |
def MetropolisCuda(circle,iterations,steps,jobs,ParaStyle,RNG,ValueType):
|
|
| 375 | 351 |
|
| 376 | 352 |
# Avec PyCUDA autoinit, rien a faire ! |
| 377 | 353 |
|
| 378 | 354 |
circleCU = cuda.InOut(circle) |
| 379 |
|
|
| 380 |
mod = SourceModule(KERNEL_CODE_CUDA) |
|
| 381 | 355 |
|
| 356 |
try: |
|
| 357 |
mod = SourceModule(KERNEL_CODE_CUDA,options=['--compiler-options','-Wall -DTRNG=%i -DTYPE=%s' % (Marsaglia[RNG],Computing[ValueType])]) |
|
| 358 |
except: |
|
| 359 |
print "Compilation seems to brake" |
|
| 360 |
|
|
| 382 | 361 |
MetropolisBlocksCU=mod.get_function("MainLoopBlocks")
|
| 383 | 362 |
MetropolisJobsCU=mod.get_function("MainLoopThreads")
|
| 384 | 363 |
MetropolisHybridCU=mod.get_function("MainLoopHybrid")
|
| 385 |
MetropolisBlocks64CU=mod.get_function("MainLoopBlocks64")
|
|
| 386 |
MetropolisJobs64CU=mod.get_function("MainLoopThreads64")
|
|
| 387 |
MetropolisHybrid64CU=mod.get_function("MainLoopHybrid64")
|
|
| 388 | 364 |
|
| 389 | 365 |
start = pycuda.driver.Event() |
| 390 | 366 |
stop = pycuda.driver.Event() |
| ... | ... | |
| 403 | 379 |
start.record() |
| 404 | 380 |
start.synchronize() |
| 405 | 381 |
if ParaStyle=='Blocks': |
| 406 |
if DoublePrecision: |
|
| 407 |
MetropolisBlocksCU(circleCU, |
|
| 408 |
numpy.uint64(iterationsCL), |
|
| 409 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 410 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 411 |
grid=(jobs,1), |
|
| 412 |
block=(1,1,1)) |
|
| 413 |
else: |
|
| 414 |
MetropolisBlocks64CU(circleCU, |
|
| 415 |
numpy.uint64(iterationsCL), |
|
| 416 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 417 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 418 |
grid=(jobs,1), |
|
| 419 |
block=(1,1,1)) |
|
| 420 |
|
|
| 382 |
MetropolisBlocksCU(circleCU, |
|
| 383 |
numpy.uint64(iterationsCL), |
|
| 384 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 385 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 386 |
grid=(jobs,1), |
|
| 387 |
block=(1,1,1)) |
|
| 421 | 388 |
print "%s with (WorkItems/Threads)=(%i,%i) %s method done" % \ |
| 422 | 389 |
(Alu,jobs,1,ParaStyle) |
| 423 | 390 |
elif ParaStyle=='Hybrid': |
| 424 | 391 |
threads=BestThreadsNumber(jobs) |
| 425 |
if DoublePrecision: |
|
| 426 |
MetropolisHybrid64CU(circleCU, |
|
| 427 |
numpy.uint64(iterationsCL), |
|
| 428 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 429 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 430 |
grid=(jobs,1), |
|
| 431 |
block=(threads,1,1)) |
|
| 432 |
else: |
|
| 433 |
MetropolisHybridCU(circleCU, |
|
| 434 |
numpy.uint64(iterationsCL), |
|
| 435 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 436 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 437 |
grid=(jobs,1), |
|
| 438 |
block=(threads,1,1)) |
|
| 439 |
print "%s with (WorkItems/Threads)=(%i,%i) %s method done" % \ |
|
| 440 |
(Alu,jobs/threads,threads,ParaStyle) |
|
| 441 |
else: |
|
| 442 |
if DoublePrecision: |
|
| 443 |
MetropolisJobs64CU(circleCU, |
|
| 444 |
numpy.uint64(iterationsCL), |
|
| 445 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 446 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 447 |
grid=(1,1), |
|
| 448 |
block=(jobs,1,1)) |
|
| 449 |
else: |
|
| 450 |
MetropolisJobsCU(circleCU, |
|
| 392 |
MetropolisHybridCU(circleCU, |
|
| 451 | 393 |
numpy.uint64(iterationsCL), |
| 452 | 394 |
numpy.uint32(nprnd(2**30/jobs)), |
| 453 | 395 |
numpy.uint32(nprnd(2**30/jobs)), |
| 454 |
grid=(1,1),
|
|
| 455 |
block=(jobs,1,1))
|
|
| 396 |
grid=(jobs,1),
|
|
| 397 |
block=(threads,1,1))
|
|
| 456 | 398 |
print "%s with (WorkItems/Threads)=(%i,%i) %s method done" % \ |
| 399 |
(Alu,jobs/threads,threads,ParaStyle) |
|
| 400 |
else: |
|
| 401 |
MetropolisJobsCU(circleCU, |
|
| 402 |
numpy.uint64(iterationsCL), |
|
| 403 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 404 |
numpy.uint32(nprnd(2**30/jobs)), |
|
| 405 |
grid=(1,1), |
|
| 406 |
block=(jobs,1,1)) |
|
| 407 |
print "%s with (WorkItems/Threads)=(%i,%i) %s method done" % \ |
|
| 457 | 408 |
(Alu,jobs,1,ParaStyle) |
| 458 | 409 |
stop.record() |
| 459 | 410 |
stop.synchronize() |
| ... | ... | |
| 820 | 771 |
start=time.time() |
| 821 | 772 |
if GpuStyle=='CUDA': |
| 822 | 773 |
try: |
| 823 |
a,m,s=MetropolisCuda(circle,Iterations,1,Jobs,ParaStyle) |
|
| 774 |
a,m,s=MetropolisCuda(circle,Iterations,1,Jobs,ParaStyle,RNG,ValueType)
|
|
| 824 | 775 |
except: |
| 825 | 776 |
print "Problem with %i // computations on Cuda" % Jobs |
| 826 | 777 |
elif GpuStyle=='OpenCL': |
| ... | ... | |
| 839 | 790 |
else: |
| 840 | 791 |
if GpuStyle=='CUDA': |
| 841 | 792 |
try: |
| 842 |
avg,med,std=MetropolisCuda(circle,Iterations,Redo,Jobs,ParaStyle) |
|
| 793 |
avg,med,std=MetropolisCuda(circle,Iterations,Redo,Jobs,ParaStyle,RNG,ValueType)
|
|
| 843 | 794 |
except: |
| 844 | 795 |
print "Problem with %i // computations on Cuda" % Jobs |
| 845 | 796 |
elif GpuStyle=='OpenCL': |
Formats disponibles : Unified diff