Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Segfault when accessing result of tensor.__add__ #2641

Open
ss32 opened this issue May 13, 2024 · 0 comments
Open

[BUG] Segfault when accessing result of tensor.__add__ #2641

ss32 opened this issue May 13, 2024 · 0 comments
Labels
bug Something isn't working mojo-repo Tag all issues with this label

Comments

@ss32
Copy link

ss32 commented May 13, 2024

Bug description

When accessing the result of tensor.__mul__ the program crashes. Computing the result does not elicit the crash, but attempting to print it or store it to a list does.

Stack trace:

Stack dump:
0.	Program arguments: mojo run grav.mojo
 #0 0x000056834b6f3407 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/user/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x128c407)
 #1 0x000056834b6f125e llvm::sys::RunSignalHandlers() (/home/user/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x128a25e)
 #2 0x000056834b6f3a9f SignalHandler(int) (/home/user/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x128ca9f)
 #3 0x0000708112642520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x000056834cec03d6 tcmalloc::SLL_Next(void*) (/home/user/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x2a593d6)
 #5 0x000056834cec0473 tcmalloc::SLL_TryPop(void**, void**) (/home/user/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x2a59473)
 #6 0x000056834cec0877 tcmalloc::ThreadCache::FreeList::TryPop(void**) (/home/user/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x2a59877)
 #7 0x000056834db24350 tc_newarray_aligned (/home/user/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x36bd350)
 #8 0x000070810863b7cc KGEN_CompilerRT_AlignedAlloc (/home/user/.modular/pkg/packages.modular.com_mojo/lib/libKGENCompilerRTShared.so.19.0git+0x3b7cc)
 #9 0x0000708098005185 
mojo crashed!
Please file a bug report.
Segmentation fault (core dumped)

Steps to reproduce

  • Include relevant code snippet or link to code that did not work as expected.

I'm unable to reproduce in a smaller toy example but will update this if I can. Commenting out solutions.append(xx) yields no errors. The same crash can be seen if the call to append is replaced with print(xx).

from tensor import Tensor
import math
 
 
alias G = 1.0
alias M = 1.0
alias m = 1.0
alias dt = 0.1
alias tEnd = 100
alias nSteps = int(tEnd / dt)
alias float_type = DType.float64
alias simd_width = 2 * simdwidthof[float_type]()
 
 
fn norm(v: Tensor[float_type]) -> SIMD[float_type, 1]:
    return math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])
 
fn two_body_first_order_ode(
    x: Tensor[float_type],
    t: Float64,
    masses: Tensor[float_type],
) -> Tensor[float_type]:
    var len = x.shape()[0]
    var dxdt = Tensor[float_type](len,1)
    var r1 = Tensor[float_type](3)
    var r2 = Tensor[float_type](3)
    for i in range(3):
        r1[i] = x[i]
        r2[i] = x[i + 3]
    try:
        var rr = r2 - r1
 
        var r3 = norm(rr)
        for i in range(3):
            dxdt[i] = x[i + 5]
            dxdt[i + 3] = x[i + 8]
            dxdt[i + 5] = -G * masses[0] * (r2[i] - r1[i]) / r3
            dxdt[i + 8] = -G * masses[1] * (r1[i] - r2[i]) / r3
        return dxdt
    except:
        print("Error in rr")
    return Tensor[float_type](len,1)
 
 
def compute_orbits():
    # Initial conditions
    r_sun = Tensor[float_type](
        (3, 1),
        -1,
        0,
        1,
    )
    r_earth = Tensor[float_type](
        (3, 1),
        1,
        0,
        0,
    )
    v_sun = Tensor[float_type](
        (3, 1),
        0,
        -0.5,
        0,
    )
    v_earth = Tensor[float_type](
        (3, 1),
        0,
        0.5,
        0,
    )
    earth_positions = Tensor[float_type](
        (3, nSteps),
    )
    sun_positions = Tensor[float_type](
        (3, nSteps),
    )
    x = Tensor[float_type](
        (9, 1),
    )
    for i in range(3):
        x[i] = r_earth[i]
        x[i + 3] = r_sun[i]
        x[i + 5] = v_earth[i]
        x[i + 8] = v_sun[i]
    masses = Tensor[float_type](
        (2, 1),
        m,
        M,
    )
    solutions = List[Tensor[DType.float64]]()
    for t in range(nSteps):
        dxdt = two_body_first_order_ode(x, t, masses)
        var m = (dxdt.__mul__(dt))
        var xx = x.__add__(m)
        solutions.append(xx)
 
fn main():
    try:
        compute_orbits()
    except Exception:
        print("Error in main")
        return
  • If applicable, add screenshots to help explain the problem.

n/a

  • If using the Playground, name the pre-existing notebook that failed and the steps that led to failure.

n/a

  • Include anything else that might help us debug the issue.

System information

  • What OS did you do install Mojo on ?
OS: Ubuntu Cinnamon 22.04.4 LTS x86_64
Host: MS-7C37 1.0
Kernel: 6.5.0-28-generic 
CPU: AMD Ryzen 7 3700X (16) @ 3.600GHz
  • Provide version information for Mojo by pasting the output of mojo -v
$ mojo -v 
mojo 24.3.0 (9882e19d)
  • Provide Modular CLI version by pasting the output of modular -v
$ modular -v
modular 0.7.4 (df7a9e8b)
@ss32 ss32 added bug Something isn't working mojo-repo Tag all issues with this label labels May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

1 participant