Skip to content

Commit

Permalink
logging with boost
Browse files Browse the repository at this point in the history
  • Loading branch information
aghaeifar committed Mar 12, 2024
1 parent 20e2030 commit 4af9a97
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 487 deletions.
10 changes: 4 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ endif()
# Find OpenMP
find_package(OpenMP)
# Boost
find_package(Boost COMPONENTS program_options REQUIRED)
find_package(Boost COMPONENTS program_options log log_setup REQUIRED)
# Set CUDA architecture (change according to your GPU)
# set(CUDA_ARCH "-arch=sm_75" CACHE STRING "CUDA architecture")
set_property(TARGET ${target} PROPERTY CUDA_ARCHITECTURES native)
Expand All @@ -32,17 +32,15 @@ set(CMAKE_CXX_STANDARD 17)

set(CMAKE_CXX_FLAGS "-lboost_program_options")

# Add Boost and CUDA include directories
include_directories(${Boost_INCLUDE_DIRS} ./include)

include_directories(${Boost_INCLUDE_DIRS})

# Add CUDA include directories
include_directories(./include)

# Add CUDA and OpenMP flags
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} ${CUDA_ARCH} -Xcompiler=${OpenMP_CXX_FLAGS}")

# Add the executable
add_executable(spinwalk ./src/spinwalk.cu ./src/kernels.cu)
add_executable(spinwalk ./src/spinwalk.cu ./src/kernels.cu ./src/file_utils.cpp)

# Link CUDA and OpenMP libraries
target_link_libraries(spinwalk ${CUDA_LIBRARIES} ${OpenMP_CXX_LIBRARIES} ${Boost_LIBRARIES})
Expand Down
1 change: 1 addition & 0 deletions config/config_default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ B0 = 9.4
SEED = 0
NUMBER_OF_SPINS = 1e5
CROSS_BOUNDARY = 1
RECORD_TRAJECTORY = 1
; m^2/Sec.
DIFFUSION_CONSTANT = 1e-9
SAMPLE_LENGTH_SCALES[0] = 0.0093691
Expand Down
405 changes: 6 additions & 399 deletions src/file_utils.h

Large diffs are not rendered by default.

35 changes: 30 additions & 5 deletions src/kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "kernels.cuh"
#include "rotation.cuh"
#include "helper_cuda.h"


#include <cuda_runtime.h>
#include <boost/log/trivial.hpp>
//---------------------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -223,8 +223,8 @@ void print_device_info()
const int mb = kb * kb;
size_t free, total;

int32_t device_count, cuda_version, driver_version;
checkCudaErrors(cudaGetDeviceCount(&device_count));
int32_t cuda_version, driver_version;
int32_t device_count = getDeviceCount();
cudaRuntimeGetVersion(&cuda_version);
cudaDriverGetVersion(&driver_version);
std::cout << "\nDriver version: "<< driver_version << ", CUDA version: "<< cuda_version << ", Number of devices: " << device_count << std::endl;
Expand All @@ -239,4 +239,29 @@ void print_device_info()
cudaMemGetInfo(&free, &total);
std::cout << "Free GPU memory: " << free / mb << " MB (out of " << total / mb << " MB)" << std::endl;
}
}
}

uint32_t getDeviceCount()
{
int32_t device_count;
checkCudaErrors(cudaGetDeviceCount(&device_count));
return device_count;
}

bool check_memory_size(size_t required_size_MB)
{
size_t free, total;
bool memory_ok = true;
int32_t device_count = getDeviceCount();
cudaDeviceProp device_properties;
std::cout << "Device(s) memeory check:" << '\n';
for(int i=0; i<device_count; i++)
{
cudaSetDevice(i);
cudaGetDeviceProperties(&device_properties, i);
cudaMemGetInfo(&free, &total);
std::cout << " Device " << i+1 << ", " << device_properties.name << ": " << (free>required_size_MB ? "OK" : "Not enough") << '\n';
memory_ok = free<required_size_MB ? false:memory_ok;
}
return memory_ok;
}
3 changes: 2 additions & 1 deletion src/kernels.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ __host__ __device__ __forceinline__ uint64_t sub2ind(uint32_t x, uint32_t y, ui
return (uint64_t((z-1)*lenx*leny) + (y-1)*lenx + x-1); // the last -1 is because of the C++ indexing starts from 0
}

uint32_t getDeviceCount();
void print_device_info();

bool check_memory_size(size_t required_size_MB);

template <typename T>
bool is_masked(std::vector<T> &XYZ0, std::vector<uint8_t> &mask, simulation_parameters *param)
Expand Down
80 changes: 47 additions & 33 deletions src/miscellaneous.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
#ifndef __MISCELLANEOUS_H__
#define __MISCELLANEOUS_H__

#include <cuda_runtime.h>

#include <algorithm>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

#include <cstring>
#include <cmath>

#define SPINWALK_VERSION_MAJOR 1
#define SPINWALK_VERSION_MINOR 4
Expand Down Expand Up @@ -46,7 +48,7 @@ typedef struct simulation_parameters
int32_t n_dummy_scan;
uint32_t n_spins, fieldmap_size[3], seed;
uint64_t matrix_length;
bool enDebug, enCrossBoundry, enMultiTissue;
bool enDebug, enCrossBoundry, enMultiTissue, enRecordTrajectory;
simulation_parameters():
TR(0.04),
dt(5e-5),
Expand All @@ -60,7 +62,8 @@ typedef struct simulation_parameters
phase_cycling(0.),
enDebug(false),
enCrossBoundry(true),
enMultiTissue(false)
enMultiTissue(false),
enRecordTrajectory(false)
{
memset(fieldmap_size, 0, 3*sizeof(fieldmap_size[0]));
memset(sample_length, 0, 3*sizeof(sample_length[0]));
Expand All @@ -77,35 +80,46 @@ typedef struct simulation_parameters
std::fill(T2, T2 + MAX_T12, 0.04);
}

void dump()
std::string dump()
{
std::stringstream ss;
ss<<"TR="<<TR<<" dt="<<dt<<" B0="<<B0<<'\n';
ss<<"T1 = "; for(int i=0; i<n_T12; i++) ss<<T1[i]<<' '; ss<<'\n';
ss<<"T2 = "; for(int i=0; i<n_T12; i++) ss<<T2[i]<<' '; ss<<'\n';
ss<<"TE = "; for(int i=0; i<n_TE; i++) ss<<TE[i]*dt<<' '; ss<<'\n';

ss<<"RF flip-angle = "; for(int i=0; i<n_RF; i++) ss<<RF_FA[i]<<' '; ss<<'\n';
ss<<"RF phase = "; for(int i=0; i<n_RF; i++) ss<<RF_PH[i]<<' '; ss<<'\n';
ss<<"RF time = "; for(int i=0; i<n_RF; i++) ss<<RF_ST[i]*dt<<' '; ss<<'\n';

ss<<"dephasing deg. = "; for(int i=0; i<n_dephasing; i++) ss<<dephasing[i]<<' '; ss<<'\n';
ss<<"dephasing time = "; for(int i=0; i<n_dephasing; i++) ss<<dephasing_T[i]*dt<<' '; ss<<'\n';
ss<<"gradient (x,y,z)=\n"; for(int i=0; i<n_gradient; i++) ss<<gradient_xyz[3*i+0]<<' '<<gradient_xyz[3*i+1]<<' '<<gradient_xyz[3*i+2]<<'\n';
ss<<"gradient time = "; for(int i=0; i<n_gradient; i++) ss<<gradient_T[i]*dt<<' '; ss<<'\n';

ss<<"sample length = "<< sample_length[0] << " x " << sample_length[1] << " x " << sample_length[2] << " m" << '\n';
ss<<"scale2grid = "<< scale2grid[0] << " x " << scale2grid[1] << " x " << scale2grid[2] << '\n';
ss<<"fieldmap size = "<< fieldmap_size[0] << " x " << fieldmap_size[1] << " x " << fieldmap_size[2] << '\n';
ss<<"diffusion const = "<<diffusion_const<<'\t'<<"dummy scans = "<<n_dummy_scan<<'\t'<<"spins = "<<n_spins<<'\n';
ss<<"samples scales = "<<n_sample_length_scales<<'\t'<<"timepoints = "<<n_timepoints<<'\t'<<"fieldmaps = "<<n_fieldmaps<<'\n';
ss<<"Multi-Tissues = "<<enMultiTissue<<'\t'<<"Boundry Condition = " << enCrossBoundry << '\n';
ss<<"Phase cycling = "<<phase_cycling<<'\t'<<"Seed = "<<seed<<'\n';
ss<<'\n';

auto fieldmap_size_MB = fieldmap_size[0] * fieldmap_size[1] * fieldmap_size[2] * (sizeof(float) + sizeof(char)) / 1024 / 1024;
auto variables_size_MB = n_spins * 3 * (4 + n_TE) * sizeof(float) / 1024 / 1024;
ss<<"Required GPU memory ≈ " << fieldmap_size_MB << " MB + " << variables_size_MB << " MB (fieldmap + variables)" << '\n';
ss<<"Required RAM ≈ " << fieldmap_size_MB << " MB + " << variables_size_MB * n_sample_length_scales << " MB (fieldmap + variables)" << '\n';

return ss.str();
}

uint32_t get_required_memory(uint32_t n_device=1)
{
std::cout<<"TR="<<TR<<" dt="<<dt<<" B0="<<B0<<'\n';
std::cout<<"T1 = "; for(int i=0; i<n_T12; i++) std::cout<<T1[i]<<' '; std::cout<<'\n';
std::cout<<"T2 = "; for(int i=0; i<n_T12; i++) std::cout<<T2[i]<<' '; std::cout<<'\n';
std::cout<<"TE = "; for(int i=0; i<n_TE; i++) std::cout<<TE[i]*dt<<' '; std::cout<<'\n';

std::cout<<"RF flip-angle = "; for(int i=0; i<n_RF; i++) std::cout<<RF_FA[i]<<' '; std::cout<<'\n';
std::cout<<"RF phase = "; for(int i=0; i<n_RF; i++) std::cout<<RF_PH[i]<<' '; std::cout<<'\n';
std::cout<<"RF time = "; for(int i=0; i<n_RF; i++) std::cout<<RF_ST[i]*dt<<' '; std::cout<<'\n';

std::cout<<"dephasing deg. = "; for(int i=0; i<n_dephasing; i++) std::cout<<dephasing[i]<<' '; std::cout<<'\n';
std::cout<<"dephasing time = "; for(int i=0; i<n_dephasing; i++) std::cout<<dephasing_T[i]*dt<<' '; std::cout<<'\n';
std::cout<<"gradient (x,y,z)=\n"; for(int i=0; i<n_gradient; i++) std::cout<<gradient_xyz[3*i+0]<<' '<<gradient_xyz[3*i+1]<<' '<<gradient_xyz[3*i+2]<<'\n';
std::cout<<"gradient time = "; for(int i=0; i<n_gradient; i++) std::cout<<gradient_T[i]*dt<<' '; std::cout<<'\n';

std::cout<<"sample length = "<< sample_length[0] << " x " << sample_length[1] << " x " << sample_length[2] << " m" << '\n';
std::cout<<"scale2grid = "<< scale2grid[0] << " x " << scale2grid[1] << " x " << scale2grid[2] << '\n';
std::cout<<"fieldmap size = "<< fieldmap_size[0] << " x " << fieldmap_size[1] << " x " << fieldmap_size[2] << '\n';
std::cout<<"diffusion const = "<<diffusion_const<<'\t'<<"dummy scans = "<<n_dummy_scan<<'\t'<<"spins = "<<n_spins<<'\n';
std::cout<<"samples scales = "<<n_sample_length_scales<<'\t'<<"timepoints = "<<n_timepoints<<'\t'<<"fieldmaps = "<<n_fieldmaps<<'\n';
std::cout<<"Multi-Tissues = "<<enMultiTissue<<'\t'<<"Boundry Condition = " << enCrossBoundry << '\n';
std::cout<<"Phase cycling = "<<phase_cycling<<'\t'<<"Seed = "<<seed<<'\n';
std::cout<<'\n';

uint16_t fieldmap_size_MB = fieldmap_size[0] * fieldmap_size[1] * fieldmap_size[2] * (sizeof(float) + sizeof(char)) / 1024 / 1024;
uint16_t variables_size_MB = n_spins * 3 * (4 + n_TE) * sizeof(float) / 1024 / 1024;
std::cout<<"Required GPU memory ≈ " << fieldmap_size_MB << " MB + " << variables_size_MB << " MB (fieldmap + variables)" << '\n';
std::cout<<"Required RAM ≈ " << fieldmap_size_MB << " MB + " << variables_size_MB * n_sample_length_scales << " MB (fieldmap + variables)" << '\n';
auto fieldmap_mask_size_MB = fieldmap_size[0] * fieldmap_size[1] * fieldmap_size[2] * (sizeof(float) + sizeof(char)) / 1024 / 1024;
auto variables_size_MB = n_spins * 3 * (4 + n_TE) * sizeof(float) / 1024 / 1024 / n_device; // 3 for x,y,z, 4 for M0, XYZ0, XYZ0_scaled, XYZ1, n_TE for M1
auto trajectory_size_MB = enRecordTrajectory ? n_spins * n_timepoints * 3 * sizeof(float) / 1024 / 1024 / n_device: 0; // 3 for x,y,z,
return fieldmap_mask_size_MB + variables_size_MB + trajectory_size_MB;
}

void prepare()
Expand All @@ -120,7 +134,7 @@ typedef struct simulation_parameters

inline void print_logo()
{
std::cout << " \n"
std::cout << " \n"
" ____ _ __ __ _ _ \n"
"/ ___| _ __ (_) _ __ \\ \\ / / __ _ | | | | __ \n"
"\\___ \\ | '_ \\ | | | '_ \\ \\ \\ /\\ / / / _` | | | | |/ / \n"
Expand Down
Loading

0 comments on commit 4af9a97

Please sign in to comment.