help-octave
[Top][All Lists]
Advanced

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

global variable to avoid call-by-value


From: Stefano Ghirlanda
Subject: global variable to avoid call-by-value
Date: Tue, 31 May 2005 13:35:06 +0200
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

Dear all,

I am developing a small library for some neural network stuff. Since a
network is potentially a large structure (node layers, weight
matrices, etc) I figured that call-by-value could be a problem.  After
reading a related thread on the list I tried to avoid call-by-value by
creating a global cell array that holds network structures, and
passing indices into this array to functions. It made no difference
(details below).

One example. Suppose I want to update the activation state of nodes in
the network. In a language with pass-by-reference I could write

  update_network( &net )

(adopting the C++ & syntax for references), while with call-by-value I
have to write something like

  net = update_network( net )

where update_network uses the following simple-minded strategy:

  function net_out = update_network( net_in )
    net_out = net_in;
    # operate only on net_out from now on
  endfunction

Note that update_network does modify the network rather heavily
updating the activation states of all nodes, possibly the nodes'
internal states and also some other maintenance variables.

I have tried use the global variable approach in more or less this
way:

  global Networks = {}; # global structure holding all networks

  function update_network( net_index )
    global Networks;
    # operate directly on Networks{ net_index }
  endfunction

I have used exaclty this strategy for every function call that would
need to modify its argument. I have then run a test teaching a
two-layer network to respond with given outputs to four inputs. I have
used a network with 25, 250, 2500 and 25000 input nodes with these
results (times in seconds):

Global variable approach: 13, 13, 15, 41  
Simple-mindes call-by value: 10, 10, 13, 39

So the latter is, if anything slightly better. Should I conclude that
Octave is very very smart in deciding when to actually copy data and
when to just count and shuffle pointers, or am I missing something?

For instance, does Octave notive that the "net" on the lhs and rhs of

  net = update_network( net )

are the same object and thus avoids any duplication of data
whatsoever?

Thanks!
Stefano

-- 
Stefano   | Department of Psychology, University of Bologna
Ghirlanda | Interdisciplinary cultural research, Stockholm University
            http://www.intercult.su.se/~stefano



-------------------------------------------------------------
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
-------------------------------------------------------------



reply via email to

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