Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix segfault in ~Region #3108

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions source/lib/include/region.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ struct Region {
FPTYPE* boxt;
FPTYPE* rec_boxt;
Region();
Region(FPTYPE* extern_boxt, FPTYPE* extern_rec_boxt);
~Region();

private:
bool self_allocated;
};

template <typename FPTYPE>
Expand Down
14 changes: 12 additions & 2 deletions source/lib/src/region.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@
Region<FPTYPE>::Region() {
boxt = new FPTYPE[BOXT_DIM];
rec_boxt = new FPTYPE[BOXT_DIM];
self_allocated = true;
}

template <typename FPTYPE>
Region<FPTYPE>::Region(FPTYPE* extern_boxt, FPTYPE* extern_rec_boxt) {
boxt = extern_boxt;
rec_boxt = extern_rec_boxt;
self_allocated = false;

Check warning on line 24 in source/lib/src/region.cc

View check run for this annotation

Codecov / codecov/patch

source/lib/src/region.cc#L21-L24

Added lines #L21 - L24 were not covered by tests
}

template <typename FPTYPE>
Region<FPTYPE>::~Region() {
delete[] boxt;
delete[] rec_boxt;
if (self_allocated) {
delete[] boxt;
delete[] rec_boxt;
}
}

template struct deepmd::Region<double>;
Expand Down
56 changes: 7 additions & 49 deletions source/lib/tests/test_coord.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ TEST_F(TestNormCoord, cpu_case2) {
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
TEST_F(TestNormCoord, gpu_case0) {
deepmd::Region<double> region;
deepmd::Region<double> region_dev;
double* new_boxt = region_dev.boxt;
double* new_rec_boxt = region_dev.rec_boxt;
init_region_cpu(region, &boxt[0]);
std::vector<double> box_info;
box_info.resize(18);
Expand All @@ -75,11 +72,8 @@ TEST_F(TestNormCoord, gpu_case0) {
std::vector<double> out_c(r0);
deepmd::malloc_device_memory_sync(box_info_dev, box_info);
deepmd::malloc_device_memory_sync(out_c_dev, out_c);
region_dev.boxt = box_info_dev;
region_dev.rec_boxt = box_info_dev + 9;
deepmd::Region<double> region_dev(box_info_dev, box_info_dev + 9);
deepmd::normalize_coord_gpu(out_c_dev, natoms, region_dev);
region_dev.boxt = new_boxt;
region_dev.rec_boxt = new_rec_boxt;
deepmd::memcpy_device_to_host(out_c_dev, out_c);
deepmd::delete_device_memory(box_info_dev);
deepmd::delete_device_memory(out_c_dev);
Expand All @@ -90,9 +84,6 @@ TEST_F(TestNormCoord, gpu_case0) {

TEST_F(TestNormCoord, gpu_case1) {
deepmd::Region<double> region;
deepmd::Region<double> region_dev;
double* new_boxt = region_dev.boxt;
double* new_rec_boxt = region_dev.rec_boxt;
init_region_cpu(region, &boxt[0]);
std::vector<double> box_info;
box_info.resize(18);
Expand All @@ -103,11 +94,8 @@ TEST_F(TestNormCoord, gpu_case1) {
std::vector<double> out_c(r1);
deepmd::malloc_device_memory_sync(box_info_dev, box_info);
deepmd::malloc_device_memory_sync(out_c_dev, out_c);
region_dev.boxt = box_info_dev;
region_dev.rec_boxt = box_info_dev + 9;
deepmd::Region<double> region_dev(box_info_dev, box_info_dev + 9);
deepmd::normalize_coord_gpu(out_c_dev, natoms, region_dev);
region_dev.boxt = new_boxt;
region_dev.rec_boxt = new_rec_boxt;
deepmd::memcpy_device_to_host(out_c_dev, out_c);
deepmd::delete_device_memory(box_info_dev);
deepmd::delete_device_memory(out_c_dev);
Expand All @@ -118,9 +106,6 @@ TEST_F(TestNormCoord, gpu_case1) {

TEST_F(TestNormCoord, gpu_case2) {
deepmd::Region<double> region;
deepmd::Region<double> region_dev;
double* new_boxt = region_dev.boxt;
double* new_rec_boxt = region_dev.rec_boxt;
init_region_cpu(region, &boxt[0]);
std::vector<double> box_info;
box_info.resize(18);
Expand All @@ -131,11 +116,8 @@ TEST_F(TestNormCoord, gpu_case2) {
std::vector<double> out_c(r2);
deepmd::malloc_device_memory_sync(box_info_dev, box_info);
deepmd::malloc_device_memory_sync(out_c_dev, out_c);
region_dev.boxt = box_info_dev;
region_dev.rec_boxt = box_info_dev + 9;
deepmd::Region<double> region_dev(box_info_dev, box_info_dev + 9);
deepmd::normalize_coord_gpu(out_c_dev, natoms, region_dev);
region_dev.boxt = new_boxt;
region_dev.rec_boxt = new_rec_boxt;
deepmd::memcpy_device_to_host(out_c_dev, out_c);
deepmd::delete_device_memory(box_info_dev);
deepmd::delete_device_memory(out_c_dev);
Expand Down Expand Up @@ -298,9 +280,6 @@ TEST_F(TestCopyCoord, gpu) {
std::vector<int> cell_info;
cell_info.resize(23);
deepmd::Region<double> region;
deepmd::Region<double> region_dev;
double* new_boxt = region_dev.boxt;
double* new_rec_boxt = region_dev.rec_boxt;
init_region_cpu(region, &boxt[0]);
deepmd::compute_cell_info(&cell_info[0], rc, region);
std::vector<double> box_info;
Expand All @@ -325,14 +304,11 @@ TEST_F(TestCopyCoord, gpu) {
int_data_dev, nloc * 3 + loc_cellnum + total_cellnum * 3 +
total_cellnum * 3 + loc_cellnum + 1 + total_cellnum +
1 + nloc);
region_dev.boxt = box_info_dev;
region_dev.rec_boxt = box_info_dev + 9;
deepmd::Region<double> region_dev(box_info_dev, box_info_dev + 9);
int ret = deepmd::copy_coord_gpu(out_c_dev, out_t_dev, mapping_dev, &nall,
int_data_dev, in_c_dev, in_t_dev, nloc,
mem_size, loc_cellnum, total_cellnum,
cell_info_dev, region_dev);
region_dev.boxt = new_boxt;
region_dev.rec_boxt = new_rec_boxt;
deepmd::memcpy_device_to_host(out_c_dev, out_c);
deepmd::memcpy_device_to_host(out_t_dev, out_t);
deepmd::memcpy_device_to_host(mapping_dev, mapping);
Expand Down Expand Up @@ -373,9 +349,6 @@ TEST_F(TestCopyCoord, gpu_lessmem) {
std::vector<int> cell_info;
cell_info.resize(23);
deepmd::Region<double> region;
deepmd::Region<double> region_dev;
double* new_boxt = region_dev.boxt;
double* new_rec_boxt = region_dev.rec_boxt;
init_region_cpu(region, &boxt[0]);
deepmd::compute_cell_info(&cell_info[0], rc, region);
std::vector<double> box_info;
Expand All @@ -400,14 +373,11 @@ TEST_F(TestCopyCoord, gpu_lessmem) {
int_data_dev, nloc * 3 + loc_cellnum + total_cellnum * 3 +
total_cellnum * 3 + loc_cellnum + 1 + total_cellnum +
1 + nloc);
region_dev.boxt = box_info_dev;
region_dev.rec_boxt = box_info_dev + 9;
deepmd::Region<double> region_dev(box_info_dev, box_info_dev + 9);
int ret = deepmd::copy_coord_gpu(out_c_dev, out_t_dev, mapping_dev, &nall,
int_data_dev, in_c_dev, in_t_dev, nloc,
mem_size, loc_cellnum, total_cellnum,
cell_info_dev, region_dev);
region_dev.boxt = new_boxt;
region_dev.rec_boxt = new_rec_boxt;
deepmd::memcpy_device_to_host(out_c_dev, out_c);
deepmd::memcpy_device_to_host(out_t_dev, out_t);
deepmd::memcpy_device_to_host(mapping_dev, mapping);
Expand Down Expand Up @@ -544,9 +514,6 @@ TEST_F(TestCopyCoordMoreCell, gpu) {
std::vector<int> cell_info;
cell_info.resize(23);
deepmd::Region<double> region;
deepmd::Region<double> region_dev;
double* new_boxt = region_dev.boxt;
double* new_rec_boxt = region_dev.rec_boxt;
init_region_cpu(region, &boxt[0]);
deepmd::compute_cell_info(&cell_info[0], rc, region);
std::vector<double> box_info;
Expand All @@ -571,14 +538,11 @@ TEST_F(TestCopyCoordMoreCell, gpu) {
int_data_dev, nloc * 3 + loc_cellnum + total_cellnum * 3 +
total_cellnum * 3 + loc_cellnum + 1 + total_cellnum +
1 + nloc);
region_dev.boxt = box_info_dev;
region_dev.rec_boxt = box_info_dev + 9;
deepmd::Region<double> region_dev(box_info_dev, box_info_dev + 9);
int ret = deepmd::copy_coord_gpu(out_c_dev, out_t_dev, mapping_dev, &nall,
int_data_dev, in_c_dev, in_t_dev, nloc,
mem_size, loc_cellnum, total_cellnum,
cell_info_dev, region_dev);
region_dev.boxt = new_boxt;
region_dev.rec_boxt = new_rec_boxt;
deepmd::memcpy_device_to_host(out_c_dev, out_c);
deepmd::memcpy_device_to_host(out_t_dev, out_t);
deepmd::memcpy_device_to_host(mapping_dev, mapping);
Expand Down Expand Up @@ -619,9 +583,6 @@ TEST_F(TestCopyCoordMoreCell, gpu_lessmem) {
std::vector<int> cell_info;
cell_info.resize(23);
deepmd::Region<double> region;
deepmd::Region<double> region_dev;
double* new_boxt = region_dev.boxt;
double* new_rec_boxt = region_dev.rec_boxt;
init_region_cpu(region, &boxt[0]);
deepmd::compute_cell_info(&cell_info[0], rc, region);
std::vector<double> box_info;
Expand All @@ -646,14 +607,11 @@ TEST_F(TestCopyCoordMoreCell, gpu_lessmem) {
int_data_dev, nloc * 3 + loc_cellnum + total_cellnum * 3 +
total_cellnum * 3 + loc_cellnum + 1 + total_cellnum +
1 + nloc);
region_dev.boxt = box_info_dev;
region_dev.rec_boxt = box_info_dev + 9;
deepmd::Region<double> region_dev(box_info_dev, box_info_dev + 9);
int ret = deepmd::copy_coord_gpu(out_c_dev, out_t_dev, mapping_dev, &nall,
int_data_dev, in_c_dev, in_t_dev, nloc,
mem_size, loc_cellnum, total_cellnum,
cell_info_dev, region_dev);
region_dev.boxt = new_boxt;
region_dev.rec_boxt = new_rec_boxt;
deepmd::memcpy_device_to_host(out_c_dev, out_c);
deepmd::memcpy_device_to_host(out_t_dev, out_t);
deepmd::memcpy_device_to_host(mapping_dev, mapping);
Expand Down
8 changes: 1 addition & 7 deletions source/lib/tests/test_simulation_region.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ TEST_F(TestRegion, cpu) {
TEST_F(TestRegion, gpu) {
// check rec_box
deepmd::Region<double> region;
deepmd::Region<double> region_dev;
double* new_boxt = region_dev.boxt;
double* new_rec_boxt = region_dev.rec_boxt;
double *boxt_dev = NULL, *rec_boxt_dev = NULL;
double *ref_rp_dev = NULL, *ref_ri_dev = NULL;
init_region_cpu(region, &ref_boxt[0]);
Expand All @@ -90,8 +87,7 @@ TEST_F(TestRegion, gpu) {
deepmd::malloc_device_memory_sync(rec_boxt_dev, region.rec_boxt, 9);
deepmd::malloc_device_memory_sync(ref_rp_dev, ref_rp);
deepmd::malloc_device_memory_sync(ref_ri_dev, ref_ri);
region_dev.boxt = boxt_dev;
region_dev.rec_boxt = rec_boxt_dev;
deepmd::Region<double> region_dev(boxt_dev, rec_boxt_dev);
// check volume
double vol[1];
double* vol_dev = NULL;
Expand Down Expand Up @@ -141,8 +137,6 @@ TEST_F(TestRegion, gpu) {
deepmd::delete_device_memory(rp2_dev);
deepmd::delete_device_memory(rp_dev);
deepmd::delete_device_memory(ri2_dev);
region_dev.boxt = new_boxt;
region_dev.rec_boxt = new_rec_boxt;
}
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM

Expand Down
8 changes: 1 addition & 7 deletions source/op/prod_env_mat_multi_device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1496,11 +1496,7 @@ static int _norm_copy_coord_gpu(OpKernelContext* context,
int* int_data_dev = cell_info_dev + 23;
deepmd::memcpy_host_to_device(box_info_dev, box_info, 18);
deepmd::memcpy_host_to_device(cell_info_dev, cell_info, 23);
deepmd::Region<FPTYPE> region_dev;
FPTYPE* new_boxt = region_dev.boxt;
FPTYPE* new_rec_boxt = region_dev.rec_boxt;
region_dev.boxt = box_info_dev;
region_dev.rec_boxt = box_info_dev + 9;
deepmd::Region<FPTYPE> region_dev(box_info_dev, box_info_dev + 9);
deepmd::normalize_coord_gpu(tmp_coord, nall, region_dev);
int tt;
for (tt = 0; tt < max_cpy_trial; ++tt) {
Expand Down Expand Up @@ -1531,8 +1527,6 @@ static int _norm_copy_coord_gpu(OpKernelContext* context,
}
}
}
region_dev.boxt = new_boxt;
region_dev.rec_boxt = new_rec_boxt;
return (tt != max_cpy_trial);
}

Expand Down
4 changes: 3 additions & 1 deletion source/tests/test_auto_batch_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ def test_execute_oom_gpu(self, mock_is_gpu_available):
self.assertEqual(result.shape, (256, 2))

@unittest.mock.patch("tensorflow.compat.v1.test.is_gpu_available")
def test_execute_oom_cpu(self, mock_is_gpu_available):
@unittest.mock.patch("tensorflow.compat.v1.config.experimental.get_visible_devices")
def test_execute_oom_cpu(self, mock_is_gpu_available, mock_get_visible_devices):
mock_is_gpu_available.return_value = False
mock_get_visible_devices.return_value = []
# initial batch size 256 = 128 * 2, nb is always 128
auto_batch_size = AutoBatchSize(256, 2.0)
nb, result = auto_batch_size.execute(self.oom, 1, 2)
Expand Down
Loading