在 VMD 的 TCL 中计算巨大矩阵时出现内存问题 - malloc:无法分配区域

Memory issue when calculating a huge matrix in VMD's TCL - malloc: can't allocate region

提问人:Nikola 提问时间:12/5/2022 更新时间:12/5/2022 访问量:85

问:

我编写了一个 *.tcl 脚本,用于在文本模式(命令行)中使用 VMD 计算轨迹帧 (n=5000) 之间的成对 RMSD。但是,当我在终端上运行脚本(vmd -dispdev text -e distmatrix.tcl)时,在处理了几帧后,脚本因内存问题而停止:

$vmd -dispdev text -e distmatrix.tcl
Info) VMD for MACOSXX86, version 1.9.3 (November 30, 2016)
Info) http://www.ks.uiuc.edu/Research/vmd/                         
Info) Email questions and bug reports to [email protected]           
Info) Please include this reference in published work using VMD:   
Info)    Humphrey, W., Dalke, A. and Schulten, K., `VMD - Visual   
Info)    Molecular Dynamics', J. Molec. Graphics 1996, 14.1, 33-38.
Info) -------------------------------------------------------------
Info) Multithreading available, 8 CPUs detected.
Info) Dynamically loaded 2 plugins in directory:
Info) /Applications/VMD/VMD 1.9.3.app/Contents/vmd/plugins/MACOSXX86/molfile
{/Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts/tcl8.5} {/Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts} {/Applications/VMD/VMD 1.9.3.app/Contents/lib} /Applications/VMD/VMD 1.9.3.app/Contents/Frameworks/Tcl.framework/Versions/8.5/Resources/Scripts ~/Library/Tcl /Library/Tcl /Network/Library/Tcl /System/Library/Tcl ~/Library/Frameworks /Library/Frameworks /Network/Library/Frameworks /System/Library/Frameworks {/Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts/vmd} {/Applications/VMD/VMD 1.9.3.app/Contents/vmd/plugins/MACOSXX86/tcl} {/Applications/VMD/VMD 1.9.3.app/Contents/vmd/plugins/noarch/tcl} /Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts/la1.0
{/Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts/tcl8.5} {/Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts} {/Applications/VMD/VMD 1.9.3.app/Contents/lib} /Applications/VMD/VMD 1.9.3.app/Contents/Frameworks/Tcl.framework/Versions/8.5/Resources/Scripts ~/Library/Tcl /Library/Tcl /Network/Library/Tcl /System/Library/Tcl ~/Library/Frameworks /Library/Frameworks /Network/Library/Frameworks /System/Library/Frameworks {/Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts/vmd} {/Applications/VMD/VMD 1.9.3.app/Contents/vmd/plugins/MACOSXX86/tcl} {/Applications/VMD/VMD 1.9.3.app/Contents/vmd/plugins/noarch/tcl} /Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts/la1.0 /Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts/orient
{/Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts/tcl8.5} {/Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts} {/Applications/VMD/VMD 1.9.3.app/Contents/lib} /Applications/VMD/VMD 1.9.3.app/Contents/Frameworks/Tcl.framework/Versions/8.5/Resources/Scripts ~/Library/Tcl /Library/Tcl /Network/Library/Tcl /System/Library/Tcl ~/Library/Frameworks /Library/Frameworks /Network/Library/Frameworks /System/Library/Frameworks {/Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts/vmd} {/Applications/VMD/VMD 1.9.3.app/Contents/vmd/plugins/MACOSXX86/tcl} {/Applications/VMD/VMD 1.9.3.app/Contents/vmd/plugins/noarch/tcl} /Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts/la1.0 /Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts/orient /Applications/VMD/VMD 1.9.3.app/Contents/vmd/scripts/vmd/get_total_charge.tcl
2.0.2
file6
5000
::m
Progress: ▏▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋               ▏  65%  (3295/5000)vmd(96350,0x5ae080) malloc: can't allocate region
*** mach_vm_map(size=8388608) failed (error code=3)
vmd(96350,0x5ae080) malloc: *** set a breakpoint in malloc_error_break to debug
vmd(96350,0x5ae080) malloc: can't allocate region
*** mach_vm_map(size=8388608) failed (error code=3)
vmd(96350,0x5ae080) malloc: *** set a breakpoint in malloc_error_break to debug
unable to realloc 1149 bytes
Abort trap: 6

有趣的是,当我在 tclsh (./distmatrix.tcl) 中运行相同的脚本时,程序运行平稳,并成功完成在输出文件中写入最终的 5000x5000 矩阵。

$./distmatrix.tcl
Progress: ▏▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋▋ ▏ 100%  (5000/5000)
Done.

为了简单起见,我测试了一个类似的简单脚本(不是真正的轨迹计算),用于生成一个 5000x5000 矩阵(下面的代码):

#!/usr/bin/tclsh

package require struct::matrix

################################################################

proc progress_bar {totalsize current} {
font color light green for the bar
    set fgreenl [exec tput setaf 40]
no color 
    set nb [exec tput sgr 0]
puts -nonewline "\rProgress: \u258F"

        if {$totalsize == 0} {
            set totalsize $current
        }

        set portion [expr 1.0 * $current/$totalsize * 40]

        for {set x 0} {$x <= $portion} {incr x} {
        puts -nonewline "${fgreenl}\u258B${nb}"
        }

        for {} {$x <= 40} {incr x} {
            puts -nonewline " "
        }
    
    puts -nonewline " \u258F [format "%3d" [expr int(100.0 * $current/$totalsize)]]%  ($current/$totalsize)"

        flush stdout
        if {$totalsize == $current} {
            puts "\nDone."
        }
}


Output file matrix
set outfile [open mat2.txt w]

################################################################

set n 5000


Create a new matrix that will store all data (pairwise RMSD values)
struct::matrix m;

Define number of rows that is equal to the number of frames in the MD trajectory
m add columns [expr $n + 1];

for {set i 0} {$i <= $n} {incr i} {
    set vecl {}
    for {set j 0} {$j <= $n} {incr j} {
set vec [expr sqrt($j)]
        lappend vecl $vec

    m add row $vecl;
    progress_bar $n $i
    after 1
}
set vec_vals [m format 2string;]
puts $outfile $vec_vals
close $outfile


exit

是否有可能克服这个内存问题(例如,以某种方式通过对第二个循环中执行的操作进行分块)?

矩阵 内存泄漏 malloc tcl vmd

评论

0赞 Donal Fellows 12/10/2022
unable to realloc 1149 bytes是一个非常糟糕的迹象;要么代码完全有问题,要么你已经彻底耗尽了内存,以至于你无法获得 1149 个连续的字节。

答: 暂无答案