Skip to content

Commit

Permalink
change cu to cpp in main file
Browse files Browse the repository at this point in the history
  • Loading branch information
aghaeifar committed Dec 21, 2024
1 parent 5e55275 commit bdf64ec
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 116 deletions.
15 changes: 11 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ message(STATUS "CMake version: ${CMAKE_VERSION}")
include(CheckLanguage)

set(project "SpinWalk")
set(project_lib "SpinWalkLib")
project(${project})

# Find HDF5
Expand Down Expand Up @@ -41,15 +42,18 @@ if(CMAKE_BUILD_TYPE MATCHES Debug)
endif()

# Add Boost and CUDA include directories
include_directories(${Boost_INCLUDE_DIRS} ${HDF5_INCLUDE_DIRS} ./include ./src ./src/shapes)
include_directories(${Boost_INCLUDE_DIRS} ${HDF5_INCLUDE_DIRS} ./include ./src)
# Add subfolders
add_subdirectory(./src/dwi)
add_subdirectory(./src/phantom)
add_subdirectory(./src/config)
add_subdirectory(./src/sim)

# Add the sources files
set(SOURCES ./src/spinwalk.cu ${SUBCOMMAND_PHANTOM} ${SUBCOMMAND_SIM} ${SUBCOMMAND_DWI} ${SUBCOMMAND_CONFIG})
set(SOURCE_MODULES ${SUBCOMMAND_PHANTOM} ${SUBCOMMAND_SIM} ${SUBCOMMAND_DWI} ${SUBCOMMAND_CONFIG})
set(SOURCE_MAIN ./src/spinwalk.cpp)

# change extension of the files if CUDA is not found
if(NOT CMAKE_CUDA_COMPILER)
set(RENAMED_SOURCES)
foreach(OLD_FILE ${SOURCES})
Expand All @@ -62,11 +66,12 @@ if(NOT CMAKE_CUDA_COMPILER)
file(COPY_FILE ${OLD_FILE} ${NEW_FILE})
list(APPEND RENAMED_SOURCES ${NEW_FILE})
endforeach()
set(SOURCES ${RENAMED_SOURCES})
set(SOURCE_MODULES ${RENAMED_SOURCES})
endif()

# Add the executable
add_executable(${project} ${SOURCES})
add_library (${project_lib} ${SOURCE_MODULES})
add_executable(${project} ${SOURCE_MAIN} ${SOURCE_MODULES})

# Add the libraries
if(CMAKE_CUDA_COMPILER)
Expand All @@ -81,10 +86,12 @@ endif()
target_link_libraries(${project} PRIVATE ${Boost_LIBRARIES} ${HDF5_CXX_LIBRARIES} TBB::tbb)

# Set the output name
set_target_properties(${project_lib} PROPERTIES OUTPUT_NAME "spinwalk")
set_target_properties(${project} PROPERTIES OUTPUT_NAME "spinwalk")

# Install the executable
if (UNIX)
install(TARGETS ${project_lib} DESTINATION lib)
install(TARGETS ${project} DESTINATION bin)
endif()

Expand Down
1 change: 1 addition & 0 deletions src/sim/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(SUBCOMMAND_SIM
${CMAKE_CURRENT_SOURCE_DIR}/monte_carlo.cu
${CMAKE_CURRENT_SOURCE_DIR}/kernels.cu
${CMAKE_CURRENT_SOURCE_DIR}/device_helper.cu
${CMAKE_CURRENT_SOURCE_DIR}/config_reader.cpp
PARENT_SCOPE)
107 changes: 107 additions & 0 deletions src/sim/device_helper.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

#ifndef DEVICE_HELPER_CUH
#define DEVICE_HELPER_CUH

#include <iostream>
#include <boost/log/trivial.hpp>
#include "device_helper.cuh"

#ifdef __CUDACC__
#include "helper_cuda.h"
#endif

//---------------------------------------------------------------------------------------------
// check for CUDA and GPU device
//---------------------------------------------------------------------------------------------

#define NO_CUDA_MSG "CUDA is not available in this build. Please recompile with CUDA enabled."

namespace sim
{
bool check_CUDA()
{
#ifdef __CUDACC__
int32_t device = 0;
cudaError_t error = cudaGetDeviceCount(&device);
if (error != cudaSuccess)
{
std::cout << "\033[1;31mError:\033[0m " <<cudaGetErrorString(error) << "\n";
return false;
}
#endif
return true;
}

uint32_t get_device_count()
{
int32_t device_count = 0;
#ifdef __CUDACC__
checkCudaErrors(cudaGetDeviceCount(&device_count));
#endif
return device_count;
}

std::string convert_cuda_version(int32_t value) {
int v = value/1000;
int h = (value % 100) / 10;
return std::to_string(v) + "." + std::to_string(h);
}

void print_device_info()
{
#ifdef __CUDACC__
if (check_CUDA() == false)
return;

size_t free, total;
int32_t cuda_version, driver_version;

checkCudaErrors(cudaRuntimeGetVersion(&cuda_version));
checkCudaErrors(cudaDriverGetVersion(&driver_version));
std::cout << "The latest version of CUDA supported by the driver: "<< convert_cuda_version(driver_version) << ", current CUDA version: "<< convert_cuda_version(cuda_version) << "\n";

int32_t device_count = get_device_count();
std::cout <<"Number of devices: " << device_count << "\n";

cudaDeviceProp device_properties;

int device_id = 0;
checkCudaErrors(cudaGetDevice(&device_id));
cudaGetDeviceProperties(&device_properties, device_id);
std::cout << device_properties.name << std::endl;
std::cout << "-Compute Capability: " << device_properties.major << "."<< device_properties.minor << std::endl;
// cudaDeviceReset();
cudaMemGetInfo(&free, &total);
std::cout << "-Free GPU Memory: " << (free >> 20) << " MB (out of " << (total >> 20) << " MB)" << std::endl;
#else
std::cout << NO_CUDA_MSG << std::endl;
#endif
}

bool check_memory_size(size_t required_size_MB)
{
bool memory_ok = true;
#ifdef __CUDACC__
if (check_CUDA() == false)
return false;

size_t free, total;
int32_t device_count = get_device_count();
cudaDeviceProp device_properties;

int device_id = 0;
checkCudaErrors(cudaGetDevice(&device_id));
checkCudaErrors(cudaGetDeviceProperties(&device_properties, device_id));
checkCudaErrors(cudaMemGetInfo(&free, &total));
free = free >> 20;
total = total >> 20;
BOOST_LOG_TRIVIAL(info) << device_properties.name << " " << device_id << ": total memroy= " << total << " MB, free memory= " << free << " MB, required memory= " << required_size_MB << " MB" << '\n';
memory_ok = free<required_size_MB ? false:true;
if(memory_ok == false)
BOOST_LOG_TRIVIAL(fatal) << "Not enough GPU memory for the simulation in device! Required=" << required_size_MB <<" MB, Available=" << free << " MB";
#endif
return memory_ok;
}

} // namespace sim
#endif // DEVICE_HELPER_CUH
82 changes: 6 additions & 76 deletions src/sim/device_helper.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,22 @@
#ifndef DEVICE_HELPER_CUH
#define DEVICE_HELPER_CUH

#include <iostream>
#include <boost/log/trivial.hpp>
#include "helper_cuda.h"

#include <string>
//---------------------------------------------------------------------------------------------
// check for CUDA and GPU device
//---------------------------------------------------------------------------------------------

namespace sim
{
static bool check_CUDA()
{
int32_t device = 0;
cudaError_t error = cudaGetDeviceCount(&device);
if (error != cudaSuccess)
{
std::cout << "\033[1;31mError:\033[0m " <<cudaGetErrorString(error) << "\n";
return false;
}
return true;
}

static uint32_t get_device_count()
{
int32_t device_count = 0;
checkCudaErrors(cudaGetDeviceCount(&device_count));
return device_count;
}

static std::string convert_cuda_version(int32_t value) {
int v = value/1000;
int h = (value % 100) / 10;
return std::to_string(v) + "." + std::to_string(h);
}

static void print_device_info()
{
if (check_CUDA() == false)
return;

size_t free, total;
int32_t cuda_version, driver_version;

checkCudaErrors(cudaRuntimeGetVersion(&cuda_version));
checkCudaErrors(cudaDriverGetVersion(&driver_version));
std::cout << "The latest version of CUDA supported by the driver: "<< convert_cuda_version(driver_version) << ", current CUDA version: "<< convert_cuda_version(cuda_version) << "\n";

int32_t device_count = get_device_count();
std::cout <<"Number of devices: " << device_count << "\n";
bool check_CUDA();

cudaDeviceProp device_properties;

int device_id = 0;
checkCudaErrors(cudaGetDevice(&device_id));
cudaGetDeviceProperties(&device_properties, device_id);
std::cout << device_properties.name << std::endl;
std::cout << "-Compute Capability: " << device_properties.major << "."<< device_properties.minor << std::endl;
// cudaDeviceReset();
cudaMemGetInfo(&free, &total);
std::cout << "-Free GPU Memory: " << (free >> 20) << " MB (out of " << (total >> 20) << " MB)" << std::endl;

}

static bool check_memory_size(size_t required_size_MB)
{
if (check_CUDA() == false)
return false;
uint32_t get_device_count();

size_t free, total;
bool memory_ok = true;
int32_t device_count = get_device_count();
cudaDeviceProp device_properties;
std::string convert_cuda_version(int32_t value) ;

int device_id = 0;
checkCudaErrors(cudaGetDevice(&device_id));
checkCudaErrors(cudaGetDeviceProperties(&device_properties, device_id));
checkCudaErrors(cudaMemGetInfo(&free, &total));
free = free >> 20;
total = total >> 20;
BOOST_LOG_TRIVIAL(info) << device_properties.name << " " << device_id << ": total memroy= " << total << " MB, free memory= " << free << " MB, required memory= " << required_size_MB << " MB" << '\n';
memory_ok = free<required_size_MB ? false:true;
if(memory_ok == false)
BOOST_LOG_TRIVIAL(fatal) << "Not enough GPU memory for the simulation in device! Required=" << required_size_MB <<" MB, Available=" << free << " MB";
void print_device_info();

return memory_ok;
}
bool check_memory_size(size_t required_size_MB);

} // namespace sim
#endif // DEVICE_HELPER_CUH
28 changes: 21 additions & 7 deletions src/sim/monte_carlo.cu
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifdef __CUDACC__
#include "device_helper.cuh"
#include <cuda_runtime.h>
#include "helper_cuda.h"
#endif


Expand All @@ -29,15 +30,28 @@ namespace bl = boost::log;
namespace sim
{

monte_carlo::monte_carlo()
monte_carlo::monte_carlo(bool gpu_disabled, int32_t device_id)
{
// param = new simulation_parameters();
// config = new config_reader();
this->gpu_disabled = true;
#ifdef __CUDACC__
gpu_disabled = false;
checkCudaErrors(cudaGetDeviceCount(&device_count));
BOOST_LOG_TRIVIAL(info) << "Number of available GPU(s): " << device_count;
#endif
this->gpu_disabled = gpu_disabled;
if(gpu_disabled == false){
if(sim::check_CUDA() == false){
std::cout << WARN_MSG << "No GPU Device found! switching to CPU mode." << std::endl;
this->gpu_disabled = gpu_disabled = true;
}
}
if(gpu_disabled == false){
uint32_t device_count = sim::get_device_count();
if (device_id >= device_count){
std::cout << ERR_MSG << "Device ID " << device_id << " is not available! Number of available GPU(s) is " << device_count << " ,switching to CPU mode!" << std::endl;
this->gpu_disabled = gpu_disabled = true;
} else {
BOOST_LOG_TRIVIAL(info) << "Number of available GPU(s): " << device_count;
cudaSetDevice(device_id);
}
}
#endif
}

monte_carlo::~monte_carlo()
Expand Down
9 changes: 3 additions & 6 deletions src/sim/monte_carlo.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ namespace sim {

class monte_carlo {
public:
monte_carlo();
monte_carlo(bool gpu_disabled=false, int32_t device_id=0);
virtual ~monte_carlo();
virtual bool run(std::string config_filename);
virtual void save(std::string filename);
#ifdef __CUDACC__
void set_gpu_disabled(bool disabled){gpu_disabled = disabled;}
#endif

protected:
virtual void allocate_memory();
Expand All @@ -42,6 +39,7 @@ class monte_carlo {
parameters param;
parameters_hvec param_hvec;
parameters_uvec param_uvec;
bool gpu_disabled;

#ifdef __CUDACC__
parameters_dvec param_dvec;
Expand All @@ -52,10 +50,9 @@ class monte_carlo {
thrust::device_vector<float> d_M1; // memory layout(row-major): [n_fov_scale x n_spins x n_TE x 3]
thrust::device_vector<uint8_t> d_T; // memory layout(row-major): [n_fov_scale x n_spins x n_TE x 1]
thrust::device_vector<uint8_t> d_mask;
bool gpu_disabled;
int32_t device_count;
#endif


};


Expand Down
Loading

0 comments on commit bdf64ec

Please sign in to comment.