Skip to content

Commit

Permalink
boost argument parser
Browse files Browse the repository at this point in the history
  • Loading branch information
aghaeifar committed Mar 11, 2024
1 parent 20509b7 commit 20e2030
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ endif()

# Find OpenMP
find_package(OpenMP)

# Boost
find_package(Boost COMPONENTS program_options 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 @@ -29,6 +30,10 @@ set_property(TARGET ${target} PROPERTY CUDA_ARCHITECTURES native)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_CXX_STANDARD 17)

set(CMAKE_CXX_FLAGS "-lboost_program_options")


include_directories(${Boost_INCLUDE_DIRS})

# Add CUDA include directories
include_directories(./include)
Expand All @@ -40,7 +45,7 @@ set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} ${CUDA_ARCH} -Xcompiler=${OpenMP_CXX_F
add_executable(spinwalk ./src/spinwalk.cu ./src/kernels.cu)

# Link CUDA and OpenMP libraries
target_link_libraries(spinwalk ${CUDA_LIBRARIES} ${OpenMP_CXX_LIBRARIES})
target_link_libraries(spinwalk ${CUDA_LIBRARIES} ${OpenMP_CXX_LIBRARIES} ${Boost_LIBRARIES})

if (UNIX)
install(TARGETS spinwalk DESTINATION bin)
Expand Down
8 changes: 7 additions & 1 deletion src/miscellaneous.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
#include <fstream>
#include <vector>


#define SPINWALK_VERSION_MAJOR 1
#define SPINWALK_VERSION_MINOR 4
#define SPINWALK_VERSION_PATCH 5

#define DEG2RAD 0.0174532925199433 // = M_PI/180
#define RAD2DEG 57.2957795130823


#define ERR_MSG "\033[1;31mError:\033[0m "
#define ROUND(x) ((long)((x)+0.5))
#define MAX_RF 256 // maximum number of RF
Expand Down Expand Up @@ -123,6 +127,8 @@ inline void print_logo()
" ___) | | |_) | | | | | | | \\ V V / | (_| | | | | < \n"
"|____/ | .__/ |_| |_| |_| \\_/\\_/ \\__,_| |_| |_|\\_\\ \n"
" |_| \n\n";

std::cout << "SpinWalk ver. " << SPINWALK_VERSION_MAJOR << "." << SPINWALK_VERSION_MINOR << "." << SPINWALK_VERSION_PATCH << std::endl;
}

#endif // __MISCELLANEOUS_H__
48 changes: 20 additions & 28 deletions src/spinwalk.cu
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
#include <random>
#include <filesystem>
#include <iomanip>
#include <boost/program_options.hpp>
#include "helper_cuda.h"
#include "kernels.cuh"
#include "file_utils.h"
#include "tqdm.h"

#define THREADS_PER_BLOCK 64

#define SPINWALK_VERSION_MAJOR 1
#define SPINWALK_VERSION_MINOR 4
#define SPINWALK_VERSION_PATCH 4

using namespace std;

bool simulate(simulation_parameters param, std::map<std::string, std::vector<std::string> > filenames, std::vector<float> sample_length_scales)
Expand Down Expand Up @@ -219,36 +216,31 @@ bool simulate(simulation_parameters param, std::map<std::string, std::vector<std
int main(int argc, char * argv[])
{
print_logo();
std::cout << "SpinWalk ver. " << SPINWALK_VERSION_MAJOR << "." << SPINWALK_VERSION_MINOR << "." << SPINWALK_VERSION_PATCH << std::endl;
// ========== parse command line arguments ==========
std::vector<std::string> config_files;
bool bVerbose = false, bHelp = false, bNoSim = false;
for(uint8_t i=1; i<argc; i++)
{
if (strcmp(argv[i], "-v") == 0)
bVerbose = true;
else if (strcmp(argv[i], "-h") == 0)
bHelp = true;
else if (strcmp(argv[i], "-n") == 0)
bNoSim = true;
else
config_files.push_back(argv[i]);
}

boost::program_options::options_description desc("Options");
desc.add_options()
("help,h", "help message (this menu)")
("verbose,v", "enable verbose mode")
("sim_off,s", "no simulation, only read config files")
("configs,c", boost::program_options::value<std::vector<std::string>>()->multitoken(), "config. files as many as you want. e.g. -c config1.ini config2.ini ... configN.ini");

boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(desc).allow_unregistered().run(), vm);
boost::program_options::notify(vm);
// ========== print help ==========
if(argc < 2 || bHelp || config_files.size() == 0)
if (vm.count("help") || vm.count("configs") == 0 || argc == 1)
{
std::cout << "Usage: " << argv[0] << " -options <config_file1> <config_file2> ... <config_filen>" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " -v: verbose" << std::endl;
std::cout << " -n: only read config" << std::endl;
std::cout << " -h: help (this menu)" << std::endl;
std::cout << desc;
print_device_info();
return 1;
return 1;
}

std::vector<std::string> config_files = vm["configs"].as<std::vector<std::string>>();
bool bVerbose = vm.count("verbose") > 0;
bool bNoSim = vm.count("sim_off") > 0;

std::cout << "Running simulation for " << config_files.size() << " config(s)..." << std::endl;
for(uint8_t cnf=0; cnf<config_files.size(); cnf++)
for(const auto& cfile : config_files)
{
map<string, vector<string> > filenames = {{"fieldmap", vector<string>()}, // input: map of off-resonance in Tesla
{"xyz0", vector<string>()}, // input: spins starting spatial positions in meters
Expand All @@ -262,7 +254,7 @@ int main(int argc, char * argv[])
// ========== read config file ==========
param.fieldmap_size[0] = param.fieldmap_size[1] = param.fieldmap_size[2] = 0;
param.sample_length[0] = param.sample_length[1] = param.sample_length[2] = 0.f;
if(file_utils::read_config(config_files[cnf], param, sample_length_scales, filenames) == false)
if(file_utils::read_config(cfile, param, sample_length_scales, filenames) == false)
{
std::cout << ERR_MSG << "reading config file failed. Aborting...!" << std::endl;
return 1;
Expand Down

0 comments on commit 20e2030

Please sign in to comment.