From 25e05907f0292310eaae27a032db0ee274413874 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Tue, 2 Jul 2024 19:46:02 +0200 Subject: Preparing benchmark work --- modules/remote-sycl/.nix/derivation.nix | 7 +- modules/remote-sycl/SConstruct | 10 +- modules/remote-sycl/benchmarks/SConscript | 39 ++++ .../benchmarks/kernel_mixed_precision.cpp | 42 ++++ modules/remote-sycl/benchmarks/mixed_precision.cpp | 110 +++++++++++ modules/remote-sycl/benchmarks/mixed_precision.hpp | 27 +++ modules/remote-sycl/c++/common.hpp | 7 +- modules/remote-sycl/c++/data.hpp | 8 +- modules/remote-sycl/c++/device.hpp | 4 +- modules/remote-sycl/c++/transfer.hpp | 3 +- modules/remote-sycl/tests/mixed_precision.cpp | 212 +++++++++++++++++++++ modules/remote-sycl/tests/sycl_basics.cpp | 9 - 12 files changed, 454 insertions(+), 24 deletions(-) create mode 100644 modules/remote-sycl/benchmarks/SConscript create mode 100644 modules/remote-sycl/benchmarks/kernel_mixed_precision.cpp create mode 100644 modules/remote-sycl/benchmarks/mixed_precision.cpp create mode 100644 modules/remote-sycl/benchmarks/mixed_precision.hpp create mode 100644 modules/remote-sycl/tests/mixed_precision.cpp (limited to 'modules/remote-sycl') diff --git a/modules/remote-sycl/.nix/derivation.nix b/modules/remote-sycl/.nix/derivation.nix index 71c5dff..ba6d1ec 100644 --- a/modules/remote-sycl/.nix/derivation.nix +++ b/modules/remote-sycl/.nix/derivation.nix @@ -12,6 +12,7 @@ , bash , build_examples ? "false" +, build_benchmarks ? "false" }: let @@ -33,8 +34,6 @@ in stdenv.mkDerivation { forstio.core forstio.codec forstio.async - forstio.io - forstio.io_codec keldu.adaptivecpp-dev ocl-icd openmp @@ -42,11 +41,11 @@ in stdenv.mkDerivation { ]; buildPhase = '' - scons build_examples=${build_examples} + scons build_benchmarks=${build_benchmarks} build_examples=${build_examples} ''; installPhase = '' - scons prefix=$out build_examples=${build_examples} install + scons prefix=$out build_benchmarks=${build_benchmarks} build_examples=${build_examples} install ''; doCheck = true; diff --git a/modules/remote-sycl/SConstruct b/modules/remote-sycl/SConstruct index 96e9df9..40344e7 100644 --- a/modules/remote-sycl/SConstruct +++ b/modules/remote-sycl/SConstruct @@ -50,14 +50,19 @@ env_vars.Add( ) ); +env_vars.Add( + BoolVariable('build_benchmarks', + help='Build benchmarks', + default=False + ) +); + env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], CPPDEFINES=['SAW_UNIX'], CXXFLAGS=['-std=c++20','-g','-Wall','-Wextra'], LIBS=['forstio-core' ,'forstio-codec' ,'forstio-async' - ,'forstio-io' - ,'forstio-io_codec' ,'acpp-rt' ,'OpenCL' ,'omp' @@ -76,6 +81,7 @@ Export('env') SConscript('c++/SConscript') SConscript('tests/SConscript') SConscript('examples/SConscript') +SConscript('benchmarks/SConscript') env.Alias('cdb', env.cdb); env.Alias('all', [env.targets]); diff --git a/modules/remote-sycl/benchmarks/SConscript b/modules/remote-sycl/benchmarks/SConscript new file mode 100644 index 0000000..9976e0e --- /dev/null +++ b/modules/remote-sycl/benchmarks/SConscript @@ -0,0 +1,39 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +benchmarks_env = env.Clone(); + + +benchmarks_sycl_env = benchmarks_env.Clone(); +benchmarks_sycl_env['CXX'] = 'acpp'; +benchmarks_sycl_env['CXXFLAGS'] += ['-O2']; + +benchmarks_env.sources = sorted(glob.glob(dir_path + "/*.cpp")) +benchmarks_env.headers = sorted(glob.glob(dir_path + "/*.hpp")) + +env.sources += benchmarks_env.sources; +env.headers += benchmarks_env.headers; + +sycl_objects = []; +benchmarks_sycl_env.add_source_files(sycl_objects, ['kernel_mixed_precision.cpp'], shared=True); + +objects_static = [] +benchmarks_env.sycl_basic = benchmarks_env.Program('#bin/benchmark_mixed_precision', ['mixed_precision.cpp', env.library_static, sycl_objects]); + +# Set Alias +env.benchmarks = [benchmarks_env.sycl_basic]; +env.Alias('benchmarks', env.benchmarks); + +if env["build_benchmarks"]: + env.targets += ['benchmarks']; + env.Install('$prefix/bin/', env.benchmarks); +#endif diff --git a/modules/remote-sycl/benchmarks/kernel_mixed_precision.cpp b/modules/remote-sycl/benchmarks/kernel_mixed_precision.cpp new file mode 100644 index 0000000..0ac9756 --- /dev/null +++ b/modules/remote-sycl/benchmarks/kernel_mixed_precision.cpp @@ -0,0 +1,42 @@ +#include "mixed_precision.hpp" + +saw::interface listen_mixed_precision(cl::sycl::event& mixed_ev, cl::sycl::event& float64_ev, cl::sycl::event& float32_ev){ + return { + /** + * Mixed + */ + [&](saw::data& in, cl::sycl::queue* cmd) -> saw::error_or { + + mixed_ev = cmd->submit([&](cl::sycl::handler& h){ + auto acc_buff = in.template access(h); + + h.parallel_for(cl::sycl::range<1>(in.size()), [=] (cl::sycl::id<1> it){ + acc_buff[0u].at(it[0u]) = acc_buff[0u].at(it[0u]) * saw::data{2.0}; + }); + }); + return saw::void_t{}; + }, + [&](saw::data& in, cl::sycl::queue* cmd) -> saw::error_or { + + float64_ev = cmd->submit([&](cl::sycl::handler& h){ + auto acc_buff = in.template access(h); + + h.parallel_for(cl::sycl::range<1>(in.size()), [=] (cl::sycl::id<1> it){ + acc_buff[0u].at(it[0u]) = acc_buff[0u].at(it[0u]) * saw::data{2.0}; + }); + }); + return saw::void_t{}; + }, + [&](saw::data& in, cl::sycl::queue* cmd) -> saw::error_or { + + float32_ev = cmd->submit([&](cl::sycl::handler& h){ + auto acc_buff = in.template access(h); + + h.parallel_for(cl::sycl::range<1>(in.size()), [=] (cl::sycl::id<1> it){ + acc_buff[0u].at(it[0u]) = acc_buff[0u].at(it[0u]) * saw::data{2.0f}; + }); + }); + return saw::void_t{}; + } + }; +} diff --git a/modules/remote-sycl/benchmarks/mixed_precision.cpp b/modules/remote-sycl/benchmarks/mixed_precision.cpp new file mode 100644 index 0000000..e63a814 --- /dev/null +++ b/modules/remote-sycl/benchmarks/mixed_precision.cpp @@ -0,0 +1,110 @@ +#include "./mixed_precision.hpp" +#include + + +int main(){ + using namespace saw; + + constexpr uint64_t max_test_size = 1024ul * 1024ul * 512ul; + + std::random_device r; + std::default_random_engine e1{r()}; + std::uniform_real_distribution<> dis{-1.0,1.0}; + + + saw::event_loop loop; + saw::wait_scope wait{loop}; + + remote rmt; + + own> rmt_addr{}; + + rmt.resolve_address().then([&](auto addr){ + rmt_addr = std::move(addr); + }).detach(); + + wait.poll(); + if(!rmt_addr){ + return -1; + } + + data mixed_host_data; + data float64_host_data; + data float32_host_data; + + cl::sycl::event mixed_ev; + cl::sycl::event float32_ev; + cl::sycl::event float64_ev; + + auto sycl_iface = listen_mixed_precision(mixed_ev, float64_ev, float32_ev); + + auto time_eval = [](std::string_view tag, cl::sycl::event& ev){ + auto end = ev.get_profiling_info(); + auto start = ev.get_profiling_info(); + + std::cout<<"Elapsed "<get_device(); + + /** + * Warmup + */ + { + uint64_t test_size = max_test_size; + mixed_host_data = {test_size}; + float64_host_data = {test_size}; + float32_host_data = {test_size}; + for(uint64_t i = 0; i < test_size; ++i){ + double gen_num = dis(e1); + mixed_host_data.at(i) = static_cast(gen_num); + float64_host_data.at(i) = static_cast(gen_num); + float32_host_data.at(i) = static_cast(gen_num); + } + data mixed_device_data{mixed_host_data}; + data float64_device_data{float64_host_data}; + data float32_device_data{float32_host_data}; + + sycl_iface.template call<"float64_32">(mixed_device_data, &(device.get_handle())); + device.get_handle().wait(); + sycl_iface.template call<"float64">(float64_device_data, &(device.get_handle())); + device.get_handle().wait(); + sycl_iface.template call<"float32">(float32_device_data, &(device.get_handle())); + device.get_handle().wait(); + + } + + /** + * Benchmark + */ + for(uint64_t test_size = 1ul; test_size < max_test_size; test_size *= 2ul){ + device.get_handle().wait(); + mixed_host_data = {test_size}; + float64_host_data = {test_size}; + float32_host_data = {test_size}; + for(uint64_t i = 0; i < test_size; ++i){ + double gen_num = dis(e1); + mixed_host_data.at(i) = static_cast(gen_num); + float64_host_data.at(i) = static_cast(gen_num); + float32_host_data.at(i) = static_cast(gen_num); + } + data mixed_device_data{mixed_host_data}; + data float64_device_data{float64_host_data}; + data float32_device_data{float32_host_data}; + + sycl_iface.template call<"float64_32">(mixed_device_data); + device.get_handle().wait(); + sycl_iface.template call<"float64">(float64_device_data); + device.get_handle().wait(); + sycl_iface.template call<"float32">(float32_device_data); + device.get_handle().wait(); + + std::cout<<"\nSize: "< +>; + +using Float64Array = Array< + Float64 +>; + +using Float32Array = Array< + Float32 +>; + +using MixedPrecisionBenchmarkInterface = Interface< + Member, "float64_32">, + Member, "float64">, + Member, "float32"> +>; +} + +saw::interface listen_mixed_precision(cl::sycl::event& mixed_ev, cl::sycl::event& float64_ev, cl::sycl::event& float32_ev); diff --git a/modules/remote-sycl/c++/common.hpp b/modules/remote-sycl/c++/common.hpp index 9bc734d..078879f 100644 --- a/modules/remote-sycl/c++/common.hpp +++ b/modules/remote-sycl/c++/common.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -17,9 +17,4 @@ class remote; template class device; -template -class data_server; - -template -class data_client; } diff --git a/modules/remote-sycl/c++/data.hpp b/modules/remote-sycl/c++/data.hpp index e436e72..d939a53 100644 --- a/modules/remote-sycl/c++/data.hpp +++ b/modules/remote-sycl/c++/data.hpp @@ -12,9 +12,11 @@ template class data { private: cl::sycl::buffer> data_; + uint64_t size_; public: data(const data& data__): - data_{&data__, 1u} + data_{&data__, 1u}, + size_{data__.size()} {} auto& get_handle() { @@ -25,6 +27,10 @@ public: return data_; } + uint64_t size() const { + return size_; + } + template auto access(cl::sycl::handler& h){ return data_.template get_access(h); diff --git a/modules/remote-sycl/c++/device.hpp b/modules/remote-sycl/c++/device.hpp index e6f3fe3..ae19524 100644 --- a/modules/remote-sycl/c++/device.hpp +++ b/modules/remote-sycl/c++/device.hpp @@ -11,7 +11,9 @@ class device final { private: cl::sycl::queue cmd_queue_; public: - device() = default; + device(): + cmd_queue_{cl::sycl::default_selector_v, cl::sycl::property_list{cl::sycl::property::queue::enable_profiling()}} + {} SAW_FORBID_COPY(device); SAW_FORBID_MOVE(device); diff --git a/modules/remote-sycl/c++/transfer.hpp b/modules/remote-sycl/c++/transfer.hpp index f535751..72b111f 100644 --- a/modules/remote-sycl/c++/transfer.hpp +++ b/modules/remote-sycl/c++/transfer.hpp @@ -6,6 +6,7 @@ #include #include +#include namespace saw { namespace impl { @@ -31,7 +32,7 @@ private: /** * Store for the data the server manages. */ - impl::data_server_redux>::type >::type values_; + typename impl::data_server_redux>::type >::type values_; public: /** * Main constructor diff --git a/modules/remote-sycl/tests/mixed_precision.cpp b/modules/remote-sycl/tests/mixed_precision.cpp new file mode 100644 index 0000000..4a62569 --- /dev/null +++ b/modules/remote-sycl/tests/mixed_precision.cpp @@ -0,0 +1,212 @@ +#include + +#include "../c++/remote.hpp" + +#include + +namespace { +namespace schema { +using namespace saw::schema; + +using TestMixedArray = Array< + MixedPrecision +>; + +using MixedFoo = Interface< + Member, "foo"> +>; + +using TestDoubleArray = Array< + Float64 +>; + +using DoubleFoo = Interface< + Member, "foo"> +>; + +using TestFloatArray = Array< + Float32 +>; + +using FloatFoo = Interface< + Member, "foo"> +>; +} + +constexpr uint64_t test_size = 1024ul; + +SAW_TEST("SYCL Mixed Test"){ + using namespace saw; + + std::random_device r; + std::default_random_engine e1{r()}; + std::uniform_real_distribution<> dis{-1.0,1.0}; + + data host_data; + host_data = {test_size}; + for(uint64_t i = 0; i < test_size; ++i){ + host_data.at(i) = static_cast(dis(e1)); + } + + saw::event_loop loop; + saw::wait_scope wait{loop}; + + remote rmt; + + own> rmt_addr{}; + + rmt.resolve_address().then([&](auto addr){ + rmt_addr = std::move(addr); + }).detach(); + + wait.poll(); + SAW_EXPECT(rmt_addr, "Remote address hasn't been filled"); + + data device_data{host_data}; + + cl::sycl::event ev; + + interface cl_iface { +[&](data& in, cl::sycl::queue* cmd) -> error_or { + + ev = cmd->submit([&](cl::sycl::handler& h){ + + auto acc_buff = in.template access(h); + + h.parallel_for(cl::sycl::range<1>(test_size), [=] (cl::sycl::id<1> it){ + acc_buff[0u].at(it[0u]) = acc_buff[0u].at(it[0u]) * data{2.0}; + }); + }); + return saw::void_t{}; + } + }; + auto& device = rmt_addr->get_device(); + + cl_iface.template call <"foo">(device_data, &(device.get_handle())); + device.get_handle().wait(); + + { + auto end = ev.get_profiling_info(); + auto start = ev.get_profiling_info(); + + std::cout<<"Elapsed kernel time: "<< (end-start) / 1.0e9 << " seconds"< dis{-1.0,1.0}; + + data host_data; + host_data = {test_size}; + for(uint64_t i = 0; i < test_size; ++i){ + host_data.at(i) = static_cast(dis(e1)); + } + + saw::event_loop loop; + saw::wait_scope wait{loop}; + + remote rmt; + + own> rmt_addr{}; + + rmt.resolve_address().then([&](auto addr){ + rmt_addr = std::move(addr); + }).detach(); + + wait.poll(); + SAW_EXPECT(rmt_addr, "Remote address hasn't been filled"); + + data device_data{host_data}; + + cl::sycl::event ev; + + interface cl_iface { +[&](data& in, cl::sycl::queue* cmd) -> error_or { + + ev = cmd->submit([&](cl::sycl::handler& h){ + + auto acc_buff = in.template access(h); + + h.parallel_for(cl::sycl::range<1>(test_size), [=] (cl::sycl::id<1> it){ + acc_buff[0u].at(it[0u]) = acc_buff[0u].at(it[0u]) * data{2.0}; + }); + }); + return saw::void_t{}; + } + }; + auto& device = rmt_addr->get_device(); + + cl_iface.template call <"foo">(device_data, &(device.get_handle())); + device.get_handle().wait(); + + { + auto end = ev.get_profiling_info(); + auto start = ev.get_profiling_info(); + + std::cout<<"Elapsed kernel time: "<< (end-start) / 1.0e9 << " seconds"< dis{-1.0,1.0}; + + data host_data; + host_data = {test_size}; + for(uint64_t i = 0; i < test_size; ++i){ + host_data.at(i) = static_cast(dis(e1)); + } + + saw::event_loop loop; + saw::wait_scope wait{loop}; + + remote rmt; + + own> rmt_addr{}; + + rmt.resolve_address().then([&](auto addr){ + rmt_addr = std::move(addr); + }).detach(); + + wait.poll(); + SAW_EXPECT(rmt_addr, "Remote address hasn't been filled"); + + data device_data{host_data}; + + cl::sycl::event ev; + + interface cl_iface { +[&](data& in, cl::sycl::queue* cmd) -> error_or { + + ev = cmd->submit([&](cl::sycl::handler& h){ + + auto acc_buff = in.template access(h); + + h.parallel_for(cl::sycl::range<1>(test_size), [=] (cl::sycl::id<1> it){ + acc_buff[0u].at(it[0u]) = acc_buff[0u].at(it[0u]) * data{2.0}; + }); + }); + return saw::void_t{}; + } + }; + auto& device = rmt_addr->get_device(); + + cl_iface.template call <"foo">(device_data, &(device.get_handle())); + device.get_handle().wait(); + + { + auto end = ev.get_profiling_info(); + auto start = ev.get_profiling_info(); + + std::cout<<"Elapsed kernel time: "<< (end-start) / 1.0e9 << " seconds"<, "foo"> >; - -using Calculator = Interface< - Member< - Function, Int64>, "add" - > -, Member< - Function, Int64>, "multiply" - > ->; } SAW_TEST("SYCL Test Setup"){ using namespace saw; -- cgit v1.2.3