mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-05 17:34:21 +00:00
Initial CUDA test
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -30,3 +30,5 @@
|
|||||||
*.exe
|
*.exe
|
||||||
*.out
|
*.out
|
||||||
*.app
|
*.app
|
||||||
|
|
||||||
|
build/
|
||||||
|
|||||||
34
CMakeLists.txt
Normal file
34
CMakeLists.txt
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.12)
|
||||||
|
|
||||||
|
project(CUDANet)
|
||||||
|
|
||||||
|
# Find CUDA
|
||||||
|
find_package(CUDA REQUIRED)
|
||||||
|
|
||||||
|
# Add CUDA include directories
|
||||||
|
include_directories(${CUDA_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
# Add project source files
|
||||||
|
set(SOURCES
|
||||||
|
src/main.cpp
|
||||||
|
src/utils/cuda_helper.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
# Set CUDA architecture (change according to your GPU)
|
||||||
|
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -arch=sm_75)
|
||||||
|
|
||||||
|
# Build executable
|
||||||
|
cuda_add_executable(${PROJECT_NAME} ${SOURCES})
|
||||||
|
|
||||||
|
# Link cuBLAS library
|
||||||
|
target_link_libraries(${PROJECT_NAME} ${CUDA_cublas_LIBRARY})
|
||||||
|
|
||||||
|
# Set include directories
|
||||||
|
target_include_directories(${PROJECT_NAME} PRIVATE
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/include/utils
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/src
|
||||||
|
)
|
||||||
|
|
||||||
|
# Set C++ standard
|
||||||
|
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
|
||||||
21
include/utils/cuda_helper.h
Normal file
21
include/utils/cuda_helper.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#ifndef CUDA_HELPER_H
|
||||||
|
#define CUDA_HELPER_H
|
||||||
|
|
||||||
|
#include <cuda_runtime.h>
|
||||||
|
|
||||||
|
// CUDA error checking macro
|
||||||
|
#define CUDA_CHECK(call) \
|
||||||
|
do { \
|
||||||
|
cudaError_t result = call; \
|
||||||
|
if (result != cudaSuccess) { \
|
||||||
|
fprintf(stderr, "CUDA error at %s:%d code=%d(%s) \"%s\" \n", \
|
||||||
|
__FILE__, __LINE__, static_cast<unsigned int>(result), \
|
||||||
|
cudaGetErrorString(result), #call); \
|
||||||
|
exit(EXIT_FAILURE); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
// Initialize CUDA and return the device properties
|
||||||
|
cudaDeviceProp initializeCUDA();
|
||||||
|
|
||||||
|
#endif // CUDA_HELPER_H
|
||||||
68
src/main.cpp
Normal file
68
src/main.cpp
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <cuda_runtime.h>
|
||||||
|
#include "cublas_v2.h"
|
||||||
|
#include "cuda_helper.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Initialize CUDA and get device properties
|
||||||
|
cudaDeviceProp deviceProp = initializeCUDA();
|
||||||
|
|
||||||
|
// Specify vector size
|
||||||
|
const int N = 5;
|
||||||
|
|
||||||
|
// Host vectors
|
||||||
|
float *h_A, *h_B, *h_C;
|
||||||
|
|
||||||
|
// Allocate host memory
|
||||||
|
h_A = (float*)malloc(N * sizeof(float));
|
||||||
|
h_B = (float*)malloc(N * sizeof(float));
|
||||||
|
h_C = (float*)malloc(N * sizeof(float));
|
||||||
|
|
||||||
|
// Initialize host vectors
|
||||||
|
for (int i = 0; i < N; ++i) {
|
||||||
|
h_A[i] = static_cast<float>(i);
|
||||||
|
h_B[i] = static_cast<float>(2 * i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocate device memory
|
||||||
|
float *d_A, *d_B, *d_C;
|
||||||
|
cudaMalloc((void**)&d_A, N * sizeof(float));
|
||||||
|
cudaMalloc((void**)&d_B, N * sizeof(float));
|
||||||
|
cudaMalloc((void**)&d_C, N * sizeof(float));
|
||||||
|
|
||||||
|
// Copy host vectors to device
|
||||||
|
cudaMemcpy(d_A, h_A, N * sizeof(float), cudaMemcpyHostToDevice);
|
||||||
|
cudaMemcpy(d_B, h_B, N * sizeof(float), cudaMemcpyHostToDevice);
|
||||||
|
|
||||||
|
// Create cuBLAS handle
|
||||||
|
cublasHandle_t handle;
|
||||||
|
cublasCreate(&handle);
|
||||||
|
|
||||||
|
// Perform vector addition: C = A + B
|
||||||
|
const float alpha = 1.0f;
|
||||||
|
const float beta = 1.0f;
|
||||||
|
cublasSaxpy(handle, N, &alpha, d_A, 1, d_B, 1);
|
||||||
|
cublasSaxpy(handle, N, &beta, d_B, 1, d_C, 1);
|
||||||
|
|
||||||
|
// Copy result from device to host
|
||||||
|
cudaMemcpy(h_C, d_C, N * sizeof(float), cudaMemcpyDeviceToHost);
|
||||||
|
|
||||||
|
// Display result
|
||||||
|
printf("Result: ");
|
||||||
|
for (int i = 0; i < N; ++i) {
|
||||||
|
printf("%f ", h_C[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
free(h_A);
|
||||||
|
free(h_B);
|
||||||
|
free(h_C);
|
||||||
|
cudaFree(d_A);
|
||||||
|
cudaFree(d_B);
|
||||||
|
cudaFree(d_C);
|
||||||
|
cublasDestroy(handle);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
36
src/utils/cuda_helper.cpp
Normal file
36
src/utils/cuda_helper.cpp
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include "cuda_helper.h"
|
||||||
|
|
||||||
|
// CUDA error checking macro
|
||||||
|
#define CUDA_CHECK(call) \
|
||||||
|
do { \
|
||||||
|
cudaError_t result = call; \
|
||||||
|
if (result != cudaSuccess) { \
|
||||||
|
std::fprintf(stderr, "CUDA error at %s:%d code=%d(%s) \"%s\" \n", \
|
||||||
|
__FILE__, __LINE__, static_cast<unsigned int>(result), \
|
||||||
|
cudaGetErrorString(result), #call); \
|
||||||
|
std::exit(EXIT_FAILURE); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
// Initialize CUDA and return the device properties
|
||||||
|
cudaDeviceProp initializeCUDA() {
|
||||||
|
int deviceCount;
|
||||||
|
CUDA_CHECK(cudaGetDeviceCount(&deviceCount));
|
||||||
|
|
||||||
|
if (deviceCount == 0) {
|
||||||
|
std::fprintf(stderr, "No CUDA devices found. Exiting.\n");
|
||||||
|
std::exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int device = 0; // You can modify this to choose a different GPU
|
||||||
|
CUDA_CHECK(cudaSetDevice(device));
|
||||||
|
|
||||||
|
cudaDeviceProp deviceProp;
|
||||||
|
CUDA_CHECK(cudaGetDeviceProperties(&deviceProp, device));
|
||||||
|
|
||||||
|
std::printf("Using CUDA device %d: %s\n", device, deviceProp.name);
|
||||||
|
|
||||||
|
return deviceProp;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user