-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgpu.cu
executable file
·161 lines (127 loc) · 3.87 KB
/
gpu.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <cuda.h>
#include "common.h"
#define NUM_THREADS 256
extern double size;
//
// benchmarking program
//
__device__ void apply_force_gpu(particle_t &particle, particle_t &neighbor)
{
double dx = neighbor.x - particle.x;
double dy = neighbor.y - particle.y;
double r2 = dx * dx + dy * dy;
if( r2 > cutoff*cutoff )
return;
//r2 = fmax( r2, min_r*min_r );
r2 = (r2 > min_r*min_r) ? r2 : min_r*min_r;
double r = sqrt( r2 );
//
// very simple short-range repulsive force
//
double coef = ( 1 - cutoff / r ) / r2 / mass;
particle.ax += coef * dx;
particle.ay += coef * dy;
}
__global__ void compute_forces_gpu(particle_t * particles, int n)
{
// Get thread (particle) ID
int tid = threadIdx.x + blockIdx.x * blockDim.x;
if(tid >= n) return;
particles[tid].ax = particles[tid].ay = 0;
for(int j = 0 ; j < n ; j++)
apply_force_gpu(particles[tid], particles[j]);
}
__global__ void move_gpu (particle_t * particles, int n, double size)
{
// Get thread (particle) ID
int tid = threadIdx.x + blockIdx.x * blockDim.x;
if(tid >= n) return;
particle_t * p = &particles[tid];
//
// slightly simplified Velocity Verlet integration
// conserves energy better than explicit Euler method
//
p->vx += p->ax * dt;
p->vy += p->ay * dt;
p->x += p->vx * dt;
p->y += p->vy * dt;
//
// bounce from walls
//
while( p->x < 0 || p->x > size )
{
p->x = p->x < 0 ? -(p->x) : 2*size-p->x;
p->vx = -(p->vx);
}
while( p->y < 0 || p->y > size )
{
p->y = p->y < 0 ? -(p->y) : 2*size-p->y;
p->vy = -(p->vy);
}
}
int main( int argc, char **argv )
{
// This takes a few seconds to initialize the runtime
cudaThreadSynchronize();
if( find_option( argc, argv, "-h" ) >= 0 )
{
printf( "Options:\n" );
printf( "-h to see this help\n" );
printf( "-n <int> to set the number of particles\n" );
printf( "-o <filename> to specify the output file name\n" );
return 0;
}
int n = read_int( argc, argv, "-n", 1000 );
char *savename = read_string( argc, argv, "-o", NULL );
FILE *fsave = savename ? fopen( savename, "w" ) : NULL;
particle_t *particles = (particle_t*) malloc( n * sizeof(particle_t) );
// GPU particle data structure
particle_t * d_particles;
cudaMalloc((void **) &d_particles, n * sizeof(particle_t));
set_size( n );
init_particles( n, particles );
cudaThreadSynchronize();
double copy_time = read_timer( );
// Copy the particles to the GPU
cudaMemcpy(d_particles, particles, n * sizeof(particle_t), cudaMemcpyHostToDevice);
cudaThreadSynchronize();
copy_time = read_timer( ) - copy_time;
//
// simulate a number of time steps
//
cudaThreadSynchronize();
double simulation_time = read_timer( );
for( int step = 0; step < NSTEPS; step++ )
{
//
// compute forces
//
int blks = (n + NUM_THREADS - 1) / NUM_THREADS;
compute_forces_gpu <<< blks, NUM_THREADS >>> (d_particles, n);
//
// move particles
//
move_gpu <<< blks, NUM_THREADS >>> (d_particles, n, size);
//
// save if necessary
//
if( fsave && (step%SAVEFREQ) == 0 ) {
// Copy the particles back to the CPU
cudaMemcpy(particles, d_particles, n * sizeof(particle_t), cudaMemcpyDeviceToHost);
save( fsave, n, particles);
}
}
cudaThreadSynchronize();
simulation_time = read_timer( ) - simulation_time;
printf( "CPU-GPU copy time = %g seconds\n", copy_time);
printf( "n = %d, simulation time = %g seconds\n", n, simulation_time );
free( particles );
cudaFree(d_particles);
if( fsave )
fclose( fsave );
return 0;
}