Skip to content

Commit

Permalink
CMake example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Maynard committed Jun 9, 2017
1 parent 33b83df commit 030fb1a
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 0 deletions.
47 changes: 47 additions & 0 deletions posts/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(cmake_and_cuda LANGUAGES CXX CUDA)

include(CTest)

add_library(particles STATIC
particle.cu
particle.h
v3.cu
v3.h
)

# Request that particles be built with -std=c++11
# As this is a public compile feature anything that links to particles
# will also build with -std=c++11
target_compile_features(particles PUBLIC cxx_std_11)

# We need to explicitly state that we need all CUDA files in the particle
# library to be built with -dc as the member functions could be called by
# other libraries and executables
set_target_properties( particles
PROPERTIES CUDA_SEPARABLE_COMPILATION ON)

if(BUILD_TESTING)

add_executable(particle_test test.cu)

set_target_properties(particle_test PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(particle_test PRIVATE particles)

add_test(NAME particles_10k COMMAND particle_test 10000 )
add_test(NAME particles_256k COMMAND particle_test 256000 )

if(APPLE)
# We need to add the default path to the driver (libcuda.dylib) as an rpath,
# so that the static cuda runtime can find it at runtime.
target_link_libraries(particle_test
PRIVATE
"-Wl,-rpath,${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES}"
)
endif()

endif()



57 changes: 57 additions & 0 deletions posts/cmake/particle.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Copyright (c) 1993-2015, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "particle.h"

particle::particle() : position(), velocity(), totalDistance(1,0,0)
{
}

__device__ __host__
void particle::advance(float d)
{
velocity.normalize();
float dx = d * velocity.x;
position.x += dx;
totalDistance.x += dx;
float dy = d * velocity.y;
position.y += dy;
totalDistance.y += dy;
float dz = d * velocity.z;
position.z += dz;
totalDistance.z += dz;
// #if __CUDA_ARCH__
// int idx = threadIdx.x + blockIdx.x*blockDim.x;
// if(idx == 0)
// {
// printf("totalDistance: %f\n", totalDistance.x );
// }
// #endif
velocity.scramble();
}

const v3& particle::getTotalDistance() const
{ return totalDistance; }
46 changes: 46 additions & 0 deletions posts/cmake/particle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Copyright (c) 1993-2015, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __particle_h__
#define __particle_h__

#include "v3.h"

class particle
{
private:
v3 position;
v3 velocity;
v3 totalDistance;

public:
particle();
__host__ __device__ void advance(float dist);
const v3& getTotalDistance() const;

};

#endif
103 changes: 103 additions & 0 deletions posts/cmake/test.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* Copyright (c) 1993-2015, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "particle.h"
#include <stdlib.h>
#include <stdio.h>

__global__ void advanceParticles(float dt, particle * pArray, int nParticles)
{
int idx = threadIdx.x + blockIdx.x*blockDim.x;
if(idx < nParticles)
{
pArray[idx].advance(dt);
}
}

int main(int argc, char ** argv)
{
cudaError_t error;
int n = 1000000;
if(argc > 1) { n = atoi(argv[1]);} // Number of particles
if(argc > 2) { srand(atoi(argv[2])); } // Random seed

error = cudaGetLastError();
if (error != cudaSuccess)
{
printf("0 %s\n",cudaGetErrorString(error));
exit(1);
}

particle * pArray = new particle[n];
particle * devPArray = NULL;
cudaMalloc(&devPArray, n*sizeof(particle));
cudaDeviceSynchronize(); error = cudaGetLastError();
if (error != cudaSuccess)
{
printf("1 %s\n",cudaGetErrorString(error));
exit(1);
}

cudaMemcpy(devPArray, pArray, n*sizeof(particle), cudaMemcpyHostToDevice);
cudaDeviceSynchronize(); error = cudaGetLastError();
if (error != cudaSuccess)
{
printf("2 %s\n",cudaGetErrorString(error));
exit(1);
}

for(int i=0; i<100; i++)
{
float dt = (float)rand()/(float) RAND_MAX; // Random distance each step
advanceParticles<<< 1 + n/256, 256>>>(dt, devPArray, n);
error = cudaGetLastError();
if (error != cudaSuccess)
{
printf("3 %s\n",cudaGetErrorString(error));
exit(1);
}

cudaDeviceSynchronize();
}
cudaMemcpy(pArray, devPArray, n*sizeof(particle), cudaMemcpyDeviceToHost);

v3 totalDistance(0,0,0);
v3 temp;
for(int i=0; i<n; i++)
{
temp = pArray[i].getTotalDistance();
totalDistance.x += temp.x;
totalDistance.y += temp.y;
totalDistance.z += temp.z;
}
float avgX = totalDistance.x /(float)n;
float avgY = totalDistance.y /(float)n;
float avgZ = totalDistance.z /(float)n;
float avgNorm = sqrt(avgX*avgX + avgY*avgY + avgZ*avgZ);
printf( "Moved %d particles 100 steps. Average distance traveled is |(%f, %f, %f)| = %f\n",
n, avgX, avgY, avgZ, avgNorm);
return 0;
}
59 changes: 59 additions & 0 deletions posts/cmake/v3.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Copyright (c) 1993-2015, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "v3.h"
#include <math.h>

v3::v3()
{ randomize(); }

v3::v3(float xIn, float yIn, float zIn) : x(xIn), y(yIn), z(zIn)
{}

void v3::randomize()
{
x = (float)rand() / (float)RAND_MAX;
y = (float)rand() / (float)RAND_MAX;
z = (float)rand() / (float)RAND_MAX;
}

__host__ __device__ void v3::normalize()
{
float t = sqrt(x*x + y*y + z*z);
x /= t;
y /= t;
z /= t;
}

__host__ __device__ void v3::scramble()
{
float tx = 0.317f*(x + 1.0) + y + z * x * x + y + z;
float ty = 0.619f*(y + 1.0) + y * y + x * y * z + y + x;
float tz = 0.124f*(z + 1.0) + z * y + x * y * z + y + x;
x = tx;
y = ty;
z = tz;
}
45 changes: 45 additions & 0 deletions posts/cmake/v3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* Copyright (c) 1993-2015, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __v3_h__
#define __v3_h__

class v3
{
public:
float x;
float y;
float z;

v3();
v3(float xIn, float yIn, float zIn);
void randomize();
__host__ __device__ void normalize();
__host__ __device__ void scramble();

};

#endif

1 comment on commit 030fb1a

@enemy1205
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excuse me,when i run these codes , I can't pass the cmake.Though My CUDA has been installed to /usr/local/cuda properly.

[main] Configuring folder: cuda 
[proc] Executing command: /usr/local/bin/cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc -DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++ -S/home/XXX/文档/test/cuda -B/home/XXX/文档/test/cuda/build -G "Unix Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The CUDA compiler identification is unknown
[cmake] -- Detecting CUDA compiler ABI info
[cmake] -- Detecting CUDA compiler ABI info - failed
[cmake] -- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc
[cmake] -- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc - broken
[cmake] -- Configuring incomplete, errors occurred!
[cmake] See also "/home/XXX/文档/test/cuda/build/CMakeFiles/CMakeOutput.log".
[cmake] See also "/home/XXX/文档/test/cuda/build/CMakeFiles/CMakeError.log".
[cmake] CMake Error at /usr/local/share/cmake-3.22/Modules/CMakeTestCUDACompiler.cmake:56 (message):
[cmake]   The CUDA compiler
[cmake] 
[cmake]     "/usr/local/cuda/bin/nvcc"
[cmake] 
[cmake]   is not able to compile a simple test program.
[cmake] 
[cmake]   It fails with the following output:
[cmake] 
[cmake]     Change Dir: /home/XXX/文档/test/cuda/build/CMakeFiles/CMakeTmp
[cmake]     
[cmake]     Run Build Command(s):/usr/bin/gmake -f Makefile cmTC_37186/fast && /usr/bin/gmake  -f CMakeFiles/cmTC_37186.dir/build.make CMakeFiles/cmTC_37186.dir/build
[cmake]     gmake[1]: 进入目录“/home/XXX/文档/test/cuda/build/CMakeFiles/CMakeTmp”
[cmake]     Building CUDA object CMakeFiles/cmTC_37186.dir/main.cu.o
[cmake]     /usr/local/cuda/bin/nvcc      -c /home/XXX/文档/test/cuda/build/CMakeFiles/CMakeTmp/main.cu -o CMakeFiles/cmTC_37186.dir/main.cu.o
[cmake]     /usr/include/stdio.h(189): error: attribute "__malloc__" does not take arguments
[cmake]     
[cmake]     /usr/include/stdio.h(201): error: attribute "__malloc__" does not take arguments
[cmake]     
[cmake]     /usr/include/stdio.h(223): error: attribute "__malloc__" does not take arguments
[cmake]     
[cmake]     /usr/include/stdio.h(260): error: attribute "__malloc__" does not take arguments
[cmake]     
[cmake]     /usr/include/stdio.h(285): error: attribute "__malloc__" does not take arguments
[cmake]     
[cmake]     /usr/include/stdio.h(294): error: attribute "__malloc__" does not take arguments
[cmake]     
[cmake]     /usr/include/stdio.h(303): error: attribute "__malloc__" does not take arguments
[cmake]     
[cmake]     /usr/include/stdio.h(309): error: attribute "__malloc__" does not take arguments
[cmake]     
[cmake]     /usr/include/stdio.h(315): error: attribute "__malloc__" does not take arguments
[cmake]     
[cmake]     /usr/include/stdio.h(830): error: attribute "__malloc__" does not take arguments
[cmake]     
[cmake]     /usr/include/stdlib.h(566): error: attribute "__malloc__" does not take arguments
[cmake]     
[cmake]     /usr/include/stdlib.h(570): error: attribute "__malloc__" does not take arguments
[cmake]     
[cmake]     /usr/include/stdlib.h(799): error: attribute "__malloc__" does not take arguments
[cmake]     
[cmake]     13 errors detected in the compilation of "/home/saika/文档/test/cuda/build/CMakeFiles/CMakeTmp/main.cu".
[cmake]     gmake[1]: *** [CMakeFiles/cmTC_37186.dir/build.make:78:CMakeFiles/cmTC_37186.dir/main.cu.o] 错误 1
[cmake]     gmake[1]: 离开目录“/home/XXX/文档/test/cuda/build/CMakeFiles/CMakeTmp”
[cmake]     gmake: *** [Makefile:127:cmTC_37186/fast] 错误 2
[cmake]     
[cmake]     
[cmake] 
[cmake]   
[cmake] 
[cmake]   CMake will not be able to correctly generate this project.
[cmake] Call Stack (most recent call first):
[cmake]   CMakeLists.txt:6 (project)
[cmake] 
[cmake] 
[proc] The command: /usr/local/bin/cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc -DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++ -S/home/XXX/文档/test/cuda -B/home/saika/文档/test/cuda/build -G "Unix Makefiles" exited with code: 1 and signal: null

Test Environment:

  • Ubuntu : 22.04
  • gcc/g++ : 11.3
  • CUDA : 11.4
  • Cmake : 3.22

Please sign in to comment.