Skip to content
This repository was archived by the owner on Mar 29, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 18 additions & 57 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
version: 2

.cargo: &cargo
name: Cargo build
.cargo_build: &cargo_build
name: Cargo Build
command: |
export PATH=/root/.cargo/bin:$PATH
cargo build -vv
cargo build -v

.cargo_test: &cargo_test
name: Cargo Test
command: |
export PATH=/root/.cargo/bin:$PATH
cargo test -v

.job_apt_template: &job_apt
steps:
Expand All @@ -13,21 +19,24 @@ version: 2
name: Install Rust
command: |
apt update
apt install -y curl
apt install -y curl clang libclang-dev
curl https://sh.rustup.rs -sSf | sh -s -- -y
- run:
<<: *cargo
<<: *cargo_build
<<: *cargo_test

.job_yum_template: &job_yum
steps:
- checkout
- run:
name: Install Rust
command: |
yum install -y curl
yum install -y epel-release
yum install -y curl clang-devel clang
curl https://sh.rustup.rs -sSf | sh -s -- -y
- run:
<<: *cargo
<<: *cargo_build
<<: *cargo_test

jobs:
latest:
Expand All @@ -54,26 +63,7 @@ jobs:
<<: *job_apt
docker:
- image: nvidia/cuda:9.0-devel-ubuntu16.04
8.0-devel-ubuntu16.04:
<<: *job_apt
docker:
- image: nvidia/cuda:8.0-devel-ubuntu16.04
8.0-devel-ubuntu14.04:
<<: *job_apt
docker:
- image: nvidia/cuda:8.0-devel-ubuntu14.04
7.5-devel-ubuntu14.04:
<<: *job_apt
docker:
- image: nvidia/cuda:7.5-devel-ubuntu14.04
7.0-devel-ubuntu14.04:
<<: *job_apt
docker:
- image: nvidia/cuda:7.0-devel-ubuntu14.04
6.5-devel-ubuntu14.04:
<<: *job_apt
docker:
- image: nvidia/cuda:6.5-devel-ubuntu14.04

10.0-devel-centos7:
<<: *job_yum
docker:
Expand All @@ -90,18 +80,7 @@ jobs:
<<: *job_yum
docker:
- image: nvidia/cuda:9.0-devel-centos7
8.0-devel-centos7:
<<: *job_yum
docker:
- image: nvidia/cuda:8.0-devel-centos7
7.5-devel-centos7:
<<: *job_yum
docker:
- image: nvidia/cuda:7.5-devel-centos7
7.0-devel-centos7:
<<: *job_yum
docker:
- image: nvidia/cuda:7.0-devel-centos7

10.0-devel-centos6:
<<: *job_yum
docker:
Expand All @@ -118,14 +97,6 @@ jobs:
<<: *job_yum
docker:
- image: nvidia/cuda:9.0-devel-centos6
8.0-devel-centos6:
<<: *job_yum
docker:
- image: nvidia/cuda:8.0-devel-centos6
7.5-devel-centos6:
<<: *job_yum
docker:
- image: nvidia/cuda:7.5-devel-centos6

workflows:
version: 2
Expand All @@ -137,21 +108,11 @@ workflows:
- 9.2-devel-ubuntu16.04
- 9.1-devel-ubuntu16.04
- 9.0-devel-ubuntu16.04
- 8.0-devel-ubuntu16.04
- 8.0-devel-ubuntu14.04
- 7.5-devel-ubuntu14.04
- 7.0-devel-ubuntu14.04
- 6.5-devel-ubuntu14.04
- 10.0-devel-centos7
- 9.2-devel-centos7
- 9.1-devel-centos7
- 9.0-devel-centos7
- 8.0-devel-centos7
- 7.5-devel-centos7
- 7.0-devel-centos7
- 10.0-devel-centos6
- 9.2-devel-centos6
- 9.1-devel-centos6
- 9.0-devel-centos6
- 8.0-devel-centos6
- 7.5-devel-centos6
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ keywords = ["GPGPU", "CUDA", "ffi"]
license = "MIT"
readme = "README.md"
categories = []

[build-dependencies]
bindgen = "~0.43"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cuda-sys
[![Crate](http://meritbadge.herokuapp.com/cuda-sys)](https://crates.io/crates/cuda-sys)
[![docs.rs](https://docs.rs/cuda-sys/badge.svg)](https://docs.rs/cuda-sys)

Rust binding to CUDA Driver(`libcuda.so`)/Runtime(`libcudart.so`) APIs
Rust binding to CUDA Driver(`libcuda.so`) and Runtime(`libcudart.so`) APIs

- This crate does not include CUDA itself. You need to install on your own.
- `$CUDA_LIBRARY_PATH` (e.g. `/opt/cuda/lib64`) will be used for `cargo:rustc-link-search`
- cuda-sys searches `/usr/local/cuda`, `/opt/cuda`, and directories specified in `$CUDA_LIBRARY_PATH` while compiling.
138 changes: 132 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,146 @@
extern crate bindgen;

use std::env;
use std::path::PathBuf;

fn find_library_paths() -> Vec<String> {
fn read_env() -> Vec<String> {
match env::var("CUDA_LIBRARY_PATH") {
Ok(path) => {
let split_char = if cfg!(target_os = "windows") { ";" } else { ":" };
let split_char = if cfg!(target_os = "windows") {
";"
} else {
":"
};

path.split(split_char).map(|s| s.to_owned()).collect::<Vec<_>>()
path.split(split_char)
.map(|s| s.to_owned())
.collect::<Vec<_>>()
}
Err(_) => vec![],
}
}

fn main() {
for p in find_library_paths() {
println!("cargo:rustc-link-search=native={}", p);
fn find_cuda() -> PathBuf {
let mut candidates = read_env();
candidates.push("/usr/local/cuda".to_string());
candidates.push("/opt/cuda".to_string());
for base in &candidates {
let base = PathBuf::from(base);
let path = base.join("include/cuda.h");
if path.is_file() {
return base;
}
}
panic!("CUDA cannot find");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

On Windows, I normally have to set my CUDA_LIBRARY_PATH to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\lib\x64. It would be nice if it could find that for me. I can send a pull request later adding this feature though.

A better error message would be:

cuda-sys cannot find the CUDA libraries. Please ensure that they are installed, or provide the path using the CUDA_LIBRARY_PATH environment variable. Searched:

  • /usr/local/cuda
  • /opt/cuda

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, hey, I addressed your issue with Windows in #6


fn main() {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let cuda_path = find_cuda();

bindgen::builder()
.header("wrapper/cuda.h")
.clang_arg(format!("-I{}/include", cuda_path.display()))
.whitelist_recursively(false)
.whitelist_type("^CU.*")
.whitelist_type("^cuuint(32|64)_t")
.whitelist_type("^cudaError_enum")
.whitelist_type("^cudaMem.*")
.whitelist_var("^CU.*")
.whitelist_function("^CU.*")
.whitelist_function("^cu.*")
.default_enum_style(bindgen::EnumVariation::Rust)
.generate()
.expect("Unable to generate CUDA bindings")
.write_to_file(out_path.join("cuda_bindings.rs"))
.expect("Unable to write CUDA bindings");

bindgen::builder()
.header("wrapper/cublas.h")
.clang_arg(format!("-I{}/include", cuda_path.display()))
.whitelist_recursively(false)
.whitelist_type("^cublas.*")
.whitelist_var("^cublas.*")
.whitelist_function("^cublas.*")
.default_enum_style(bindgen::EnumVariation::Rust)
.generate()
.expect("Unable to generate CUBLAS bindings")
.write_to_file(out_path.join("cublas_bindings.rs"))
.expect("Unable to write CUBLAS bindings");

bindgen::builder()
.header("wrapper/cucomplex.h")
.clang_arg(format!("-I{}/include", cuda_path.display()))
.whitelist_recursively(false)
.whitelist_type("^cu.*Complex$")
.default_enum_style(bindgen::EnumVariation::Rust)
.generate()
.expect("Unable to generate CUComplex bindings")
.write_to_file(out_path.join("cucomplex_bindings.rs"))
.expect("Unable to write CUComplex bindings");
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like this generates bindings for CUComplex and CUBLAS, but only links to CUBLAS. Was that intended? It seems like it should either link to CUComplex as well, or it should not generate bindings for it.

I'd prefer to have separate cudart-sys, cublas-sys and cucomplex-sys crates, and have cuda-sys only include the driver API, if that's possible.

Copy link

@kngwyu kngwyu Nov 29, 2018

Choose a reason for hiding this comment

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

Looks only

pub type cuFloatComplex = float2;
pub type cuDoubleComplex = double2;
pub type cuComplex = cuFloatComplex;

are generated from CUComplex.h

Copy link
Contributor

Choose a reason for hiding this comment

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

@bheisler Thanks for the review!

I can add one more problem to the list. cublas doesn't compile using CUDA 8. It's missing the __half and __half2 types. Unfortunately, I couldn't get Bindgen to detect those types in cuda_fp16.h. As a work-around, I've added them manually in LutzCle/accel@74a82a4.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've create an issue to track *-sys crate separation #9


bindgen::builder()
.header("wrapper/cudart.h")
.clang_arg(format!("-I{}/include", cuda_path.display()))
.whitelist_recursively(false)
.whitelist_type("^cuda.*")
.whitelist_type("^surfaceReference")
.whitelist_type("^textureReference")
.whitelist_var("^cuda.*")
.whitelist_function("^cuda.*")
.default_enum_style(bindgen::EnumVariation::Rust)
.generate()
.expect("Unable to generate CUDA RT bindings")
.write_to_file(out_path.join("cudart_bindings.rs"))
.expect("Unable to write CUDA RT bindings");

bindgen::builder()
.header("wrapper/driver_types.h")
.clang_arg(format!("-I{}/include", cuda_path.display()))
.whitelist_recursively(false)
.whitelist_type("^CU.*")
.whitelist_type("^cuda.*")
.default_enum_style(bindgen::EnumVariation::Rust)
.generate()
.expect("Unable to generate driver types bindings")
.write_to_file(out_path.join("driver_types_bindings.rs"))
.expect("Unable to write driver types bindings");

bindgen::builder()
.header("wrapper/library_types.h")
.clang_arg(format!("-I{}/include", cuda_path.display()))
.whitelist_recursively(false)
.whitelist_type("^cuda.*")
.whitelist_type("^libraryPropertyType.*")
.default_enum_style(bindgen::EnumVariation::Rust)
.generate()
.expect("Unable to generate library types bindings")
.write_to_file(out_path.join("library_types_bindings.rs"))
.expect("Unable to write library types bindings");

bindgen::builder()
.header("wrapper/vector_types.h")
.clang_arg(format!("-I{}/include", cuda_path.display()))
// .whitelist_recursively(false)
.whitelist_type("^u?char[0-9]$")
.whitelist_type("^dim[0-9]$")
.whitelist_type("^double[0-9]$")
.whitelist_type("^float[0-9]$")
.whitelist_type("^u?int[0-9]$")
.whitelist_type("^u?long[0-9]$")
.whitelist_type("^u?longlong[0-9]$")
.whitelist_type("^u?short[0-9]$")
.default_enum_style(bindgen::EnumVariation::Rust)
.derive_copy(true)
.generate()
.expect("Unable to generate vector types bindings")
.write_to_file(out_path.join("vector_types_bindings.rs"))
.expect("Unable to write vector types bindings");

println!(
"cargo:rustc-link-search=native={}/lib64",
cuda_path.display()
);
println!("cargo:rustc-link-lib=dylib=cuda");
println!("cargo:rustc-link-lib=dylib=cudart");
println!("cargo:rustc-link-lib=dylib=cublas");
Expand Down
Loading