|
| 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