CyprienBosserelle / xbeach_gpu

Lightweight version of XBeach that uses CUDA C to run on the GPU. The code is used to simulate coastal waves, currents, sediment transport and beach morphology changes during extreme events. Visit:
http://cyprienbosserelle.github.io/xbeach_gpu/
GNU General Public License v3.0
13 stars 1 forks source link

Adding optimisation at build time yields unresolved min/max errors #52

Open davidbenncsiro opened 1 year ago

davidbenncsiro commented 1 year ago

A colleague noticed that when an optimisation switch is added to CCFLAGS or NVCCFLAGS (e.g. -O1 or -O2) in the Makefile, min/max unresolved errors occur. Adding the std:: prefix to each occurrence solves that problem, as follows:

diff --git a/makjonswap.cpp b/makjonswap.cpp
index f9043a0..2a3df0e 100644
--- a/makjonswap.cpp
+++ b/makjonswap.cpp
@@ -76,12 +76,12 @@ void makjonswap(XBGPUParam Param, std::vector<Wavebndparam> wavebnd, int step, i
    double * x, *y;

    double Hs = wavebnd[step].Hs;
-   double Tp = max(wavebnd[step].Tp,1.5); // for very small Tp the wave group generator will request a Giant amount of memory so we need to cap it here
+   double Tp = std::max(wavebnd[step].Tp,1.5); // for very small Tp the wave group generator will request a Giant amount of memory so we need to cap it here
    double Dp = wavebnd[step].Dp; // converted to the main angle already
    double mainang = Dp;
    double fp = 1 / Tp;
    double gam = wavebnd[step].gamma;
-   double scoeff = max(round(min(wavebnd[step].s,1000.0)),10.0);
+   double scoeff = std::max(round(std::min(wavebnd[step].s,1000.0)),10.0);

    printf("Generating JONSWAP spectrum: Hs=%f, Tp=%f, Dp=%f, gam=%f, s=%f\n",Hs,Tp,Dp,gam,scoeff);
@@ -311,8 +311,8 @@ void GenWGnLBW(XBGPUParam Param, int nf, int ndir,double * HRfreq,double * HRdir
    Sfmax = 0.0;
    for (int n = 0; n < nf; n++)
    {
-       fmax = max(fmax,HRfreq[n]);
-       Sfmax = max(Sfmax, Sf[n]);
+       fmax = std::max(fmax,HRfreq[n]);
+       Sfmax = std::max(Sfmax, Sf[n]);
    }
    fmax = 2.0*fmax; //???? 

@@ -340,7 +340,7 @@ void GenWGnLBW(XBGPUParam Param, int nf, int ndir,double * HRfreq,double * HRdir
    //wave frequency range
    K = ceil(Param.rtlength*(HRfreq[ind2] - HRfreq[ind1]) + 1);
    //also include minimum number of components
-   K = (int)max(K*1.0, Kmin*1.0);// workaround because template for int not compiling for some reason
+   K = (int)std::max(K*1.0, Kmin*1.0);// workaround because template for int not compiling for some reason

    fgen = (double *)malloc(K*sizeof(double));
    phigen = (double *)malloc(K*sizeof(double));
@@ -474,7 +474,7 @@ void GenWGnLBW(XBGPUParam Param, int nf, int ndir,double * HRfreq,double * HRdir

        if (Sf[n] >= Sfmax*trepfac)
        {
-           temptrep += (Sf[n] / max(HRfreq[n], 0.001));
+           temptrep += (Sf[n] / std::max(HRfreq[n], 0.001));
            tempf += Sf[n];

        }
@@ -534,7 +534,7 @@ void GenWGnLBW(XBGPUParam Param, int nf, int ndir,double * HRfreq,double * HRdir
    double Tbc = 1 / fgen[0]; //Should be Trep or 1/fpeak...
    int ntaper = (int)((5.0*Tbc) / dtin);

-   for (int n = 0; n < (int)min(1.0*ntaper, 1.0*tslen); n++)
+   for (int n = 0; n < (int)std::min(1.0*ntaper, 1.0*tslen); n++)
    {
        taperf[n] = tanh(5.0*n / ntaper); //
        taperw[n] = tanh(5.0*n / ntaper); //
@@ -587,7 +587,7 @@ void GenWGnLBW(XBGPUParam Param, int nf, int ndir,double * HRfreq,double * HRdir
    for (int i = 0; i < K; i++)
    {
        dummy = interp1DMono(nf, HRfreq, Sf, fgen[i]);
-       vargenq[i] = min(vargen[i] ,dummy);
+       vargenq[i] = std::min(vargen[i] ,dummy);
    }

    //////////////////////////////////////
@@ -1077,7 +1077,7 @@ void GenWGnLBW(XBGPUParam Param, int nf, int ndir,double * HRfreq,double * HRdir
            //Modification Robert + Jaap: make sure that the bound long wave amplitude does not
            //  !explode when offshore boundary is too close to shore,
            //  !by limiting the interaction group velocity
-           cg3[i + m*(K - 1)] = min(cg3[i + m*(K - 1)], Param.nmax*sqrt(Param.g / k3*tanh(k3*Param.offdepth)));
+           cg3[i + m*(K - 1)] = std::min(cg3[i + m*(K - 1)], Param.nmax*sqrt(Param.g / k3*tanh(k3*Param.offdepth)));

            //Determine difference - interaction coefficient according to Herbers 1994
            //  !eq.A5
diff --git a/read_input.cpp b/read_input.cpp
index d34dc01..e3285a2 100644
--- a/read_input.cpp
+++ b/read_input.cpp
@@ -1363,20 +1363,20 @@ XBGPUParam checkparamsanity(XBGPUParam XParam, std::vector<SLBnd> slbnd, std::ve
        XParam.endtime = 1.0 / tiny; //==huge
    //  if (slbnd.back().time>0.0 && wndbnd.back().time > 0.0)
    //  {
-   //      XParam.endtime = min(slbnd.back().time, wndbnd.back().time);
+   //      XParam.endtime = std::min(slbnd.back().time, wndbnd.back().time);
    //  }
    //
    //  
    }

    //Check that endtime is no longer than the shortest boundary
-   double endbnd = min(slbnd.back().time, wndbnd.back().time);
+   double endbnd = std::min(slbnd.back().time, wndbnd.back().time);

-   endbnd = min(endbnd, wavbnd.back().time);
+   endbnd = std::min(endbnd, wavbnd.back().time);

-   XParam.endtime = min(XParam.endtime, endbnd);
+   XParam.endtime = std::min(XParam.endtime, endbnd);

    // Issue a warning?

diff --git a/tools.cpp b/tools.cpp
index 29cc687..a6fd4d6 100644
--- a/tools.cpp
+++ b/tools.cpp
@@ -46,11 +46,11 @@ double interp1D(int nx, double *x, double *y, double xx)
        diffx = xx - x[i];
        if (diffx <= 0.0)
        {
-           indx = (int)max(i*1.0 - 1, 0.0);
+           indx = (int)std::max(i*1.0 - 1, 0.0);
            break;
        }
    }
-   indxp = (int)min(indx*1.0 + 1, nx*1.0 - 1);
+   indxp = (int)std::min(indx*1.0 + 1, nx*1.0 - 1);
    prevx = x[indx];
    nextx = x[indxp];
    prevy = y[indx];
@@ -73,13 +73,13 @@ double interp1DMono(int nx, double *x, double *y, double xx)
    int indx = 0;
    int indxp;

-   double diffx = max(xx-x[0],0.0);//This has to be positive!
+   double diffx = std::max(xx-x[0],0.0);//This has to be positive!

    indx = (int)floor(diffx / dx);

-   indxp = (int)min(indx*1.0 + 1, nx*1.0 - 1);
+   indxp = (int)std::min(indx*1.0 + 1, nx*1.0 - 1);
    prevx = x[indx];
    nextx = x[indxp];
    prevy = y[indx];
@@ -110,13 +110,13 @@ double Interp2(int nx, int ny, double *x, double *y, double *z, double xx, doubl
        diffx = xx - x[i];
        if (diffx <= 0.0)
        {
-           indx = (int)max(i*1.0 - 1, 0.0);
+           indx = (int)std::max(i*1.0 - 1, 0.0);
            break;
        }
    }

    x1 = x[indx];
-   indxp = (int)min(indx*1.0 + 1, nx*1.0 - 1);
+   indxp = (int)std::min(indx*1.0 + 1, nx*1.0 - 1);
    x2 = x[indxp];

    int indy = 0;
@@ -131,13 +131,13 @@ double Interp2(int nx, int ny, double *x, double *y, double *z, double xx, doubl
        diffy = yy - y[i];
        if (diffy <= 0.0)
        {
-           indy = (int)max(i*1.0 - 1, 0.0);
+           indy = (int)std::max(i*1.0 - 1, 0.0);
            break;
        }
    }

    y1 = y[indy];
-   indyp = (int)min(indy*1.0 + 1, ny*1.0 - 1);
+   indyp = (int)std::min(indy*1.0 + 1, ny*1.0 - 1);
    y2 = y[indyp];

    q11 = z[indx + indy*nx];

See also comments about the same problem in #51.