Loading graph_driver/xrays.cpp +3 −5 Original line number Diff line number Diff line Loading @@ -24,8 +24,6 @@ const bool print_expressions = false; int main(int argc, const char * argv[]) { START_GPU std::mutex sync; typedef float base; //typedef double base; //typedef std::complex<float> base; Loading @@ -45,7 +43,7 @@ int main(int argc, const char * argv[]) { static_cast<unsigned int> (1))); for (size_t i = 0, ie = threads.size(); i < ie; i++) { threads[i] = std::thread([num_times, num_rays, &sync] (const size_t thread_number, threads[i] = std::thread([num_times, num_rays] (const size_t thread_number, const size_t num_threads) -> void { const size_t local_num_rays = num_rays/num_threads + std::min(thread_number, num_rays%num_threads); Loading Loading @@ -89,7 +87,7 @@ int main(int argc, const char * argv[]) { kz->set(static_cast<base> (0.0)); //kz->set(static_cast<base> (10.0)); auto eq = equilibrium::make_efit<base, use_safe_math> (NC_FILE, sync); auto eq = equilibrium::make_efit<base, use_safe_math> (NC_FILE); //auto eq = equilibrium::make_slab_density<base, use_safe_math> (); //auto eq = equilibrium::make_slab_field<base, use_safe_math> (); //auto eq = equilibrium::make_no_magnetic_field<base, use_safe_math> (); Loading graph_framework/cuda_context.hpp +36 −32 Original line number Diff line number Diff line Loading @@ -16,32 +16,6 @@ #include "node.hpp" namespace gpu { //------------------------------------------------------------------------------ /// @brief Initalize cuda. //------------------------------------------------------------------------------ static void init() { cuInit(0); } //------------------------------------------------------------------------------ /// @brief Class representing a cuda gpu context. //------------------------------------------------------------------------------ template<typename T, bool SAFE_MATH=false> class cuda_context { private: /// The cuda device. CUdevice device; /// The cuda context. CUcontext context; /// The cuda code library. CUmodule module; /// Argument map. std::map<graph::leaf_node<T, SAFE_MATH> *, CUdeviceptr> kernel_arguments; /// Result buffer. CUdeviceptr result_buffer; /// Cuda stream. CUstream stream; //------------------------------------------------------------------------------ /// @brief Check results of realtime compile. /// @params[in] name Name of the operation. Loading Loading @@ -72,6 +46,36 @@ namespace gpu { #endif } //------------------------------------------------------------------------------ /// @brief Initalize cuda. //------------------------------------------------------------------------------ static CUresult cuda_init() { const CUresult result = cuInit(0); check_error(result, "cuInit"); return result; } /// Initalize Cuda. static const CUresult result = cuda_init(); //------------------------------------------------------------------------------ /// @brief Class representing a cuda gpu context. //------------------------------------------------------------------------------ template<typename T, bool SAFE_MATH=false> class cuda_context { private: /// The cuda device. CUdevice device; /// The cuda context. CUcontext context; /// The cuda code library. CUmodule module; /// Argument map. std::map<graph::leaf_node<T, SAFE_MATH> *, CUdeviceptr> kernel_arguments; /// Result buffer. CUdeviceptr result_buffer; /// Cuda stream. CUstream stream; //------------------------------------------------------------------------------ /// @brief Check results of async cuda functions. /// Loading graph_framework/equilibrium.hpp +4 −4 Original line number Diff line number Diff line Loading @@ -19,6 +19,9 @@ #include "arithmetic.hpp" namespace equilibrium { /// Lock to syncronize netcdf accross threads. static std::mutex sync; //****************************************************************************** // Equilibrium interface //****************************************************************************** Loading Loading @@ -1108,13 +1111,10 @@ namespace equilibrium { /// @brief Convenience function to build an EFIT equilibrium. /// /// @params[in] spline_file File name of contains the spline functions. /// @params[in,out] sync Mutex to ensure the netcdf file is read only by one /// thread. /// @returns A constructed EFIT equilibrium. //------------------------------------------------------------------------------ template<typename T, bool SAFE_MATH=false> shared<T, SAFE_MATH> make_efit(const std::string spline_file, std::mutex &sync) { shared<T, SAFE_MATH> make_efit(const std::string spline_file) { int ncid; sync.lock(); nc_open(spline_file.c_str(), NC_NOWRITE, &ncid); Loading graph_framework/jit.hpp +4 −14 Original line number Diff line number Diff line Loading @@ -18,24 +18,12 @@ #ifdef USE_METAL #define START_GPU @autoreleasepool { #define END_GPU } #elif defined (USE_CUDA) #define START_GPU jit::init(); #define END_GPU #else #define START_GPU #define END_GPU #endif namespace jit { //------------------------------------------------------------------------------ /// @brief Initalize the gpu\_context. //------------------------------------------------------------------------------ static void init() { #ifdef USE_CUDA gpu::init(); #endif } //------------------------------------------------------------------------------ /// @brief Class for JIT compile of the GPU kernels. //------------------------------------------------------------------------------ Loading Loading @@ -70,7 +58,9 @@ namespace jit { /// @returns The maximum available concurrency. //------------------------------------------------------------------------------ static size_t max_concurrency() { return gpu_context_type::max_concurrency(); const size_t num = gpu_context_type::max_concurrency(); std::cout << "Located " << num << " devices." << std::endl; return num; } //------------------------------------------------------------------------------ Loading graph_framework/output.hpp +18 −1 Original line number Diff line number Diff line Loading @@ -6,11 +6,16 @@ #ifndef output_h #define output_h #include <mutex> #include <netcdf.h> #include "jit.hpp" namespace output { /// Lock to syncronize netcdf accross threads. static std::mutex sync; //------------------------------------------------------------------------------ /// @brief Class representing a netcdf based output file. //------------------------------------------------------------------------------ Loading Loading @@ -50,6 +55,7 @@ namespace output { result_file(const std::string &filename="", const size_t num_rays=0) : num_rays(num_rays) { sync.lock(); nc_create(filename.c_str(), filename.empty() || num_rays == 0 ? NC_DISKLESS : NC_CLOBBER, &ncid); Loading @@ -63,6 +69,7 @@ namespace output { nc_def_dim(ncid, "ray_dim", 1, &ray_dim); nc_def_dim(ncid, "num_rays", num_rays*1, &num_rays_dim); } sync.unlock(); } //------------------------------------------------------------------------------ Loading @@ -85,6 +92,7 @@ namespace output { jit::context<T, SAFE_MATH> &context) { variable var; const std::array<int, 3> dims = {unlimited_dim, num_rays_dim, ray_dim}; sync.lock(); if constexpr (jit::is_float<T> ()) { nc_def_var(ncid, name.c_str(), NC_FLOAT, dims.size(), dims.data(), &var.id); Loading @@ -92,6 +100,7 @@ namespace output { nc_def_var(ncid, name.c_str(), NC_DOUBLE, dims.size(), dims.data(), &var.id); } sync.unlock(); var.buffer = context.get_buffer(node); Loading @@ -102,7 +111,9 @@ namespace output { /// @brief End define mode. //------------------------------------------------------------------------------ void end_define_mode() const { sync.lock(); nc_enddef(ncid); sync.unlock(); } //------------------------------------------------------------------------------ Loading @@ -110,9 +121,12 @@ namespace output { //------------------------------------------------------------------------------ void write() { size_t size; sync.lock(); nc_inq_dimlen(ncid, unlimited_dim, &size); sync.unlock(); const std::array<size_t, 3> start = {size, 0, 0}; for (variable &var : variables) { sync.lock(); if constexpr (jit::is_float<T> ()) { if constexpr (jit::is_complex<T> ()) { const std::array<size_t, 3> count = {1, num_rays, 2}; Loading @@ -134,9 +148,12 @@ namespace output { var.buffer); } } sync.unlock(); } sync.lock(); nc_sync(ncid); sync.unlock(); } }; } Loading Loading
graph_driver/xrays.cpp +3 −5 Original line number Diff line number Diff line Loading @@ -24,8 +24,6 @@ const bool print_expressions = false; int main(int argc, const char * argv[]) { START_GPU std::mutex sync; typedef float base; //typedef double base; //typedef std::complex<float> base; Loading @@ -45,7 +43,7 @@ int main(int argc, const char * argv[]) { static_cast<unsigned int> (1))); for (size_t i = 0, ie = threads.size(); i < ie; i++) { threads[i] = std::thread([num_times, num_rays, &sync] (const size_t thread_number, threads[i] = std::thread([num_times, num_rays] (const size_t thread_number, const size_t num_threads) -> void { const size_t local_num_rays = num_rays/num_threads + std::min(thread_number, num_rays%num_threads); Loading Loading @@ -89,7 +87,7 @@ int main(int argc, const char * argv[]) { kz->set(static_cast<base> (0.0)); //kz->set(static_cast<base> (10.0)); auto eq = equilibrium::make_efit<base, use_safe_math> (NC_FILE, sync); auto eq = equilibrium::make_efit<base, use_safe_math> (NC_FILE); //auto eq = equilibrium::make_slab_density<base, use_safe_math> (); //auto eq = equilibrium::make_slab_field<base, use_safe_math> (); //auto eq = equilibrium::make_no_magnetic_field<base, use_safe_math> (); Loading
graph_framework/cuda_context.hpp +36 −32 Original line number Diff line number Diff line Loading @@ -16,32 +16,6 @@ #include "node.hpp" namespace gpu { //------------------------------------------------------------------------------ /// @brief Initalize cuda. //------------------------------------------------------------------------------ static void init() { cuInit(0); } //------------------------------------------------------------------------------ /// @brief Class representing a cuda gpu context. //------------------------------------------------------------------------------ template<typename T, bool SAFE_MATH=false> class cuda_context { private: /// The cuda device. CUdevice device; /// The cuda context. CUcontext context; /// The cuda code library. CUmodule module; /// Argument map. std::map<graph::leaf_node<T, SAFE_MATH> *, CUdeviceptr> kernel_arguments; /// Result buffer. CUdeviceptr result_buffer; /// Cuda stream. CUstream stream; //------------------------------------------------------------------------------ /// @brief Check results of realtime compile. /// @params[in] name Name of the operation. Loading Loading @@ -72,6 +46,36 @@ namespace gpu { #endif } //------------------------------------------------------------------------------ /// @brief Initalize cuda. //------------------------------------------------------------------------------ static CUresult cuda_init() { const CUresult result = cuInit(0); check_error(result, "cuInit"); return result; } /// Initalize Cuda. static const CUresult result = cuda_init(); //------------------------------------------------------------------------------ /// @brief Class representing a cuda gpu context. //------------------------------------------------------------------------------ template<typename T, bool SAFE_MATH=false> class cuda_context { private: /// The cuda device. CUdevice device; /// The cuda context. CUcontext context; /// The cuda code library. CUmodule module; /// Argument map. std::map<graph::leaf_node<T, SAFE_MATH> *, CUdeviceptr> kernel_arguments; /// Result buffer. CUdeviceptr result_buffer; /// Cuda stream. CUstream stream; //------------------------------------------------------------------------------ /// @brief Check results of async cuda functions. /// Loading
graph_framework/equilibrium.hpp +4 −4 Original line number Diff line number Diff line Loading @@ -19,6 +19,9 @@ #include "arithmetic.hpp" namespace equilibrium { /// Lock to syncronize netcdf accross threads. static std::mutex sync; //****************************************************************************** // Equilibrium interface //****************************************************************************** Loading Loading @@ -1108,13 +1111,10 @@ namespace equilibrium { /// @brief Convenience function to build an EFIT equilibrium. /// /// @params[in] spline_file File name of contains the spline functions. /// @params[in,out] sync Mutex to ensure the netcdf file is read only by one /// thread. /// @returns A constructed EFIT equilibrium. //------------------------------------------------------------------------------ template<typename T, bool SAFE_MATH=false> shared<T, SAFE_MATH> make_efit(const std::string spline_file, std::mutex &sync) { shared<T, SAFE_MATH> make_efit(const std::string spline_file) { int ncid; sync.lock(); nc_open(spline_file.c_str(), NC_NOWRITE, &ncid); Loading
graph_framework/jit.hpp +4 −14 Original line number Diff line number Diff line Loading @@ -18,24 +18,12 @@ #ifdef USE_METAL #define START_GPU @autoreleasepool { #define END_GPU } #elif defined (USE_CUDA) #define START_GPU jit::init(); #define END_GPU #else #define START_GPU #define END_GPU #endif namespace jit { //------------------------------------------------------------------------------ /// @brief Initalize the gpu\_context. //------------------------------------------------------------------------------ static void init() { #ifdef USE_CUDA gpu::init(); #endif } //------------------------------------------------------------------------------ /// @brief Class for JIT compile of the GPU kernels. //------------------------------------------------------------------------------ Loading Loading @@ -70,7 +58,9 @@ namespace jit { /// @returns The maximum available concurrency. //------------------------------------------------------------------------------ static size_t max_concurrency() { return gpu_context_type::max_concurrency(); const size_t num = gpu_context_type::max_concurrency(); std::cout << "Located " << num << " devices." << std::endl; return num; } //------------------------------------------------------------------------------ Loading
graph_framework/output.hpp +18 −1 Original line number Diff line number Diff line Loading @@ -6,11 +6,16 @@ #ifndef output_h #define output_h #include <mutex> #include <netcdf.h> #include "jit.hpp" namespace output { /// Lock to syncronize netcdf accross threads. static std::mutex sync; //------------------------------------------------------------------------------ /// @brief Class representing a netcdf based output file. //------------------------------------------------------------------------------ Loading Loading @@ -50,6 +55,7 @@ namespace output { result_file(const std::string &filename="", const size_t num_rays=0) : num_rays(num_rays) { sync.lock(); nc_create(filename.c_str(), filename.empty() || num_rays == 0 ? NC_DISKLESS : NC_CLOBBER, &ncid); Loading @@ -63,6 +69,7 @@ namespace output { nc_def_dim(ncid, "ray_dim", 1, &ray_dim); nc_def_dim(ncid, "num_rays", num_rays*1, &num_rays_dim); } sync.unlock(); } //------------------------------------------------------------------------------ Loading @@ -85,6 +92,7 @@ namespace output { jit::context<T, SAFE_MATH> &context) { variable var; const std::array<int, 3> dims = {unlimited_dim, num_rays_dim, ray_dim}; sync.lock(); if constexpr (jit::is_float<T> ()) { nc_def_var(ncid, name.c_str(), NC_FLOAT, dims.size(), dims.data(), &var.id); Loading @@ -92,6 +100,7 @@ namespace output { nc_def_var(ncid, name.c_str(), NC_DOUBLE, dims.size(), dims.data(), &var.id); } sync.unlock(); var.buffer = context.get_buffer(node); Loading @@ -102,7 +111,9 @@ namespace output { /// @brief End define mode. //------------------------------------------------------------------------------ void end_define_mode() const { sync.lock(); nc_enddef(ncid); sync.unlock(); } //------------------------------------------------------------------------------ Loading @@ -110,9 +121,12 @@ namespace output { //------------------------------------------------------------------------------ void write() { size_t size; sync.lock(); nc_inq_dimlen(ncid, unlimited_dim, &size); sync.unlock(); const std::array<size_t, 3> start = {size, 0, 0}; for (variable &var : variables) { sync.lock(); if constexpr (jit::is_float<T> ()) { if constexpr (jit::is_complex<T> ()) { const std::array<size_t, 3> count = {1, num_rays, 2}; Loading @@ -134,9 +148,12 @@ namespace output { var.buffer); } } sync.unlock(); } sync.lock(); nc_sync(ncid); sync.unlock(); } }; } Loading