arrayfire / arrayfire-rust

Rust wrapper for ArrayFire
BSD 3-Clause "New" or "Revised" License
810 stars 58 forks source link

[BUG] Memory accumulation when using load_image in a loop #365

Open 3togo opened 1 year ago

3togo commented 1 year ago

The following program crashed. But why?

use arrayfire::*;

fn main() {
    let mut y =constant!(0 as u8;1);
    for i in 0..1000 {
        let x = load_image::<u8>("2dzs/robot_2dz.png".to_string(), true);
        let z = or(&x, &y, true);
        y = z.copy();
        println!("i={}", i);
    }
    println!("max(y)={:#?}", max_all(&y));
}
i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
i=11
i=12
i=13
i=14
i=15
i=16
i=17
i=18
i=19
i=20
i=21
i=22
i=23
i=24
i=25
i=26
i=27
i=28
i=29
i=30
i=31
i=32
i=33
i=34
i=35
i=36
i=37
i=38
i=39
i=40
i=41
i=42
i=43
i=44
i=45
i=46
i=47
i=48
i=49
i=50
i=51
thread 'main' panicked at 'Error message: System or Device ran out of memory
Last error: In function af_load_image
In file src/api/c/imageio.cpp:393

 0# 0x00007F4FC1A299AF in /opt/arrayfire/lib/libafcuda.so.3
 1# 0x0000560F6F590AFF in target/debug/crash
 2# 0x0000560F6F590099 in target/debug/crash
 3# 0x0000560F6F5903AB in target/debug/crash
 4# 0x0000560F6F59031E in target/debug/crash
 5# 0x0000560F6F58FC41 in target/debug/crash
 6# 0x0000560F6F5A707F in target/debug/crash
 7# 0x0000560F6F58FC10 in target/debug/crash
 8# 0x0000560F6F5902EC in target/debug/crash
 9# 0x00007F4FFB823510 in /lib/x86_64-linux-gnu/libc.so.6
10# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6
11# 0x0000560F6F58FB15 in target/debug/crash
', /home/joe/.cargo/registry/src/mirrors.ustc.edu.cn-12df342d903acd47/arrayfire-3.8.0/src/core/error.rs:37:14
stack backtrace:
   0: rust_begin_unwind
             at /rustc/c07a8b4e09f356c7468b69c50cac7fc5b5000b8a/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/c07a8b4e09f356c7468b69c50cac7fc5b5000b8a/library/core/src/panicking.rs:142:14
   2: arrayfire::core::error::handle_error_general
             at /home/joe/.cargo/registry/src/mirrors.ustc.edu.cn-12df342d903acd47/arrayfire-3.8.0/src/core/error.rs:37:14
   3: arrayfire::core::error::Callback::call
             at /home/joe/.cargo/registry/src/mirrors.ustc.edu.cn-12df342d903acd47/arrayfire-3.8.0/src/core/error.rs:29:9
   4: arrayfire::core::error::HANDLE_ERROR
             at /home/joe/.cargo/registry/src/mirrors.ustc.edu.cn-12df342d903acd47/arrayfire-3.8.0/src/core/error.rs:95:5
   5: arrayfire::image::load_image
             at /home/joe/.cargo/registry/src/mirrors.ustc.edu.cn-12df342d903acd47/arrayfire-3.8.0/src/image/mod.rs:317:9
   6: crash::main
             at ./src/main.rs:6:17
   7: core::ops::function::FnOnce::call_once
             at /rustc/c07a8b4e09f356c7468b69c50cac7fc5b5000b8a/library/core/src/ops/function.rs:248:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
3togo commented 1 year ago

The revised version will still fail. How to avoid memory accumulation?

use arrayfire::*;
// use std::mem;
fn main() {
    let device = get_device();
    for i in 0..1000 {
        let x = load_image::<u8>("2dzs/robot_2dz.png".to_string(), true);
        x.eval();
        // mem::forget(x);
        drop(x);
        sync(device);
        println!("i={}", i);
    }

}

3togo commented 1 year ago

use load_image_native instead of load_image could avoid the problem

3togo commented 1 year ago

Actually load_image_native will still cause memory accumulation but much slower than that of load_image.

How to free the accumulated memory to avoid the program crashing?