From help-octave-request at bevo dot che dot wisc dot edu Thu Jan 9 14:20:20 2003 Subject: Memory usage From: "David Pruitt" To: "Octave" Date: Thu, 9 Jan 2003 14:20:14 -0600 This is a multi-part message in MIME format. ------=_NextPart_000_002B_01C2B7EA.3C92D040 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Here are two functions that I am using to sort through a database prior to analysis. I call these function many times through a particular run. My issue is the consumption of memory through the run. If you recall my previous message, a long run will consume >750MB of memory while I can account for only about 10MB. John Eaton suggested that looping in some functions might lead to virtual memory leaks. I would appreciate any comment on my programming in two example functions. Thanks for the help. Example1.m: function [ out, weight, nmax, idx ] = S_out(code, mat1, mat2, S1, S2 ) # # Input: # code - the three-column vector of three-letter reference codes # mat1 - matrix of values: if mat1 is a matrix of # a n-length time series of values for m observations, the m values # are averaged over time. # mat2 - matrix of identifying codes # # Output: # out - m-vector of weighted values # weight - weights # n - largest number of observations for any identifying code # idx - index of codes found in mat2 # m = size(idx,1); n = size(cap_mat,1); if n > 1 mat1_mean = mean(mat1); else mat1_mean = mat1; endif for i = 1:m ix = []; ix = find( idx(i,:) != 0 ); n(i) = size(ix,2); out(i) = sum( mat1_mean( idx(i, ix) ) ); endfor; nmax = max(n); weight = zeros(m,nmax); imx = find(mat != 0); for i = imx' ix = []; ix = find( idx(i,:) != 0 ); weight(i,ix) = (mat1_mean( idx(i, ix) ))/out(i); endfor; endfunction; Example 2: function [ Rtn_val, idx ] = S_Index(mat, code) # # Return the ID-sorted indicies of elements in the input code # # Input: # mat = database values (including several character strings - positions known) # code = the three-letter from the database # S1, S2 = reference indicies # # Output: # Rtn_val = Vector of identification numbers # idx = the set of indicies from the ID-sorted list # # ID = mat(:,1:5); matrix = toascii(mat(:,S1:S2)); c_code = toascii(code); m = size(code,1); n = size(sec_mat,2); idx = zeros(m,n); Rtn_val = zeros(m,1); for i = 1:m iz = []; iz1 = []; iz2 = []; iz3 = []; iz1 = find( matrix(:,1) == c_code(i,1) ); iz2 = find( matrix(:,2) == c_code(i,2) ); iz3 = find( matrix(:,3) == c_code(i,3) ); iz = intersection(iz1,iz2); iz = intersection(iz,iz3); n = size(iz,2); idx(i,1:n) = iz; for j = 1:n Rtn_val = str2num(ID(j,1:5)); endfor; endfor; endfunction; David Pruitt, PhD Ashdon Investment Analysis and Reseach, LLC Phone: 931.695.3220 Mobile: 931.639.2477 Fax: 931.695.5545 e-mail: david dot pruitt at ashdonanalysis dot com ------=_NextPart_000_002B_01C2B7EA.3C92D040 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Here are two functions that I am using to sort = through a database prior to analysis.  I call these function many times = through a particular run.  My issue is the consumption of memory through the run.  If you recall my previous message, a long run will consume = >750MB of memory while I can account for only about 10MB.  John Eaton = suggested that looping in some functions might lead to virtual memory leaks.  = I would appreciate any comment on my programming in two example = functions. 

 

Thanks for the help.

 

Example1.m:

function [ out, = weight, nmax, idx ] =3D S_out(code, mat1, mat2, S1, S2 )

#

# = Input:

#       code - the three-column vector of three-letter reference = codes

#       mat1 - matrix of values: if mat1 is a matrix of

#         a n-length time series of values for m observations, the m = values

#         are averaged over time.

#       mat2 - matrix of identifying codes

#

# = Output:

#        out - m-vector of weighted values

#        weight - weights

#        n - largest number of observations for any identifying = code

#        idx - index of codes found in mat2

#

  m =3D = size(idx,1);

  n =3D = size(cap_mat,1);

  if n > = 1

    = mat1_mean =3D mean(mat1);

  = else

    = mat1_mean =3D mat1;

  = endif

  for i =3D = 1:m

    = ix =3D [];

    = ix =3D find( idx(i,:) !=3D 0 );

    = n(i) =3D size(ix,2);

    = out(i) =3D sum( mat1_mean( idx(i, ix) ) );

  = endfor;

  nmax =3D = max(n);

 

  weight =3D zeros(m,nmax);

  imx =3D = find(mat !=3D 0);

  for i =3D = imx'

    = ix =3D [];

    = ix =3D find( idx(i,:) !=3D 0 );

    weight(i,ix) =3D (mat1_mean( idx(i, ix) ))/out(i);

  = endfor;

 

endfunction;

 

Example 2:

function  [ = Rtn_val, idx ] =3D S_Index(mat, code)

#

#  Return the = ID-sorted indicies of elements in the input code

#

#  = Input:

#           mat =3D database values (including several character strings - positions = known)

#           code =3D the three-letter from the database

#           S1, S2 =3D reference indicies

#

#  = Output:

#          Rtn_val =3D Vector of identification numbers

#          idx =3D the set of indicies from the ID-sorted list

#

#

  ID =3D = mat(:,1:5);

  matrix =3D = toascii(mat(:,S1:S2));

  c_code =3D toascii(code);

 

  m =3D = size(code,1);

  n =3D = size(sec_mat,2);

  idx =3D = zeros(m,n);

  Rtn_val =3D = zeros(m,1);

  for i =3D = 1:m

 

    = iz =3D [];

    = iz1 =3D [];

    = iz2 =3D [];

    = iz3 =3D [];

    = iz1 =3D find( matrix(:,1) =3D=3D c_code(i,1) );

   =  iz2 =3D find( matrix(:,2) =3D=3D c_code(i,2) );

    = iz3 =3D find( matrix(:,3) =3D=3D c_code(i,3) );

    = iz =3D intersection(iz1,iz2);

    = iz =3D intersection(iz,iz3);

    = n =3D size(iz,2);

    idx(i,1:n) =3D iz;

    = for j =3D 1:n

      Rtn_val =3D str2num(ID(j,1:5));

    = endfor;

 

  = endfor;

 

 

endfunction;

 

 

David Pruitt, PhD

Ashdon Investment Analysis and Reseach, = LLC

Phone: 931.695.3220

Mobile: = 931.639.2477

Fax: 931.695.5545

e-mail: david dot pruitt@ashdonanalys= is.com

 

------=_NextPart_000_002B_01C2B7EA.3C92D040-- ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------