help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Too much vectorizing


From: Kim Hansen
Subject: Re: Too much vectorizing
Date: Sat, 26 Apr 2008 21:26:21 +0200

On Thu, Apr 24, 2008 at 3:28 PM, Kim Hansen <address@hidden> wrote:
> I have "over vectorized" the following function, it allocates 1.2GB
>  when it runs and that is not at all necessary as the result is very
>  sparse.
>
>  The problem is that the size of A_capac, Lim and F is 2673x11385,
>  A_capac is sparse and uses only 848kB but Lim and F is full and uses
>  243MB each.
>
>  I could rewrite the function to use a lot of loops inside each other
>  and then it wouldn't use as much memory but is there a better way?
>
>  function cap_20 = cap_20(nb_locations, nb_mappings, nb_trips, a_capac, f, 
> lim)
>   A_capac = spkron(a_capac, eye(nb_locations));
>   Lim = kron(lim, ones(nb_locations*(nb_trips), nb_locations));
>   F = kron(ones(1,nb_mappings), kron(f, ones(nb_locations,1)));
>   cap_20.A.c20 =             A_capac .* (Lim .* 1    + !Lim .* F);
>   cap_20.A.flag_heavies_20 = A_capac .* (Lim .* (F-1));
>  endfunction
>
>  I have placed example data for the function in http://ange.dk/~kim/cap_20/

I rewrote my function to this:


function cap_20 = cap_20(nb_locations, nb_mappings, nb_trips, a_capac, f, lim)

  One = spkron(ones(nb_trips,nb_mappings), eye(nb_locations));

  i = reshape(1:nb_trips*nb_locations, nb_locations, nb_trips).';
  j = repmat(1:nb_locations, nb_trips, 1);
  fd = sparse(i, j, f, nb_trips*nb_locations, nb_locations);
  F = repmat(fd, 1, nb_mappings);

  A_capac = spkron(a_capac, eye(nb_locations));
  Lim = spkron(kron(lim, ones(nb_trips,1)), eye(nb_locations));

  cap_20 = struct();
  cap_20.A.c20 =             A_capac .* (Lim .* 1    + (One-Lim) .* F);
  cap_20.A.flag_heavies_20 = A_capac .* (Lim .* (F-One));

endfunction

-- 
Kim Hansen
Vadgårdsvej 3, 2.tv
2860 Søborg
Fastnet: 3956 2437 -- Mobil: 3091 2437



reply via email to

[Prev in Thread] Current Thread [Next in Thread]