Skip to content

Commit 1097062

Browse files
authored
resolve "Multiplication result converted to larger type" (#3159)
Follow up #3149. --------- Signed-off-by: Jinzhe Zeng <[email protected]>
1 parent 4d82430 commit 1097062

32 files changed

+202
-117
lines changed

source/api_c/include/deepmd.hpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,14 +1060,15 @@ class DeepPot {
10601060
const int &nloc,
10611061
const std::vector<VALUETYPE> &fparam,
10621062
const std::vector<VALUETYPE> &aparam) const {
1063-
if (fparam.size() != dfparam && fparam.size() != nframes * dfparam) {
1063+
if (fparam.size() != dfparam &&
1064+
fparam.size() != static_cast<size_t>(nframes) * dfparam) {
10641065
throw deepmd::hpp::deepmd_exception(
10651066
"the dim of frame parameter provided is not consistent with what the "
10661067
"model uses");
10671068
}
10681069

1069-
if (aparam.size() != daparam * nloc &&
1070-
aparam.size() != nframes * daparam * nloc) {
1070+
if (aparam.size() != static_cast<size_t>(daparam) * nloc &&
1071+
aparam.size() != static_cast<size_t>(nframes) * daparam * nloc) {
10711072
throw deepmd::hpp::deepmd_exception(
10721073
"the dim of atom parameter provided is not consistent with what the "
10731074
"model uses");
@@ -1081,9 +1082,10 @@ class DeepPot {
10811082
if (param.size() == dparam) {
10821083
out_param.resize(static_cast<size_t>(nframes) * dparam);
10831084
for (int ii = 0; ii < nframes; ++ii) {
1084-
std::copy(param.begin(), param.end(), out_param.begin() + ii * dparam);
1085+
std::copy(param.begin(), param.end(),
1086+
out_param.begin() + static_cast<std::ptrdiff_t>(ii) * dparam);
10851087
}
1086-
} else if (param.size() == nframes * dparam) {
1088+
} else if (param.size() == static_cast<size_t>(nframes) * dparam) {
10871089
out_param = param;
10881090
}
10891091
}
@@ -1184,7 +1186,8 @@ class DeepPotModelDevi {
11841186

11851187
// memory will be continous for std::vector but not std::vector<std::vector>
11861188
std::vector<double> energy_flat(numb_models);
1187-
std::vector<VALUETYPE> force_flat(numb_models * natoms * 3);
1189+
std::vector<VALUETYPE> force_flat(static_cast<size_t>(numb_models) *
1190+
natoms * 3);
11881191
std::vector<VALUETYPE> virial_flat(numb_models * 9);
11891192
double *ener_ = &energy_flat[0];
11901193
VALUETYPE *force_ = &force_flat[0];
@@ -1260,10 +1263,13 @@ class DeepPotModelDevi {
12601263
const int *atype_ = &atype[0];
12611264

12621265
std::vector<double> energy_flat(numb_models);
1263-
std::vector<VALUETYPE> force_flat(numb_models * natoms * 3);
1266+
std::vector<VALUETYPE> force_flat(static_cast<size_t>(numb_models) *
1267+
natoms * 3);
12641268
std::vector<VALUETYPE> virial_flat(numb_models * 9);
1265-
std::vector<VALUETYPE> atom_energy_flat(numb_models * natoms);
1266-
std::vector<VALUETYPE> atom_virial_flat(numb_models * natoms * 9);
1269+
std::vector<VALUETYPE> atom_energy_flat(static_cast<size_t>(numb_models) *
1270+
natoms);
1271+
std::vector<VALUETYPE> atom_virial_flat(static_cast<size_t>(numb_models) *
1272+
natoms * 9);
12671273
double *ener_ = &energy_flat[0];
12681274
VALUETYPE *force_ = &force_flat[0];
12691275
VALUETYPE *virial_ = &virial_flat[0];
@@ -1402,8 +1408,8 @@ class DeepPotModelDevi {
14021408

14031409
for (unsigned ii = 0; ii < numb_models; ++ii) {
14041410
for (unsigned jj = 0; jj < nloc; ++jj) {
1405-
const VALUETYPE *tmp_f = &(xx[ii][jj * stride]);
1406-
const VALUETYPE *tmp_avg = &(avg[jj * stride]);
1411+
const VALUETYPE *tmp_f = &(xx[ii][static_cast<size_t>(jj) * stride]);
1412+
const VALUETYPE *tmp_avg = &(avg[static_cast<size_t>(jj) * stride]);
14071413
for (unsigned dd = 0; dd < stride; ++dd) {
14081414
VALUETYPE vdiff = tmp_f[dd] - tmp_avg[dd];
14091415
std[jj] += vdiff * vdiff;
@@ -1432,7 +1438,7 @@ class DeepPotModelDevi {
14321438
assert(nloc * stride == ndof);
14331439

14341440
for (unsigned ii = 0; ii < nloc; ++ii) {
1435-
const VALUETYPE *tmp_avg = &(avg[ii * stride]);
1441+
const VALUETYPE *tmp_avg = &(avg[static_cast<size_t>(ii) * stride]);
14361442
VALUETYPE f_norm = 0.0;
14371443
for (unsigned dd = 0; dd < stride; ++dd) {
14381444
f_norm += tmp_avg[dd] * tmp_avg[dd];
@@ -1477,14 +1483,15 @@ class DeepPotModelDevi {
14771483
const int &nloc,
14781484
const std::vector<VALUETYPE> &fparam,
14791485
const std::vector<VALUETYPE> &aparam) const {
1480-
if (fparam.size() != dfparam && fparam.size() != nframes * dfparam) {
1486+
if (fparam.size() != dfparam &&
1487+
fparam.size() != static_cast<size_t>(nframes) * dfparam) {
14811488
throw deepmd::hpp::deepmd_exception(
14821489
"the dim of frame parameter provided is not consistent with what the "
14831490
"model uses");
14841491
}
14851492

1486-
if (aparam.size() != daparam * nloc &&
1487-
aparam.size() != nframes * daparam * nloc) {
1493+
if (aparam.size() != static_cast<size_t>(daparam) * nloc &&
1494+
aparam.size() != static_cast<size_t>(nframes) * daparam * nloc) {
14881495
throw deepmd::hpp::deepmd_exception(
14891496
"the dim of atom parameter provided is not consistent with what the "
14901497
"model uses");
@@ -1498,9 +1505,10 @@ class DeepPotModelDevi {
14981505
if (param.size() == dparam) {
14991506
out_param.resize(static_cast<size_t>(nframes) * dparam);
15001507
for (int ii = 0; ii < nframes; ++ii) {
1501-
std::copy(param.begin(), param.end(), out_param.begin() + ii * dparam);
1508+
std::copy(param.begin(), param.end(),
1509+
out_param.begin() + static_cast<std::ptrdiff_t>(ii) * dparam);
15021510
}
1503-
} else if (param.size() == nframes * dparam) {
1511+
} else if (param.size() == static_cast<size_t>(nframes) * dparam) {
15041512
out_param = param;
15051513
}
15061514
}

source/api_c/src/c_api.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ void DP_SelectMapInt(const int* in,
14141414
int* out) {
14151415
std::vector<int> in_(in, in + stride * nall1);
14161416
std::vector<int> fwd_map_(fwd_map, fwd_map + nall1);
1417-
std::vector<int> out_(stride * nall2);
1417+
std::vector<int> out_(static_cast<size_t>(stride) * nall2);
14181418
deepmd::select_map(out_, in_, fwd_map_, stride);
14191419
if (out) {
14201420
std::copy(out_.begin(), out_.end(), out);

source/api_cc/src/AtomMap.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ void AtomMap::forward(typename std::vector<VALUETYPE>::iterator out,
3939
int gro_i = idx_map[ii];
4040
for (int dd = 0; dd < stride; ++dd) {
4141
// out[ii*stride+dd] = in[gro_i*stride+dd];
42-
*(out + kk * nall * stride + ii * stride + dd) =
43-
*(in + kk * nall * stride + gro_i * stride + dd);
42+
*(out + static_cast<std::ptrdiff_t>(kk) * nall * stride +
43+
static_cast<std::ptrdiff_t>(ii) * stride + dd) =
44+
*(in + static_cast<std::ptrdiff_t>(kk) * nall * stride +
45+
static_cast<std::ptrdiff_t>(gro_i) * stride + dd);
4446
}
4547
}
4648
}
@@ -58,8 +60,10 @@ void AtomMap::backward(typename std::vector<VALUETYPE>::iterator out,
5860
int gro_i = idx_map[ii];
5961
for (int dd = 0; dd < stride; ++dd) {
6062
// out[gro_i*stride+dd] = in[ii*stride+dd];
61-
*(out + kk * nall * stride + gro_i * stride + dd) =
62-
*(in + kk * nall * stride + ii * stride + dd);
63+
*(out + static_cast<std::ptrdiff_t>(kk) * nall * stride +
64+
static_cast<std::ptrdiff_t>(gro_i) * stride + dd) =
65+
*(in + static_cast<std::ptrdiff_t>(kk) * nall * stride +
66+
static_cast<std::ptrdiff_t>(ii) * stride + dd);
6367
}
6468
}
6569
}

source/api_cc/src/DeepPot.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,8 @@ void DeepPotModelDevi::compute_std(
761761

762762
for (unsigned ii = 0; ii < numb_models; ++ii) {
763763
for (unsigned jj = 0; jj < nloc; ++jj) {
764-
const VALUETYPE* tmp_f = &(xx[ii][jj * stride]);
765-
const VALUETYPE* tmp_avg = &(avg[jj * stride]);
764+
const VALUETYPE* tmp_f = &(xx[ii][static_cast<size_t>(jj) * stride]);
765+
const VALUETYPE* tmp_avg = &(avg[static_cast<size_t>(jj) * stride]);
766766
for (unsigned dd = 0; dd < stride; ++dd) {
767767
VALUETYPE vdiff = tmp_f[dd] - tmp_avg[dd];
768768
std[jj] += vdiff * vdiff;
@@ -833,7 +833,7 @@ void DeepPotModelDevi::compute_relative_std(std::vector<VALUETYPE>& std,
833833
assert(nloc * stride == ndof);
834834

835835
for (unsigned ii = 0; ii < nloc; ++ii) {
836-
const VALUETYPE* tmp_avg = &(avg[ii * stride]);
836+
const VALUETYPE* tmp_avg = &(avg[static_cast<size_t>(ii) * stride]);
837837
VALUETYPE f_norm = 0.0;
838838
for (unsigned dd = 0; dd < stride; ++dd) {
839839
f_norm += tmp_avg[dd] * tmp_avg[dd];

source/ipi/src/Convert.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void Convert<VALUETYPE>::forward(std::vector<VALUETYPE>& out,
3030
const int stride) const {
3131
assert(in.size() == stride * idx_map.size());
3232
int natoms = idx_map.size();
33-
out.resize(stride * natoms);
33+
out.resize(static_cast<size_t>(stride) * natoms);
3434
for (int ii = 0; ii < natoms; ++ii) {
3535
int gro_i = idx_map[ii];
3636
for (int dd = 0; dd < stride; ++dd) {
@@ -45,7 +45,7 @@ void Convert<VALUETYPE>::backward(std::vector<VALUETYPE>& out,
4545
const int stride) const {
4646
int natoms = idx_map.size();
4747
assert(in.size() == stride * idx_map.size());
48-
out.resize(stride * natoms);
48+
out.resize(static_cast<size_t>(stride) * natoms);
4949
for (int ii = 0; ii < natoms; ++ii) {
5050
int gro_i = idx_map[ii];
5151
for (int dd = 0; dd < stride; ++dd) {

source/lib/tests/test_env_mat_a.cc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,11 +504,12 @@ TEST_F(TestEnvMatA, prod_cpu) {
504504
deepmd::InputNlist inlist(nloc, &ilist[0], &numneigh[0], &firstneigh[0]);
505505
deepmd::convert_nlist(inlist, nlist_a_cpy);
506506

507-
std::vector<double> em(nloc * ndescrpt), em_deriv(nloc * ndescrpt * 3),
508-
rij(nloc * nnei * 3);
509-
std::vector<int> nlist(nloc * nnei);
510-
std::vector<double> avg(ntypes * ndescrpt, 0);
511-
std::vector<double> std(ntypes * ndescrpt, 1);
507+
std::vector<double> em(static_cast<size_t>(nloc) * ndescrpt),
508+
em_deriv(static_cast<size_t>(nloc) * ndescrpt * 3),
509+
rij(static_cast<size_t>(nloc) * nnei * 3);
510+
std::vector<int> nlist(static_cast<size_t>(nloc) * nnei);
511+
std::vector<double> avg(static_cast<size_t>(ntypes) * ndescrpt, 0);
512+
std::vector<double> std(static_cast<size_t>(ntypes) * ndescrpt, 1);
512513
deepmd::prod_env_mat_a_cpu(&em[0], &em_deriv[0], &rij[0], &nlist[0],
513514
&posi_cpy[0], &atype_cpy[0], inlist, max_nbor_size,
514515
&avg[0], &std[0], nloc, nall, rc, rc_smth, sec_a);
@@ -538,11 +539,12 @@ TEST_F(TestEnvMatA, prod_cpu_equal_cpu) {
538539
std::vector<int *> firstneigh(nloc);
539540
deepmd::InputNlist inlist(nloc, &ilist[0], &numneigh[0], &firstneigh[0]);
540541
convert_nlist(inlist, nlist_a_cpy);
541-
std::vector<double> em(nloc * ndescrpt), em_deriv(nloc * ndescrpt * 3),
542-
rij(nloc * nnei * 3);
543-
std::vector<int> nlist(nloc * nnei);
544-
std::vector<double> avg(ntypes * ndescrpt, 0);
545-
std::vector<double> std(ntypes * ndescrpt, 1);
542+
std::vector<double> em(static_cast<size_t>(nloc) * ndescrpt),
543+
em_deriv(static_cast<size_t>(nloc) * ndescrpt * 3),
544+
rij(static_cast<size_t>(nloc) * nnei * 3);
545+
std::vector<int> nlist(static_cast<size_t>(nloc) * nnei);
546+
std::vector<double> avg(static_cast<size_t>(ntypes) * ndescrpt, 0);
547+
std::vector<double> std(static_cast<size_t>(ntypes) * ndescrpt, 1);
546548
deepmd::prod_env_mat_a_cpu(&em[0], &em_deriv[0], &rij[0], &nlist[0],
547549
&posi_cpy[0], &atype_cpy[0], inlist, max_nbor_size,
548550
&avg[0], &std[0], nloc, nall, rc, rc_smth, sec_a);

source/lib/tests/test_env_mat_a_mix.cc

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,12 @@ TEST_F(TestEnvMatAMix, prod_cpu) {
532532
deepmd::InputNlist inlist(nloc, &ilist[0], &numneigh[0], &firstneigh[0]);
533533
deepmd::convert_nlist(inlist, nlist_a_cpy);
534534

535-
std::vector<double> em(nloc * ndescrpt), em_deriv(nloc * ndescrpt * 3),
536-
rij(nloc * nnei * 3);
537-
std::vector<int> nlist(nloc * nnei);
538-
std::vector<int> ntype(nloc * nnei);
539-
bool *nmask = new bool[nloc * nnei];
535+
std::vector<double> em(static_cast<size_t>(nloc) * ndescrpt),
536+
em_deriv(static_cast<size_t>(nloc) * ndescrpt * 3),
537+
rij(static_cast<size_t>(nloc) * nnei * 3);
538+
std::vector<int> nlist(static_cast<size_t>(nloc) * nnei);
539+
std::vector<int> ntype(static_cast<size_t>(nloc) * nnei);
540+
bool *nmask = new bool[static_cast<size_t>(nloc) * nnei];
540541
memset(nmask, 0, sizeof(bool) * nloc * nnei);
541542
std::vector<double> avg(ntypes * ndescrpt, 0);
542543
std::vector<double> std(ntypes * ndescrpt, 1);
@@ -575,11 +576,12 @@ TEST_F(TestEnvMatAMix, prod_cpu_equal_cpu) {
575576
std::vector<int *> firstneigh(nloc);
576577
deepmd::InputNlist inlist(nloc, &ilist[0], &numneigh[0], &firstneigh[0]);
577578
convert_nlist(inlist, nlist_a_cpy);
578-
std::vector<double> em(nloc * ndescrpt), em_deriv(nloc * ndescrpt * 3),
579-
rij(nloc * nnei * 3);
580-
std::vector<int> nlist(nloc * nnei);
581-
std::vector<double> avg(ntypes * ndescrpt, 0);
582-
std::vector<double> std(ntypes * ndescrpt, 1);
579+
std::vector<double> em(static_cast<size_t>(nloc) * ndescrpt),
580+
em_deriv(static_cast<size_t>(nloc) * ndescrpt * 3),
581+
rij(static_cast<size_t>(nloc) * nnei * 3);
582+
std::vector<int> nlist(static_cast<size_t>(nloc) * nnei);
583+
std::vector<double> avg(static_cast<size_t>(ntypes) * ndescrpt, 0);
584+
std::vector<double> std(static_cast<size_t>(ntypes) * ndescrpt, 1);
583585
deepmd::prod_env_mat_a_cpu(&em[0], &em_deriv[0], &rij[0], &nlist[0],
584586
&posi_cpy[0], &atype[0], inlist, max_nbor_size,
585587
&avg[0], &std[0], nloc, nall, rc, rc_smth, sec_a,
@@ -652,11 +654,12 @@ TEST_F(TestEnvMatAMix, prod_gpu) {
652654
deepmd::InputNlist inlist(nloc, &ilist[0], &numneigh[0], &firstneigh[0]),
653655
gpu_inlist;
654656
convert_nlist(inlist, nlist_a_cpy);
655-
std::vector<double> em(nloc * ndescrpt, 0.0),
656-
em_deriv(nloc * ndescrpt * 3, 0.0), rij(nloc * nnei * 3, 0.0);
657-
std::vector<int> nlist(nloc * nnei, 0);
658-
std::vector<int> ntype(nloc * nnei, 0);
659-
bool *nmask = new bool[nloc * nnei];
657+
std::vector<double> em(static_cast<size_t>(nloc) * ndescrpt, 0.0),
658+
em_deriv(nloc * ndescrpt * 3, 0.0),
659+
rij(static_cast<size_t>(nloc) * nnei * 3, 0.0);
660+
std::vector<int> nlist(static_cast<size_t>(nloc) * nnei, 0);
661+
std::vector<int> ntype(static_cast<size_t>(nloc) * nnei, 0);
662+
bool *nmask = new bool[static_cast<size_t>(nloc) * nnei];
660663
memset(nmask, 0, sizeof(bool) * nloc * nnei);
661664
std::vector<double> avg(ntypes * ndescrpt, 0);
662665
std::vector<double> std(ntypes * ndescrpt, 1);

source/lib/tests/test_env_mat_a_nvnmd.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ TEST_F(TestEnvMatANvnmd, prod_cpu) {
274274
deepmd::convert_nlist(inlist, nlist_a_cpy);
275275

276276
std::vector<double> em(nloc * ndescrpt), em_deriv(nloc * ndescrpt * 3),
277-
rij(nloc * nnei * 3);
277+
rij(static_cast<size_t>(nloc) * nnei * 3);
278278
std::vector<int> nlist(nloc * nnei);
279279
std::vector<double> avg(ntypes * ndescrpt, 0);
280280
std::vector<double> std(ntypes * ndescrpt, 1);
@@ -308,7 +308,7 @@ TEST_F(TestEnvMatANvnmd, prod_cpu_equal_cpu) {
308308
deepmd::InputNlist inlist(nloc, &ilist[0], &numneigh[0], &firstneigh[0]);
309309
convert_nlist(inlist, nlist_a_cpy);
310310
std::vector<double> em(nloc * ndescrpt), em_deriv(nloc * ndescrpt * 3),
311-
rij(nloc * nnei * 3);
311+
rij(static_cast<size_t>(nloc) * nnei * 3);
312312
std::vector<int> nlist(nloc * nnei);
313313
std::vector<double> avg(ntypes * ndescrpt, 0);
314314
std::vector<double> std(ntypes * ndescrpt, 1);

source/lmp/compute_deeptensor_atom.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,6 @@ void ComputeDeeptensorAtom::compute_peratom() {
178178
------------------------------------------------------------------------- */
179179

180180
double ComputeDeeptensorAtom::memory_usage() {
181-
double bytes = nmax * size_peratom_cols * sizeof(double);
181+
double bytes = static_cast<size_t>(nmax) * size_peratom_cols * sizeof(double);
182182
return bytes;
183183
}

source/op/ewald_recp.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ class EwaldRecpOp : public OpKernel {
5656

5757
// check the sizes
5858
OP_REQUIRES(
59-
context, (nsamples * nloc * 3 == coord_tensor.shape().dim_size(0)),
59+
context,
60+
(static_cast<int64_t>(nsamples) * nloc * 3 ==
61+
coord_tensor.shape().dim_size(0)),
6062
errors::InvalidArgument("coord number of samples should match"));
6163
OP_REQUIRES(
62-
context, (nsamples * nloc * 1 == charge_tensor.shape().dim_size(0)),
64+
context,
65+
(static_cast<int64_t>(nsamples) * nloc * 1 ==
66+
charge_tensor.shape().dim_size(0)),
6367
errors::InvalidArgument("charge number of samples should match"));
6468
OP_REQUIRES(
6569
context, (nsamples * 9 == box_tensor.shape().dim_size(0)),

0 commit comments

Comments
 (0)