Skip to content

Commit

Permalink
Use overload in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Jun 28, 2022
1 parent 4f30f34 commit 990b33a
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 66 deletions.
3 changes: 1 addition & 2 deletions examples/10_streaming_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ int main()
constexpr unsigned long length = 10ul;
Extent global_extent = {length};
Dataset dataset = Dataset(datatype, global_extent);
std::shared_ptr<position_t> local_data(
new position_t[length], [](position_t const *ptr) { delete[] ptr; });
std::shared_ptr<position_t[]> local_data(new position_t[length]);

WriteIterations iterations = series.writeIterations();
for (size_t i = 0; i < 100; ++i)
Expand Down
3 changes: 1 addition & 2 deletions examples/13_write_dynamic_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ chunks = "auto"
constexpr unsigned long length = 10ul;
Extent global_extent = {length};
Dataset dataset = Dataset(datatype, global_extent);
std::shared_ptr<position_t> local_data(
new position_t[length], [](position_t const *ptr) { delete[] ptr; });
std::shared_ptr<position_t[]> local_data(new position_t[length]);

WriteIterations iterations = series.writeIterations();
for (size_t i = 0; i < 100; ++i)
Expand Down
18 changes: 3 additions & 15 deletions examples/7_extended_write_serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ int main()
// data is assumed to reside behind a pointer as a contiguous
// column-major array shared data ownership during IO is indicated with
// a smart pointer
std::shared_ptr<double> partial_mesh(
new double[5], [](double const *p) {
delete[] p;
p = nullptr;
});
std::shared_ptr<double[]> partial_mesh(new double[5]);

// before storing record data, you must specify the dataset once per
// component this describes the datatype and shape of data as it should
Expand Down Expand Up @@ -130,20 +126,12 @@ int main()
io::ParticleSpecies electrons = cur_it.particles["electrons"];

io::Extent mpiDims{4};
std::shared_ptr<float> partial_particlePos(
new float[2], [](float const *p) {
delete[] p;
p = nullptr;
});
std::shared_ptr<float[]> partial_particlePos(new float[2]);
dtype = io::determineDatatype(partial_particlePos);
d = io::Dataset(dtype, mpiDims);
electrons["position"]["x"].resetDataset(d);

std::shared_ptr<uint64_t> partial_particleOff(
new uint64_t[2], [](uint64_t const *p) {
delete[] p;
p = nullptr;
});
std::shared_ptr<uint64_t[]> partial_particleOff(new uint64_t[2]);
dtype = io::determineDatatype(partial_particleOff);
d = io::Dataset(dtype, mpiDims);
electrons["positionOffset"]["x"].resetDataset(d);
Expand Down
10 changes: 5 additions & 5 deletions examples/8a_benchmark_write_parallel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ class Timer
*/

template <typename T>
std::shared_ptr<T>
std::shared_ptr<T[]>
createDataCPU(const unsigned long &size, const T &val, const T &increment)
{
auto E = std::shared_ptr<T>{new T[size], [](T *d) { delete[] d; }};
auto E = std::shared_ptr<T[]>{new T[size]};

for (unsigned long i = 0ul; i < size; i++)
{
Expand All @@ -197,7 +197,7 @@ createDataCPU(const unsigned long &size, const T &val, const T &increment)

#if openPMD_HAVE_CUDA_EXAMPLES
template <typename T>
std::shared_ptr<T>
std::shared_ptr<T[]>
createDataGPU(const unsigned long &size, const T &val, const T &increment)
{
auto myCudaMalloc = [](size_t mySize) {
Expand All @@ -206,7 +206,7 @@ createDataGPU(const unsigned long &size, const T &val, const T &increment)
return ptr;
};
auto deleter = [](T *ptr) { cudaFree(ptr); };
auto E = std::shared_ptr<T>{(T *)myCudaMalloc(size * sizeof(T)), deleter};
auto E = std::shared_ptr<T[]>{(T *)myCudaMalloc(size * sizeof(T)), deleter};

T *data = new T[size];
for (unsigned long i = 0ul; i < size; i++)
Expand All @@ -222,7 +222,7 @@ createDataGPU(const unsigned long &size, const T &val, const T &increment)
#endif

template <typename T>
std::shared_ptr<T>
std::shared_ptr<T[]>
createData(const unsigned long &size, const T &val, const T &increment)
{
#if openPMD_HAVE_CUDA_EXAMPLES
Expand Down
2 changes: 1 addition & 1 deletion examples/8b_benchmark_read_parallel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ template <typename T>
std::shared_ptr<T>
createData(const unsigned long &size, const T &val, bool increment = false)
{
auto E = std::shared_ptr<T>{new T[size], [](T *d) { delete[] d; }};
auto E = std::shared_ptr<T[]>{new T[size]};

for (unsigned long i = 0ul; i < size; i++)
{
Expand Down
7 changes: 3 additions & 4 deletions test/ParallelIOTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ void write_test_zero_extent(
position_global.begin(), position_global.end(), [&pos] {
return pos++;
});
std::shared_ptr<double> position_local(
new double[rank], [](double const *p) { delete[] p; });
std::shared_ptr<double[]> position_local(new double[rank]);
uint64_t offset;
if (rank != 0)
offset = ((rank - 1) * (rank - 1) + (rank - 1)) / 2;
Expand All @@ -171,8 +170,8 @@ void write_test_zero_extent(
positionOffset_global.begin(),
positionOffset_global.end(),
[&posOff] { return posOff++; });
std::shared_ptr<uint64_t> positionOffset_local(
new uint64_t[rank], [](uint64_t const *p) { delete[] p; });
std::shared_ptr<uint64_t[]> positionOffset_local(
new uint64_t[rank]);

e["positionOffset"]["x"].resetDataset(
Dataset(determineDatatype(positionOffset_local), {num_cells}));
Expand Down
52 changes: 15 additions & 37 deletions test/SerialIOTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,7 @@ void close_and_copy_attributable_test(std::string file_ending)
constexpr unsigned long length = 10ul;
Extent global_extent = {length};
Dataset dataset = Dataset(datatype, global_extent);
std::shared_ptr<position_t> local_data(
new position_t[length], [](position_t const *ptr) { delete[] ptr; });
std::shared_ptr<position_t[]> local_data(new position_t[length]);

std::unique_ptr<Iteration> iteration_ptr;
for (size_t i = 0; i < 100; i += 10)
Expand Down Expand Up @@ -880,8 +879,7 @@ inline void constant_scalar(std::string file_ending)
E_x.makeConstant(static_cast<float>(13.37));
auto E_y = s.iterations[1].meshes["E"]["y"];
E_y.resetDataset(Dataset(Datatype::UINT, {1, 2, 3}));
std::shared_ptr<unsigned int> E(
new unsigned int[6], [](unsigned int const *p) { delete[] p; });
std::shared_ptr<unsigned int[]> E(new unsigned int[6]);
unsigned int e{0};
std::generate(E.get(), E.get() + 6, [&e] { return e++; });
E_y.storeChunk(E, {0, 0, 0}, {1, 2, 3});
Expand Down Expand Up @@ -915,9 +913,7 @@ inline void constant_scalar(std::string file_ending)
vel_x.makeConstant(static_cast<short>(-1));
auto vel_y = s.iterations[1].particles["e"]["velocity"]["y"];
vel_y.resetDataset(Dataset(Datatype::ULONGLONG, {3, 2, 1}));
std::shared_ptr<unsigned long long> vel(
new unsigned long long[6],
[](unsigned long long const *p) { delete[] p; });
std::shared_ptr<unsigned long long[]> vel(new unsigned long long[6]);
unsigned long long v{0};
std::generate(vel.get(), vel.get() + 6, [&v] { return v++; });
vel_y.storeChunk(vel, {0, 0, 0}, {3, 2, 1});
Expand Down Expand Up @@ -1100,10 +1096,7 @@ TEST_CASE("flush_without_position_positionOffset", "[serial]")
RecordComponent weighting = e["weighting"][RecordComponent::SCALAR];
weighting.resetDataset(Dataset(Datatype::FLOAT, Extent{2, 2}));
weighting.storeChunk(
std::shared_ptr<float>(
new float[4](), [](float const *ptr) { delete[] ptr; }),
{0, 0},
{2, 2});
std::shared_ptr<float[]>(new float[4]()), {0, 0}, {2, 2});

s.flush();

Expand All @@ -1114,10 +1107,7 @@ TEST_CASE("flush_without_position_positionOffset", "[serial]")
RecordComponent rc = e[key][dim];
rc.resetDataset(Dataset(Datatype::FLOAT, Extent{2, 2}));
rc.storeChunk(
std::shared_ptr<float>(
new float[4](), [](float const *ptr) { delete[] ptr; }),
{0, 0},
{2, 2});
std::shared_ptr<float[]>(new float[4]()), {0, 0}, {2, 2});
}
}
}
Expand Down Expand Up @@ -2086,11 +2076,8 @@ inline void sample_write_thetaMode(std::string file_ending)
unsigned int const N_r = 20;
unsigned int const N_z = 64;

std::shared_ptr<float> E_r_data(
new float[num_fields * N_r * N_z], [](float const *p) { delete[] p; });
std::shared_ptr<double> E_t_data(
new double[num_fields * N_r * N_z],
[](double const *p) { delete[] p; });
std::shared_ptr<float[]> E_r_data(new float[num_fields * N_r * N_z]);
std::shared_ptr<double[]> E_t_data(new double[num_fields * N_r * N_z]);
float e_r{0};
std::generate(
E_r_data.get(), E_r_data.get() + num_fields * N_r * N_z, [&e_r] {
Expand Down Expand Up @@ -4818,9 +4805,7 @@ void variableBasedParticleData()
Datatype datatype = determineDatatype<position_t>();
Extent global_extent = {length};
Dataset dataset = Dataset(datatype, global_extent);
std::shared_ptr<position_t> local_data(
new position_t[length],
[](position_t const *ptr) { delete[] ptr; });
std::shared_ptr<position_t[]> local_data(new position_t[length]);

WriteIterations iterations = series.writeIterations();
for (size_t i = 0; i < 10; ++i)
Expand Down Expand Up @@ -4918,16 +4903,14 @@ TEST_CASE("automatically_deactivate_span", "[serial][adios2]")
E_uncompressed.storeChunk<int>(
{0}, {10}, [&spanWorkaround](size_t size) {
spanWorkaround = true;
return std::shared_ptr<int>(
new int[size]{}, [](auto *ptr) { delete[] ptr; });
return std::shared_ptr<int[]>(new int[size]{});
});

REQUIRE(!spanWorkaround);

E_compressed.storeChunk<int>({0}, {10}, [&spanWorkaround](size_t size) {
spanWorkaround = true;
return std::shared_ptr<int>(
new int[size]{}, [](auto *ptr) { delete[] ptr; });
return std::shared_ptr<int[]>(new int[size]{});
});

REQUIRE(spanWorkaround);
Expand Down Expand Up @@ -4969,8 +4952,7 @@ TEST_CASE("automatically_deactivate_span", "[serial][adios2]")
E_uncompressed.storeChunk<int>(
{0}, {10}, [&spanWorkaround](size_t size) {
spanWorkaround = true;
return std::shared_ptr<int>(
new int[size]{}, [](auto *ptr) { delete[] ptr; });
return std::shared_ptr<int[]>(new int[size]{});
});

REQUIRE(!spanWorkaround);
Expand All @@ -4980,8 +4962,7 @@ TEST_CASE("automatically_deactivate_span", "[serial][adios2]")
E_compressed.storeChunk<int>(
{0}, {10}, [&spanWorkaround](size_t size) {
spanWorkaround = true;
return std::shared_ptr<int>(
new int[size]{}, [](auto *ptr) { delete[] ptr; });
return std::shared_ptr<int[]>(new int[size]{});
});
}
catch (std::invalid_argument const &e)
Expand Down Expand Up @@ -5033,17 +5014,15 @@ TEST_CASE("automatically_deactivate_span", "[serial][adios2]")
E_uncompressed.storeChunk<int>(
{0}, {10}, [&spanWorkaround](size_t size) {
spanWorkaround = true;
return std::shared_ptr<int>(
new int[size]{}, [](auto *ptr) { delete[] ptr; });
return std::shared_ptr<int[]>(new int[size]{});
});

REQUIRE(spanWorkaround);

spanWorkaround = false;
E_compressed.storeChunk<int>({0}, {10}, [&spanWorkaround](size_t size) {
spanWorkaround = true;
return std::shared_ptr<int>(
new int[size]{}, [](auto *ptr) { delete[] ptr; });
return std::shared_ptr<int[]>(new int[size]{});
});

REQUIRE(spanWorkaround);
Expand Down Expand Up @@ -5091,8 +5070,7 @@ void iterate_nonstreaming_series(
*/
[&taskSupportedByBackend](size_t size) {
taskSupportedByBackend = false;
return std::shared_ptr<int>{
new int[size], [](auto *ptr) { delete[] ptr; }};
return std::shared_ptr<int[]>{new int[size]};
});
if (writeSeries.backend() == "ADIOS2")
{
Expand Down

0 comments on commit 990b33a

Please sign in to comment.