The Langevin function and its inverse

The Langevin function is a special function that occurs in computations involving paramagnetic materials and elastomeres. It is defined as L(x) = coth(x) - 1/x. In most practical use cases, the function of interest is really the inverse of the Langevin function, i.e. L⁻¹(x), for which there is no analytic closed-form expression.

Traditionally, various simple approximation of low accuracy (about 1%) have been used for the inverse Langevin function. However, researchers have noticed more recently that this can have a noticeable negative impact on the overall accuracy of simulations. For example:

https://hal.archives-ouvertes.fr/hal-01361406
Amine Ammar, “Effect of the inverse Langevin approximation on the solution of the Fokker-Planck equation of non-linear dilute polymer”, Journal of Non-Newtonian Fluid Mechanics, No 231, 2016, pp. 1-5

As it turns out, both the Langevin function and its inverse allow for the use of the special function unit in GPUs (also known as the multifunction unit, or MUFU), resulting in accurate single-precision approximations that are also fast. Given that the Langevin function is only used in a small corner of the scientific computation application space, a GPU implementation will be useful to only a handful of people, but I figured there is no harm in sharing what I already have :-)

Code below updated 9/10/2021

/*
  Copyright (c) 2018-2021, Norbert Juffa
  All rights reserved.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright 
     notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* Implementation of the Langevin function L(x) = coth(x) - 1/x.
   maximum error: 1.54 ulp
*/
__device__ float langevinf (float a)
{
    float r, t;

    t = fabsf (a);
    if (t > 1.8125f) {
        float e;
        e = 2.0f * 1.442695f * t;
        asm ("ex2.approx.ftz.f32 %0,%0;" : "+f"(e));
        e = e - 1.0f;
        asm ("rcp.approx.ftz.f32 %0,%0;" : "+f"(e));
        asm ("rcp.approx.ftz.f32 %0,%0;" : "+f"(t));
        r = fmaf (2.0f, e, 1.0f - t);
        r = copysignf (r, a);
    } else {
        float s;
        s = a * a;
        r =              7.70960469e-8f;
        r = fmaf (r, s, -1.65101926e-6f);
        r = fmaf (r, s,  2.03457112e-5f);
        r = fmaf (r, s, -2.10521728e-4f);
        r = fmaf (r, s,  2.11580913e-3f);
        r = fmaf (r, s, -2.22220998e-2f);
        r = fmaf (r, s,  8.33333284e-2f);
        r = fmaf (r, a,  0.25f * a);
    }
    return r;
}

/* Inverse Langevin function L⁻¹(x). Maximum error: 3.925 ulp */
__device__ float langevininvf (float x)
{
    float fa, p, r, t;
    fa = fabsf (x);
    if ((fa > (57.0f / 64.0f)) && (fa <= 1.0f)) {
        t = fa - 1.0f;
        asm ("rcp.approx.ftz.f32 %0,%0;" : "+f"(t));
        r = copysignf (t, x);
    } else {
        t = fmaf (x, -x, 1.0f);
        asm ("lg2.approx.ftz.f32 %0,%0;" : "+f"(t));
        p =              2.69152224e-6f;  //  0x1.694000p-19
        p = fmaf (p, t, -1.40199758e-4f); // -0x1.26052cp-13
        p = fmaf (p, t, -1.82510854e-3f); // -0x1.de70f6p-10
        p = fmaf (p, t, -8.87932349e-3f); // -0x1.22f52ap-7
        p = fmaf (p, t, -2.20804960e-2f); // -0x1.69c450p-6
        p = fmaf (p, t, -3.11916713e-2f); // -0x1.ff0b5ap-6
        p = fmaf (p, t, -2.84342300e-2f); // -0x1.d1ddcep-6
        p = fmaf (p, t, -1.95302274e-2f); // -0x1.3ffbb6p-6
        p = fmaf (p, t,  1.15530221e-2f); //  0x1.7a91c6p-7
        p = fmaf (p, t, -6.13459945e-2f); // -0x1.f68be0p-5
        p = fmaf (p, t,  1.91382647e-1f); //  0x1.87f3a0p-3
        p = fmaf (p, t, -6.23836875e-1f); // -0x1.3f678cp-1
        p = fmaf (p, t,  5.00000000e-1f); //  0x1.000000p-1
        t = x + x;
        r = fmaf (p, t, t);
    }
    return r;
}

Njuffa, can you give a couple of examples where you applied it?
My thing is rock mechanics, which involves interaction with fluids, and though I never dealt with this function directly, it is there.

I am CS guy, not a domain expert, so I have not personally used this function in my work. One of my specialties is the implementation of special functions, and I noticed a cluster of publications on the inverse Langevin function in recent years, so I figured I should try my hand at it.

My approach, inspired by Mike Giles’s work on the erfinv() function, differs from anything I have encountered in the literature and provides much better accuracy than commonly used ones with a minimum of instructions and memory. Extending this to full double precision seems very challenging, but based on my current understanding, that is not something that scientific use cases require at this time.

There is one very recent paper that uses piece-wise approximation by a large-ish number of minimax polynomials that could also be of interest, but in general I favor approaches relying on computation rather than tables, as the computational throughput of processors tends to increase faster than the memory throughput.

José María Benitez and Francisco Javier Montáns, “A simple and efficient numerical procedure to compute the inverse Langevin function with high accuracy”. arXiv preprint arXiv:1806.08068 (2018).

I will certainly have a look at this paper and see how/where this function is being applied in other fields.
I have also come across an article at Mark Harris’ blog which has your contribution on a linear interpolation implementation. I hope I can ask you a few questions about it later, and on CUDA Fortran too, as it seems you contributed on a book. Don’t really want to hijack this topic to discuss totally unrelated things.

I do not recall directly contributing to any CUDA books, other than possibly with a couple of ideas. I certainly reviewed some chapters of some CUDA books and you probably saw my name in the acknowledgements because of that.

Yes, I contributed the linear interpolation with two FMAs as a CUDA tech tip. I am willing to bet that a number of people have come up with that idea independently, so this is just a minor thing. In case you are wondering, I do not believe the interpolation is monotonic, but it does hit the end-points accurately, has small overall error, and is very efficient.