bmad-sim / GTPSA.jl

Julia interface to the Generalised Truncated Power Series Algebra (GTPSA) library
https://bmad-sim.github.io/GTPSA.jl/
GNU General Public License v3.0
13 stars 1 forks source link

junk in tpsa #109

Closed mattsignorelli closed 6 months ago

mattsignorelli commented 6 months ago

Nasty bug,with the latest GTPSA C code. Two completely equivalent codes (C vs calling C directly in Julia), yet I'm only seeing the bug in Julia

The Julia code:

using GTPSA
import GTPSA: Desc, RTPSA, CTPSA
using Printf

const NV = Cint(4);
const MO = UInt8(5);
const MAD_TPSA = GTPSA.MAD_TPSA

const d = @ccall MAD_TPSA.mad_desc_newv(NV::Cint, MO::Cint)::Ptr{Desc}

x = @ccall MAD_TPSA.mad_tpsa_newd(d::Ptr{Desc}, 255::UInt8)::Ptr{RTPSA}
y = @ccall MAD_TPSA.mad_tpsa_newd(d::Ptr{Desc}, 255::UInt8)::Ptr{RTPSA}
z = @ccall MAD_TPSA.mad_tpsa_newd(d::Ptr{Desc}, 255::UInt8)::Ptr{RTPSA}

m = UInt8[0x5]

@ccall MAD_TPSA.mad_tpsa_seti(x::Ptr{RTPSA}, 0::Cint, 0.::Cdouble, 1.::Cdouble)::Cvoid;
@ccall MAD_TPSA.mad_tpsa_setm(y::Ptr{RTPSA}, 1::Cint, m::Ptr{UInt8}, 0.::Cdouble, 1.::Cdouble)::Cvoid;
@ccall MAD_TPSA.mad_tpsa_add(x::Ptr{RTPSA}, y::Ptr{RTPSA}, z::Ptr{RTPSA})::Cvoid;

@ccall MAD_TPSA.mad_tpsa_print(z::Ptr{RTPSA}, ""::Cstring, 0.::Cdouble, 0::Cint, C_NULL::Ptr{Cvoid})::Cvoid

@ccall MAD_TPSA.mad_tpsa_del(x::Ptr{RTPSA})::Cvoid;
@ccall MAD_TPSA.mad_tpsa_del(y::Ptr{RTPSA})::Cvoid;
@ccall MAD_TPSA.mad_tpsa_del(z::Ptr{RTPSA})::Cvoid;
@ccall MAD_TPSA.mad_desc_del(C_NULL::Ptr{Cvoid})::Cvoid;

The C code:

#include "mad_tpsa.h"

enum { NV = 4,
       MO = 5, 
};

int main(void)
{
  const desc_t *d = mad_desc_newv(NV, MO);

  tpsa_t* x = mad_tpsa_newd(d, 255);
  tpsa_t* y = mad_tpsa_newd(d, 255);
  tpsa_t* z = mad_tpsa_newd(d, 255);

  ord_t m[] = {5};

  mad_tpsa_seti(x, 0, 0.,1.);
  mad_tpsa_setm(y, 1, m, 0.,1.);

  mad_tpsa_add(x, y, z);

  mad_tpsa_print(z, "", 0., 0, 0);

  mad_tpsa_del(x);
  mad_tpsa_del(y);
  mad_tpsa_del(z);
  mad_desc_del(0);
  return 0;
}

The Julia outputs:

         :  R, NV =   4, MO =  5
 *******************************************************
     I   COEFFICIENT             ORDER   EXPONENTS
     1   1.0000000000000000E+00    0     0 0  0 0
     2                      NAN    1     1 0  0 0
     3   0.0000000000000000E+00    1     0 1  0 0
     4                      NAN    1     0 0  1 0
     5                      NAN    1     0 0  0 1
     6                      NAN    2     2 0  0 0
     7  5.2150180590755063E-310    2     1 1  0 0
     8                      NAN    2     0 2  0 0
     9  2.9643938750474793E-323    2     1 0  1 0
    10                      NAN    2     0 1  1 0
    11                      NAN    2     0 0  2 0
    12                      NAN    2     1 0  0 1
    13  5.2150168558794153E-310    2     0 1  0 1
    14                      NAN    2     0 0  1 1
    15  3.3951932656432488E-313    2     0 0  0 2
    16                      NAN    3     3 0  0 0
    17                      NAN    3     2 1  0 0
    18                      NAN    3     1 2  0 0
    19  5.2150180588344023E-310    3     0 3  0 0
    20                      NAN    3     2 0  1 0
    21  5.2150180588660225E-310    3     1 1  1 0
    22                      NAN    3     0 2  1 0
    23                      NAN    3     1 0  2 0
    24                      NAN    3     0 1  2 0
    25  5.2150180590715538E-310    3     0 0  3 0
    26                      NAN    3     2 0  0 1
    27  5.2150180590676013E-310    3     1 1  0 1
    28                      NAN    3     0 2  0 1
    29                      NAN    3     1 0  1 1
    30                      NAN    3     0 1  1 1
    31  5.2150180601221350E-310    3     0 0  2 1
    32                      NAN    3     1 0  0 2
    33                      NAN    3     0 1  0 2
    34                      NAN    3     0 0  1 2
    35                      NAN    3     0 0  0 3
    36                      NAN    4     4 0  0 0
    37                      NAN    4     3 1  0 0
    38                      NAN    4     2 2  0 0
    39  2.2163604321088704E-314    4     1 3  0 0
    40                      NAN    4     0 4  0 0
    41                      NAN    4     3 0  1 0
    42  2.1831624696840257E-314    4     2 1  1 0
    43                      NAN    4     1 2  1 0
    44                      NAN    4     0 3  1 0
    45                      NAN    4     2 0  2 0
    46                      NAN    4     1 1  2 0
    47                      NAN    4     0 2  2 0
    48                      NAN    4     1 0  3 0
    49  2.1831554499993295E-314    4     0 1  3 0
    50                      NAN    4     0 0  4 0
    51  2.1899901011822867E-314    4     3 0  0 1
    52                      NAN    4     2 1  0 1
    53                      NAN    4     1 2  0 1
    54  8.4879831643551549E-314    4     0 3  0 1
    55  2.1899901011822867E-314    4     2 0  1 1
    56                      NAN    4     1 1  1 1
    57  5.2150180540091596E-310    4     0 2  1 1
    58                      NAN    4     1 0  2 1
    59                      NAN    4     0 1  2 1
    60                      NAN    4     0 0  3 1
    61   0.0000000000000000E+00    4     2 0  0 2
    62                      NAN    4     1 1  0 2
    63  2.1899921248751720E-314    4     0 2  0 2
    64                      NAN    4     1 0  1 2
    65                      NAN    4     0 1  1 2
    66  2.1889572589259181E-314    4     0 0  2 2
    67  2.9643938750474793E-323    4     1 0  0 3
    68                      NAN    4     0 1  0 3
    69   0.0000000000000000E+00    4     0 0  1 3
    70                      NAN    4     0 0  0 4
    71   1.0000000000000000E+00    5     5 0  0 0
    72   0.0000000000000000E+00    5     4 1  0 0
    73   0.0000000000000000E+00    5     3 2  0 0
    74   0.0000000000000000E+00    5     2 3  0 0
    75   0.0000000000000000E+00    5     1 4  0 0
    76   0.0000000000000000E+00    5     0 5  0 0
    77   0.0000000000000000E+00    5     4 0  1 0
    78   0.0000000000000000E+00    5     3 1  1 0
    79   0.0000000000000000E+00    5     2 2  1 0
    80   0.0000000000000000E+00    5     1 3  1 0
    81   0.0000000000000000E+00    5     0 4  1 0
    82   0.0000000000000000E+00    5     3 0  2 0
    83   0.0000000000000000E+00    5     2 1  2 0
    84   0.0000000000000000E+00    5     1 2  2 0
    85   0.0000000000000000E+00    5     0 3  2 0
    86   0.0000000000000000E+00    5     2 0  3 0
    87   0.0000000000000000E+00    5     1 1  3 0
    88   0.0000000000000000E+00    5     0 2  3 0
    89   0.0000000000000000E+00    5     1 0  4 0
    90   0.0000000000000000E+00    5     0 1  4 0
    91   0.0000000000000000E+00    5     0 0  5 0
    92   0.0000000000000000E+00    5     4 0  0 1
    93   0.0000000000000000E+00    5     3 1  0 1
    94   0.0000000000000000E+00    5     2 2  0 1
    95   0.0000000000000000E+00    5     1 3  0 1
    96   0.0000000000000000E+00    5     0 4  0 1
    97   0.0000000000000000E+00    5     3 0  1 1
    98   0.0000000000000000E+00    5     2 1  1 1
    99   0.0000000000000000E+00    5     1 2  1 1
   100   0.0000000000000000E+00    5     0 3  1 1
   101   0.0000000000000000E+00    5     2 0  2 1
   102   0.0000000000000000E+00    5     1 1  2 1
   103   0.0000000000000000E+00    5     0 2  2 1
   104   0.0000000000000000E+00    5     1 0  3 1
   105   0.0000000000000000E+00    5     0 1  3 1
   106   0.0000000000000000E+00    5     0 0  4 1
   107   0.0000000000000000E+00    5     3 0  0 2
   108   0.0000000000000000E+00    5     2 1  0 2
   109   0.0000000000000000E+00    5     1 2  0 2
   110   0.0000000000000000E+00    5     0 3  0 2
   111   0.0000000000000000E+00    5     2 0  1 2
   112   0.0000000000000000E+00    5     1 1  1 2
   113   0.0000000000000000E+00    5     0 2  1 2
   114   0.0000000000000000E+00    5     1 0  2 2
   115   0.0000000000000000E+00    5     0 1  2 2
   116   0.0000000000000000E+00    5     0 0  3 2
   117   0.0000000000000000E+00    5     2 0  0 3
   118   0.0000000000000000E+00    5     1 1  0 3
   119   0.0000000000000000E+00    5     0 2  0 3
   120   0.0000000000000000E+00    5     1 0  1 3
   121   0.0000000000000000E+00    5     0 1  1 3
   122   0.0000000000000000E+00    5     0 0  2 3
   123   0.0000000000000000E+00    5     1 0  0 4
   124   0.0000000000000000E+00    5     0 1  0 4
   125   0.0000000000000000E+00    5     0 0  1 4
   126   0.0000000000000000E+00    5     0 0  0 5

The C code does not have this junk. Both use the exact same library compiled with the exact same compiler/settings.

mattsignorelli commented 6 months ago

The junk appears to enter on line 291 of mad_tpsa_ops.c, where the c->hi is set

mattsignorelli commented 6 months ago

The junk appears with different NV on different machines, e.g. on my Ubuntu WSL (4GB memory) I get junk at NV=1, on the mac student I get junk at NV=4 --- this could hopefully explain the mystery to some degree.

mattsignorelli commented 6 months ago

The bug appears in C with -fsanitize=address, reported upstream...

mattsignorelli commented 6 months ago

Resolved