Skip to content

Commit 36d3ce3

Browse files
authored
Merge pull request #472 from esmf-org/fix/opt-fpe0
Alternative approach to prevent log10() pre-computation in optimized mode
2 parents 2b498d2 + 21e06d2 commit 36d3ce3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/Infrastructure/VM/src/ESMCI_VMKernel.C

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,7 +1874,9 @@ void *VMK::startup(class VMKPlan *vmp, void *(fctp)(void *, void *),
18741874
#endif
18751875
// next, determine new_npets and new_mypet_base ...
18761876
int new_mypet_base=0;
1877-
int new_npets=0;
1877+
volatile int new_npets=0; // volatile to prevent precompute of log10() below
1878+
// in optimized mode or else might trigger SIGFPE
1879+
// for new_npets==1 if user code sets FPE trapping
18781880
int found_my_pet_flag = 0;
18791881
for (int ii=0; ii<npets; ii++){
18801882
int i = vmp->petlist[ii]; // indirection to preserve petlist order
@@ -2596,8 +2598,8 @@ void *VMK::startup(class VMKPlan *vmp, void *(fctp)(void *, void *),
25962598
pos = stdTemp.rfind('*'); // right most asterisk
25972599
if (pos != string::npos){
25982600
// found wildcard -> replace with local pet number
2599-
int digits = (int) log10(new_npets); // always safe
2600-
if (digits * 10 != new_npets) ++digits; // correct number of digits
2601+
int digits = 1; // default number of digits needed
2602+
if (new_npets>1) digits = (int) log10(new_npets-1) + 1;
26012603
std::stringstream label; // fill with zeros from left
26022604
label << setw(digits) << setfill('0') << to_string(sarg[i].mypet);
26032605
sarg[i].stdoutName = stdTemp.substr(0, pos) + label.str()
@@ -2613,8 +2615,8 @@ void *VMK::startup(class VMKPlan *vmp, void *(fctp)(void *, void *),
26132615
pos = stdTemp.rfind('*'); // right most asterisk
26142616
if (pos != string::npos){
26152617
// found wildcard -> replace with local pet number
2616-
int digits = (int) log10(new_npets); // always safe
2617-
if (digits * 10 != new_npets) ++digits; // correct number of digits
2618+
int digits = 1; // default number of digits needed
2619+
if (new_npets>1) digits = (int) log10(new_npets-1) + 1;
26182620
std::stringstream label; // fill with zeros from left
26192621
label << setw(digits) << setfill('0') << to_string(sarg[i].mypet);
26202622
sarg[i].stderrName = stdTemp.substr(0, pos) + label.str()

0 commit comments

Comments
 (0)