\hypertarget{namespacemom__coms}{}\section{mom\+\_\+coms Module Reference}
\label{namespacemom__coms}\index{mom\+\_\+coms@{mom\+\_\+coms}}


\subsection{Detailed Description}
Interfaces to non-\/domain-\/oriented communication subroutines, including the M\+O\+M6 reproducing sums facility. \subsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
interface \mbox{\hyperlink{interfacemom__coms_1_1assignment_07_0A_08}{assignment(=)}}
\begin{DoxyCompactList}\small\item\em Copy the value of one extended-\/fixed-\/point number into another. \end{DoxyCompactList}\item 
interface \mbox{\hyperlink{interfacemom__coms_1_1efp__sum__across__pes}{efp\+\_\+sum\+\_\+across\+\_\+pes}}
\begin{DoxyCompactList}\small\item\em Sum a value or 1-\/d array of values across processors, returning the sums in place. \end{DoxyCompactList}\item 
type \mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}
\begin{DoxyCompactList}\small\item\em The Extended Fixed Point (E\+FP) type provides a public interface for doing sums and taking differences with this type. \end{DoxyCompactList}\item 
interface \mbox{\hyperlink{interfacemom__coms_1_1operator_07_09_08}{operator(+)}}
\begin{DoxyCompactList}\small\item\em Add two extended-\/fixed-\/point numbers. \end{DoxyCompactList}\item 
interface \mbox{\hyperlink{interfacemom__coms_1_1operator_07-_08}{operator(-\/)}}
\begin{DoxyCompactList}\small\item\em Subtract one extended-\/fixed-\/point number from another. \end{DoxyCompactList}\item 
interface \mbox{\hyperlink{interfacemom__coms_1_1reproducing__sum}{reproducing\+\_\+sum}}
\begin{DoxyCompactList}\small\item\em Find an accurate and order-\/invariant sum of a distributed 2d or 3d field. \end{DoxyCompactList}\item 
interface \mbox{\hyperlink{interfacemom__coms_1_1reproducing__sum__efp}{reproducing\+\_\+sum\+\_\+efp}}
\begin{DoxyCompactList}\small\item\em Find an accurate and order-\/invariant sum of a distributed 2d field, returning the result in the form of an extended fixed point value that can be converted back with E\+F\+P\+\_\+to\+\_\+real. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}) function \mbox{\hyperlink{namespacemom__coms_a81eab26b0e062043ae4b13949d90a5dc}{reproducing\+\_\+efp\+\_\+sum\+\_\+2d}} (array, isr, ier, jsr, jer, overflow\+\_\+check, err, only\+\_\+on\+\_\+\+PE)
\begin{DoxyCompactList}\small\item\em This subroutine uses a conversion to an integer representation of real numbers to give an order-\/invariant sum of distributed 2-\/D arrays that reproduces across domain decomposition, with the result returned as an extended fixed point type that can be converted back to a real number using E\+F\+P\+\_\+to\+\_\+real. This technique is described in Hallberg \& Adcroft, 2014, Parallel Computing, doi\+:10.\+1016/j.parco.\+2014.\+04.\+007. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__coms_a82b35df61c7d0aba1712fc3ce7b47685}{reproducing\+\_\+sum\+\_\+2d}} (array, isr, ier, jsr, jer, E\+F\+P\+\_\+sum, reproducing, overflow\+\_\+check, err, only\+\_\+on\+\_\+\+PE)
\begin{DoxyCompactList}\small\item\em This subroutine uses a conversion to an integer representation of real numbers to give an order-\/invariant sum of distributed 2-\/D arrays that reproduces across domain decomposition. This technique is described in Hallberg \& Adcroft, 2014, Parallel Computing, doi\+:10.\+1016/j.parco.\+2014.\+04.\+007. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__coms_aa98bb5adb44798d397f668edf62832e8}{reproducing\+\_\+sum\+\_\+3d}} (array, isr, ier, jsr, jer, sums, E\+F\+P\+\_\+sum, E\+F\+P\+\_\+lay\+\_\+sums, err, only\+\_\+on\+\_\+\+PE)
\begin{DoxyCompactList}\small\item\em This subroutine uses a conversion to an integer representation of real numbers to give an order-\/invariant sum of distributed 3-\/D arrays that reproduces across domain decomposition. This technique is described in Hallberg \& Adcroft, 2014, Parallel Computing, doi\+:10.\+1016/j.parco.\+2014.\+04.\+007. \end{DoxyCompactList}\item 
integer(kind=8) function, dimension(\mbox{\hyperlink{namespacemom__coms_ac1760c24e671a314322e4b1b33e0db39}{ni}}) \mbox{\hyperlink{namespacemom__coms_a0cc261620495abf3313937726883b95e}{real\+\_\+to\+\_\+ints}} (r, prec\+\_\+error, overflow)
\begin{DoxyCompactList}\small\item\em Convert a real number into the array of integers constitute its extended-\/fixed-\/point representation. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__coms_a24ac5b7cc37b1498f23b61eea03fb8c3}{ints\+\_\+to\+\_\+real}} (ints)
\begin{DoxyCompactList}\small\item\em Convert the array of integers that constitute an extended-\/fixed-\/point representation into a real number. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__coms_a8228dee8e8e72652d5a58f483f0dc661}{increment\+\_\+ints}} (int\+\_\+sum, int2, prec\+\_\+error)
\begin{DoxyCompactList}\small\item\em Increment an array of integers that constitutes an extended-\/fixed-\/point representation with a another E\+FP number. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__coms_aebe91f9c9bd6de5e9523c1f663e8a04d}{increment\+\_\+ints\+\_\+faster}} (int\+\_\+sum, r, max\+\_\+mag\+\_\+term)
\begin{DoxyCompactList}\small\item\em Increment an E\+FP number with a real number without doing any carrying of of overflows and using only minimal error checking. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__coms_a3fe107391eb9d2c199eb09e451f4dbb2}{carry\+\_\+overflow}} (int\+\_\+sum, prec\+\_\+error)
\begin{DoxyCompactList}\small\item\em This subroutine handles carrying of the overflow. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__coms_ab8feff19e782af36bb7ccccd5ba9eddc}{regularize\+\_\+ints}} (int\+\_\+sum)
\begin{DoxyCompactList}\small\item\em This subroutine carries the overflow, and then makes sure that all integers are of the same sign as the overall value. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__coms_addf8caf9a58bed7059b5bb6660d73c4e}\label{namespacemom__coms_addf8caf9a58bed7059b5bb6660d73c4e}} 
logical function, public \mbox{\hyperlink{namespacemom__coms_addf8caf9a58bed7059b5bb6660d73c4e}{query\+\_\+efp\+\_\+overflow\+\_\+error}} ()
\begin{DoxyCompactList}\small\item\em Returns the status of the module\textquotesingle{}s error flag. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__coms_a9a44466e43db89b2525996feedd11b7a}\label{namespacemom__coms_a9a44466e43db89b2525996feedd11b7a}} 
subroutine, public \mbox{\hyperlink{namespacemom__coms_a9a44466e43db89b2525996feedd11b7a}{reset\+\_\+efp\+\_\+overflow\+\_\+error}} ()
\begin{DoxyCompactList}\small\item\em Reset the module\textquotesingle{}s error flag to false. \end{DoxyCompactList}\item 
type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}) function, public \mbox{\hyperlink{namespacemom__coms_abfb8af92b0a6ee8b7f5d8391f6893977}{efp\+\_\+plus}} (E\+F\+P1, E\+F\+P2)
\begin{DoxyCompactList}\small\item\em Add two extended-\/fixed-\/point numbers. \end{DoxyCompactList}\item 
type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}) function, public \mbox{\hyperlink{namespacemom__coms_ac9cda4ec7606fa2e47d79d759d9e6694}{efp\+\_\+minus}} (E\+F\+P1, E\+F\+P2)
\begin{DoxyCompactList}\small\item\em Subract one extended-\/fixed-\/point number from another. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__coms_a63ee6c200b7e9e34624430be6347fbec}{efp\+\_\+assign}} (E\+F\+P1, E\+F\+P2)
\begin{DoxyCompactList}\small\item\em Copy one extended-\/fixed-\/point number into another. \end{DoxyCompactList}\item 
real function, public \mbox{\hyperlink{namespacemom__coms_a4aaf51b372bcaf7b46939145577eff92}{efp\+\_\+to\+\_\+real}} (E\+F\+P1)
\begin{DoxyCompactList}\small\item\em Return the real number that an extended-\/fixed-\/point number corresponds with. \end{DoxyCompactList}\item 
real function, public \mbox{\hyperlink{namespacemom__coms_a8f23ba3eaaf03101afa61c339fac805b}{efp\+\_\+real\+\_\+diff}} (E\+F\+P1, E\+F\+P2)
\begin{DoxyCompactList}\small\item\em Take the difference between two extended-\/fixed-\/point numbers (E\+F\+P1 -\/ E\+F\+P2) and return the result as a real number. \end{DoxyCompactList}\item 
type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}) function, public \mbox{\hyperlink{namespacemom__coms_ac8f1b5be23b128cd8bb956ebda917edb}{real\+\_\+to\+\_\+efp}} (val, overflow)
\begin{DoxyCompactList}\small\item\em Return the extended-\/fixed-\/point number that a real number corresponds with. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__coms_a6cbcab29d87b134dcd5c4bdb922b4344}{efp\+\_\+list\+\_\+sum\+\_\+across\+\_\+pes}} (E\+F\+Ps, nval, errors)
\begin{DoxyCompactList}\small\item\em This subroutine does a sum across P\+Es of a list of E\+FP variables, returning the sums in place, with all overflows carried. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__coms_aa6ad80e8330fc146562404a4e756916b}{efp\+\_\+val\+\_\+sum\+\_\+across\+\_\+pes}} (E\+FP, error)
\begin{DoxyCompactList}\small\item\em This subroutine does a sum across P\+Es of an E\+FP variable, returning the sums in place, with all overflows carried. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__coms_a6a5fa5714820df840a829d6d1b432a75}\label{namespacemom__coms_a6a5fa5714820df840a829d6d1b432a75}} 
subroutine, public \mbox{\hyperlink{namespacemom__coms_a6a5fa5714820df840a829d6d1b432a75}{mom\+\_\+infra\+\_\+end}}
\begin{DoxyCompactList}\small\item\em This subroutine carries out all of the calls required to close out the infrastructure cleanly. This should only be called in ocean-\/only runs, as the coupler takes care of this in coupled runs. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Variables}
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{namespacemom__coms_a53dec744e5fb972fe74863d02a5b3f7f}\label{namespacemom__coms_a53dec744e5fb972fe74863d02a5b3f7f}} 
integer(kind=8), parameter \mbox{\hyperlink{namespacemom__coms_a53dec744e5fb972fe74863d02a5b3f7f}{prec}} =2\+\_\+8$\ast$$\ast$46
\begin{DoxyCompactList}\small\item\em The precision of each integer. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__coms_a885379ffe89eb60f2b0071abb9e8c638}\label{namespacemom__coms_a885379ffe89eb60f2b0071abb9e8c638}} 
real, parameter \mbox{\hyperlink{namespacemom__coms_a885379ffe89eb60f2b0071abb9e8c638}{r\+\_\+prec}} =2.\+0$\ast$$\ast$46
\begin{DoxyCompactList}\small\item\em A real version of prec. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__coms_af2d95ca11334965b9f5ecf2593692cd7}\label{namespacemom__coms_af2d95ca11334965b9f5ecf2593692cd7}} 
real, parameter \mbox{\hyperlink{namespacemom__coms_af2d95ca11334965b9f5ecf2593692cd7}{i\+\_\+prec}} =1.\+0/(2.\+0$\ast$$\ast$46)
\begin{DoxyCompactList}\small\item\em The inverse of prec. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__coms_a2aeaf40b69c9d498459bc534a38451d8}\label{namespacemom__coms_a2aeaf40b69c9d498459bc534a38451d8}} 
integer, parameter \mbox{\hyperlink{namespacemom__coms_a2aeaf40b69c9d498459bc534a38451d8}{max\+\_\+count\+\_\+prec}} =2$\ast$$\ast$(63-\/46)-\/1
\begin{DoxyCompactList}\small\item\em The number of values that can be added together with the current value of prec before there will be roundoff problems. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__coms_ac1760c24e671a314322e4b1b33e0db39}\label{namespacemom__coms_ac1760c24e671a314322e4b1b33e0db39}} 
integer, parameter \mbox{\hyperlink{namespacemom__coms_ac1760c24e671a314322e4b1b33e0db39}{ni}} =6
\begin{DoxyCompactList}\small\item\em The number of long integers to use to represent a real number. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__coms_ad18445cd567da1ccb36b4080c93b4d61}\label{namespacemom__coms_ad18445cd567da1ccb36b4080c93b4d61}} 
real, dimension(\mbox{\hyperlink{namespacemom__coms_ac1760c24e671a314322e4b1b33e0db39}{ni}}), parameter \mbox{\hyperlink{namespacemom__coms_ad18445cd567da1ccb36b4080c93b4d61}{pr}} = (/ \mbox{\hyperlink{namespacemom__coms_a885379ffe89eb60f2b0071abb9e8c638}{r\+\_\+prec}}$\ast$$\ast$2, \mbox{\hyperlink{namespacemom__coms_a885379ffe89eb60f2b0071abb9e8c638}{r\+\_\+prec}}, 1.\+0, 1.\+0/\mbox{\hyperlink{namespacemom__coms_a885379ffe89eb60f2b0071abb9e8c638}{r\+\_\+prec}}, 1.\+0/\mbox{\hyperlink{namespacemom__coms_a885379ffe89eb60f2b0071abb9e8c638}{r\+\_\+prec}}$\ast$$\ast$2, 1.\+0/\mbox{\hyperlink{namespacemom__coms_a885379ffe89eb60f2b0071abb9e8c638}{r\+\_\+prec}}$\ast$$\ast$3 /)
\begin{DoxyCompactList}\small\item\em An array of the real precision of each of the integers. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__coms_ab2035af144ae0d121f4259db705c4185}\label{namespacemom__coms_ab2035af144ae0d121f4259db705c4185}} 
real, dimension(\mbox{\hyperlink{namespacemom__coms_ac1760c24e671a314322e4b1b33e0db39}{ni}}), parameter \mbox{\hyperlink{namespacemom__coms_ab2035af144ae0d121f4259db705c4185}{i\+\_\+pr}} = (/ 1.\+0/\mbox{\hyperlink{namespacemom__coms_a885379ffe89eb60f2b0071abb9e8c638}{r\+\_\+prec}}$\ast$$\ast$2, 1.\+0/\mbox{\hyperlink{namespacemom__coms_a885379ffe89eb60f2b0071abb9e8c638}{r\+\_\+prec}}, 1.\+0, \mbox{\hyperlink{namespacemom__coms_a885379ffe89eb60f2b0071abb9e8c638}{r\+\_\+prec}}, \mbox{\hyperlink{namespacemom__coms_a885379ffe89eb60f2b0071abb9e8c638}{r\+\_\+prec}}$\ast$$\ast$2, \mbox{\hyperlink{namespacemom__coms_a885379ffe89eb60f2b0071abb9e8c638}{r\+\_\+prec}}$\ast$$\ast$3 /)
\begin{DoxyCompactList}\small\item\em An array of the inverse of the real precision of each of the integers. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__coms_a863bd692f968d62288323e0badf23933}\label{namespacemom__coms_a863bd692f968d62288323e0badf23933}} 
real, parameter \mbox{\hyperlink{namespacemom__coms_a863bd692f968d62288323e0badf23933}{max\+\_\+efp\+\_\+float}} = \mbox{\hyperlink{namespacemom__coms_ad18445cd567da1ccb36b4080c93b4d61}{pr}}(1) $\ast$ (2.$\ast$$\ast$63 -\/ 1.)
\begin{DoxyCompactList}\small\item\em The largest float with an E\+FP representation. N\+O\+TE\+: Only the first bin can exceed precision, but is bounded by the largest signed integer. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__coms_a7af397491bbb8f8e6e9a268492bebc33}\label{namespacemom__coms_a7af397491bbb8f8e6e9a268492bebc33}} 
logical \mbox{\hyperlink{namespacemom__coms_a7af397491bbb8f8e6e9a268492bebc33}{overflow\+\_\+error}} = .false.
\begin{DoxyCompactList}\small\item\em This becomes true if an overflow is encountered. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__coms_adfb52edef53a82f62d4c9a0786ce6bdf}\label{namespacemom__coms_adfb52edef53a82f62d4c9a0786ce6bdf}} 
logical \mbox{\hyperlink{namespacemom__coms_adfb52edef53a82f62d4c9a0786ce6bdf}{nan\+\_\+error}} = .false.
\begin{DoxyCompactList}\small\item\em This becomes true if a NaN is encountered. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__coms_ad6c2231f3de989b71d4455ca97b24d8a}\label{namespacemom__coms_ad6c2231f3de989b71d4455ca97b24d8a}} 
logical \mbox{\hyperlink{namespacemom__coms_ad6c2231f3de989b71d4455ca97b24d8a}{debug}} = .false.
\begin{DoxyCompactList}\small\item\em Making this true enables debugging output. \end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__coms_a3fe107391eb9d2c199eb09e451f4dbb2}\label{namespacemom__coms_a3fe107391eb9d2c199eb09e451f4dbb2}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!carry\+\_\+overflow@{carry\+\_\+overflow}}
\index{carry\+\_\+overflow@{carry\+\_\+overflow}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{carry\+\_\+overflow()}{carry\_overflow()}}
{\footnotesize\ttfamily subroutine mom\+\_\+coms\+::carry\+\_\+overflow (\begin{DoxyParamCaption}\item[{integer(kind=8), dimension(\mbox{\hyperlink{namespacemom__coms_ac1760c24e671a314322e4b1b33e0db39}{ni}}), intent(inout)}]{int\+\_\+sum,  }\item[{integer(kind=8), intent(in)}]{prec\+\_\+error }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine handles carrying of the overflow. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in,out}  & {\em int\+\_\+sum} & The array of E\+FP integers being modified by carries, but without changing value.\\
\hline
\mbox{\tt in}  & {\em prec\+\_\+error} & The P\+E-\/count dependent precision of the integers that is safe from overflows during global sums. This will be larger than the compile-\/time precision parameter, and is used to detect overflows. \\
\hline
\end{DoxyParams}


Definition at line 629 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
629   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{dimension(ni)}, \textcolor{keywordtype}{intent(inout)} :: int\_sum\textcolor{comment}{  !< The array of EFP integers being}
630 \textcolor{comment}{                                              !! modified by carries, but without changing value.}
631   \textcolor{keywordtype}{integer(kind=8)},                \textcolor{keywordtype}{intent(in)}    :: prec\_error\textcolor{comment}{  !< The PE-count dependent precision of the}
632 \textcolor{comment}{                                              !! integers that is safe from overflows during global}
633 \textcolor{comment}{                                              !! sums.  This will be larger than the compile-time}
634 \textcolor{comment}{                                              !! precision parameter, and is used to detect overflows.}
635 
636   \textcolor{comment}{! This subroutine handles carrying of the overflow.}
637   \textcolor{keywordtype}{integer} :: i, num\_carry
638 
639   \textcolor{keywordflow}{do} i=ni,2,-1 ; \textcolor{keywordflow}{if} (abs(int\_sum(i)) >= prec) \textcolor{keywordflow}{then}
640     num\_carry = int(int\_sum(i) * i\_prec)
641     int\_sum(i) = int\_sum(i) - num\_carry*prec
642     int\_sum(i-1) = int\_sum(i-1) + num\_carry
643 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo}
644   \textcolor{keywordflow}{if} (abs(int\_sum(1)) > prec\_error) \textcolor{keywordflow}{then}
645     overflow\_error = .true.
646 \textcolor{keywordflow}{  endif}
647 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_a63ee6c200b7e9e34624430be6347fbec}\label{namespacemom__coms_a63ee6c200b7e9e34624430be6347fbec}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!efp\+\_\+assign@{efp\+\_\+assign}}
\index{efp\+\_\+assign@{efp\+\_\+assign}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{efp\+\_\+assign()}{efp\_assign()}}
{\footnotesize\ttfamily subroutine mom\+\_\+coms\+::efp\+\_\+assign (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), intent(out)}]{E\+F\+P1,  }\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), intent(in)}]{E\+F\+P2 }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Copy one extended-\/fixed-\/point number into another. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt out}  & {\em efp1} & The recipient extended fixed point number\\
\hline
\mbox{\tt in}  & {\em efp2} & The source extended fixed point number \\
\hline
\end{DoxyParams}


Definition at line 729 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
729   \textcolor{keywordtype}{type}(EFP\_type), \textcolor{keywordtype}{intent(out)} :: EFP1\textcolor{comment}{ !< The recipient extended fixed point number}
730   \textcolor{keywordtype}{type}(EFP\_type), \textcolor{keywordtype}{intent(in)}  :: EFP2\textcolor{comment}{ !< The source extended fixed point number}
731   \textcolor{keywordtype}{integer} i
732   \textcolor{comment}{! This subroutine assigns all components of the extended fixed point type}
733   \textcolor{comment}{! variable on the RHS (EFP2) to the components of the variable on the LHS}
734   \textcolor{comment}{! (EFP1).}
735 
736   \textcolor{keywordflow}{do} i=1,ni ; efp1%v(i) = efp2%v(i) ;\textcolor{keywordflow}{ enddo}
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_a6cbcab29d87b134dcd5c4bdb922b4344}\label{namespacemom__coms_a6cbcab29d87b134dcd5c4bdb922b4344}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!efp\+\_\+list\+\_\+sum\+\_\+across\+\_\+pes@{efp\+\_\+list\+\_\+sum\+\_\+across\+\_\+pes}}
\index{efp\+\_\+list\+\_\+sum\+\_\+across\+\_\+pes@{efp\+\_\+list\+\_\+sum\+\_\+across\+\_\+pes}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{efp\+\_\+list\+\_\+sum\+\_\+across\+\_\+pes()}{efp\_list\_sum\_across\_pes()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+coms\+::efp\+\_\+list\+\_\+sum\+\_\+across\+\_\+pes (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), dimension(\+:), intent(inout)}]{E\+F\+Ps,  }\item[{integer, intent(in)}]{nval,  }\item[{logical, dimension(\+:), intent(out), optional}]{errors }\end{DoxyParamCaption})}



This subroutine does a sum across P\+Es of a list of E\+FP variables, returning the sums in place, with all overflows carried. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in,out}  & {\em efps} & The list of extended fixed point numbers\\
\hline
\mbox{\tt in}  & {\em nval} & The number of values being summed.\\
\hline
\mbox{\tt out}  & {\em errors} & A list of error flags for each sum \\
\hline
\end{DoxyParams}


Definition at line 789 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
789   \textcolor{keywordtype}{type}(EFP\_type), \textcolor{keywordtype}{dimension(:)}, &
790               \textcolor{keywordtype}{intent(inout)} :: EFPs\textcolor{comment}{   !< The list of extended fixed point numbers}
791 \textcolor{comment}{                                      !! being summed across PEs.}
792   \textcolor{keywordtype}{integer},    \textcolor{keywordtype}{intent(in)}    :: nval\textcolor{comment}{   !< The number of values being summed.}
793   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(:)}, &
794            \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)}   :: errors\textcolor{comment}{ !< A list of error flags for each sum}
795 
796   \textcolor{comment}{!   This subroutine does a sum across PEs of a list of EFP variables,}
797   \textcolor{comment}{! returning the sums in place, with all overflows carried.}
798 
799   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{dimension(ni,nval)} :: ints
800   \textcolor{keywordtype}{integer(kind=8)} :: prec\_error
801   \textcolor{keywordtype}{logical} :: error\_found
802   \textcolor{keywordtype}{character(len=256)} :: mesg
803   \textcolor{keywordtype}{integer} :: i, n
804 
805   \textcolor{keywordflow}{if} (num\_pes() > max\_count\_prec) \textcolor{keyword}{call }mom\_error(fatal, &
806     \textcolor{stringliteral}{"reproducing\_sum: Too many processors are being used for the value of "}//&
807     \textcolor{stringliteral}{"prec.  Reduce prec to (2^63-1)/num\_PEs."})
808 
809   prec\_error = (2\_8**62 + (2\_8**62 - 1)) / num\_pes()
810   \textcolor{comment}{! overflow\_error is an overflow error flag for the whole module.}
811   overflow\_error = .false. ; error\_found = .false.
812 
813   \textcolor{keywordflow}{do} i=1,nval ; \textcolor{keywordflow}{do} n=1,ni ; ints(n,i) = efps(i)%v(n) ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
814 
815   \textcolor{keyword}{call }sum\_across\_pes(ints(:,:), ni*nval)
816 
817   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(errors)) errors(:) = .false.
818   \textcolor{keywordflow}{do} i=1,nval
819     overflow\_error = .false.
820     \textcolor{keyword}{call }carry\_overflow(ints(:,i), prec\_error)
821     \textcolor{keywordflow}{do} n=1,ni ; efps(i)%v(n) = ints(n,i) ;\textcolor{keywordflow}{ enddo}
822     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(errors)) errors(i) = overflow\_error
823     \textcolor{keywordflow}{if} (overflow\_error) \textcolor{keywordflow}{then}
824       \textcolor{keyword}{write} (mesg,\textcolor{stringliteral}{'("EFP\_list\_sum\_across\_PEs error at ",i6," val was ",ES12.6, ", prec\_error = ",ES12.6)'}) 
      &
825              i, efp\_to\_real(efps(i)), \textcolor{keywordtype}{real}(prec\_error)
826       \textcolor{keyword}{call }mom\_error(warning, mesg)
827 \textcolor{keywordflow}{    endif}
828     error\_found = error\_found .or. overflow\_error
829 \textcolor{keywordflow}{  enddo}
830   \textcolor{keywordflow}{if} (error\_found .and. .not.(\textcolor{keyword}{present}(errors))) \textcolor{keywordflow}{then}
831     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Overflow in EFP\_list\_sum\_across\_PEs."})
832 \textcolor{keywordflow}{  endif}
833 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_ac9cda4ec7606fa2e47d79d759d9e6694}\label{namespacemom__coms_ac9cda4ec7606fa2e47d79d759d9e6694}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!efp\+\_\+minus@{efp\+\_\+minus}}
\index{efp\+\_\+minus@{efp\+\_\+minus}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{efp\+\_\+minus()}{efp\_minus()}}
{\footnotesize\ttfamily type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}) function, public mom\+\_\+coms\+::efp\+\_\+minus (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), intent(in)}]{E\+F\+P1,  }\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), intent(in)}]{E\+F\+P2 }\end{DoxyParamCaption})}



Subract one extended-\/fixed-\/point number from another. 

\begin{DoxyReturn}{Returns}
The result in extended fixed point format
\end{DoxyReturn}

\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em efp1} & The first extended fixed point number\\
\hline
\mbox{\tt in}  & {\em efp2} & The extended fixed point number being subtracted from the first extended fixed point number \\
\hline
\end{DoxyParams}


Definition at line 716 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
716   \textcolor{keywordtype}{type}(EFP\_type)             :: EFP\_minus\textcolor{comment}{ !< The result in extended fixed point format}
717   \textcolor{keywordtype}{type}(EFP\_type), \textcolor{keywordtype}{intent(in)} :: EFP1\textcolor{comment}{ !< The first extended fixed point number}
718   \textcolor{keywordtype}{type}(EFP\_type), \textcolor{keywordtype}{intent(in)} :: EFP2\textcolor{comment}{ !< The extended fixed point number being}
719 \textcolor{comment}{                        !! subtracted from the first extended fixed point number}
720   \textcolor{keywordtype}{integer} :: i
721 
722   \textcolor{keywordflow}{do} i=1,ni ; efp\_minus%v(i) = -1*efp2%v(i) ;\textcolor{keywordflow}{ enddo}
723 
724   \textcolor{keyword}{call }increment\_ints(efp\_minus%v(:), efp1%v(:))
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_abfb8af92b0a6ee8b7f5d8391f6893977}\label{namespacemom__coms_abfb8af92b0a6ee8b7f5d8391f6893977}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!efp\+\_\+plus@{efp\+\_\+plus}}
\index{efp\+\_\+plus@{efp\+\_\+plus}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{efp\+\_\+plus()}{efp\_plus()}}
{\footnotesize\ttfamily type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}) function, public mom\+\_\+coms\+::efp\+\_\+plus (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), intent(in)}]{E\+F\+P1,  }\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), intent(in)}]{E\+F\+P2 }\end{DoxyParamCaption})}



Add two extended-\/fixed-\/point numbers. 

\begin{DoxyReturn}{Returns}
The result in extended fixed point format
\end{DoxyReturn}

\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em efp1} & The first extended fixed point number\\
\hline
\mbox{\tt in}  & {\em efp2} & The second extended fixed point number \\
\hline
\end{DoxyParams}


Definition at line 705 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
705   \textcolor{keywordtype}{type}(EFP\_type)             :: EFP\_plus\textcolor{comment}{ !< The result in extended fixed point format}
706   \textcolor{keywordtype}{type}(EFP\_type), \textcolor{keywordtype}{intent(in)} :: EFP1\textcolor{comment}{ !< The first extended fixed point number}
707   \textcolor{keywordtype}{type}(EFP\_type), \textcolor{keywordtype}{intent(in)} :: EFP2\textcolor{comment}{ !< The second extended fixed point number}
708 
709   efp\_plus = efp1
710 
711   \textcolor{keyword}{call }increment\_ints(efp\_plus%v(:), efp2%v(:))
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_a8f23ba3eaaf03101afa61c339fac805b}\label{namespacemom__coms_a8f23ba3eaaf03101afa61c339fac805b}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!efp\+\_\+real\+\_\+diff@{efp\+\_\+real\+\_\+diff}}
\index{efp\+\_\+real\+\_\+diff@{efp\+\_\+real\+\_\+diff}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{efp\+\_\+real\+\_\+diff()}{efp\_real\_diff()}}
{\footnotesize\ttfamily real function, public mom\+\_\+coms\+::efp\+\_\+real\+\_\+diff (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), intent(in)}]{E\+F\+P1,  }\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), intent(in)}]{E\+F\+P2 }\end{DoxyParamCaption})}



Take the difference between two extended-\/fixed-\/point numbers (E\+F\+P1 -\/ E\+F\+P2) and return the result as a real number. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em efp1} & The first extended fixed point number\\
\hline
\mbox{\tt in}  & {\em efp2} & The extended fixed point number being subtracted from the first extended fixed point number\\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
The real result 
\end{DoxyReturn}


Definition at line 751 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
751   \textcolor{keywordtype}{type}(EFP\_type), \textcolor{keywordtype}{intent(in)} :: EFP1\textcolor{comment}{  !< The first extended fixed point number}
752   \textcolor{keywordtype}{type}(EFP\_type), \textcolor{keywordtype}{intent(in)} :: EFP2\textcolor{comment}{  !< The extended fixed point number being}
753 \textcolor{comment}{                        !! subtracted from the first extended fixed point number}
754   \textcolor{keywordtype}{real} :: EFP\_real\_diff\textcolor{comment}{ !< The real result}
755 
756   \textcolor{keywordtype}{type}(EFP\_type)             :: EFP\_diff
757 
758   efp\_diff = efp1 - efp2
759   efp\_real\_diff = efp\_to\_real(efp\_diff)
760 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_a4aaf51b372bcaf7b46939145577eff92}\label{namespacemom__coms_a4aaf51b372bcaf7b46939145577eff92}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!efp\+\_\+to\+\_\+real@{efp\+\_\+to\+\_\+real}}
\index{efp\+\_\+to\+\_\+real@{efp\+\_\+to\+\_\+real}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{efp\+\_\+to\+\_\+real()}{efp\_to\_real()}}
{\footnotesize\ttfamily real function, public mom\+\_\+coms\+::efp\+\_\+to\+\_\+real (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), intent(inout)}]{E\+F\+P1 }\end{DoxyParamCaption})}



Return the real number that an extended-\/fixed-\/point number corresponds with. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in,out}  & {\em efp1} & The extended fixed point number being converted \\
\hline
\end{DoxyParams}


Definition at line 741 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
741   \textcolor{keywordtype}{type}(EFP\_type), \textcolor{keywordtype}{intent(inout)} :: EFP1\textcolor{comment}{ !< The extended fixed point number being converted}
742   \textcolor{keywordtype}{real} :: EFP\_to\_real
743 
744   \textcolor{keyword}{call }regularize\_ints(efp1%v)
745   efp\_to\_real = ints\_to\_real(efp1%v)
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_aa6ad80e8330fc146562404a4e756916b}\label{namespacemom__coms_aa6ad80e8330fc146562404a4e756916b}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!efp\+\_\+val\+\_\+sum\+\_\+across\+\_\+pes@{efp\+\_\+val\+\_\+sum\+\_\+across\+\_\+pes}}
\index{efp\+\_\+val\+\_\+sum\+\_\+across\+\_\+pes@{efp\+\_\+val\+\_\+sum\+\_\+across\+\_\+pes}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{efp\+\_\+val\+\_\+sum\+\_\+across\+\_\+pes()}{efp\_val\_sum\_across\_pes()}}
{\footnotesize\ttfamily subroutine mom\+\_\+coms\+::efp\+\_\+val\+\_\+sum\+\_\+across\+\_\+pes (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), intent(inout)}]{E\+FP,  }\item[{logical, intent(out), optional}]{error }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine does a sum across P\+Es of an E\+FP variable, returning the sums in place, with all overflows carried. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in,out}  & {\em efp} & The extended fixed point numbers being summed across P\+Es.\\
\hline
\mbox{\tt out}  & {\em error} & An error flag for this sum \\
\hline
\end{DoxyParams}


Definition at line 839 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
839   \textcolor{keywordtype}{type}(EFP\_type),  \textcolor{keywordtype}{intent(inout)} :: EFP\textcolor{comment}{   !< The extended fixed point numbers}
840 \textcolor{comment}{                                          !! being summed across PEs.}
841   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: error\textcolor{comment}{ !< An error flag for this sum}
842 
843   \textcolor{comment}{!   This subroutine does a sum across PEs of a list of EFP variables,}
844   \textcolor{comment}{! returning the sums in place, with all overflows carried.}
845 
846   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{dimension(ni)} :: ints
847   \textcolor{keywordtype}{integer(kind=8)} :: prec\_error
848   \textcolor{keywordtype}{logical} :: error\_found
849   \textcolor{keywordtype}{character(len=256)} :: mesg
850   \textcolor{keywordtype}{integer} :: n
851 
852   \textcolor{keywordflow}{if} (num\_pes() > max\_count\_prec) \textcolor{keyword}{call }mom\_error(fatal, &
853     \textcolor{stringliteral}{"reproducing\_sum: Too many processors are being used for the value of "}//&
854     \textcolor{stringliteral}{"prec.  Reduce prec to (2^63-1)/num\_PEs."})
855 
856   prec\_error = (2\_8**62 + (2\_8**62 - 1)) / num\_pes()
857   \textcolor{comment}{! overflow\_error is an overflow error flag for the whole module.}
858   overflow\_error = .false. ; error\_found = .false.
859 
860   \textcolor{keywordflow}{do} n=1,ni ; ints(n) = efp%v(n) ;\textcolor{keywordflow}{ enddo}
861 
862   \textcolor{keyword}{call }sum\_across\_pes(ints(:), ni)
863 
864   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(error)) error = .false.
865 
866   overflow\_error = .false.
867   \textcolor{keyword}{call }carry\_overflow(ints(:), prec\_error)
868   \textcolor{keywordflow}{do} n=1,ni ; efp%v(n) = ints(n) ;\textcolor{keywordflow}{ enddo}
869   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(error)) error = overflow\_error
870   \textcolor{keywordflow}{if} (overflow\_error) \textcolor{keywordflow}{then}
871     \textcolor{keyword}{write} (mesg,\textcolor{stringliteral}{'("EFP\_val\_sum\_across\_PEs error val was ",ES12.6, ", prec\_error = ",ES12.6)'}) &
872            efp\_to\_real(efp), \textcolor{keywordtype}{real}(prec\_error)
873     \textcolor{keyword}{call }mom\_error(warning, mesg)
874 \textcolor{keywordflow}{  endif}
875   error\_found = error\_found .or. overflow\_error
876 
877   \textcolor{keywordflow}{if} (error\_found .and. .not.(\textcolor{keyword}{present}(error))) \textcolor{keywordflow}{then}
878     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Overflow in EFP\_val\_sum\_across\_PEs."})
879 \textcolor{keywordflow}{  endif}
880 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_a8228dee8e8e72652d5a58f483f0dc661}\label{namespacemom__coms_a8228dee8e8e72652d5a58f483f0dc661}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!increment\+\_\+ints@{increment\+\_\+ints}}
\index{increment\+\_\+ints@{increment\+\_\+ints}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{increment\+\_\+ints()}{increment\_ints()}}
{\footnotesize\ttfamily subroutine mom\+\_\+coms\+::increment\+\_\+ints (\begin{DoxyParamCaption}\item[{integer(kind=8), dimension(\mbox{\hyperlink{namespacemom__coms_ac1760c24e671a314322e4b1b33e0db39}{ni}}), intent(inout)}]{int\+\_\+sum,  }\item[{integer(kind=8), dimension(\mbox{\hyperlink{namespacemom__coms_ac1760c24e671a314322e4b1b33e0db39}{ni}}), intent(in)}]{int2,  }\item[{integer(kind=8), intent(in), optional}]{prec\+\_\+error }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Increment an array of integers that constitutes an extended-\/fixed-\/point representation with a another E\+FP number. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in,out}  & {\em int\+\_\+sum} & The array of E\+FP integers being incremented\\
\hline
\mbox{\tt in}  & {\em int2} & The array of E\+FP integers being added\\
\hline
\mbox{\tt in}  & {\em prec\+\_\+error} & The P\+E-\/count dependent precision of the integers that is safe from overflows during global sums. This will be larger than the compile-\/time precision parameter, and is used to detect overflows. \\
\hline
\end{DoxyParams}


Definition at line 563 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
563   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{dimension(ni)}, \textcolor{keywordtype}{intent(inout)} :: int\_sum\textcolor{comment}{ !< The array of EFP integers being incremented}
564   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{dimension(ni)}, \textcolor{keywordtype}{intent(in)}    :: int2\textcolor{comment}{    !< The array of EFP integers being added}
565   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{optional},      \textcolor{keywordtype}{intent(in)}    :: prec\_error\textcolor{comment}{ !< The PE-count dependent precision of the}
566 \textcolor{comment}{                                              !! integers that is safe from overflows during global}
567 \textcolor{comment}{                                              !! sums.  This will be larger than the compile-time}
568 \textcolor{comment}{                                              !! precision parameter, and is used to detect overflows.}
569 
570   \textcolor{comment}{! This subroutine increments a number with another, both using the integer}
571   \textcolor{comment}{! representation in real\_to\_ints.}
572   \textcolor{keywordtype}{integer} :: i
573 
574   \textcolor{keywordflow}{do} i=ni,2,-1
575     int\_sum(i) = int\_sum(i) + int2(i)
576     \textcolor{comment}{! Carry the local overflow.}
577     \textcolor{keywordflow}{if} (int\_sum(i) > prec) \textcolor{keywordflow}{then}
578       int\_sum(i) = int\_sum(i) - prec
579       int\_sum(i-1) = int\_sum(i-1) + 1
580     \textcolor{keywordflow}{elseif} (int\_sum(i) < -prec) \textcolor{keywordflow}{then}
581       int\_sum(i) = int\_sum(i) + prec
582       int\_sum(i-1) = int\_sum(i-1) - 1
583 \textcolor{keywordflow}{    endif}
584 \textcolor{keywordflow}{  enddo}
585   int\_sum(1) = int\_sum(1) + int2(1)
586   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(prec\_error)) \textcolor{keywordflow}{then}
587     \textcolor{keywordflow}{if} (abs(int\_sum(1)) > prec\_error) overflow\_error = .true.
588   \textcolor{keywordflow}{else}
589     \textcolor{keywordflow}{if} (abs(int\_sum(1)) > prec) overflow\_error = .true.
590 \textcolor{keywordflow}{  endif}
591 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_aebe91f9c9bd6de5e9523c1f663e8a04d}\label{namespacemom__coms_aebe91f9c9bd6de5e9523c1f663e8a04d}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!increment\+\_\+ints\+\_\+faster@{increment\+\_\+ints\+\_\+faster}}
\index{increment\+\_\+ints\+\_\+faster@{increment\+\_\+ints\+\_\+faster}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{increment\+\_\+ints\+\_\+faster()}{increment\_ints\_faster()}}
{\footnotesize\ttfamily subroutine mom\+\_\+coms\+::increment\+\_\+ints\+\_\+faster (\begin{DoxyParamCaption}\item[{integer(kind=8), dimension(\mbox{\hyperlink{namespacemom__coms_ac1760c24e671a314322e4b1b33e0db39}{ni}}), intent(inout)}]{int\+\_\+sum,  }\item[{real, intent(in)}]{r,  }\item[{real, intent(inout)}]{max\+\_\+mag\+\_\+term }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Increment an E\+FP number with a real number without doing any carrying of of overflows and using only minimal error checking. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in,out}  & {\em int\+\_\+sum} & The array of E\+FP integers being incremented\\
\hline
\mbox{\tt in}  & {\em r} & The real number being added.\\
\hline
\mbox{\tt in,out}  & {\em max\+\_\+mag\+\_\+term} & A running maximum magnitude of the r\textquotesingle{}s. \\
\hline
\end{DoxyParams}


Definition at line 597 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
597   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{dimension(ni)}, \textcolor{keywordtype}{intent(inout)} :: int\_sum\textcolor{comment}{  !< The array of EFP integers being incremented}
598   \textcolor{keywordtype}{real},                           \textcolor{keywordtype}{intent(in)}    :: r\textcolor{comment}{        !< The real number being added.}
599   \textcolor{keywordtype}{real},                           \textcolor{keywordtype}{intent(inout)} :: max\_mag\_term\textcolor{comment}{ !< A running maximum magnitude of the r's.}
600 
601   \textcolor{comment}{! This subroutine increments a number with another, both using the integer}
602   \textcolor{comment}{! representation in real\_to\_ints, but without doing any carrying of overflow.}
603   \textcolor{comment}{! The entire operation is embedded in a single call for greater speed.}
604   \textcolor{keywordtype}{real} :: rs
605   \textcolor{keywordtype}{integer(kind=8)} :: ival
606   \textcolor{keywordtype}{integer} :: sgn, i
607 
608   \textcolor{keywordflow}{if} ((r >= 1e30) .eqv. (r < 1e30)) \textcolor{keywordflow}{then} ; nan\_error = .true. ; \textcolor{keywordflow}{return} ;\textcolor{keywordflow}{ endif}
609   sgn = 1 ; \textcolor{keywordflow}{if} (r<0.0) sgn = -1
610   rs = abs(r)
611   \textcolor{keywordflow}{if} (rs > abs(max\_mag\_term)) max\_mag\_term = r
612 
613   \textcolor{comment}{! Abort if the number has no EFP representation}
614   \textcolor{keywordflow}{if} (rs > max\_efp\_float) \textcolor{keywordflow}{then}
615     overflow\_error = .true.
616     \textcolor{keywordflow}{return}
617 \textcolor{keywordflow}{  endif}
618 
619   \textcolor{keywordflow}{do} i=1,ni
620     ival = int(rs*i\_pr(i), 8)
621     rs = rs - ival*pr(i)
622     int\_sum(i) = int\_sum(i) + sgn*ival
623 \textcolor{keywordflow}{  enddo}
624 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_a24ac5b7cc37b1498f23b61eea03fb8c3}\label{namespacemom__coms_a24ac5b7cc37b1498f23b61eea03fb8c3}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!ints\+\_\+to\+\_\+real@{ints\+\_\+to\+\_\+real}}
\index{ints\+\_\+to\+\_\+real@{ints\+\_\+to\+\_\+real}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{ints\+\_\+to\+\_\+real()}{ints\_to\_real()}}
{\footnotesize\ttfamily real function mom\+\_\+coms\+::ints\+\_\+to\+\_\+real (\begin{DoxyParamCaption}\item[{integer(kind=8), dimension(\mbox{\hyperlink{namespacemom__coms_ac1760c24e671a314322e4b1b33e0db39}{ni}}), intent(in)}]{ints }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Convert the array of integers that constitute an extended-\/fixed-\/point representation into a real number. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em ints} & The array of E\+FP integers \\
\hline
\end{DoxyParams}


Definition at line 550 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
550   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{dimension(ni)}, \textcolor{keywordtype}{intent(in)} :: ints\textcolor{comment}{ !< The array of EFP integers}
551   \textcolor{keywordtype}{real} :: r
552   \textcolor{comment}{! This subroutine reverses the conversion in real\_to\_ints.}
553 
554   \textcolor{keywordtype}{integer} :: i
555 
556   r = 0.0
557   \textcolor{keywordflow}{do} i=1,ni ; r = r + pr(i)*ints(i) ;\textcolor{keywordflow}{ enddo}
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_ac8f1b5be23b128cd8bb956ebda917edb}\label{namespacemom__coms_ac8f1b5be23b128cd8bb956ebda917edb}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!real\+\_\+to\+\_\+efp@{real\+\_\+to\+\_\+efp}}
\index{real\+\_\+to\+\_\+efp@{real\+\_\+to\+\_\+efp}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{real\+\_\+to\+\_\+efp()}{real\_to\_efp()}}
{\footnotesize\ttfamily type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}) function, public mom\+\_\+coms\+::real\+\_\+to\+\_\+efp (\begin{DoxyParamCaption}\item[{real, intent(in)}]{val,  }\item[{logical, intent(inout), optional}]{overflow }\end{DoxyParamCaption})}



Return the extended-\/fixed-\/point number that a real number corresponds with. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em val} & The real number being converted\\
\hline
\mbox{\tt in,out}  & {\em overflow} & Returns true if the conversion is being done on a value that is too large to be represented \\
\hline
\end{DoxyParams}


Definition at line 765 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
765   \textcolor{keywordtype}{real},              \textcolor{keywordtype}{intent(in)}    :: val\textcolor{comment}{ !< The real number being converted}
766   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(inout)} :: overflow\textcolor{comment}{ !< Returns true if the conversion is being}
767 \textcolor{comment}{                                          !! done on a value that is too large to be represented}
768   \textcolor{keywordtype}{type}(EFP\_type) :: real\_to\_EFP
769 
770   \textcolor{keywordtype}{logical} :: over
771   \textcolor{keywordtype}{character(len=80)} :: mesg
772 
773   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(overflow)) \textcolor{keywordflow}{then}
774     real\_to\_efp%v(:) = real\_to\_ints(val, overflow=overflow)
775   \textcolor{keywordflow}{else}
776     over = .false.
777     real\_to\_efp%v(:) = real\_to\_ints(val, overflow=over)
778     \textcolor{keywordflow}{if} (over) \textcolor{keywordflow}{then}
779       \textcolor{keyword}{write}(mesg, \textcolor{stringliteral}{'(ES13.5)'}) val
780       \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"Overflow in real\_to\_EFP conversion of "}//trim(mesg))
781 \textcolor{keywordflow}{    endif}
782 \textcolor{keywordflow}{  endif}
783 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_a0cc261620495abf3313937726883b95e}\label{namespacemom__coms_a0cc261620495abf3313937726883b95e}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!real\+\_\+to\+\_\+ints@{real\+\_\+to\+\_\+ints}}
\index{real\+\_\+to\+\_\+ints@{real\+\_\+to\+\_\+ints}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{real\+\_\+to\+\_\+ints()}{real\_to\_ints()}}
{\footnotesize\ttfamily integer(kind=8) function, dimension(\mbox{\hyperlink{namespacemom__coms_ac1760c24e671a314322e4b1b33e0db39}{ni}}) mom\+\_\+coms\+::real\+\_\+to\+\_\+ints (\begin{DoxyParamCaption}\item[{real, intent(in)}]{r,  }\item[{integer(kind=8), intent(in), optional}]{prec\+\_\+error,  }\item[{logical, intent(inout), optional}]{overflow }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Convert a real number into the array of integers constitute its extended-\/fixed-\/point representation. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em r} & The real number being converted\\
\hline
\mbox{\tt in}  & {\em prec\+\_\+error} & The P\+E-\/count dependent precision of the integers that is safe from overflows during global sums. This will be larger than the compile-\/time precision parameter, and is used to detect overflows.\\
\hline
\mbox{\tt in,out}  & {\em overflow} & Returns true if the conversion is being done on a value that is too large to be represented \\
\hline
\end{DoxyParams}


Definition at line 508 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
508   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: r\textcolor{comment}{  !< The real number being converted}
509   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)} :: prec\_error\textcolor{comment}{  !< The PE-count dependent precision of the}
510 \textcolor{comment}{                                              !! integers that is safe from overflows during global}
511 \textcolor{comment}{                                              !! sums.  This will be larger than the compile-time}
512 \textcolor{comment}{                                              !! precision parameter, and is used to detect overflows.}
513   \textcolor{keywordtype}{logical},         \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(inout)} :: overflow\textcolor{comment}{ !< Returns true if the conversion is being}
514 \textcolor{comment}{                                              !! done on a value that is too large to be represented}
515   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{dimension(ni)}  :: ints
516   \textcolor{comment}{!   This subroutine converts a real number to an equivalent representation}
517   \textcolor{comment}{! using several long integers.}
518 
519   \textcolor{keywordtype}{real} :: rs
520   \textcolor{keywordtype}{character(len=80)} :: mesg
521   \textcolor{keywordtype}{integer(kind=8)} :: ival, prec\_err
522   \textcolor{keywordtype}{integer} :: sgn, i
523 
524   prec\_err = prec ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(prec\_error)) prec\_err = prec\_error
525   ints(:) = 0\_8
526   \textcolor{keywordflow}{if} ((r >= 1e30) .eqv. (r < 1e30)) \textcolor{keywordflow}{then} ; nan\_error = .true. ; \textcolor{keywordflow}{return} ;\textcolor{keywordflow}{ endif}
527 
528   sgn = 1 ; \textcolor{keywordflow}{if} (r<0.0) sgn = -1
529   rs = abs(r)
530 
531   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(overflow)) \textcolor{keywordflow}{then}
532     \textcolor{keywordflow}{if} (.not.(rs < prec\_err*pr(1))) overflow = .true.
533     \textcolor{keywordflow}{if} ((r >= 1e30) .eqv. (r < 1e30)) overflow = .true.
534   \textcolor{keywordflow}{elseif} (.not.(rs < prec\_err*pr(1))) \textcolor{keywordflow}{then}
535     \textcolor{keyword}{write}(mesg, \textcolor{stringliteral}{'(ES13.5)'}) r
536     \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"Overflow in real\_to\_ints conversion of "}//trim(mesg))
537 \textcolor{keywordflow}{  endif}
538 
539   \textcolor{keywordflow}{do} i=1,ni
540     ival = int(rs*i\_pr(i), 8)
541     rs = rs - ival*pr(i)
542     ints(i) = sgn*ival
543 \textcolor{keywordflow}{  enddo}
544 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_ab8feff19e782af36bb7ccccd5ba9eddc}\label{namespacemom__coms_ab8feff19e782af36bb7ccccd5ba9eddc}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!regularize\+\_\+ints@{regularize\+\_\+ints}}
\index{regularize\+\_\+ints@{regularize\+\_\+ints}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{regularize\+\_\+ints()}{regularize\_ints()}}
{\footnotesize\ttfamily subroutine mom\+\_\+coms\+::regularize\+\_\+ints (\begin{DoxyParamCaption}\item[{integer(kind=8), dimension(\mbox{\hyperlink{namespacemom__coms_ac1760c24e671a314322e4b1b33e0db39}{ni}}), intent(inout)}]{int\+\_\+sum }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine carries the overflow, and then makes sure that all integers are of the same sign as the overall value. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in,out}  & {\em int\+\_\+sum} & The array of integers being modified to take a \\
\hline
\end{DoxyParams}


Definition at line 653 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
653   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{dimension(ni)}, &
654     \textcolor{keywordtype}{intent(inout)} :: int\_sum\textcolor{comment}{ !< The array of integers being modified to take a}
655 \textcolor{comment}{                             !! regular form with all integers of the same sign,}
656 \textcolor{comment}{                             !! but without changing value.}
657 
658   \textcolor{comment}{! This subroutine carries the overflow, and then makes sure that}
659   \textcolor{comment}{! all integers are of the same sign as the overall value.}
660   \textcolor{keywordtype}{logical} :: positive
661   \textcolor{keywordtype}{integer} :: i, num\_carry
662 
663   \textcolor{keywordflow}{do} i=ni,2,-1 ; \textcolor{keywordflow}{if} (abs(int\_sum(i)) >= prec) \textcolor{keywordflow}{then}
664     num\_carry = int(int\_sum(i) * i\_prec)
665     int\_sum(i) = int\_sum(i) - num\_carry*prec
666     int\_sum(i-1) = int\_sum(i-1) + num\_carry
667 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo}
668 
669   \textcolor{comment}{! Determine the sign of the final number.}
670   positive = .true.
671   \textcolor{keywordflow}{do} i=1,ni
672     \textcolor{keywordflow}{if} (abs(int\_sum(i)) > 0) \textcolor{keywordflow}{then}
673       \textcolor{keywordflow}{if} (int\_sum(i) < 0) positive = .false.
674       \textcolor{keywordflow}{exit}
675 \textcolor{keywordflow}{    endif}
676 \textcolor{keywordflow}{  enddo}
677 
678   \textcolor{keywordflow}{if} (positive) \textcolor{keywordflow}{then}
679     \textcolor{keywordflow}{do} i=ni,2,-1 ; \textcolor{keywordflow}{if} (int\_sum(i) < 0) \textcolor{keywordflow}{then}
680       int\_sum(i) = int\_sum(i) + prec
681       int\_sum(i-1) = int\_sum(i-1) - 1
682 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
683   \textcolor{keywordflow}{else}
684     \textcolor{keywordflow}{do} i=ni,2,-1 ; \textcolor{keywordflow}{if} (int\_sum(i) > 0) \textcolor{keywordflow}{then}
685       int\_sum(i) = int\_sum(i) - prec
686       int\_sum(i-1) = int\_sum(i-1) + 1
687 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
688 \textcolor{keywordflow}{  endif}
689 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_a81eab26b0e062043ae4b13949d90a5dc}\label{namespacemom__coms_a81eab26b0e062043ae4b13949d90a5dc}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!reproducing\+\_\+efp\+\_\+sum\+\_\+2d@{reproducing\+\_\+efp\+\_\+sum\+\_\+2d}}
\index{reproducing\+\_\+efp\+\_\+sum\+\_\+2d@{reproducing\+\_\+efp\+\_\+sum\+\_\+2d}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{reproducing\+\_\+efp\+\_\+sum\+\_\+2d()}{reproducing\_efp\_sum\_2d()}}
{\footnotesize\ttfamily type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}) function mom\+\_\+coms\+::reproducing\+\_\+efp\+\_\+sum\+\_\+2d (\begin{DoxyParamCaption}\item[{real, dimension(\+:,\+:), intent(in)}]{array,  }\item[{integer, intent(in), optional}]{isr,  }\item[{integer, intent(in), optional}]{ier,  }\item[{integer, intent(in), optional}]{jsr,  }\item[{integer, intent(in), optional}]{jer,  }\item[{logical, intent(in), optional}]{overflow\+\_\+check,  }\item[{integer, intent(out), optional}]{err,  }\item[{logical, intent(in), optional}]{only\+\_\+on\+\_\+\+PE }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine uses a conversion to an integer representation of real numbers to give an order-\/invariant sum of distributed 2-\/D arrays that reproduces across domain decomposition, with the result returned as an extended fixed point type that can be converted back to a real number using E\+F\+P\+\_\+to\+\_\+real. This technique is described in Hallberg \& Adcroft, 2014, Parallel Computing, doi\+:10.\+1016/j.parco.\+2014.\+04.\+007. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em array} & The array to be summed\\
\hline
\mbox{\tt in}  & {\em isr} & The starting i-\/index of the sum, noting that the array indices starts at 1\\
\hline
\mbox{\tt in}  & {\em ier} & The ending i-\/index of the sum, noting that the array indices starts at 1\\
\hline
\mbox{\tt in}  & {\em jsr} & The starting j-\/index of the sum, noting that the array indices starts at 1\\
\hline
\mbox{\tt in}  & {\em jer} & The ending j-\/index of the sum, noting that the array indices starts at 1\\
\hline
\mbox{\tt in}  & {\em overflow\+\_\+check} & If present and false, disable checking for overflows in incremental results. This can speed up calculations if the number of values being summed is small enough\\
\hline
\mbox{\tt out}  & {\em err} & If present, return an error code instead of triggering any fatal errors directly from this routine.\\
\hline
\mbox{\tt in}  & {\em only\+\_\+on\+\_\+pe} & If present and true, do not do the sum across processors, only reporting the local sum\\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
The result in extended fixed point format 
\end{DoxyReturn}


Definition at line 93 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
93   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:,:)},     \textcolor{keywordtype}{intent(in)}  :: array\textcolor{comment}{   !< The array to be summed}
94   \textcolor{keywordtype}{integer},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: isr\textcolor{comment}{     !< The starting i-index of the sum, noting}
95 \textcolor{comment}{                                                   !! that the array indices starts at 1}
96   \textcolor{keywordtype}{integer},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: ier\textcolor{comment}{     !< The ending i-index of the sum, noting}
97 \textcolor{comment}{                                                   !! that the array indices starts at 1}
98   \textcolor{keywordtype}{integer},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: jsr\textcolor{comment}{     !< The starting j-index of the sum, noting}
99 \textcolor{comment}{                                                   !! that the array indices starts at 1}
100   \textcolor{keywordtype}{integer},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: jer\textcolor{comment}{     !< The ending j-index of the sum, noting}
101 \textcolor{comment}{                                                   !! that the array indices starts at 1}
102   \textcolor{keywordtype}{logical},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: overflow\_check\textcolor{comment}{ !< If present and false, disable}
103 \textcolor{comment}{                                                !! checking for overflows in incremental results.}
104 \textcolor{comment}{                                                !! This can speed up calculations if the number}
105 \textcolor{comment}{                                                !! of values being summed is small enough}
106   \textcolor{keywordtype}{integer},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: err\textcolor{comment}{  !< If present, return an error code instead of}
107 \textcolor{comment}{                                                !! triggering any fatal errors directly from}
108 \textcolor{comment}{                                                !! this routine.}
109   \textcolor{keywordtype}{logical},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: only\_on\_PE\textcolor{comment}{ !< If present and true, do not do the sum}
110 \textcolor{comment}{                                                !! across processors, only reporting the local sum}
111   \textcolor{keywordtype}{type}(EFP\_type)                        :: EFP\_sum\textcolor{comment}{  !< The result in extended fixed point format}
112 
113   \textcolor{comment}{!   This subroutine uses a conversion to an integer representation}
114   \textcolor{comment}{! of real numbers to give order-invariant sums that will reproduce}
115   \textcolor{comment}{! across PE count.  This idea comes from R. Hallberg and A. Adcroft.}
116 
117   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{dimension(ni)}  :: ints\_sum
118   \textcolor{keywordtype}{integer(kind=8)} :: ival, prec\_error
119   \textcolor{keywordtype}{real}    :: rs
120   \textcolor{keywordtype}{real}    :: max\_mag\_term
121   \textcolor{keywordtype}{logical} :: over\_check, do\_sum\_across\_PEs
122   \textcolor{keywordtype}{character(len=256)} :: mesg
123   \textcolor{keywordtype}{integer} :: i, j, n, is, ie, js, je, sgn
124 
125   \textcolor{keywordflow}{if} (num\_pes() > max\_count\_prec) \textcolor{keyword}{call }mom\_error(fatal, &
126     \textcolor{stringliteral}{"reproducing\_sum: Too many processors are being used for the value of "}//&
127     \textcolor{stringliteral}{"prec.  Reduce prec to (2^63-1)/num\_PEs."})
128 
129   prec\_error = (2\_8**62 + (2\_8**62 - 1)) / num\_pes()
130 
131   is = 1 ; ie = \textcolor{keyword}{size}(array,1) ; js = 1 ; je = \textcolor{keyword}{size}(array,2 )
132   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(isr)) \textcolor{keywordflow}{then}
133     \textcolor{keywordflow}{if} (isr < is) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Value of isr too small in reproducing\_EFP\_sum\_2d."})
134     is = isr
135 \textcolor{keywordflow}{  endif}
136   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ier)) \textcolor{keywordflow}{then}
137     \textcolor{keywordflow}{if} (ier > ie) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Value of ier too large in reproducing\_EFP\_sum\_2d."})
138     ie = ier
139 \textcolor{keywordflow}{  endif}
140   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(jsr)) \textcolor{keywordflow}{then}
141     \textcolor{keywordflow}{if} (jsr < js) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Value of jsr too small in reproducing\_EFP\_sum\_2d."})
142     js = jsr
143 \textcolor{keywordflow}{  endif}
144   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(jer)) \textcolor{keywordflow}{then}
145     \textcolor{keywordflow}{if} (jer > je) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Value of jer too large in reproducing\_EFP\_sum\_2d."})
146     je = jer
147 \textcolor{keywordflow}{  endif}
148 
149   over\_check = .true. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(overflow\_check)) over\_check = overflow\_check
150   do\_sum\_across\_pes = .true. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(only\_on\_pe)) do\_sum\_across\_pes = .not.only\_on\_pe
151 
152   overflow\_error = .false. ; nan\_error = .false. ; max\_mag\_term = 0.0
153   ints\_sum(:) = 0
154   \textcolor{keywordflow}{if} (over\_check) \textcolor{keywordflow}{then}
155     \textcolor{keywordflow}{if} ((je+1-js)*(ie+1-is) < max\_count\_prec) \textcolor{keywordflow}{then}
156       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
157         \textcolor{keyword}{call }increment\_ints\_faster(ints\_sum, array(i,j), max\_mag\_term)
158 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
159       \textcolor{keyword}{call }carry\_overflow(ints\_sum, prec\_error)
160     \textcolor{keywordflow}{elseif} ((ie+1-is) < max\_count\_prec) \textcolor{keywordflow}{then}
161       \textcolor{keywordflow}{do} j=js,je
162         \textcolor{keywordflow}{do} i=is,ie
163           \textcolor{keyword}{call }increment\_ints\_faster(ints\_sum, array(i,j), max\_mag\_term)
164 \textcolor{keywordflow}{        enddo}
165         \textcolor{keyword}{call }carry\_overflow(ints\_sum, prec\_error)
166 \textcolor{keywordflow}{      enddo}
167     \textcolor{keywordflow}{else}
168       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
169         \textcolor{keyword}{call }increment\_ints(ints\_sum, real\_to\_ints(array(i,j), prec\_error), &
170                             prec\_error)
171 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
172 \textcolor{keywordflow}{    endif}
173   \textcolor{keywordflow}{else}
174     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
175       sgn = 1 ; \textcolor{keywordflow}{if} (array(i,j)<0.0) sgn = -1
176       rs = abs(array(i,j))
177       \textcolor{keywordflow}{do} n=1,ni
178         ival = int(rs*i\_pr(n), 8)
179         rs = rs - ival*pr(n)
180         ints\_sum(n) = ints\_sum(n) + sgn*ival
181 \textcolor{keywordflow}{      enddo}
182 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
183     \textcolor{keyword}{call }carry\_overflow(ints\_sum, prec\_error)
184 \textcolor{keywordflow}{  endif}
185 
186   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(err)) \textcolor{keywordflow}{then}
187     err = 0
188     \textcolor{keywordflow}{if} (overflow\_error) &
189       err = err+2
190     \textcolor{keywordflow}{if} (nan\_error) &
191       err = err+4
192     \textcolor{keywordflow}{if} (err > 0) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} n=1,ni ; ints\_sum(n) = 0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
193   \textcolor{keywordflow}{else}
194     \textcolor{keywordflow}{if} (nan\_error) \textcolor{keywordflow}{then}
195       \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"NaN in input field of reproducing\_EFP\_sum(\_2d)."})
196 \textcolor{keywordflow}{    endif}
197     \textcolor{keywordflow}{if} (abs(max\_mag\_term) >= prec\_error*pr(1)) \textcolor{keywordflow}{then}
198       \textcolor{keyword}{write}(mesg, \textcolor{stringliteral}{'(ES13.5)'}) max\_mag\_term
199       \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"Overflow in reproducing\_EFP\_sum(\_2d) conversion of "}//trim(mesg))
200 \textcolor{keywordflow}{    endif}
201     \textcolor{keywordflow}{if} (overflow\_error) \textcolor{keywordflow}{then}
202       \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Overflow in reproducing\_EFP\_sum(\_2d)."})
203 \textcolor{keywordflow}{    endif}
204 \textcolor{keywordflow}{  endif}
205 
206   \textcolor{keywordflow}{if} (do\_sum\_across\_pes) \textcolor{keyword}{call }sum\_across\_pes(ints\_sum, ni)
207 
208   \textcolor{keyword}{call }regularize\_ints(ints\_sum)
209 
210   efp\_sum%v(:) = ints\_sum(:)
211 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_a82b35df61c7d0aba1712fc3ce7b47685}\label{namespacemom__coms_a82b35df61c7d0aba1712fc3ce7b47685}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!reproducing\+\_\+sum\+\_\+2d@{reproducing\+\_\+sum\+\_\+2d}}
\index{reproducing\+\_\+sum\+\_\+2d@{reproducing\+\_\+sum\+\_\+2d}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{reproducing\+\_\+sum\+\_\+2d()}{reproducing\_sum\_2d()}}
{\footnotesize\ttfamily real function mom\+\_\+coms\+::reproducing\+\_\+sum\+\_\+2d (\begin{DoxyParamCaption}\item[{real, dimension(\+:,\+:), intent(in)}]{array,  }\item[{integer, intent(in), optional}]{isr,  }\item[{integer, intent(in), optional}]{ier,  }\item[{integer, intent(in), optional}]{jsr,  }\item[{integer, intent(in), optional}]{jer,  }\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), intent(out), optional}]{E\+F\+P\+\_\+sum,  }\item[{logical, intent(in), optional}]{reproducing,  }\item[{logical, intent(in), optional}]{overflow\+\_\+check,  }\item[{integer, intent(out), optional}]{err,  }\item[{logical, intent(in), optional}]{only\+\_\+on\+\_\+\+PE }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine uses a conversion to an integer representation of real numbers to give an order-\/invariant sum of distributed 2-\/D arrays that reproduces across domain decomposition. This technique is described in Hallberg \& Adcroft, 2014, Parallel Computing, doi\+:10.\+1016/j.parco.\+2014.\+04.\+007. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em array} & The array to be summed\\
\hline
\mbox{\tt in}  & {\em isr} & The starting i-\/index of the sum, noting that the array indices starts at 1\\
\hline
\mbox{\tt in}  & {\em ier} & The ending i-\/index of the sum, noting that the array indices starts at 1\\
\hline
\mbox{\tt in}  & {\em jsr} & The starting j-\/index of the sum, noting that the array indices starts at 1\\
\hline
\mbox{\tt in}  & {\em jer} & The ending j-\/index of the sum, noting that the array indices starts at 1\\
\hline
\mbox{\tt out}  & {\em efp\+\_\+sum} & The result in extended fixed point format\\
\hline
\mbox{\tt in}  & {\em reproducing} & If present and false, do the sum using the naive non-\/reproducing approach\\
\hline
\mbox{\tt in}  & {\em overflow\+\_\+check} & If present and false, disable checking for overflows in incremental results. This can speed up calculations if the number of values being summed is small enough\\
\hline
\mbox{\tt out}  & {\em err} & If present, return an error code instead of triggering any fatal errors directly from this routine.\\
\hline
\mbox{\tt in}  & {\em only\+\_\+on\+\_\+pe} & If present and true, do not do the sum across processors, only reporting the local sum\\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
Result 
\end{DoxyReturn}


Definition at line 220 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
220   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:,:)},     \textcolor{keywordtype}{intent(in)}  :: array\textcolor{comment}{   !< The array to be summed}
221   \textcolor{keywordtype}{integer},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: isr\textcolor{comment}{     !< The starting i-index of the sum, noting}
222 \textcolor{comment}{                                                   !! that the array indices starts at 1}
223   \textcolor{keywordtype}{integer},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: ier\textcolor{comment}{     !< The ending i-index of the sum, noting}
224 \textcolor{comment}{                                                   !! that the array indices starts at 1}
225   \textcolor{keywordtype}{integer},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: jsr\textcolor{comment}{     !< The starting j-index of the sum, noting}
226 \textcolor{comment}{                                                   !! that the array indices starts at 1}
227   \textcolor{keywordtype}{integer},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: jer\textcolor{comment}{     !< The ending j-index of the sum, noting}
228 \textcolor{comment}{                                                   !! that the array indices starts at 1}
229   \textcolor{keywordtype}{type}(EFP\_type), \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: EFP\_sum\textcolor{comment}{  !< The result in extended fixed point format}
230   \textcolor{keywordtype}{logical},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: reproducing\textcolor{comment}{ !< If present and false, do the sum}
231 \textcolor{comment}{                                                !! using the naive non-reproducing approach}
232   \textcolor{keywordtype}{logical},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: overflow\_check\textcolor{comment}{ !< If present and false, disable}
233 \textcolor{comment}{                                                !! checking for overflows in incremental results.}
234 \textcolor{comment}{                                                !! This can speed up calculations if the number}
235 \textcolor{comment}{                                                !! of values being summed is small enough}
236   \textcolor{keywordtype}{integer},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: err\textcolor{comment}{  !< If present, return an error code instead of}
237 \textcolor{comment}{                                                !! triggering any fatal errors directly from}
238 \textcolor{comment}{                                                !! this routine.}
239   \textcolor{keywordtype}{logical},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: only\_on\_PE\textcolor{comment}{ !< If present and true, do not do the sum}
240 \textcolor{comment}{                                                !! across processors, only reporting the local sum}
241   \textcolor{keywordtype}{real}                                  :: sum\textcolor{comment}{  !< Result}
242 
243   \textcolor{comment}{!   This subroutine uses a conversion to an integer representation}
244   \textcolor{comment}{! of real numbers to give order-invariant sums that will reproduce}
245   \textcolor{comment}{! across PE count.  This idea comes from R. Hallberg and A. Adcroft.}
246 
247   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{dimension(ni)}  :: ints\_sum
248   \textcolor{keywordtype}{integer(kind=8)} :: prec\_error
249   \textcolor{keywordtype}{real}    :: rsum(1), rs
250   \textcolor{keywordtype}{logical} :: repro, do\_sum\_across\_PEs
251   \textcolor{keywordtype}{character(len=256)} :: mesg
252   \textcolor{keywordtype}{type}(EFP\_type) :: EFP\_val \textcolor{comment}{! An extended fixed point version of the sum}
253   \textcolor{keywordtype}{integer} :: i, j, n, is, ie, js, je
254 
255   \textcolor{keywordflow}{if} (num\_pes() > max\_count\_prec) \textcolor{keyword}{call }mom\_error(fatal, &
256     \textcolor{stringliteral}{"reproducing\_sum: Too many processors are being used for the value of "}//&
257     \textcolor{stringliteral}{"prec.  Reduce prec to (2^63-1)/num\_PEs."})
258 
259   prec\_error = (2\_8**62 + (2\_8**62 - 1)) / num\_pes()
260 
261   is = 1 ; ie = \textcolor{keyword}{size}(array,1) ; js = 1 ; je = \textcolor{keyword}{size}(array,2 )
262   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(isr)) \textcolor{keywordflow}{then}
263     \textcolor{keywordflow}{if} (isr < is) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Value of isr too small in reproducing\_sum\_2d."})
264     is = isr
265 \textcolor{keywordflow}{  endif}
266   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ier)) \textcolor{keywordflow}{then}
267     \textcolor{keywordflow}{if} (ier > ie) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Value of ier too large in reproducing\_sum\_2d."})
268     ie = ier
269 \textcolor{keywordflow}{  endif}
270   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(jsr)) \textcolor{keywordflow}{then}
271     \textcolor{keywordflow}{if} (jsr < js) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Value of jsr too small in reproducing\_sum\_2d."})
272     js = jsr
273 \textcolor{keywordflow}{  endif}
274   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(jer)) \textcolor{keywordflow}{then}
275     \textcolor{keywordflow}{if} (jer > je) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Value of jer too large in reproducing\_sum\_2d."})
276     je = jer
277 \textcolor{keywordflow}{  endif}
278 
279   repro = .true. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(reproducing)) repro = reproducing
280   do\_sum\_across\_pes = .true. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(only\_on\_pe)) do\_sum\_across\_pes = .not.only\_on\_pe
281 
282   \textcolor{keywordflow}{if} (repro) \textcolor{keywordflow}{then}
283     efp\_val = reproducing\_efp\_sum\_2d(array, isr, ier, jsr, jer, overflow\_check, err, only\_on\_pe)
284     sum = ints\_to\_real(efp\_val%v)
285     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(efp\_sum)) efp\_sum = efp\_val
286     \textcolor{keywordflow}{if} (debug) ints\_sum(:) = efp\_sum%v(:)
287   \textcolor{keywordflow}{else}
288     rsum(1) = 0.0
289     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
290       rsum(1) = rsum(1) + array(i,j)
291 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
292     \textcolor{keywordflow}{if} (do\_sum\_across\_pes) \textcolor{keyword}{call }sum\_across\_pes(rsum,1)
293     sum = rsum(1)
294 
295     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(err)) \textcolor{keywordflow}{then} ; err = 0 ;\textcolor{keywordflow}{ endif}
296 
297     \textcolor{keywordflow}{if} (debug .or. \textcolor{keyword}{present}(efp\_sum)) \textcolor{keywordflow}{then}
298       overflow\_error = .false.
299       ints\_sum = real\_to\_ints(sum, prec\_error, overflow\_error)
300       \textcolor{keywordflow}{if} (overflow\_error) \textcolor{keywordflow}{then}
301         \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(err)) \textcolor{keywordflow}{then}
302           err = err + 2
303         \textcolor{keywordflow}{else}
304           \textcolor{keyword}{write}(mesg, \textcolor{stringliteral}{'(ES13.5)'}) sum
305           \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"Repro\_sum\_2d: Overflow in real\_to\_ints conversion of "}//trim(mesg))
306 \textcolor{keywordflow}{        endif}
307 \textcolor{keywordflow}{      endif}
308 \textcolor{keywordflow}{    endif}
309     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(efp\_sum)) efp\_sum%v(:) = ints\_sum(:)
310 \textcolor{keywordflow}{  endif}
311 
312   \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}
313     \textcolor{keyword}{write}(mesg,\textcolor{stringliteral}{'("2d RS: ", ES24.16, 6 Z17.16)'}) sum, ints\_sum(1:ni)
314     \textcolor{keyword}{call }mom\_mesg(mesg, 3)
315 \textcolor{keywordflow}{  endif}
316 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__coms_aa98bb5adb44798d397f668edf62832e8}\label{namespacemom__coms_aa98bb5adb44798d397f668edf62832e8}} 
\index{mom\+\_\+coms@{mom\+\_\+coms}!reproducing\+\_\+sum\+\_\+3d@{reproducing\+\_\+sum\+\_\+3d}}
\index{reproducing\+\_\+sum\+\_\+3d@{reproducing\+\_\+sum\+\_\+3d}!mom\+\_\+coms@{mom\+\_\+coms}}
\subsubsection{\texorpdfstring{reproducing\+\_\+sum\+\_\+3d()}{reproducing\_sum\_3d()}}
{\footnotesize\ttfamily real function mom\+\_\+coms\+::reproducing\+\_\+sum\+\_\+3d (\begin{DoxyParamCaption}\item[{real, dimension(\+:,\+:,\+:), intent(in)}]{array,  }\item[{integer, intent(in), optional}]{isr,  }\item[{integer, intent(in), optional}]{ier,  }\item[{integer, intent(in), optional}]{jsr,  }\item[{integer, intent(in), optional}]{jer,  }\item[{real, dimension(\+:), intent(out), optional}]{sums,  }\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), intent(out), optional}]{E\+F\+P\+\_\+sum,  }\item[{type(\mbox{\hyperlink{structmom__coms_1_1efp__type}{efp\+\_\+type}}), dimension(\+:), intent(out), optional}]{E\+F\+P\+\_\+lay\+\_\+sums,  }\item[{integer, intent(out), optional}]{err,  }\item[{logical, intent(in), optional}]{only\+\_\+on\+\_\+\+PE }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine uses a conversion to an integer representation of real numbers to give an order-\/invariant sum of distributed 3-\/D arrays that reproduces across domain decomposition. This technique is described in Hallberg \& Adcroft, 2014, Parallel Computing, doi\+:10.\+1016/j.parco.\+2014.\+04.\+007. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em array} & The array to be summed\\
\hline
\mbox{\tt in}  & {\em isr} & The starting i-\/index of the sum, noting that the array indices starts at 1\\
\hline
\mbox{\tt in}  & {\em ier} & The ending i-\/index of the sum, noting that the array indices starts at 1\\
\hline
\mbox{\tt in}  & {\em jsr} & The starting j-\/index of the sum, noting that the array indices starts at 1\\
\hline
\mbox{\tt in}  & {\em jer} & The ending j-\/index of the sum, noting that the array indices starts at 1\\
\hline
\mbox{\tt out}  & {\em sums} & The sums by vertical layer\\
\hline
\mbox{\tt out}  & {\em efp\+\_\+sum} & The result in extended fixed point format\\
\hline
\mbox{\tt out}  & {\em efp\+\_\+lay\+\_\+sums} & The sums by vertical layer in E\+FP format\\
\hline
\mbox{\tt out}  & {\em err} & If present, return an error code instead of triggering any fatal errors directly from this routine.\\
\hline
\mbox{\tt in}  & {\em only\+\_\+on\+\_\+pe} & If present and true, do not do the sum across processors, only reporting the local sum\\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
Result 
\end{DoxyReturn}


Definition at line 325 of file M\+O\+M\+\_\+coms.\+F90.


\begin{DoxyCode}
325   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:,:,:)},       \textcolor{keywordtype}{intent(in)}  :: array\textcolor{comment}{   !< The array to be summed}
326   \textcolor{keywordtype}{integer},            \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: isr\textcolor{comment}{     !< The starting i-index of the sum, noting}
327 \textcolor{comment}{                                                       !! that the array indices starts at 1}
328   \textcolor{keywordtype}{integer},            \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: ier\textcolor{comment}{     !< The ending i-index of the sum, noting}
329 \textcolor{comment}{                                                       !! that the array indices starts at 1}
330   \textcolor{keywordtype}{integer},            \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: jsr\textcolor{comment}{     !< The starting j-index of the sum, noting}
331 \textcolor{comment}{                                                       !! that the array indices starts at 1}
332   \textcolor{keywordtype}{integer},            \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: jer\textcolor{comment}{     !< The ending j-index of the sum, noting}
333 \textcolor{comment}{                                                       !! that the array indices starts at 1}
334   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: sums\textcolor{comment}{    !< The sums by vertical layer}
335   \textcolor{keywordtype}{type}(EFP\_type),     \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: EFP\_sum\textcolor{comment}{ !< The result in extended fixed point format}
336   \textcolor{keywordtype}{type}(EFP\_type), \textcolor{keywordtype}{dimension(:)}, &
337                       \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: EFP\_lay\_sums\textcolor{comment}{ !< The sums by vertical layer in EFP format}
338   \textcolor{keywordtype}{integer},            \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: err\textcolor{comment}{  !< If present, return an error code instead of}
339 \textcolor{comment}{                                                    !! triggering any fatal errors directly from}
340 \textcolor{comment}{                                                    !! this routine.}
341   \textcolor{keywordtype}{logical},            \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: only\_on\_PE\textcolor{comment}{ !< If present and true, do not do the sum}
342 \textcolor{comment}{                                                    !! across processors, only reporting the local sum}
343   \textcolor{keywordtype}{real}                                      :: sum\textcolor{comment}{  !< Result}
344 
345   \textcolor{comment}{!   This subroutine uses a conversion to an integer representation}
346   \textcolor{comment}{! of real numbers to give order-invariant sums that will reproduce}
347   \textcolor{comment}{! across PE count.  This idea comes from R. Hallberg and A. Adcroft.}
348 
349   \textcolor{keywordtype}{real}    :: val, max\_mag\_term
350   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{dimension(ni)}  :: ints\_sum
351   \textcolor{keywordtype}{integer(kind=8)}, \textcolor{keywordtype}{dimension(ni,size(array,3))}  :: ints\_sums
352   \textcolor{keywordtype}{integer(kind=8)} :: prec\_error
353   \textcolor{keywordtype}{character(len=256)} :: mesg
354   \textcolor{keywordtype}{logical} :: do\_sum\_across\_PEs
355   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, ke, isz, jsz, n
356 
357   \textcolor{keywordflow}{if} (num\_pes() > max\_count\_prec) \textcolor{keyword}{call }mom\_error(fatal, &
358     \textcolor{stringliteral}{"reproducing\_sum: Too many processors are being used for the value of "}//&
359     \textcolor{stringliteral}{"prec.  Reduce prec to (2^63-1)/num\_PEs."})
360 
361   prec\_error = (2\_8**62 + (2\_8**62 - 1)) / num\_pes()
362   max\_mag\_term = 0.0
363 
364   is = 1 ; ie = \textcolor{keyword}{size}(array,1) ; js = 1 ; je = \textcolor{keyword}{size}(array,2) ; ke = \textcolor{keyword}{size}(array,3)
365   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(isr)) \textcolor{keywordflow}{then}
366     \textcolor{keywordflow}{if} (isr < is) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Value of isr too small in reproducing\_sum(\_3d)."})
367     is = isr
368 \textcolor{keywordflow}{  endif}
369   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ier)) \textcolor{keywordflow}{then}
370     \textcolor{keywordflow}{if} (ier > ie) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Value of ier too large in reproducing\_sum(\_3d)."})
371     ie = ier
372 \textcolor{keywordflow}{  endif}
373   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(jsr)) \textcolor{keywordflow}{then}
374     \textcolor{keywordflow}{if} (jsr < js) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Value of jsr too small in reproducing\_sum(\_3d)."})
375     js = jsr
376 \textcolor{keywordflow}{  endif}
377   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(jer)) \textcolor{keywordflow}{then}
378     \textcolor{keywordflow}{if} (jer > je) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Value of jer too large in reproducing\_sum(\_3d)."})
379     je = jer
380 \textcolor{keywordflow}{  endif}
381   jsz = je+1-js; isz = ie+1-is
382 
383   do\_sum\_across\_pes = .true. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(only\_on\_pe)) do\_sum\_across\_pes = .not.only\_on\_pe
384 
385   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(sums) .or. \textcolor{keyword}{present}(efp\_lay\_sums)) \textcolor{keywordflow}{then}
386     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(sums)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (\textcolor{keyword}{size}(sums) < ke) \textcolor{keywordflow}{then}
387       \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Sums is smaller than the vertical extent of array in reproducing\_sum(\_3d)."})
388 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}
389     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(efp\_lay\_sums)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (\textcolor{keyword}{size}(efp\_lay\_sums) < ke) \textcolor{keywordflow}{then}
390       \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Sums is smaller than the vertical extent of array in reproducing\_sum(\_3d)."})
391 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}
392     ints\_sums(:,:) = 0
393     overflow\_error = .false. ; nan\_error = .false. ; max\_mag\_term = 0.0
394     \textcolor{keywordflow}{if} (jsz*isz < max\_count\_prec) \textcolor{keywordflow}{then}
395       \textcolor{keywordflow}{do} k=1,ke
396         \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
397           \textcolor{keyword}{call }increment\_ints\_faster(ints\_sums(:,k), array(i,j,k), max\_mag\_term)
398 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}
399         \textcolor{keyword}{call }carry\_overflow(ints\_sums(:,k), prec\_error)
400 \textcolor{keywordflow}{      enddo}
401     \textcolor{keywordflow}{elseif} (isz < max\_count\_prec) \textcolor{keywordflow}{then}
402       \textcolor{keywordflow}{do} k=1,ke ; \textcolor{keywordflow}{do} j=js,je
403         \textcolor{keywordflow}{do} i=is,ie
404           \textcolor{keyword}{call }increment\_ints\_faster(ints\_sums(:,k), array(i,j,k), max\_mag\_term)
405 \textcolor{keywordflow}{        enddo}
406         \textcolor{keyword}{call }carry\_overflow(ints\_sums(:,k), prec\_error)
407 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
408     \textcolor{keywordflow}{else}
409       \textcolor{keywordflow}{do} k=1,ke ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
410         \textcolor{keyword}{call }increment\_ints(ints\_sums(:,k), &
411                             real\_to\_ints(array(i,j,k), prec\_error), prec\_error)
412 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
413 \textcolor{keywordflow}{    endif}
414     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(err)) \textcolor{keywordflow}{then}
415       err = 0
416       \textcolor{keywordflow}{if} (abs(max\_mag\_term) >= prec\_error*pr(1)) err = err+1
417       \textcolor{keywordflow}{if} (overflow\_error) err = err+2
418       \textcolor{keywordflow}{if} (nan\_error) err = err+2
419       \textcolor{keywordflow}{if} (err > 0) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,ke ; \textcolor{keywordflow}{do} n=1,ni ; ints\_sums(n,k) = 0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
420     \textcolor{keywordflow}{else}
421       \textcolor{keywordflow}{if} (nan\_error) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"NaN in input field of reproducing\_sum(\_3d)."})
422       \textcolor{keywordflow}{if} (abs(max\_mag\_term) >= prec\_error*pr(1)) \textcolor{keywordflow}{then}
423         \textcolor{keyword}{write}(mesg, \textcolor{stringliteral}{'(ES13.5)'}) max\_mag\_term
424         \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"Overflow in reproducing\_sum(\_3d) conversion of "}//trim(mesg))
425 \textcolor{keywordflow}{      endif}
426       \textcolor{keywordflow}{if} (overflow\_error) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Overflow in reproducing\_sum(\_3d)."})
427 \textcolor{keywordflow}{    endif}
428 
429     \textcolor{keywordflow}{if} (do\_sum\_across\_pes) \textcolor{keyword}{call }sum\_across\_pes(ints\_sums(:,1:ke), ni*ke)
430 
431     sum = 0.0
432     \textcolor{keywordflow}{do} k=1,ke
433       \textcolor{keyword}{call }regularize\_ints(ints\_sums(:,k))
434       val = ints\_to\_real(ints\_sums(:,k))
435       \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(sums)) sums(k) = val
436       sum = sum + val
437 \textcolor{keywordflow}{    enddo}
438     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(efp\_lay\_sums)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,ke
439       efp\_lay\_sums(k)%v(:) = ints\_sums(:,k)
440 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif}
441 
442     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(efp\_sum)) \textcolor{keywordflow}{then}
443       efp\_sum%v(:) = 0
444       \textcolor{keywordflow}{do} k=1,ke ; \textcolor{keyword}{call }increment\_ints(efp\_sum%v(:), ints\_sums(:,k)) ;\textcolor{keywordflow}{ enddo}
445 \textcolor{keywordflow}{    endif}
446 
447     \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}
448       \textcolor{keywordflow}{do} n=1,ni ; ints\_sum(n) = 0 ;\textcolor{keywordflow}{ enddo}
449       \textcolor{keywordflow}{do} k=1,ke ; \textcolor{keywordflow}{do} n=1,ni ; ints\_sum(n) = ints\_sum(n) + ints\_sums(n,k) ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
450       \textcolor{keyword}{write}(mesg,\textcolor{stringliteral}{'("3D RS: ", ES24.16, 6 Z17.16)'}) sum, ints\_sum(1:ni)
451       \textcolor{keyword}{call }mom\_mesg(mesg, 3)
452 \textcolor{keywordflow}{    endif}
453   \textcolor{keywordflow}{else}
454     ints\_sum(:) = 0
455     overflow\_error = .false. ; nan\_error = .false. ; max\_mag\_term = 0.0
456     \textcolor{keywordflow}{if} (jsz*isz < max\_count\_prec) \textcolor{keywordflow}{then}
457       \textcolor{keywordflow}{do} k=1,ke
458         \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
459           \textcolor{keyword}{call }increment\_ints\_faster(ints\_sum, array(i,j,k), max\_mag\_term)
460 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}
461         \textcolor{keyword}{call }carry\_overflow(ints\_sum, prec\_error)
462 \textcolor{keywordflow}{      enddo}
463     \textcolor{keywordflow}{elseif} (isz < max\_count\_prec) \textcolor{keywordflow}{then}
464       \textcolor{keywordflow}{do} k=1,ke ; \textcolor{keywordflow}{do} j=js,je
465         \textcolor{keywordflow}{do} i=is,ie
466           \textcolor{keyword}{call }increment\_ints\_faster(ints\_sum, array(i,j,k), max\_mag\_term)
467 \textcolor{keywordflow}{        enddo}
468         \textcolor{keyword}{call }carry\_overflow(ints\_sum, prec\_error)
469 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
470     \textcolor{keywordflow}{else}
471       \textcolor{keywordflow}{do} k=1,ke ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
472         \textcolor{keyword}{call }increment\_ints(ints\_sum, real\_to\_ints(array(i,j,k), prec\_error), &
473                             prec\_error)
474 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
475 \textcolor{keywordflow}{    endif}
476     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(err)) \textcolor{keywordflow}{then}
477       err = 0
478       \textcolor{keywordflow}{if} (abs(max\_mag\_term) >= prec\_error*pr(1)) err = err+1
479       \textcolor{keywordflow}{if} (overflow\_error) err = err+2
480       \textcolor{keywordflow}{if} (nan\_error) err = err+2
481       \textcolor{keywordflow}{if} (err > 0) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} n=1,ni ; ints\_sum(n) = 0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
482     \textcolor{keywordflow}{else}
483       \textcolor{keywordflow}{if} (nan\_error) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"NaN in input field of reproducing\_sum(\_3d)."})
484       \textcolor{keywordflow}{if} (abs(max\_mag\_term) >= prec\_error*pr(1)) \textcolor{keywordflow}{then}
485         \textcolor{keyword}{write}(mesg, \textcolor{stringliteral}{'(ES13.5)'}) max\_mag\_term
486         \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"Overflow in reproducing\_sum(\_3d) conversion of "}//trim(mesg))
487 \textcolor{keywordflow}{      endif}
488       \textcolor{keywordflow}{if} (overflow\_error) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Overflow in reproducing\_sum(\_3d)."})
489 \textcolor{keywordflow}{    endif}
490 
491     \textcolor{keywordflow}{if} (do\_sum\_across\_pes) \textcolor{keyword}{call }sum\_across\_pes(ints\_sum, ni)
492 
493     \textcolor{keyword}{call }regularize\_ints(ints\_sum)
494     sum = ints\_to\_real(ints\_sum)
495 
496     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(efp\_sum)) efp\_sum%v(:) = ints\_sum(:)
497 
498     \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}
499       \textcolor{keyword}{write}(mesg,\textcolor{stringliteral}{'("3d RS: ", ES24.16, 6 Z17.16)'}) sum, ints\_sum(1:ni)
500       \textcolor{keyword}{call }mom\_mesg(mesg, 3)
501 \textcolor{keywordflow}{    endif}
502 \textcolor{keywordflow}{  endif}
503 
\end{DoxyCode}
