Skip to content

Commit 52148e8

Browse files
2 parents 5e4eea7 + 4d7c8c1 commit 52148e8

File tree

9 files changed

+182
-0
lines changed

9 files changed

+182
-0
lines changed

rust-cuda-project/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "meu-projeto-rust"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]

rust-cuda-project/Makefile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Caminhos para os arquivos
2+
CUDA_SRC = src/dot_product.cu
3+
CUDA_OBJ = dot_product.o
4+
CUDA_LIB = libdot_product.so
5+
RUST_TARGET = target/debug/meu-projeto-rust # Substitua com o nome do seu pacote Rust
6+
7+
# Compilador CUDA
8+
NVCC = nvcc
9+
CC = gcc
10+
CARGO = cargo
11+
12+
# Flags de compilação
13+
CUDA_FLAGS = --compiler-options '-fPIC' -c
14+
CC_FLAGS = -shared -lcudart
15+
16+
# Compilação CUDA: cria o arquivo de objeto
17+
$(CUDA_OBJ): $(CUDA_SRC)
18+
$(NVCC) $(CUDA_FLAGS) $(CUDA_SRC) -o $(CUDA_OBJ)
19+
20+
# Criação da biblioteca compartilhada a partir do objeto compilado
21+
$(CUDA_LIB): $(CUDA_OBJ)
22+
$(CC) $(CC_FLAGS) $(CUDA_OBJ) -o $(CUDA_LIB)
23+
24+
# Compilar o código Rust e vincular a biblioteca compartilhada
25+
rust: $(CUDA_LIB)
26+
cargo build
27+
28+
# Limpeza de arquivos gerados
29+
clean:
30+
rm -f $(CUDA_OBJ) $(CUDA_LIB)
31+
cargo clean
32+
33+
# Compilar tudo
34+
all: rust
35+
36+
.PHONY: clean all

rust-cuda-project/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Meu Projeto Rust
2+
3+
Este é um projeto de exemplo em Rust.
4+
5+
## Estrutura do Projeto
6+
7+
```
8+
meu-projeto-rust
9+
├── src
10+
│ ├── main.rs
11+
├── Cargo.toml
12+
└── README.md
13+
```
14+
15+
## Como Configurar
16+
17+
Para configurar o projeto, você precisa ter o Rust e o Cargo instalados. Você pode instalar o Rust através do [rustup](https://rustup.rs/).
18+
19+
## Como Executar
20+
21+
Para executar o projeto, navegue até o diretório do projeto e use o seguinte comando:
22+
23+
```
24+
cargo run
25+
```
26+
27+
Isso irá compilar e executar o programa.

rust-cuda-project/build.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
println!("cargo:rustc-link-search=.");
3+
println!("cargo:rustc-link-lib=dylib=dot_product");
4+
println!("cargo:rustc-link-search=native=/usr/local/cuda/lib64");
5+
println!("cargo:rustc-link-lib=dylib=cudart");
6+
}

rust-cuda-project/dot_product.o

11.2 KB
Binary file not shown.

rust-cuda-project/libdot_product.so

21.4 KB
Binary file not shown.

rust-cuda-project/src/dot_product.cu

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// dot_product.cu
2+
#include <cuda_runtime.h>
3+
#include <stdio.h>
4+
5+
extern "C" {
6+
7+
// Kernel CUDA para multiplicar os vetores
8+
__global__ void dot_product_kernel(const double* x, const double* y, double* partial_sum, int n) {
9+
__shared__ double cache[256]; // Cache compartilhado para redução
10+
int tid = threadIdx.x + blockIdx.x * blockDim.x;
11+
int cacheIndex = threadIdx.x;
12+
13+
double temp = 0.0;
14+
while (tid < n) {
15+
temp += x[tid] * y[tid];
16+
tid += blockDim.x * gridDim.x;
17+
}
18+
19+
cache[cacheIndex] = temp;
20+
21+
__syncthreads();
22+
23+
// Redução paralela
24+
int i = blockDim.x/2;
25+
while (i != 0) {
26+
if (cacheIndex < i) {
27+
cache[cacheIndex] += cache[cacheIndex + i];
28+
}
29+
__syncthreads();
30+
i /= 2;
31+
}
32+
33+
if (cacheIndex == 0)
34+
partial_sum[blockIdx.x] = cache[0];
35+
}
36+
37+
// Função wrapper para ser chamada do Rust
38+
void dot_product_gpu(const double* x, const double* y, double* result, int n) {
39+
double *d_x, *d_y, *d_partial_sum;
40+
int blocks = 32;
41+
int threads = 256;
42+
43+
cudaMalloc((void**)&d_x, n * sizeof(double));
44+
cudaMalloc((void**)&d_y, n * sizeof(double));
45+
cudaMalloc((void**)&d_partial_sum, blocks * sizeof(double));
46+
47+
cudaMemcpy(d_x, x, n * sizeof(double), cudaMemcpyHostToDevice);
48+
cudaMemcpy(d_y, y, n * sizeof(double), cudaMemcpyHostToDevice);
49+
50+
dot_product_kernel<<<blocks, threads>>>(d_x, d_y, d_partial_sum, n);
51+
52+
double* h_partial_sum = (double*) malloc(blocks * sizeof(double));
53+
cudaMemcpy(h_partial_sum, d_partial_sum, blocks * sizeof(double), cudaMemcpyDeviceToHost);
54+
55+
*result = 0.0;
56+
for (int i = 0; i < blocks; i++) {
57+
*result += h_partial_sum[i];
58+
}
59+
60+
free(h_partial_sum);
61+
cudaFree(d_x);
62+
cudaFree(d_y);
63+
cudaFree(d_partial_sum);
64+
}
65+
66+
}

rust-cuda-project/src/dot_product.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// dot_product.h
2+
3+
#ifndef DOT_PRODUCT_H
4+
#define DOT_PRODUCT_H
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
void dot_product_gpu(const double* x, const double* y, double* result, int n);
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif
15+
16+
#endif

rust-cuda-project/src/main.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::ffi::c_double;
2+
use std::os::raw::c_int;
3+
4+
extern "C" {
5+
fn dot_product_gpu(x: *const c_double, y: *const c_double, result: *mut c_double, n: c_int);
6+
}
7+
8+
// Função Rust
9+
fn vecvecmul(x: &[f64], y: &[f64]) -> f64 {
10+
assert_eq!(x.len(), y.len());
11+
let mut result: f64 = 0.0;
12+
13+
unsafe {
14+
dot_product_gpu(x.as_ptr(), y.as_ptr(), &mut result as *mut f64, x.len() as i32);
15+
}
16+
17+
result
18+
}
19+
20+
fn main() {
21+
let a = vec![1.0, 2.0, 3.0];
22+
let b = vec![4.0, 5.0, 6.0];
23+
let dot = vecvecmul(&a, &b);
24+
println!("Produto interno: {}", dot); // Deve imprimir 32.0
25+
}

0 commit comments

Comments
 (0)