Skip to content

Commit

Permalink
[cpp] Convert malloc/free to new/delete
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Sep 24, 2024
1 parent 2abc382 commit d3c717d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 36 deletions.
12 changes: 6 additions & 6 deletions src/cpp/pffdtd/engine_cuda_3d.cu
Original file line number Diff line number Diff line change
Expand Up @@ -1384,12 +1384,12 @@ auto run(Simulation3D<Real> const& sim) -> void {
gpuErrchk(cudaFree(gd->vh1));
gpuErrchk(cudaFree(gd->gh1));
gpuErrchk(cudaFree(gd->u_out_buf));
free(ghd->bn_mask);
free(ghd->bn_ixyz);
free(ghd->bnl_ixyz);
free(ghd->bna_ixyz);
free(ghd->in_ixyz);
free(ghd->out_ixyz);
delete[] ghd->bn_mask;
delete[] ghd->bn_ixyz;
delete[] ghd->bnl_ixyz;
delete[] ghd->bna_ixyz;
delete[] ghd->in_ixyz;
delete[] ghd->out_ixyz;
}
gpuErrchk(cudaFreeHost(u_out_buf));

Expand Down
3 changes: 0 additions & 3 deletions src/cpp/pffdtd/hdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ auto readDataset(hid_t file, char const* dset_str, int ndims, hsize_t* dims, voi
case DataType::Int8: *out = allocate_zeros<int8_t>(N); break;
case DataType::Bool: *out = allocate_zeros<bool>(N); break;
}
if (*out == nullptr) {
raise<std::bad_alloc>();
}

herr_t status = 0;
switch (t) {
Expand Down
16 changes: 8 additions & 8 deletions src/cpp/pffdtd/simulation_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ template<typename Real>
ssaf_bn[i] = (Real)saf_bn[i]; // just cast
}
}
std::free(saf_bn);
delete[] saf_bn;

////////////////////////////////////////////////////////////////////////
// Read signals HDF5 dataset
Expand Down Expand Up @@ -346,7 +346,7 @@ template<typename Real>
mat_quads[mij].bFh = (Real)bFh;
mat_beta[i] += (Real)b;
}
std::free(DEF);
delete[] DEF;
}

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -395,7 +395,7 @@ template<typename Real>
}
}
fmt::println("adj_bn double checked");
std::free(adj_bn_bool);
delete[] adj_bn_bool;

//////////////////
// calculate K_bn from adj_bn
Expand Down Expand Up @@ -440,7 +440,7 @@ template<typename Real>
}
}
fmt::println("bn_mask double checked");
std::free(bn_mask_raw);
delete[] bn_mask_raw;

// count Nbl
int64_t Nbl = 0;
Expand All @@ -463,8 +463,8 @@ template<typename Real>
}
PFFDTD_ASSERT(j == Nbl);
}
std::free(mat_bn);
std::free(ssaf_bn);
delete[] mat_bn;
delete[] ssaf_bn;

fmt::println("separated non-rigid bn");

Expand Down Expand Up @@ -519,8 +519,8 @@ template<typename Real>
for (int64_t cc = 0; cc < Nba; cc++) {
Q_bna_sorted[cc] = Q_bna_unsorted[bna_sort_keys[cc]];
}
std::free(bna_sort_keys);
std::free(Q_bna_unsorted);
delete[] bna_sort_keys;
delete[] Q_bna_unsorted;
fmt::println("sorted ABC nodes for FCC/GPU");
}
}
Expand Down
34 changes: 17 additions & 17 deletions src/cpp/pffdtd/simulation_3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,23 @@ void rescaleOutput(Simulation3D<Real>& sim) {

template<typename Real>
void freeSimulation3D(Simulation3D<Real> const& sim) {
std::free(sim.bn_ixyz);
std::free(sim.bnl_ixyz);
std::free(sim.bna_ixyz);
std::free(sim.Q_bna);
std::free(sim.adj_bn);
std::free(sim.mat_bnl);
std::free(sim.bn_mask);
std::free(sim.ssaf_bnl);
std::free(sim.K_bn);
std::free(sim.in_ixyz);
std::free(sim.out_ixyz);
std::free(sim.out_reorder);
std::free(sim.in_sigs);
std::free(sim.u_out);
std::free(sim.Mb);
std::free(sim.mat_beta);
std::free(sim.mat_quads);
delete[] sim.bn_ixyz;
delete[] sim.bnl_ixyz;
delete[] sim.bna_ixyz;
delete[] sim.Q_bna;
delete[] sim.adj_bn;
delete[] sim.mat_bnl;
delete[] sim.bn_mask;
delete[] sim.ssaf_bnl;
delete[] sim.K_bn;
delete[] sim.in_ixyz;
delete[] sim.out_ixyz;
delete[] sim.out_reorder;
delete[] sim.in_sigs;
delete[] sim.u_out;
delete[] sim.Mb;
delete[] sim.mat_beta;
delete[] sim.mat_quads;
}

} // namespace pffdtd
4 changes: 2 additions & 2 deletions src/cpp/pffdtd/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace pffdtd {

template<typename T>
[[nodiscard]] auto allocate_zeros(std::integral auto count) -> T* {
auto* const ptr = new T[count];
auto const bytes = static_cast<size_t>(count) * sizeof(T);
auto* const ptr = std::malloc(bytes);
std::memset(ptr, 0, bytes);
std::memset(static_cast<void*>(ptr), 0, bytes);
return reinterpret_cast<T*>(ptr);
}

Expand Down

0 comments on commit d3c717d

Please sign in to comment.