diff --git a/cpp/oneapi/dal/io/csv/backend/cpu/read_kernel.cpp b/cpp/oneapi/dal/io/csv/backend/cpu/read_kernel.cpp index 30f6fde0258..74bf054c93f 100644 --- a/cpp/oneapi/dal/io/csv/backend/cpu/read_kernel.cpp +++ b/cpp/oneapi/dal/io/csv/backend/cpu/read_kernel.cpp @@ -34,26 +34,32 @@ namespace oneapi::dal::csv::backend { namespace interop = dal::backend::interop; namespace daal_dm = daal::data_management; -template <> -table read_kernel_cpu::operator()(const dal::backend::context_cpu& ctx, - const detail::data_source_base& ds, - const read_args
& args) const { - daal_dm::CsvDataSourceOptions csv_options(daal_dm::operator|( - daal_dm::operator|(daal_dm::CsvDataSourceOptions::allocateNumericTable, - daal_dm::CsvDataSourceOptions::createDictionaryFromContext), - (ds.get_parse_header() ? daal_dm::CsvDataSourceOptions::parseHeader - : daal_dm::CsvDataSourceOptions::byDefault))); - - daal_dm::FileDataSource daal_data_source(ds.get_file_name().c_str(), - csv_options); - interop::status_to_exception(daal_data_source.status()); - - daal_data_source.getFeatureManager().setDelimiter(ds.get_delimiter()); - daal_data_source.loadDataBlock(); - interop::status_to_exception(daal_data_source.status()); - - return oneapi::dal::backend::interop::convert_from_daal_homogen_table( - daal_data_source.getNumericTable()); -} +template +struct read_kernel_cpu { + table operator()(const dal::backend::context_cpu& ctx, + const detail::data_source_base& ds, + const read_args
& args) const { + daal_dm::CsvDataSourceOptions csv_options(daal_dm::operator|( + daal_dm::operator|(daal_dm::CsvDataSourceOptions::allocateNumericTable, + daal_dm::CsvDataSourceOptions::createDictionaryFromContext), + (ds.get_parse_header() ? daal_dm::CsvDataSourceOptions::parseHeader + : daal_dm::CsvDataSourceOptions::byDefault))); + + daal_dm::FileDataSource daal_data_source( + ds.get_file_name().c_str(), + csv_options); + interop::status_to_exception(daal_data_source.status()); + + daal_data_source.getFeatureManager().setDelimiter(ds.get_delimiter()); + daal_data_source.loadDataBlock(); + interop::status_to_exception(daal_data_source.status()); + + return oneapi::dal::backend::interop::convert_from_daal_homogen_table( + daal_data_source.getNumericTable()); + } +}; + +template struct read_kernel_cpu; +template struct read_kernel_cpu; } // namespace oneapi::dal::csv::backend diff --git a/cpp/oneapi/dal/io/csv/backend/cpu/read_kernel.hpp b/cpp/oneapi/dal/io/csv/backend/cpu/read_kernel.hpp index 001ec4ccac7..fe7a5b38548 100644 --- a/cpp/oneapi/dal/io/csv/backend/cpu/read_kernel.hpp +++ b/cpp/oneapi/dal/io/csv/backend/cpu/read_kernel.hpp @@ -21,7 +21,7 @@ namespace oneapi::dal::csv::backend { -template +template struct read_kernel_cpu { Object operator()(const dal::backend::context_cpu& ctx, const detail::data_source_base& ds, diff --git a/cpp/oneapi/dal/io/csv/backend/gpu/read_kernel.hpp b/cpp/oneapi/dal/io/csv/backend/gpu/read_kernel.hpp index 94f460b9a6d..148523c3e7a 100644 --- a/cpp/oneapi/dal/io/csv/backend/gpu/read_kernel.hpp +++ b/cpp/oneapi/dal/io/csv/backend/gpu/read_kernel.hpp @@ -21,7 +21,7 @@ namespace oneapi::dal::csv::backend { -template +template struct read_kernel_gpu { Object operator()(const dal::backend::context_gpu& ctx, const detail::data_source_base& ds, diff --git a/cpp/oneapi/dal/io/csv/backend/gpu/read_kernel_dpc.cpp b/cpp/oneapi/dal/io/csv/backend/gpu/read_kernel_dpc.cpp index ffd236c2aa4..a7bebc896b1 100644 --- a/cpp/oneapi/dal/io/csv/backend/gpu/read_kernel_dpc.cpp +++ b/cpp/oneapi/dal/io/csv/backend/gpu/read_kernel_dpc.cpp @@ -36,45 +36,50 @@ namespace oneapi::dal::csv::backend { namespace interop = dal::backend::interop; namespace daal_dm = daal::data_management; -template <> -table read_kernel_gpu
::operator()(const dal::backend::context_gpu& ctx, - const detail::data_source_base& ds, - const read_args
& args) const { - auto& queue = ctx.get_queue(); - - daal_dm::CsvDataSourceOptions csv_options(daal_dm::operator|( - daal_dm::operator|(daal_dm::CsvDataSourceOptions::allocateNumericTable, - daal_dm::CsvDataSourceOptions::createDictionaryFromContext), - (ds.get_parse_header() ? daal_dm::CsvDataSourceOptions::parseHeader - : daal_dm::CsvDataSourceOptions::byDefault))); - - daal_dm::FileDataSource daal_data_source(ds.get_file_name().c_str(), - csv_options); - interop::status_to_exception(daal_data_source.status()); - - daal_data_source.getFeatureManager().setDelimiter(ds.get_delimiter()); - daal_data_source.loadDataBlock(); - interop::status_to_exception(daal_data_source.status()); - - auto nt = daal_data_source.getNumericTable(); - - daal_dm::BlockDescriptor block; - const std::int64_t row_count = nt->getNumberOfRows(); - const std::int64_t column_count = nt->getNumberOfColumns(); - - interop::status_to_exception(nt->getBlockOfRows(0, row_count, daal_dm::readOnly, block)); - DAAL_DATA_TYPE* data = block.getBlockPtr(); - - auto arr = - array::empty(queue, row_count * column_count, sycl::usm::alloc::device); - dal::detail::memcpy_host2usm(queue, - arr.get_mutable_data(), - data, - sizeof(DAAL_DATA_TYPE) * row_count * column_count); - - interop::status_to_exception(nt->releaseBlockOfRows(block)); - - return dal::detail::homogen_table_builder{}.reset(arr, row_count, column_count).build(); -} +template +struct read_kernel_gpu { + table operator()(const dal::backend::context_gpu& ctx, + const detail::data_source_base& ds, + const read_args
& args) const { + auto& queue = ctx.get_queue(); + + daal_dm::CsvDataSourceOptions csv_options(daal_dm::operator|( + daal_dm::operator|(daal_dm::CsvDataSourceOptions::allocateNumericTable, + daal_dm::CsvDataSourceOptions::createDictionaryFromContext), + (ds.get_parse_header() ? daal_dm::CsvDataSourceOptions::parseHeader + : daal_dm::CsvDataSourceOptions::byDefault))); + + daal_dm::FileDataSource daal_data_source( + ds.get_file_name().c_str(), + csv_options); + interop::status_to_exception(daal_data_source.status()); + + daal_data_source.getFeatureManager().setDelimiter(ds.get_delimiter()); + daal_data_source.loadDataBlock(); + interop::status_to_exception(daal_data_source.status()); + + auto nt = daal_data_source.getNumericTable(); + + daal_dm::BlockDescriptor block; + const std::int64_t row_count = nt->getNumberOfRows(); + const std::int64_t column_count = nt->getNumberOfColumns(); + + interop::status_to_exception(nt->getBlockOfRows(0, row_count, daal_dm::readOnly, block)); + Float* data = block.getBlockPtr(); + + auto arr = array::empty(queue, row_count * column_count, sycl::usm::alloc::device); + dal::detail::memcpy_host2usm(queue, + arr.get_mutable_data(), + data, + sizeof(Float) * row_count * column_count); + + interop::status_to_exception(nt->releaseBlockOfRows(block)); + + return dal::detail::homogen_table_builder{}.reset(arr, row_count, column_count).build(); + } +}; + +template struct read_kernel_gpu; +template struct read_kernel_gpu; } // namespace oneapi::dal::csv::backend diff --git a/cpp/oneapi/dal/io/csv/common.hpp b/cpp/oneapi/dal/io/csv/common.hpp index c9efd2ac9df..4dd498adb6b 100644 --- a/cpp/oneapi/dal/io/csv/common.hpp +++ b/cpp/oneapi/dal/io/csv/common.hpp @@ -27,6 +27,9 @@ namespace oneapi::dal::csv { namespace detail { namespace v1 { +template +constexpr bool is_valid_float_v = dal::detail::is_one_of_v; + struct data_source_tag {}; class data_source_impl; @@ -65,32 +68,48 @@ class ONEDAL_EXPORT data_source_base : public base { using v1::data_source_tag; using v1::data_source_impl; using v1::data_source_base; +using v1::is_valid_float_v; } // namespace detail namespace v1 { +/// Used for the specification of data source configuration. +/// +/// @tparam Float The type of the floating-point that the data source will operate with. +/// Must be a floating-point type. +template class data_source : public detail::data_source_base { + static_assert(detail::is_valid_float_v); + public: + using float_t = Float; + + /// Constructs a data_source object from a C-style string file name. explicit data_source(const char* file_name) : data_source_base(file_name) {} + /// Constructs a data_source from C++-style std::string file name. explicit data_source(const std::string& file_name) : data_source_base(file_name.c_str()) {} + /// Sets the delimiter character for parsing the data source file. auto& set_delimiter(char value) { set_delimiter_impl(value); return *this; } + /// Specifies whether to parse the header of the data source file. auto& set_parse_header(bool value) { set_parse_header_impl(value); return *this; } + /// Sets the file name for the data source via the C-style string. auto& set_file_name(const char* value) { set_file_name_impl(value); return *this; } + /// Sets the file name for the data source via the C++-style std::string. auto& set_file_name(const std::string& value) { set_file_name_impl(value.c_str()); return *this; diff --git a/cpp/oneapi/dal/io/csv/detail/read_ops.cpp b/cpp/oneapi/dal/io/csv/detail/read_ops.cpp index 296fa38b120..57eea1fc1a5 100644 --- a/cpp/oneapi/dal/io/csv/detail/read_ops.cpp +++ b/cpp/oneapi/dal/io/csv/detail/read_ops.cpp @@ -22,13 +22,20 @@ namespace oneapi::dal::csv::detail { namespace v1 { using dal::detail::host_policy; -table read_ops_dispatcher::operator()(const host_policy& policy, - const data_source_base& ds, - const read_args
& args) const { + +template +table read_ops_dispatcher::operator()( + const host_policy& policy, + const data_source_base& ds, + const read_args
& args) const { using kernel_dispatcher_t = dal::backend::kernel_dispatcher< // - KERNEL_SINGLE_NODE_CPU(backend::read_kernel_cpu
)>; + KERNEL_SINGLE_NODE_CPU(backend::read_kernel_cpu)>; return kernel_dispatcher_t()(policy, ds, args); } +#define INSTANTIATE(F) template struct ONEDAL_EXPORT read_ops_dispatcher; +INSTANTIATE(float) +INSTANTIATE(double) + } // namespace v1 } // namespace oneapi::dal::csv::detail diff --git a/cpp/oneapi/dal/io/csv/detail/read_ops.hpp b/cpp/oneapi/dal/io/csv/detail/read_ops.hpp index a15ba8940ad..57393e092f1 100644 --- a/cpp/oneapi/dal/io/csv/detail/read_ops.hpp +++ b/cpp/oneapi/dal/io/csv/detail/read_ops.hpp @@ -24,11 +24,11 @@ namespace oneapi::dal::csv::detail { namespace v1 { -template +template struct read_ops_dispatcher; -template -struct read_ops_dispatcher { +template +struct read_ops_dispatcher { Object operator()(const dal::detail::host_policy& policy, const data_source_base& ds, const dal::preview::csv::read_args& args) const { @@ -40,8 +40,8 @@ struct read_ops_dispatcher { } }; -template <> -struct ONEDAL_EXPORT read_ops_dispatcher { +template +struct read_ops_dispatcher { table operator()(const dal::detail::host_policy& policy, const data_source_base& ds, const dal::csv::read_args
& args) const; @@ -49,8 +49,8 @@ struct ONEDAL_EXPORT read_ops_dispatcher { #ifdef ONEDAL_DATA_PARALLEL -template <> -struct ONEDAL_EXPORT read_ops_dispatcher { +template +struct read_ops_dispatcher { table operator()(const dal::detail::data_parallel_policy& ctx, const data_source_base& ds, const dal::csv::read_args
& args) const; @@ -61,8 +61,9 @@ struct ONEDAL_EXPORT read_ops_dispatcher struct read_ops; -template -struct read_ops { +template +struct read_ops { + using float_t = typename DataSource::float_t; using input_t = dal::preview::csv::read_args; using result_t = Object; @@ -75,14 +76,15 @@ struct read_ops { template auto operator()(const Policy& ctx, const data_source_base& ds, const input_t& args) const { check_preconditions(ds, args); - auto result = read_ops_dispatcher()(ctx, ds, args); + auto result = read_ops_dispatcher()(ctx, ds, args); check_postconditions(ds, args, result); return result; } }; -template <> -struct read_ops { +template +struct read_ops { + using float_t = typename DataSource::float_t; using input_t = read_args
; using result_t = table; @@ -95,7 +97,7 @@ struct read_ops { template auto operator()(const Policy& ctx, const data_source_base& ds, const input_t& args) const { check_preconditions(ds, args); - const auto result = read_ops_dispatcher()(ctx, ds, args); + const auto result = read_ops_dispatcher()(ctx, ds, args); check_postconditions(ds, args, result); return result; } diff --git a/cpp/oneapi/dal/io/csv/detail/read_ops_dpc.cpp b/cpp/oneapi/dal/io/csv/detail/read_ops_dpc.cpp index b604b46a94d..ffd658fd11e 100644 --- a/cpp/oneapi/dal/io/csv/detail/read_ops_dpc.cpp +++ b/cpp/oneapi/dal/io/csv/detail/read_ops_dpc.cpp @@ -23,15 +23,21 @@ namespace oneapi::dal::csv::detail { namespace v1 { using dal::detail::data_parallel_policy; -table read_ops_dispatcher::operator()( +template +table read_ops_dispatcher::operator()( const data_parallel_policy& ctx, const data_source_base& ds, const read_args
& args) const { - using kernel_dispatcher_t = - dal::backend::kernel_dispatcher), - KERNEL_SINGLE_NODE_GPU(backend::read_kernel_gpu
)>; + using kernel_dispatcher_t = dal::backend::kernel_dispatcher< + KERNEL_SINGLE_NODE_CPU(backend::read_kernel_cpu), + KERNEL_SINGLE_NODE_GPU(backend::read_kernel_gpu)>; return kernel_dispatcher_t{}(ctx, ds, args); } +#define INSTANTIATE(F) \ + template struct ONEDAL_EXPORT read_ops_dispatcher; +INSTANTIATE(float) +INSTANTIATE(double) + } // namespace v1 } // namespace oneapi::dal::csv::detail diff --git a/examples/oneapi/cpp/BUILD b/examples/oneapi/cpp/BUILD index e473cddd284..a90cf71b7df 100644 --- a/examples/oneapi/cpp/BUILD +++ b/examples/oneapi/cpp/BUILD @@ -52,6 +52,17 @@ dal_example_suite( extra_deps = _TEST_DEPS, ) +dal_example_suite( + name = "misc", + compile_as = [ "c++" ], + srcs = glob(["source/misc/*.cpp"]), + dal_deps = [ + "@onedal//cpp/oneapi/dal:io", + ], + data = _DATA_DEPS, + extra_deps = _TEST_DEPS, +) + dal_example_suite( name = "jaccard", compile_as = [ "c++" ], diff --git a/examples/oneapi/cpp/source/misc/read_batch.cpp b/examples/oneapi/cpp/source/misc/read_batch.cpp new file mode 100644 index 00000000000..bf863424a9a --- /dev/null +++ b/examples/oneapi/cpp/source/misc/read_batch.cpp @@ -0,0 +1,44 @@ +/******************************************************************************* +* Copyright contributors to the oneDAL project +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*******************************************************************************/ + +#include "oneapi/dal/io/csv.hpp" + +#include "example_util/utils.hpp" + +namespace dal = oneapi::dal; +template +void run() { + const auto data_file_name = get_data_path("covcormoments_dense.csv"); + + const auto table = + dal::read(dal::csv::data_source{ data_file_name }); + + const auto type = table.get_metadata().get_data_type(0); + switch (type) { + case dal::data_type::float64: std::cout << "table data type: double " << std::endl; break; + case dal::data_type::float32: std::cout << "table data type: float " << std::endl; break; + default: break; + } + + std::cout << table << std::endl; +} + +int main(int argc, char const *argv[]) { + run(); + run(); + + return 0; +} diff --git a/examples/oneapi/dpc/BUILD b/examples/oneapi/dpc/BUILD index f7f69bdd350..5a354226e3a 100644 --- a/examples/oneapi/dpc/BUILD +++ b/examples/oneapi/dpc/BUILD @@ -41,6 +41,17 @@ dal_example_suite( extra_deps = _TEST_DEPS, ) +dal_example_suite( + name = "misc", + compile_as = [ "dpc++" ], + srcs = glob(["source/misc/*.cpp"]), + dal_deps = [ + "@onedal//cpp/oneapi/dal:io", + ], + data = _DATA_DEPS, + extra_deps = _TEST_DEPS, +) + dal_algo_example_suite( algos = [ "basic_statistics", diff --git a/examples/oneapi/dpc/source/misc/read_batch.cpp b/examples/oneapi/dpc/source/misc/read_batch.cpp new file mode 100644 index 00000000000..0a792ef5b52 --- /dev/null +++ b/examples/oneapi/dpc/source/misc/read_batch.cpp @@ -0,0 +1,58 @@ +/******************************************************************************* +* Copyright contributors to the oneDAL project +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*******************************************************************************/ + +#include +#include +#include + +#ifndef ONEDAL_DATA_PARALLEL +#define ONEDAL_DATA_PARALLEL +#endif + +#include "oneapi/dal/io/csv.hpp" + +#include "example_util/utils.hpp" + +namespace dal = oneapi::dal; +template +void run(sycl::queue &q) { + const auto data_file_name = get_data_path("covcormoments_dense.csv"); + + const auto table = + dal::read(q, dal::csv::data_source{ data_file_name }); + + const auto type = table.get_metadata().get_data_type(0); + switch (type) { + case dal::data_type::float64: std::cout << "table data type: double " << std::endl; break; + case dal::data_type::float32: std::cout << "table data type: float " << std::endl; break; + default: break; + } + + std::cout << table << std::endl; +} + +int main(int argc, char const *argv[]) { + for (auto d : list_devices()) { + std::cout << "Running on " << d.get_platform().get_info() + << ", " << d.get_info() << "\n" + << std::endl; + auto q = sycl::queue{ d }; + run(q); + run(q); + } + + return 0; +}