Skip to content

Commit a7953f1

Browse files
committed
Added separate compilation and linking example
1 parent 4bd22ea commit a7953f1

6 files changed

Lines changed: 152 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <particle.h>
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
5+
__global__ void advanceParticles(float dt, particle * pArray, int nParticles)
6+
{
7+
int idx = threadIdx.x + blockIdx.x*blockDim.x;
8+
if(idx < nParticles)
9+
{
10+
pArray[idx].advance(dt);
11+
}
12+
}
13+
14+
int main(int argc, char ** argv)
15+
{
16+
int n = 1000000;
17+
if(argc > 1) { n = atoi(argv[1]);} // Number of particles
18+
if(argc > 2) { srand(atoi(argv[2])); } // Random seed
19+
20+
particle * pArray = new particle[n];
21+
particle * devPArray = NULL;
22+
cudaMalloc(&devPArray, n*sizeof(particle));
23+
cudaMemcpy(devPArray, pArray, n*sizeof(particle), cudaMemcpyHostToDevice);
24+
for(int i=0; i<100; i++)
25+
{
26+
float dt = (float)rand()/(float) RAND_MAX; // Random distance each step
27+
advanceParticles<<< 1 + n/256, 256>>>(dt, devPArray, n);
28+
cudaDeviceSynchronize();
29+
}
30+
cudaMemcpy(pArray, devPArray, n*sizeof(particle), cudaMemcpyDeviceToHost);
31+
v3 totalDistance(0,0,0);
32+
v3 temp;
33+
for(int i=0; i<n; i++)
34+
{
35+
temp = pArray[i].getTotalDistance();
36+
totalDistance.x += temp.x;
37+
totalDistance.y += temp.y;
38+
totalDistance.z += temp.z;
39+
}
40+
float avgX = totalDistance.x /(float)n;
41+
float avgY = totalDistance.y /(float)n;
42+
float avgZ = totalDistance.z /(float)n;
43+
float avgNorm = sqrt(avgX*avgX + avgY*avgY + avgZ*avgZ);
44+
printf( "Moved %d particles 100 steps. Average distance traveled is |(%f, %f, %f)| = %f\n",
45+
n, avgX, avgY, avgZ, avgNorm);
46+
return 0;
47+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
objects = main.o particle.o v3.o
2+
3+
all: $(objects)
4+
nvcc -arch=sm_20 $(objects) -o app
5+
6+
%.o: %.cpp
7+
nvcc -x cu -arch=sm_20 -I. -dc $< -o $@
8+
9+
clean:
10+
rm -f *.o app
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <particle.h>
2+
3+
particle::particle() : position(), velocity(), totalDistance(0,0,0)
4+
{}
5+
6+
__device__ __host__
7+
void particle::advance(float d)
8+
{
9+
velocity.normalize();
10+
float dx = d * velocity.x;
11+
position.x += dx;
12+
totalDistance.x += dx;
13+
float dy = d * velocity.y;
14+
position.y += dy;
15+
totalDistance.y += dy;
16+
float dz = d * velocity.z;
17+
position.z += dz;
18+
totalDistance.z += dz;
19+
velocity.scramble();
20+
}
21+
22+
const v3& particle::getTotalDistance() const
23+
{ return totalDistance; }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef __particle_h__
2+
#define __particle_h__
3+
4+
#include <v3.h>
5+
6+
class particle
7+
{
8+
private:
9+
v3 position;
10+
v3 velocity;
11+
v3 totalDistance;
12+
13+
public:
14+
particle();
15+
__host__ __device__ void advance(float dist);
16+
const v3& getTotalDistance() const;
17+
18+
};
19+
20+
#endif
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <v3.h>
2+
#include <math.h>
3+
4+
v3::v3()
5+
{ randomize(); }
6+
7+
v3::v3(float xIn, float yIn, float zIn) : x(xIn), y(yIn), z(zIn)
8+
{}
9+
10+
void v3::randomize()
11+
{
12+
x = (float)rand() / (float)RAND_MAX;
13+
y = (float)rand() / (float)RAND_MAX;
14+
z = (float)rand() / (float)RAND_MAX;
15+
}
16+
17+
__host__ __device__ void v3::normalize()
18+
{
19+
float t = sqrt(x*x + y*y + z*z);
20+
x /= t;
21+
y /= t;
22+
z /= t;
23+
}
24+
25+
__host__ __device__ void v3::scramble()
26+
{
27+
float tx = 0.317f*(x + 1.0) + y + z * x * x + y + z;
28+
float ty = 0.619f*(y + 1.0) + y * y + x * y * z + y + x;
29+
float tz = 0.124f*(z + 1.0) + z * y + x * y * z + y + x;
30+
x = tx;
31+
y = ty;
32+
z = tz;
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef __v3_h__
2+
#define __v3_h__
3+
4+
class v3
5+
{
6+
public:
7+
float x;
8+
float y;
9+
float z;
10+
11+
v3();
12+
v3(float xIn, float yIn, float zIn);
13+
void randomize();
14+
__host__ __device__ void normalize();
15+
__host__ __device__ void scramble();
16+
17+
};
18+
19+
#endif

0 commit comments

Comments
 (0)