Skip to content

Commit c7be0ff

Browse files
committed
Added CUDA.
1 parent de72244 commit c7be0ff

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ implemented for:
2020
- 'boost.system',
2121
- 'boost.test',
2222
- 'boost.thread',
23+
- 'cuda',
2324
- 'eigen',
2425
- 'fftw',
2526
- 'hdf5',

__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
_check_dict as _hdf5_check_dict
5959
_check_dict = dict(_check_dict, **_hdf5_check_dict)
6060

61+
from .cuda import CheckCUDA, \
62+
_check_dict as _cuda_check_dict
63+
_check_dict = dict(_check_dict, **_cuda_check_)
64+
6165
def AddLibOptions(add_method, lib_names):
6266
r"""
6367
Provide the `AddOption` method for your enviromnent, and all command line

_tools.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def _setupPaths(env,
2929
if lib:
3030
env.PrependUnique(LIBPATH=[lib])
3131

32-
def _checkLibs(context, try_libs, source_file):
32+
def _checkLibs(context, try_libs, source_file, file_ending='.cpp'):
3333
init_libs = context.env.get('LIBS', [])
3434
context.env.PrependUnique(LIBS=[try_libs])
35-
result = context.TryLink(source_file, '.cpp')
35+
result = context.TryLink(source_file, file_ending)
3636
if not result :
3737
context.env.Replace(LIBS=init_libs)
3838
return result

cuda.py

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# -*- python -*-
2+
#
3+
# Copyright Christoph Lassner 2015.
4+
#
5+
# Distributed under the Boost Software License, Version 1.0.
6+
# (See http://www.boost.org/LICENSE_1_0.txt)
7+
8+
from __future__ import print_function
9+
import os
10+
11+
from ._tools import _checkLibs, _setupPaths
12+
13+
from SCons.SConf import CheckContext
14+
CheckContext.checkLibs = _checkLibs
15+
16+
_check_dict = {}
17+
_cuda_option_dict = {'--cuda-dir':
18+
{'dest':"cuda_prefix",
19+
'type':"string",
20+
'nargs':1,
21+
'action':"store",
22+
'metavar':"DIR",
23+
'default':os.environ.get("CUDA_ROOT"),
24+
'help':"prefix for cuda; should contain the 'bin', 'include', and 'lib' folders."},
25+
'--cuda-inc-dir':
26+
{'dest':"cuda_include",
27+
'type':"string",
28+
'nargs':1,
29+
'action':"store",
30+
'metavar':"DIR",
31+
'help':"this folder should contain 'cuda.h'.",
32+
'default':os.environ.get("CUDA_INCLUDE_DIR")},
33+
'--cuda-lib-dir':
34+
{'dest':"cuda_lib",
35+
'type':"string",
36+
'nargs':1,
37+
'action':"store",
38+
'metavar':"DIR",
39+
'help':"this folder should contain 'libcudart_static.a'.",
40+
'default':os.environ.get("CUDA_LIB_DIR")}}
41+
42+
def CheckCUDA(context):
43+
context.Message("Checking for CUDA...")
44+
if os.name == 'nt':
45+
result, nvcc_cmd = context.TryAction("where nvcc")
46+
result, cl_cmd = context.TryAction("where cl")
47+
nvcc_cmd = None
48+
else:
49+
result, nvcc_cmd = context.TryAction("which nvcc > $TARGET")
50+
context.Result(result)
51+
if result:
52+
if not nvcc_cmd is None:
53+
print("Using nvcc at", nvcc_cmd.strip())
54+
context.Message("Check building with CUDA...")
55+
sample_source_file = r"""
56+
#include <stdio.h>
57+
58+
__global__
59+
void add(int *a, int *b, int *c ) {
60+
*c = *a + *b;
61+
}
62+
63+
int main( void ) {
64+
int a, b, c;
65+
// host copies of a, b, c
66+
int *dev_a, *dev_b, *dev_c;
67+
// device copies of a, b, c
68+
int size = sizeof(int);
69+
// we need space for an integer
70+
// allocate device copies of a, b, c
71+
cudaMalloc( (void**)&dev_a, size );
72+
cudaMalloc( (void**)&dev_b, size );
73+
cudaMalloc( (void**)&dev_c, size );
74+
a = 2;
75+
b = 7;
76+
// copy inputs to device
77+
cudaMemcpy(dev_a, &a, size, cudaMemcpyHostToDevice);
78+
cudaMemcpy(dev_b, &b, size, cudaMemcpyHostToDevice);
79+
// launch add() kernel on GPU, passing parameters
80+
add<<< 1, 1 >>>(dev_a,dev_b,dev_c);
81+
// copy device result back to host copy of c
82+
cudaMemcpy( &c,dev_c, size,cudaMemcpyDeviceToHost);
83+
cudaFree(dev_a);
84+
cudaFree(dev_b);
85+
cudaFree(dev_c);
86+
return 0;
87+
}
88+
"""
89+
context.Message('Check building with CUDA... ')
90+
ex_prefix_dir = context.env.GetOption("cuda_prefix")
91+
ex_lib_dir = context.env.GetOption("cuda_lib")
92+
ex_include_dir = context.env.GetOption("cuda_include")
93+
_setupPaths(context.env,
94+
prefix = ex_prefix_dir,
95+
include = ex_include_dir,
96+
lib = ex_lib_dir
97+
)
98+
context.env.Tool('nvcc')
99+
result_bld = (context.checkLibs(['cublas', 'cublas_device',
100+
'cufft', 'cufftw', 'curand'],
101+
sample_source_file,
102+
'.cu'))
103+
if not result_bld:
104+
context.Result(0)
105+
print("Cannot build with CUDA.")
106+
return False
107+
result, output = context.TryRun(sample_source_file, '.cu')
108+
if not result:
109+
print("Warning: cannot run program built with CUDA. Continuing anyway.")
110+
context.Result(1)
111+
return True
112+
_check_dict['cuda'] = {'options': {},
113+
'checks': [CheckCUDA]}

0 commit comments

Comments
 (0)