Skip to content

Commit 030fb1a

Browse files
author
Robert Maynard
committed
CMake example.
1 parent 33b83df commit 030fb1a

File tree

6 files changed

+357
-0
lines changed

6 files changed

+357
-0
lines changed

posts/cmake/CMakeLists.txt

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
3+
project(cmake_and_cuda LANGUAGES CXX CUDA)
4+
5+
include(CTest)
6+
7+
add_library(particles STATIC
8+
particle.cu
9+
particle.h
10+
v3.cu
11+
v3.h
12+
)
13+
14+
# Request that particles be built with -std=c++11
15+
# As this is a public compile feature anything that links to particles
16+
# will also build with -std=c++11
17+
target_compile_features(particles PUBLIC cxx_std_11)
18+
19+
# We need to explicitly state that we need all CUDA files in the particle
20+
# library to be built with -dc as the member functions could be called by
21+
# other libraries and executables
22+
set_target_properties( particles
23+
PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
24+
25+
if(BUILD_TESTING)
26+
27+
add_executable(particle_test test.cu)
28+
29+
set_target_properties(particle_test PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
30+
target_link_libraries(particle_test PRIVATE particles)
31+
32+
add_test(NAME particles_10k COMMAND particle_test 10000 )
33+
add_test(NAME particles_256k COMMAND particle_test 256000 )
34+
35+
if(APPLE)
36+
# We need to add the default path to the driver (libcuda.dylib) as an rpath,
37+
# so that the static cuda runtime can find it at runtime.
38+
target_link_libraries(particle_test
39+
PRIVATE
40+
"-Wl,-rpath,${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES}"
41+
)
42+
endif()
43+
44+
endif()
45+
46+
47+

posts/cmake/particle.cu

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Copyright (c) 1993-2015, NVIDIA CORPORATION. All rights reserved.
2+
*
3+
* Redistribution and use in source and binary forms, with or without
4+
* modification, are permitted provided that the following conditions
5+
* are met:
6+
* * Redistributions of source code must retain the above copyright
7+
* notice, this list of conditions and the following disclaimer.
8+
* * Redistributions in binary form must reproduce the above copyright
9+
* notice, this list of conditions and the following disclaimer in the
10+
* documentation and/or other materials provided with the distribution.
11+
* * Neither the name of NVIDIA CORPORATION nor the names of its
12+
* contributors may be used to endorse or promote products derived
13+
* from this software without specific prior written permission.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
#include "particle.h"
28+
29+
particle::particle() : position(), velocity(), totalDistance(1,0,0)
30+
{
31+
}
32+
33+
__device__ __host__
34+
void particle::advance(float d)
35+
{
36+
velocity.normalize();
37+
float dx = d * velocity.x;
38+
position.x += dx;
39+
totalDistance.x += dx;
40+
float dy = d * velocity.y;
41+
position.y += dy;
42+
totalDistance.y += dy;
43+
float dz = d * velocity.z;
44+
position.z += dz;
45+
totalDistance.z += dz;
46+
// #if __CUDA_ARCH__
47+
// int idx = threadIdx.x + blockIdx.x*blockDim.x;
48+
// if(idx == 0)
49+
// {
50+
// printf("totalDistance: %f\n", totalDistance.x );
51+
// }
52+
// #endif
53+
velocity.scramble();
54+
}
55+
56+
const v3& particle::getTotalDistance() const
57+
{ return totalDistance; }

posts/cmake/particle.h

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Copyright (c) 1993-2015, NVIDIA CORPORATION. All rights reserved.
2+
*
3+
* Redistribution and use in source and binary forms, with or without
4+
* modification, are permitted provided that the following conditions
5+
* are met:
6+
* * Redistributions of source code must retain the above copyright
7+
* notice, this list of conditions and the following disclaimer.
8+
* * Redistributions in binary form must reproduce the above copyright
9+
* notice, this list of conditions and the following disclaimer in the
10+
* documentation and/or other materials provided with the distribution.
11+
* * Neither the name of NVIDIA CORPORATION nor the names of its
12+
* contributors may be used to endorse or promote products derived
13+
* from this software without specific prior written permission.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
#ifndef __particle_h__
28+
#define __particle_h__
29+
30+
#include "v3.h"
31+
32+
class particle
33+
{
34+
private:
35+
v3 position;
36+
v3 velocity;
37+
v3 totalDistance;
38+
39+
public:
40+
particle();
41+
__host__ __device__ void advance(float dist);
42+
const v3& getTotalDistance() const;
43+
44+
};
45+
46+
#endif

posts/cmake/test.cu

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* Copyright (c) 1993-2015, NVIDIA CORPORATION. All rights reserved.
2+
*
3+
* Redistribution and use in source and binary forms, with or without
4+
* modification, are permitted provided that the following conditions
5+
* are met:
6+
* * Redistributions of source code must retain the above copyright
7+
* notice, this list of conditions and the following disclaimer.
8+
* * Redistributions in binary form must reproduce the above copyright
9+
* notice, this list of conditions and the following disclaimer in the
10+
* documentation and/or other materials provided with the distribution.
11+
* * Neither the name of NVIDIA CORPORATION nor the names of its
12+
* contributors may be used to endorse or promote products derived
13+
* from this software without specific prior written permission.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
#include "particle.h"
28+
#include <stdlib.h>
29+
#include <stdio.h>
30+
31+
__global__ void advanceParticles(float dt, particle * pArray, int nParticles)
32+
{
33+
int idx = threadIdx.x + blockIdx.x*blockDim.x;
34+
if(idx < nParticles)
35+
{
36+
pArray[idx].advance(dt);
37+
}
38+
}
39+
40+
int main(int argc, char ** argv)
41+
{
42+
cudaError_t error;
43+
int n = 1000000;
44+
if(argc > 1) { n = atoi(argv[1]);} // Number of particles
45+
if(argc > 2) { srand(atoi(argv[2])); } // Random seed
46+
47+
error = cudaGetLastError();
48+
if (error != cudaSuccess)
49+
{
50+
printf("0 %s\n",cudaGetErrorString(error));
51+
exit(1);
52+
}
53+
54+
particle * pArray = new particle[n];
55+
particle * devPArray = NULL;
56+
cudaMalloc(&devPArray, n*sizeof(particle));
57+
cudaDeviceSynchronize(); error = cudaGetLastError();
58+
if (error != cudaSuccess)
59+
{
60+
printf("1 %s\n",cudaGetErrorString(error));
61+
exit(1);
62+
}
63+
64+
cudaMemcpy(devPArray, pArray, n*sizeof(particle), cudaMemcpyHostToDevice);
65+
cudaDeviceSynchronize(); error = cudaGetLastError();
66+
if (error != cudaSuccess)
67+
{
68+
printf("2 %s\n",cudaGetErrorString(error));
69+
exit(1);
70+
}
71+
72+
for(int i=0; i<100; i++)
73+
{
74+
float dt = (float)rand()/(float) RAND_MAX; // Random distance each step
75+
advanceParticles<<< 1 + n/256, 256>>>(dt, devPArray, n);
76+
error = cudaGetLastError();
77+
if (error != cudaSuccess)
78+
{
79+
printf("3 %s\n",cudaGetErrorString(error));
80+
exit(1);
81+
}
82+
83+
cudaDeviceSynchronize();
84+
}
85+
cudaMemcpy(pArray, devPArray, n*sizeof(particle), cudaMemcpyDeviceToHost);
86+
87+
v3 totalDistance(0,0,0);
88+
v3 temp;
89+
for(int i=0; i<n; i++)
90+
{
91+
temp = pArray[i].getTotalDistance();
92+
totalDistance.x += temp.x;
93+
totalDistance.y += temp.y;
94+
totalDistance.z += temp.z;
95+
}
96+
float avgX = totalDistance.x /(float)n;
97+
float avgY = totalDistance.y /(float)n;
98+
float avgZ = totalDistance.z /(float)n;
99+
float avgNorm = sqrt(avgX*avgX + avgY*avgY + avgZ*avgZ);
100+
printf( "Moved %d particles 100 steps. Average distance traveled is |(%f, %f, %f)| = %f\n",
101+
n, avgX, avgY, avgZ, avgNorm);
102+
return 0;
103+
}

posts/cmake/v3.cu

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Copyright (c) 1993-2015, NVIDIA CORPORATION. All rights reserved.
2+
*
3+
* Redistribution and use in source and binary forms, with or without
4+
* modification, are permitted provided that the following conditions
5+
* are met:
6+
* * Redistributions of source code must retain the above copyright
7+
* notice, this list of conditions and the following disclaimer.
8+
* * Redistributions in binary form must reproduce the above copyright
9+
* notice, this list of conditions and the following disclaimer in the
10+
* documentation and/or other materials provided with the distribution.
11+
* * Neither the name of NVIDIA CORPORATION nor the names of its
12+
* contributors may be used to endorse or promote products derived
13+
* from this software without specific prior written permission.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
#include "v3.h"
28+
#include <math.h>
29+
30+
v3::v3()
31+
{ randomize(); }
32+
33+
v3::v3(float xIn, float yIn, float zIn) : x(xIn), y(yIn), z(zIn)
34+
{}
35+
36+
void v3::randomize()
37+
{
38+
x = (float)rand() / (float)RAND_MAX;
39+
y = (float)rand() / (float)RAND_MAX;
40+
z = (float)rand() / (float)RAND_MAX;
41+
}
42+
43+
__host__ __device__ void v3::normalize()
44+
{
45+
float t = sqrt(x*x + y*y + z*z);
46+
x /= t;
47+
y /= t;
48+
z /= t;
49+
}
50+
51+
__host__ __device__ void v3::scramble()
52+
{
53+
float tx = 0.317f*(x + 1.0) + y + z * x * x + y + z;
54+
float ty = 0.619f*(y + 1.0) + y * y + x * y * z + y + x;
55+
float tz = 0.124f*(z + 1.0) + z * y + x * y * z + y + x;
56+
x = tx;
57+
y = ty;
58+
z = tz;
59+
}

posts/cmake/v3.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* Copyright (c) 1993-2015, NVIDIA CORPORATION. All rights reserved.
2+
*
3+
* Redistribution and use in source and binary forms, with or without
4+
* modification, are permitted provided that the following conditions
5+
* are met:
6+
* * Redistributions of source code must retain the above copyright
7+
* notice, this list of conditions and the following disclaimer.
8+
* * Redistributions in binary form must reproduce the above copyright
9+
* notice, this list of conditions and the following disclaimer in the
10+
* documentation and/or other materials provided with the distribution.
11+
* * Neither the name of NVIDIA CORPORATION nor the names of its
12+
* contributors may be used to endorse or promote products derived
13+
* from this software without specific prior written permission.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
#ifndef __v3_h__
28+
#define __v3_h__
29+
30+
class v3
31+
{
32+
public:
33+
float x;
34+
float y;
35+
float z;
36+
37+
v3();
38+
v3(float xIn, float yIn, float zIn);
39+
void randomize();
40+
__host__ __device__ void normalize();
41+
__host__ __device__ void scramble();
42+
43+
};
44+
45+
#endif

0 commit comments

Comments
 (0)