Skip to content

Commit

Permalink
Change C++ standard to c++17 for non-CUDA sources (NVIDIA#3423)
Browse files Browse the repository at this point in the history
* Change C++ standard to c++17 for non-cuda sources in CMakeLists.txt
* Adjust code to avoid collisions with C++17 STL features
* Move some core tests from kernels to core. More work on porting to C++17.

Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
  • Loading branch information
mzient authored and cyyever committed Jan 23, 2022
1 parent 8bd14e1 commit 32cc1d8
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 45 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ set(CMAKE_CUDA_FLAGS_RELEASE "${CMAKE_CUDA_FLAGS_RELEASE} -DDALI_DEBUG=0")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -O2 -g3")
set(CMAKE_CUDA_FLAGS_RELWITHDEBINFO "${CMAKE_CUDA_FLAGS_RELWITHDEBINFO} -g -lineinfo")

# Use -std=c++14 (and not gnu++14)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_C_STANDARD 11)
Expand Down
7 changes: 3 additions & 4 deletions dali/kernels/test/tuple_test.cc → dali/core/tuple_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
// Copyright (c) 2018-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -13,9 +13,8 @@
// limitations under the License.

#include <gtest/gtest.h>
#include "dali/kernels/kernel.h"
#include "dali/kernels/type_tag.h"
#include "dali/core/static_switch.h"
#include <tuple>
#include "dali/core/tuple_helpers.h"

namespace dali {
namespace kernels {
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions dali/core/utils_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ DEVICE_TEST(CoreUtilsDev, Volume, 1, 1) {

DEVICE_TEST(CoreUtilsDev, Size, 1, 1) {
int a0[] = { 42 };
DEV_EXPECT_EQ(size(a0), 1u);
DEV_EXPECT_EQ(dali::size(a0), 1u);
int a1[] = { 2, 3, 4 };
DEV_EXPECT_EQ(size(a1), 3u);
DEV_EXPECT_EQ(dali::size(a1), 3u);

SmallVector<int, 5> v;
v.resize(10);
DEV_EXPECT_EQ(v.size(), 10u);
DEV_EXPECT_EQ(size(v), 10u);
DEV_EXPECT_EQ(dali::size(v), 10u);
}

DEFINE_TEST_KERNEL(CoreUtilsDev, Span, span<float> data) {
Expand Down
11 changes: 6 additions & 5 deletions dali/kernels/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,31 +135,32 @@ class Scratchpad {
template <typename Collection, typename T = std::remove_const_t<element_t<Collection>>>
if_array_like<Collection, T*>
ToGPU(cudaStream_t stream, const Collection &c) {
T *ptr = AllocateGPU<T>(size(c));
CUDA_CALL(cudaMemcpyAsync(ptr, &c[0], size(c) * sizeof(T), cudaMemcpyHostToDevice, stream));
auto n = dali::size(c);
T *ptr = AllocateGPU<T>(n);
CUDA_CALL(cudaMemcpyAsync(ptr, &c[0], n * sizeof(T), cudaMemcpyHostToDevice, stream));
return ptr;
}

template <typename Collection, typename T = std::remove_const_t<element_t<Collection>>>
if_iterable<Collection, T*>
ToHost(const Collection &c) {
T *ptr = AllocateHost<T>(size(c));
T *ptr = AllocateHost<T>(dali::size(c));
std::copy(begin(c), end(c), ptr);
return ptr;
}

template <typename Collection, typename T = std::remove_const_t<element_t<Collection>>>
if_iterable<Collection, T*>
ToPinned(const Collection &c) {
T *ptr = AllocatePinned<T>(size(c));
T *ptr = AllocatePinned<T>(dali::size(c));
std::copy(begin(c), end(c), ptr);
return ptr;
}

template <typename Collection, typename T = std::remove_const_t<element_t<Collection>>>
if_iterable<Collection, T*>
ToManaged(const Collection &c) {
T *ptr = AllocateManaged<T>(size(c));
T *ptr = AllocateManaged<T>(dali::size(c));
std::copy(begin(c), end(c), ptr);
return ptr;
}
Expand Down
4 changes: 2 additions & 2 deletions dali/kernels/reduce/reduce_drop_dims.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ struct DropDims {
template <typename Indices>
static int simplify(int64_t *out_shape, unsigned &out_mask,
const Indices &in_shape, uint64_t axis_mask) {
int dims = size(in_shape);
int dims = dali::size(in_shape);
int d = 0;
out_shape[0] = in_shape[0];
bool prev = axis_mask & 1;
Expand Down Expand Up @@ -169,7 +169,7 @@ struct DropDims {
template <typename Indices>
DropDims(const Indices &in_shape, uint64_t reduced_axes) {
memset(this, 0, sizeof(*this));
if (size(in_shape) == 0) {
if (dali::size(in_shape) == 0) {
start = -1;
return;
}
Expand Down
12 changes: 6 additions & 6 deletions dali/kernels/scratch_copy_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ inline void copy_to_buffer(char *buffer, const size_t *offsets) {}
*/
template <typename Collection, typename... Collections>
void copy_to_buffer(char *buffer,
const size_t *offsets,
const Collection &c,
const Collections &... tail) {
const size_t *offsets,
const Collection &c,
const Collections &... tail) {
using T = std::remove_cv_t<element_t<Collection>>;
std::copy(dali::begin(c), dali::end(c), reinterpret_cast<T*>(buffer + offsets[0]));
copy_to_buffer(buffer, offsets+1, tail...);
Expand All @@ -54,12 +54,12 @@ inline void GetCollectionOffsets(size_t base, size_t *offsets) { *offsets = base
*/
template <typename Collection, typename... Collections>
void GetCollectionOffsets(size_t base, size_t *offsets,
const Collection &c,
const Collections &...tail) {
const Collection &c,
const Collections &...tail) {
using T = std::remove_cv_t<element_t<Collection>>;
base = align_up(base, alignof(T));
*offsets = base;
base += size(c) * sizeof(T);
base += dali::size(c) * sizeof(T);
GetCollectionOffsets(base, offsets + 1, tail...);
}

Expand Down
2 changes: 1 addition & 1 deletion dali/kernels/test/scratch_copy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace dali {
namespace kernels {

template <typename C, typename = element_t<C>>
constexpr auto size_bytes(const C &c) { return size(c) * sizeof(element_t<C>); }
constexpr auto size_bytes(const C &c) { return dali::size(c) * sizeof(element_t<C>); }

TEST(Scratchpad, ToContiguous) {
ScratchpadEstimator se;
Expand Down
6 changes: 3 additions & 3 deletions dali/operators/geometry/mt_transform_attr.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
// Copyright (c) 2020-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -106,15 +106,15 @@ class DLL_PUBLIC MTTransformAttr {
/** @brief Fill the diagonal with a scalar value, put zeros elsewhere */
template <typename Container>
void MakeDiagonalMatrix(Container &&mtx, float value) {
assert(static_cast<int>(size(mtx)) == input_pt_dim_ * output_pt_dim_);
assert(static_cast<int>(dali::size(mtx)) == input_pt_dim_ * output_pt_dim_);
for (int i = 0, k = 0; i < output_pt_dim_; i++)
for (int j = 0; j < input_pt_dim_; j++, k++)
mtx[k] = (i == j ? value : 0);
}

template <typename OutRange, typename InRange>
void Repeat(OutRange &&out, InRange &&in, int times) {
ssize_t n = size(in);
ssize_t n = dali::size(in);
resize_if_possible(out, n * times);
for (ssize_t i = 0, k = 0; i < times; i++)
for (ssize_t j = 0; j < n; j++, k++)
Expand Down
4 changes: 2 additions & 2 deletions dali/operators/segmentation/random_object_bbox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ void StoreBox(const OutListCPU<int, 1> &out1,
const OutListCPU<int, 1> &out2,
RandomObjectBBox::OutputFormat format,
int sample_idx, Lo &&start, Hi &&end) {
assert(size(start) == size(end));
int ndim = size(start);
assert(dali::size(start) == dali::size(end));
int ndim = dali::size(start);
switch (format) {
case RandomObjectBBox::Out_Box:
for (int i = 0; i < ndim; i++) {
Expand Down
4 changes: 2 additions & 2 deletions dali/pipeline/data/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class DLL_PUBLIC Buffer {
return shrink_threshold_;
}

static constexpr double kMaxGrowthFactor = 4;
DLL_PUBLIC static constexpr double kMaxGrowthFactor = 4;

protected:
// Helper to resize the underlying allocation
Expand Down Expand Up @@ -413,7 +413,7 @@ DLL_PUBLIC double Buffer<Backend>::shrink_threshold_ =
std::is_same<Backend, CPUBackend>::value ? 0.5 : 0;

template <typename Backend>
DLL_PUBLIC constexpr double Buffer<Backend>::kMaxGrowthFactor;
constexpr double Buffer<Backend>::kMaxGrowthFactor;


// Macro so we don't have to list these in all
Expand Down
6 changes: 3 additions & 3 deletions include/dali/core/random.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
// Copyright (c) 2020-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,7 +37,7 @@ void random_permutation(Collection &out, RNG &rng) {
*/
template <typename Collection, typename RNG>
void random_derangement(Collection &out, RNG &rng) {
int N = size(out);
int N = dali::size(out);
std::iota(dali::begin(out), dali::end(out), 0);
for (int i = 0; i < N-1; i++) {
std::uniform_int_distribution<int> dist(i+1, N-1);
Expand Down Expand Up @@ -76,7 +76,7 @@ random_sequence(Collection &out, T lo, T hi, RNG &rng) {
*/
template <typename Collection, typename RNG>
void random_sequence_no_fixed_points(Collection &out, int lo, int hi, RNG &rng) {
int N = size(out);
int N = dali::size(out);
std::uniform_int_distribution<int> dist1(lo, hi-1);
int i = 0;
// when index is below lo, no fixed points possible
Expand Down
4 changes: 2 additions & 2 deletions include/dali/core/tensor_shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -1363,14 +1363,14 @@ template <int out_ndim, int ndim>
void collapse_dims(TensorListShape<out_ndim> &result,
const TensorListShape<ndim> &shape,
std::initializer_list<std::pair<int, int>> dim_groups) {
collapse_dims<out_ndim>(result, shape, make_span(dim_groups.begin(), size(dim_groups)));
collapse_dims<out_ndim>(result, shape, make_span(dim_groups.begin(), dali::size(dim_groups)));
}

template <int out_ndim = DynamicDimensions, int ndim>
TensorListShape<out_ndim> collapse_dims(const TensorListShape<ndim> &shape,
std::initializer_list<std::pair<int, int>> dim_groups) {
TensorListShape<out_ndim> result;
collapse_dims<out_ndim>(result, shape, make_span(dim_groups.begin(), size(dim_groups)));
collapse_dims<out_ndim>(result, shape, make_span(dim_groups.begin(), dali::size(dim_groups)));
return result;
}

Expand Down
10 changes: 5 additions & 5 deletions include/dali/core/tensor_view.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved.
// Copyright (c) 2017-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,8 +38,8 @@ struct check_implicit_conversion {

template <typename Shape, typename Position>
bool ContainsCoords(const Shape &shape, const Position &pos) {
const int shape_dim = size(shape);
const int pos_dim = size(pos);
const int shape_dim = dali::size(shape);
const int pos_dim = dali::size(pos);
if (pos_dim > shape_dim) {
return false;
}
Expand All @@ -60,8 +60,8 @@ template <typename Shape, typename Position>
DALI_HOST_DEV if_array_like<Position, ptrdiff_t> CalcOffset(const Shape &shape,
const Position &pos) {
ptrdiff_t ofs = pos[0];
const int pos_dim = size(pos);
const int shape_dim = size(shape);
const int pos_dim = dali::size(pos);
const int shape_dim = dali::size(shape);
int i;
for (i = 1; i < pos_dim; i++) {
ofs *= shape[i];
Expand Down
6 changes: 3 additions & 3 deletions include/dali/core/tuple_helpers.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
// Copyright (c) 2018-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -155,8 +155,8 @@ constexpr auto apply(F &&f, std::tuple<T...> &&args)

template <typename F, typename... Args>
constexpr auto apply_all(F &&f, Args&&... args)
->decltype(apply(f, std::tuple_cat(as_tuple(args)...))) {
return apply(f, std::tuple_cat(as_tuple(args)...));
->decltype(dali::detail::apply(f, std::tuple_cat(as_tuple(args)...))) {
return dali::detail::apply(f, std::tuple_cat(as_tuple(args)...));
}

template <size_t total, typename Type, typename Tuple>
Expand Down
2 changes: 1 addition & 1 deletion include/dali/core/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ using if_indexable = if_istype<decltype(std::declval<Collection>()[0]), T>;

template <typename Collection, typename T = void>
using if_array_like = if_indexable<Collection,
if_istype<decltype(size(std::declval<Collection>())), T>>;
if_istype<decltype(dali::size(std::declval<Collection>())), T>>;

template <typename It>
using is_integer_iterator = std::is_integral<
Expand Down
2 changes: 1 addition & 1 deletion third_party/libcudacxx

0 comments on commit 32cc1d8

Please sign in to comment.