Skip to content

Commit 5e6b985

Browse files
committed
demo
0 parents  commit 5e6b985

File tree

5 files changed

+340
-0
lines changed

5 files changed

+340
-0
lines changed

CMakeLists.txt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# The name of our project is "ANNetGPGPU". CMakeLists files in this project can
2+
# refer to the root source directory of the project as ${ANNetGPU_SOURCE_DIR} and
3+
# to the root binary directory of the project as ${ANNetGPU_SOURCE_DIR}.
4+
CMAKE_MINIMUM_REQUIRED (VERSION 2.8)
5+
PROJECT (ANNetGPGPU)
6+
7+
# Standard CUDA architecture
8+
SET(__CUDA_CAB__ "20")
9+
10+
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR})
11+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
12+
13+
FIND_PACKAGE(CUDA)
14+
FIND_PACKAGE(CUDAThrust)
15+
16+
IF (SWIG_FOUND AND PYTHONLIBS_FOUND)
17+
SET(CMAKE_SWIG_FLAGS "")
18+
19+
INCLUDE(${SWIG_USE_FILE})
20+
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
21+
ENDIF()
22+
23+
INCLUDE (FindOpenMP)
24+
IF(OPENMP_FOUND)
25+
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
26+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
27+
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
28+
ENDIF()
29+
30+
IF (CUDA_FOUND)
31+
LIST(APPEND CUDA_NVCC_FLAGS -Xcompiler -fopenmp)
32+
LIST(APPEND CUDA_NVCC_FLAGS -arch=sm_${__CUDA_CAB__})
33+
34+
ADD_DEFINITIONS("-DCUDA") # needed for conditional compilation of some files
35+
36+
IF (CUDATHRUST_FOUND)
37+
INCLUDE_DIRECTORIES (${CUDA_SDK_ROOT_DIR}/C/common/inc/)
38+
INCLUDE_DIRECTORIES (${CUDATHRUST_INCLUDE_DIR})
39+
ENDIF (CUDATHRUST_FOUND)
40+
ENDIF (CUDA_FOUND)
41+
42+
# Make sure the compiler can find include files from our ANNet library.
43+
INCLUDE_DIRECTORIES (${ANNetGPGPU_SOURCE_DIR})
44+
45+
SET( ANPythonCPUInterfaceFiles
46+
PyNetCPU.i
47+
)
48+
49+
SET( ANPythonGPUInterfaceFiles
50+
PyNetGPU.i
51+
)
52+
53+
SET( ANSourceFiles
54+
#Functions.cpp
55+
)
56+
57+
SET( ANCUDASourceFiles
58+
Functions.cu
59+
)
60+
61+
# Create a library called "ANNet" which includes the source files listed in "ANSourceFiles".
62+
# Build ANNet C library
63+
IF (CUDA_FOUND)
64+
CUDA_ADD_LIBRARY (ANNet STATIC ${ANCUDASourceFiles} ${ANSourceFiles} ${BZIP_INCLUDE_DIRS} OPTIONS -D __CUDA_CAB__=${__CUDA_CAB__})
65+
66+
CUDA_ADD_EXECUTABLE (ctest main.cpp)
67+
TARGET_LINK_LIBRARIES (ctest ANNet)
68+
ENDIF(CUDA_FOUND)
69+
70+

FindCUDAThrust.cmake

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# - Tools for building CUDA Thrust files
2+
# For more info on Thrust, see http://code.google.com/p/thrust/
3+
#
4+
# This script locates the Thrust template library files.
5+
# It looks in CUDA_TOOLKIT_ROOT_DIR, CUDA_SDK_ROOT_DIR, CUDA_INCLUDE_DIRS.
6+
# This script relies on the FindCUDA.cmake script being run first;
7+
# for more info on FindCUDA.cmake, see:
8+
# https://gforge.sci.utah.edu/gf/project/findcuda/scmsvn/
9+
#
10+
# This script makes use of the standard find_package arguments of
11+
# REQUIRED and QUIET.
12+
#
13+
# The script defines the following variables:
14+
# CUDATHRUST_FOUND -- Set to TRUE if found; set to FALSE if not found.
15+
# CUDATHRUST_INCLUDE_DIR -- Include directory for Thrust headers.
16+
#
17+
#
18+
# Joseph K. Bradley, Carnegie Mellon University
19+
# -- http://www.cs.cmu.edu/~jkbradle
20+
#
21+
# Copyright (c) 2010. Joseph K. Bradley. All rights reserved.
22+
#
23+
# This code is licensed under the MIT License.
24+
# See the FindCUDAThrust.cmake script for the text of the license.
25+
#
26+
# The MIT License
27+
#
28+
# License for the specific language governing rights and limitations under
29+
# Permission is hereby granted, free of charge, to any person obtaining a
30+
# copy of this software and associated documentation files (the "Software"),
31+
# to deal in the Software without restriction, including without limitation
32+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
33+
# and/or sell copies of the Software, and to permit persons to whom the
34+
# Software is furnished to do so, subject to the following conditions:
35+
#
36+
# The above copyright notice and this permission notice shall be included
37+
# in all copies or substantial portions of the Software.
38+
#
39+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
40+
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
42+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
43+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
44+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
45+
# DEALINGS IN THE SOFTWARE.
46+
#
47+
###############################################################################
48+
49+
# FindCUDAThrust.cmake
50+
51+
# CMake must find CUDA before looking for Thrust.
52+
if(CUDA_FOUND)
53+
# Look for Thrust in CUDA directories.
54+
find_path(CUDATHRUST_INCLUDE
55+
thrust/version.h
56+
PATHS ${CUDA_INCLUDE_DIRS} ${CUDA_TOOLKIT_ROOT_DIR} ${CUDA_SDK_ROOT_DIR}
57+
NO_DEFAULT_PATH
58+
)
59+
# Look for Thrust in default search paths.
60+
find_path(CUDATHRUST_INCLUDE thrust/version.h)
61+
mark_as_advanced(CUDATHRUST_INCLUDE)
62+
if(CUDATHRUST_INCLUDE)
63+
# Thrust was found.
64+
message(STATUS "CUDA Thrust found: " ${CUDATHRUST_INCLUDE})
65+
set(CUDATHRUST_FOUND TRUE)
66+
set (CUDATHRUST_INCLUDE_DIRS ${CUDATHRUST_INCLUDE})
67+
else(CUDATHRUST_INCLUDE)
68+
# Thrust was not found.
69+
set(CUDATHRUST_FOUND FALSE)
70+
if(CUDATHRUST_FIND_REQUIRED)
71+
message(FATAL_ERROR "CUDA Thrust not found!")
72+
else(CUDATHRUST_FIND_REQUIRED)
73+
if (NOT CUDATHRUST_FIND_QUIETLY)
74+
message(STATUS "CUDA Thrust not found")
75+
endif(NOT CUDATHRUST_FIND_QUIETLY)
76+
endif(CUDATHRUST_FIND_REQUIRED)
77+
endif(CUDATHRUST_INCLUDE)
78+
else(CUDA_FOUND)
79+
if(NOT CUDATHRUST_FIND_QUIETLY)
80+
message(STATUS "CUDA must be found before CMake looks for Thrust!")
81+
endif(NOT CUDATHRUST_FIND_QUIETLY)
82+
set(CUDATHRUST_FOUND FALSE)
83+
endif(CUDA_FOUND)

Functions.cu

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//#include <iostream>
2+
#include "Functions.h"
3+
#include <iostream>
4+
#include <thrust/extrema.h>
5+
#include <thrust/distance.h>
6+
#include <thrust/device_vector.h>
7+
8+
9+
DistFunction<fcn_gaussian_nhood,fcn_rad_decay,fcn_lrate_decay> fcn_gaussian((char*)"gaussian");
10+
11+
typedef DistFunction<fcn_gaussian_nhood,fcn_rad_decay,fcn_lrate_decay> gaussian;
12+
13+
template <class F>
14+
struct functor {
15+
float fCycle;
16+
float fCycles;
17+
18+
functor(float cycle, float cycles) : fCycle(cycle), fCycles(cycles) {}
19+
20+
__host__ __device__
21+
float operator()(float lrate) {
22+
return F::lrate_decay(lrate, fCycle, fCycles);
23+
}
24+
};
25+
26+
27+
28+
void test() {
29+
unsigned int iWidth = 4096;
30+
thrust::device_vector<float> dvLearningRate(iWidth, 0.f);
31+
thrust::device_vector<float> dvLRate(iWidth, 0.f);
32+
33+
thrust::transform( dvLRate.begin(),
34+
dvLRate.end(),
35+
dvLearningRate.begin(),
36+
functor<gaussian>(1, 100) );
37+
}
38+

Functions.h

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
#-------------------------------------------------------------------------------
3+
# Copyright (c) 2012 Daniel <dgrat> Frenzel.
4+
# All rights reserved. This program and the accompanying materials
5+
# are made available under the terms of the GNU Lesser Public License v2.1
6+
# which accompanies this distribution, and is available at
7+
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
8+
#
9+
# Contributors:
10+
# Daniel <dgrat> Frenzel - initial API and implementation
11+
#-------------------------------------------------------------------------------
12+
*/
13+
14+
#ifndef TRANSFERFUNCTIONS_H_
15+
#define TRANSFERFUNCTIONS_H_
16+
17+
#ifndef SWIG
18+
#include <cmath>
19+
#include <stdio.h>
20+
#include <string.h>
21+
#endif
22+
23+
#define PI 3.14159265358979323846f
24+
25+
26+
27+
typedef float (*pDistanceFu) (float, float);
28+
typedef float (*pDecayFu) (float, float, float);
29+
30+
31+
//////////////////////////////////////////////////////////////////////////////////////////////
32+
/*
33+
* Distance functions for self organizing maps
34+
*/
35+
//////////////////////////////////////////////////////////////////////////////////////////////
36+
#ifdef __CUDACC__
37+
__host__ __device__
38+
#endif
39+
inline float
40+
fcn_bubble_nhood (float dist, float sigmaT) {
41+
if(dist < sigmaT)
42+
return 1.f;
43+
else return 0.f;
44+
}
45+
46+
//////////////////////////////////////////////////////////////////////////////////////////////
47+
#ifdef __CUDACC__
48+
__host__ __device__
49+
#endif
50+
inline float
51+
fcn_gaussian_nhood (float dist, float sigmaT) {
52+
return exp(-pow(dist, 2.f)/(2.f*pow(sigmaT, 2.f)));
53+
}
54+
55+
//////////////////////////////////////////////////////////////////////////////////////////////
56+
#ifdef __CUDACC__
57+
__host__ __device__
58+
#endif
59+
inline float
60+
fcn_cutgaussian_nhood (float dist, float sigmaT) {
61+
if(dist < sigmaT)
62+
return exp(-pow(dist, 2.f)/(2.f*pow(sigmaT, 2.f)));
63+
else return 0.f;
64+
}
65+
66+
67+
//////////////////////////////////////////////////////////////////////////////////////////////
68+
#ifdef __CUDACC__
69+
__host__ __device__
70+
#endif
71+
inline float
72+
fcn_mexican_nhood (float dist, float sigmaT) {
73+
return 2.f/(sqrt(3.f * sigmaT) * pow(PI, 0.25f) ) *
74+
(1.f-pow(dist, 2.f) / pow(sigmaT, 2.f) ) *
75+
fcn_gaussian_nhood(dist, sigmaT);
76+
}
77+
78+
79+
//////////////////////////////////////////////////////////////////////////////////////////////
80+
#ifdef __CUDACC__
81+
__host__ __device__
82+
#endif
83+
inline float
84+
fcn_epanechicov_nhood (float dist, float sigmaT) {
85+
float fVal = 1 - pow(dist/sigmaT, 2.f);
86+
if(fVal > 0)
87+
return fVal;
88+
else return 0.f;
89+
}
90+
91+
//////////////////////////////////////////////////////////////////////////////////////////////
92+
#ifdef __CUDACC__
93+
__host__ __device__
94+
#endif
95+
inline float
96+
fcn_rad_decay (float sigma0, float T, float lambda) {
97+
return std::floor(sigma0*exp(-T/lambda) + 0.5f);
98+
}
99+
100+
//////////////////////////////////////////////////////////////////////////////////////////////
101+
#ifdef __CUDACC__
102+
__host__ __device__
103+
#endif
104+
inline float
105+
fcn_lrate_decay (float sigma0, float T, float lambda) {
106+
return sigma0*exp(-T/lambda);
107+
}
108+
109+
/**
110+
* @class DistFunction
111+
* @brief Represents a neighborhood and decay function.
112+
* Consists of a distance and a decay function.
113+
* Normally just the neighborhood function is free to be changed.
114+
*/
115+
typedef float (*pDistanceFu) (float, float);
116+
typedef float (*pDecayFu) (float, float, float);
117+
118+
template <pDistanceFu Dist, pDecayFu Rad, pDecayFu LRate>
119+
class DistFunction {
120+
public:
121+
DistFunction() {}
122+
DistFunction(const char *cstr) : name(cstr) {};
123+
124+
const char *name;
125+
126+
#ifdef __CUDACC__
127+
__host__ __device__
128+
#endif
129+
static float distance(float a, float b) { return Dist(a,b); };
130+
#ifdef __CUDACC__
131+
__host__ __device__
132+
#endif
133+
static float rad_decay(float a, float b, float c) { return Rad(a,b,c); };
134+
#ifdef __CUDACC__
135+
__host__ __device__
136+
#endif
137+
static float lrate_decay(float a, float b, float c) { return LRate(a,b,c); };
138+
};
139+
140+
void test();
141+
142+
#endif /* TRANSFERFUNCTIONS_H_ */

main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "Functions.h"
2+
3+
4+
int main() {
5+
test();
6+
return 1;
7+
}

0 commit comments

Comments
 (0)