<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
 
 <title>The Julia Blog</title>
 
 <link href="http://julialang.org/blog" />
 <updated>2013-05-20T11:06:22-07:00</updated>
 <id>http://julialang.org/blog</id>
 <author>
   <name>Julia Developers</name>
   <email>julia-dev@googlegroups.com</email>
 </author>

 
 <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/JuliaLang" /><feedburner:info uri="julialang" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
   <title>Passing Julia Callback Functions to C</title>
   <link href="http://feedproxy.google.com/~r/JuliaLang/~3/37Rn8aFhrvY/callback" />
   <updated>2013-05-10T00:00:00-07:00</updated>
   <id>http://julialang.org/blog/2013/05/callback</id>
   <content type="html">&lt;p&gt;One of the great strengths of Julia is that it is so easy to &lt;a href="http://docs.julialang.org/en/latest/manual/calling-c-and-fortran-code.html"&gt;call C
code&lt;/a&gt; natively, with no special &amp;ldquo;glue&amp;rdquo; routines or overhead to marshal
arguments and convert return values.  For example, if you want to call
&lt;a href="http://www.gnu.org/software/gsl/"&gt;GNU GSL&lt;/a&gt; to compute a special function
like a &lt;a href="http://linux.math.tifr.res.in/manuals/html/gsl-ref-html/gsl-ref_7.html"&gt;Debye integral&lt;/a&gt;, it is as easy as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;debye_1(x) = ccall((:gsl_sf_debye_1,:libgsl), Cdouble, (Cdouble,), x)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;at which point you can compute &lt;code&gt;debye_1(2)&lt;/code&gt;, &lt;code&gt;debye_1(3.7)&lt;/code&gt;, and so
on.  (Even easier would be to use Jiahao Chen&amp;rsquo;s
&lt;a href="https://github.com/jiahao/GSL.jl"&gt;GSL package&lt;/a&gt; for Julia, which has
already created such wrappers for you.)  This makes a vast array of
existing C libraries accessible to you in Julia (along with Fortran
libraries and other languages with C-accessible calling conventions).&lt;/p&gt;

&lt;p&gt;In fact, you can even go the other way around, passing Julia routines
to C, so that C code is calling Julia code in the form of &lt;em&gt;callback&lt;/em&gt;
functions.   For example, a C library for numerical integration might
expect you to pass the integrand as a &lt;em&gt;function&lt;/em&gt; argument, which the
library will then call to evaluate the integrand as many times as
needed to estimate the integral.  Callback functions are also natural
for optimization, root-finding, and many other numerical tasks, as well
as in many non-numerical problems.  The purpose of this blog post is to
illustrate the techniques for passing Julia functions as callbacks to
C routines, which is straightforward and efficient but requires some
lower-level understanding of how functions and other values are passed as
arguments.&lt;/p&gt;

&lt;p&gt;The code in this post requires Julia 0.2 (or a recent &lt;code&gt;git&lt;/code&gt; facsimile
thereof); the key features needed for callback functions (especially
&lt;code&gt;unsafe_pointer_to_objref&lt;/code&gt;) are not available in Julia 0.1.&lt;/p&gt;

&lt;h2 id="Sorting+with+`qsort`"&gt;Sorting with &lt;code&gt;qsort&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Perhaps the most well-known example of a callback parameter is
provided by the
&lt;a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/qsort.html"&gt;qsort&lt;/a&gt;
function, part of the ANSI C standard library and declared in C as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;void qsort(void *base, size_t nmemb, size_t size,
           int(*compare)(const void *a, const void *b));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;base&lt;/code&gt; argument is a pointer to an array of length &lt;code&gt;nmemb&lt;/code&gt;, with
elements of &lt;code&gt;size&lt;/code&gt; bytes each.  &lt;code&gt;compare&lt;/code&gt; is a callback function which
takes pointers to two elements &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; and returns an integer
less/greater than zero if &lt;code&gt;a&lt;/code&gt; should appear before/after &lt;code&gt;b&lt;/code&gt; (or zero
if any order is permitted).  Now, suppose that we have a 1d array &lt;code&gt;A&lt;/code&gt;
of values in Julia that we want to sort using the &lt;code&gt;qsort&lt;/code&gt; function
(rather than Julia&amp;rsquo;s built-in &lt;code&gt;sort&lt;/code&gt; function).  Before we worry about
calling &lt;code&gt;qsort&lt;/code&gt; and passing arguments, we need to write a comparison
function that works for some arbitrary type &lt;code&gt;T&lt;/code&gt;, e.g.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function mycompare{T}(a_::Ptr{T}, b_::Ptr{T})
    a = unsafe_load(a_)
    b = unsafe_load(b_)
    return a &amp;lt; b ? cint(-1) : a &amp;gt; b ? cint(+1) : cint(0)
end
cint(n) = convert(Cint, n)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice that we use the built-in function &lt;code&gt;unsafe_load&lt;/code&gt; to fetch the
values pointed to by the arguments &lt;code&gt;a_&lt;/code&gt; and &lt;code&gt;b_&lt;/code&gt; (which is &amp;ldquo;unsafe&amp;rdquo;
because it will crash if these are not valid pointers, but &lt;code&gt;qsort&lt;/code&gt;
will always pass valid pointers).  Also, we have to be a little
careful about return values: &lt;code&gt;qsort&lt;/code&gt; expects a function returning a C
&lt;code&gt;int&lt;/code&gt;, so we must be sure to return &lt;code&gt;Cint&lt;/code&gt; (the corresponding type in
Julia) via a call to &lt;code&gt;convert&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, how do we pass this to C?  A function pointer in C is essentially
just a pointer to the memory location of the machine code implementing
that function, whereas a function value &lt;code&gt;mycompare&lt;/code&gt; (of type
&lt;code&gt;Function&lt;/code&gt;) in Julia is quite different.  Thanks to Julia&amp;rsquo;s &lt;a href="http://en.wikipedia.org/wiki/Just-in-time_compilation"&gt;JIT
compilation&lt;/a&gt;
approach,a Julia function may not even be &lt;em&gt;compiled&lt;/em&gt; until the first
time it is called, and in general the &lt;em&gt;same&lt;/em&gt; Julia function may be
compiled into &lt;em&gt;multiple&lt;/em&gt; machine-code instantiations, which are
specialized for arguments of different types (e.g. different &lt;code&gt;T&lt;/code&gt; in
this case).  So, you can imagine that &lt;code&gt;mycompare&lt;/code&gt; must internally
point to a rather complicated data structure (a &lt;code&gt;jl_function_t&lt;/code&gt; in
&lt;code&gt;julia.h&lt;/code&gt;, if you are interested), which holds information about the
argument types, the compiled versions (if any), and so on.  In
general, it must store a
&lt;a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29"&gt;closure&lt;/a&gt;
with information about the environment in which the function was
defined; we will talk more about this below.  In any case, it is a
very different object than a simple pointer to machine code for one
set of argument types.  Fortunately, we can get the latter simply by
calling a &lt;a href="docs.julialang.org/en/latest/stdlib/base.html#c-interface"&gt;built-in Julia function&lt;/a&gt; called &lt;code&gt;cfunction&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;const mycompare_c = cfunction(mycompare, Cint, (Ptr{Cdouble}, Ptr{Cdouble}))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here, we pass &lt;code&gt;cfunction&lt;/code&gt; three arguments: the function &lt;code&gt;mycompare&lt;/code&gt;,
the return type &lt;code&gt;Cint&lt;/code&gt;, and a tuple of the argument types, in this case to
sort an array of &lt;code&gt;Cdouble&lt;/code&gt; (&lt;code&gt;Float64&lt;/code&gt;) elements.  Julia compiles a version of
&lt;code&gt;mycompare&lt;/code&gt; specialized for these argument types (if it has not done
so already), and returns a &lt;code&gt;Ptr{Void}&lt;/code&gt; holding the address of the
machine code, &lt;em&gt;exactly&lt;/em&gt; what we need to pass to &lt;code&gt;qsort&lt;/code&gt;.  We are
now ready to call &lt;code&gt;qsort&lt;/code&gt; on some sample data:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;A = [1.3, -2.7, 4.4, 3.1]
ccall(:qsort, Void, (Ptr{Cdouble}, Csize_t, Csize_t, Ptr{Void}),
      A, length(A), sizeof(eltype(A)), mycompare_c)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;After this executes, &lt;code&gt;A&lt;/code&gt; is changed to the sorted array &lt;code&gt;[ -2.7, 1.3,
3.1, 4.4]&lt;/code&gt;.  Note that Julia knows how to convert an array
&lt;code&gt;A::Vector{Cdouble}&lt;/code&gt; into a &lt;code&gt;Ptr{Cdouble}&lt;/code&gt;, how to compute the &lt;code&gt;sizeof&lt;/code&gt;
a type in bytes (identical to C&amp;rsquo;s &lt;code&gt;sizeof&lt;/code&gt; operator), and so on.  For fun,
try inserting a &lt;code&gt;println("mycompare($a,$b)")&lt;/code&gt; line into mycompare, which
will allow you to see the comparisons that &lt;code&gt;qsort&lt;/code&gt; is performing (and to verify
that it is really calling the Julia function that you passed to it).&lt;/p&gt;

&lt;h2 id="The+problem+with+closures"&gt;The problem with closures&lt;/h2&gt;

&lt;p&gt;We aren&amp;rsquo;t done yet, however.  If you start passing callback functions to
C routines, it won&amp;rsquo;t be long before you discover that &lt;code&gt;cfunction&lt;/code&gt; doesn&amp;rsquo;t
always work.  For example, suppose we tried to declare our comparison
function inline, via:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mycomp = cfunction((a_,b_) -&amp;gt; unsafe_load(a_) &amp;lt; unsafe_load(b_) ? 
                              cint(-1) : cint(+1),
                   Cint, (Ptr{Cdouble}, Ptr{Cdouble}))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Julia barfs on this, printing &lt;code&gt;ERROR: function is not yet c-callable&lt;/code&gt;.  In
general, &lt;code&gt;cfunction&lt;/code&gt; only works for &amp;ldquo;top-level&amp;rdquo; functions: named
functions defined in the top-level (global or module) scope, but &lt;em&gt;not&lt;/em&gt;
anonymous (&lt;code&gt;args -&amp;gt; value&lt;/code&gt;) functions and not functions defined within
other functions (&amp;ldquo;nested&amp;rdquo; functions).  The reason for this stems from
one important concept in computer science: a
&lt;a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29"&gt;closure&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To understand the need for closures, and the difficulty they pose for
callback functions, suppose that we wanted to provide a nicer interface
for qsort, one which permitted the user to simply pass a &lt;code&gt;lessthan&lt;/code&gt; function
returning &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; while hiding all of the low-level business with
pointers, &lt;code&gt;Cint&lt;/code&gt;, and so on.  We might &lt;em&gt;like&lt;/em&gt; to do something of the form:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function qsort!{T}(A::Vector{T}, lessthan::Function)
    function mycompare(a_::Ptr{T}, b_::Ptr{T})
        a = unsafe_load(a_)
        b = unsafe_load(b_)
        return lessthan(a, b) ? cint(-1) : cint(+1)
    end
    mycompare_c = cfunction(mycompare, Cint, (Ptr{T}, Ptr{T}))
    ccall(:qsort, Void, (Ptr{T}, Csize_t, Csize_t, Ptr{Void}),
          A, length(A), sizeof(T), mycompare_c)
    A
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then we could simply call &lt;code&gt;qsort!([1.3, -2.7, 4.4, 3.1], &amp;lt;)&lt;/code&gt; to sort
in ascending order using the built-in &lt;code&gt;&amp;lt;&lt;/code&gt; comparison, or any other
comparison function we wanted.  Unfortunately &lt;code&gt;cfunction&lt;/code&gt; will again
barf when you try to call &lt;code&gt;qsort!&lt;/code&gt;, and it is no longer so difficult
to understand why.  Notice that the nested &lt;code&gt;mycompare&lt;/code&gt; function is no
longer self-contained: it uses the variable &lt;code&gt;lessthan&lt;/code&gt; from the
surrounding scope.  This is a common pattern for nested functions and
anonymous functions: often, they are parameterized by local variables
in the environment where the function is defined.  Technically, the
ability to have this kind of dependency is provided by &lt;a href="http://en.wikipedia.org/wiki/Scope_%28computer_science%29"&gt;lexical
scoping&lt;/a&gt; in
a programming language like Julia, and is typical of any language in
which functions are
&amp;ldquo;&lt;a href="http://en.wikipedia.org/wiki/First-class_function"&gt;first-class&lt;/a&gt;&amp;rdquo;
objects.  In order to support lexical scoping, a Julia &lt;code&gt;Function&lt;/code&gt;
object needs to internally carry around a pointer to the variables in
the enclosing environment, and this encapsulation is called a
&lt;em&gt;closure&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In contrast, a C function pointer is &lt;em&gt;not&lt;/em&gt; a closure.  It doesn&amp;rsquo;t
enclose a pointer to the environment in which the function was
defined, or anything else for that matter; it is just the address of a
stream of instructions.  This makes it hard, in C, to write functions
to transform other functions (&lt;a href="http://en.wikipedia.org/wiki/Higher-order_function"&gt;higher-order
functions&lt;/a&gt;) or to
parameterize functions by local variables.  This apparently leaves us
with two options, neither of which is especially attractive:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We could store &lt;code&gt;lessthan&lt;/code&gt; in a global variable, and reference that
from a top-level &lt;code&gt;mycompare&lt;/code&gt; function.  (This is the traditional solution
for C programmers calling &lt;code&gt;qsort&lt;/code&gt; with parameterized comparison functions.)
The problem with this strategy is that it is not &lt;a href="http://en.wikipedia.org/wiki/Reentrancy_%28computing%29"&gt;re-entrant&lt;/a&gt;: it prevents us from calling &lt;code&gt;qsort!&lt;/code&gt;
recursively (e.g. if the comparison function itself needs to do a sort, for
some complicated datastructure), or from calling &lt;code&gt;qsort!&lt;/code&gt; from multiple
threads (when a future Julia version supports shared-memory parallelism).
Still, this is better than nothing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every time &lt;code&gt;qsort!&lt;/code&gt; is called, Julia could JIT-compile a new version
of &lt;code&gt;mycompare&lt;/code&gt;, which hard-codes the reference to the &lt;code&gt;lessthan&lt;/code&gt;
argument passed on that call.  This is technically possible and has
been implemented in some languages (e.g. reportedly &lt;a href="http://www.gnu.org/software/guile/manual/html_node/Dynamic-FFI.html"&gt;GNU
Guile&lt;/a&gt;
and &lt;a href="http://luajit.org/ext_ffi_semantics.html"&gt;Lua&lt;/a&gt;
do something like this).  However, this strategy
comes at a price: it requires that callbacks be recompiled every time
a parameter in them changes, which is not true of the global-variable
strategy.  Anyway, it is not implemented yet in Julia.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Fortunately, there is often a &lt;em&gt;third&lt;/em&gt; option, because C programmers
long ago recognized these limitations of function pointers, and
devised a workaround: most modern C callback interfaces allow
arbitrary data to be passed through to the callback via a
&amp;ldquo;pass-through&amp;rdquo; (or &amp;ldquo;thunk&amp;rdquo;) pointer parameter.  As explained in the
next section, we can exploit this technique in Julia to pass a &amp;ldquo;true&amp;rdquo;
closure as a callback.&lt;/p&gt;

&lt;h2 id="Passing+closures+via+pass-through+pointers"&gt;Passing closures via pass-through pointers&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;qsort&lt;/code&gt; interface is nowadays considered rather antiquated.  Years
ago, it was supplemented on BSD-Unix systems, and eventually in GNU
libc, by a function called &lt;code&gt;qsort_r&lt;/code&gt; that solves the problem of passing
parameters to the callback in a re-entrant way.  This is how the BSD (e.g. MacOS)
&lt;code&gt;qsort_r&lt;/code&gt; function &lt;a href="https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/qsort_r.3.html"&gt;is defined&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;void qsort_r(void *base, size_t nmemb, size_t size, void *thunk,
             int (*compare)(void *thunk, const void *a, const void *b));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Compared to &lt;code&gt;qsort&lt;/code&gt;, there is an extra &lt;code&gt;thunk&lt;/code&gt; parameter, and this is
&lt;em&gt;passed through&lt;/em&gt; to the &lt;code&gt;compare&lt;/code&gt; function as its first argument.  In this
way, you can pass a pointer to &lt;em&gt;arbitrary&lt;/em&gt; data through to your callback,
and we can exploit this to pass a closure through for an arbitrary Julia
callback.&lt;/p&gt;

&lt;p&gt;All we need is a way to convert a Julia &lt;code&gt;Function&lt;/code&gt; into an opaque
&lt;code&gt;Ptr{Void}&lt;/code&gt; so that we can pass it through to our callback, and then a
way to convert the opaque pointer back into a &lt;code&gt;Function&lt;/code&gt;.  The former
is automatic if we simply declare the &lt;code&gt;ccall&lt;/code&gt; argument as type &lt;code&gt;Any&lt;/code&gt;
(which passes the argument as an opaque Julia object pointer), and the
latter is accomplished by the built-in function
&lt;code&gt;unsafe_pointer_to_objref&lt;/code&gt;.  (Technically, we could use type
&lt;code&gt;Function&lt;/code&gt; or an explicit call to &lt;code&gt;pointer_from_objref&lt;/code&gt; instead of
&lt;code&gt;Any&lt;/code&gt;.)  Using these, we can now define a working high-level &lt;code&gt;qsort!&lt;/code&gt;
function that takes an arbitrary &lt;code&gt;lessthan&lt;/code&gt; comparison-function
argument:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function qsort!_compare{T}(lessthan_::Ptr{Void}, a_::Ptr{T}, b_::Ptr{T})
    a = unsafe_load(a_)
    b = unsafe_load(b_)
    lessthan = unsafe_pointer_to_objref(lessthan_)::Function
    return lessthan(a, b) ? cint(-1) : cint(+1)
end

function qsort!{T}(A::Vector{T}, lessthan::Function=&amp;lt;)
    compare_c = cfunction(qsort!_compare, Cint, (Ptr{Void}, Ptr{T}, Ptr{T}))
    ccall(:qsort_r, Void, (Ptr{T}, Csize_t, Csize_t, Any, Ptr{Void}),
          A, length(A), sizeof(T), lessthan, compare_c)
    return A
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;qsort!_compare&lt;/code&gt; is a top-level function, so &lt;code&gt;cfunction&lt;/code&gt; has no
problem with it, and it will only be compiled once per type &lt;code&gt;T&lt;/code&gt; to be
sorted (rather than once per call to &lt;code&gt;qsort!&lt;/code&gt; or per &lt;code&gt;lessthan&lt;/code&gt;
function).  We use the explicit &lt;code&gt;::Function&lt;/code&gt; assertion to tell
the compiler that we will only pass &lt;code&gt;Function&lt;/code&gt; pointers in
&lt;code&gt;lessthan_&lt;/code&gt;. Note that we gave the &lt;code&gt;lessthan&lt;/code&gt; argument a default value
of &lt;code&gt;&amp;lt;&lt;/code&gt; (default arguments being a &lt;a href="https://github.com/JuliaLang/julia/issues/1817"&gt;recent
feature&lt;/a&gt; added to
Julia).&lt;/p&gt;

&lt;p&gt;We can now do &lt;code&gt;qsort!([1.3, -2.7, 4.4, 3.1])&lt;/code&gt; and it will
return the array sorted in ascending order, or &lt;code&gt;qsort!([1.3, -2.7,
4.4, 3.1], &amp;gt;)&lt;/code&gt; to sort in descending order.&lt;/p&gt;

&lt;h4 id="Warning:+`qsort_r`+is+not+portable"&gt;Warning: &lt;code&gt;qsort_r&lt;/code&gt; is not portable&lt;/h4&gt;

&lt;p&gt;The example above has one major problem that has nothing to do with
Julia: the &lt;code&gt;qsort_r&lt;/code&gt; function is not portable.  The above example
won&amp;rsquo;t work on Windows, since the Windows C library doesn&amp;rsquo;t define
&lt;code&gt;qsort_r&lt;/code&gt; (instead, it has a function called
&lt;a href="http://msdn.microsoft.com/en-us/library/4xc60xas%28VS.80%29.aspx"&gt;qsort_s&lt;/a&gt;,
which of course uses an argument order incompatible with &lt;em&gt;both&lt;/em&gt; the
BSD and GNU &lt;code&gt;qsort_r&lt;/code&gt; functions).  Worse, it will crash on GNU/Linux
systems, which &lt;em&gt;do&lt;/em&gt; provide &lt;code&gt;qsort_r&lt;/code&gt; but with an
&lt;a href="http://www.memoryhole.net/kyle/2009/11/qsort_r.html"&gt;incompatible&lt;/a&gt;
&lt;a href="http://www.cygwin.com/ml/libc-alpha/2008-12/msg00008.html"&gt;calling
convention&lt;/a&gt;. And
as a result it is difficult to use &lt;code&gt;qsort_r&lt;/code&gt; in a way that does not
crash either on GNU/Linux or BSD (e.g. MacOS) systems.  This is how
glibc&amp;rsquo;s &lt;code&gt;qsort_r&lt;/code&gt; is defined:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;void qsort_r(void *base, size_t nmemb, size_t size,
             int (*compare)(const void *a, const void *b, void *thunk),
              void *thunk);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that the position of the &lt;code&gt;thunk&lt;/code&gt; argument is moved, both in &lt;code&gt;qsort_r&lt;/code&gt; itself and
in the comparison function.   So, the corresponding &lt;code&gt;qsort!&lt;/code&gt; Julia code on
GNU/Linux systems should be:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function qsort!_compare{T}(a_::Ptr{T}, b_::Ptr{T}, lessthan_::Ptr{Void})
    a = unsafe_load(a_)
    b = unsafe_load(b_)
    lessthan = unsafe_pointer_to_objref(lessthan_)::Function
    return lessthan(a, b) ? cint(-1) : cint(+1)
end

function qsort!{T}(A::Vector{T}, lessthan::Function=&amp;lt;)
    compare_c = cfunction(qsort!_compare, Cint, (Ptr{T}, Ptr{T}, Ptr{Void}))
    ccall(:qsort_r, Void, (Ptr{T}, Csize_t, Csize_t, Ptr{Void}, Any),
          A, length(A), sizeof(T), compare_c, lessthan)
    return A
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you really needed to call &lt;code&gt;qsort_r&lt;/code&gt; from Julia, you could use the
above definitions if &lt;code&gt;OS_NAME == :Linux&lt;/code&gt; and the BSD definitions
otherwise, with a third version using &lt;code&gt;qsort_s&lt;/code&gt; on Windows, but
fortunately there is not much need as Julia comes with its own
perfectly adequate &lt;code&gt;sort&lt;/code&gt; and &lt;code&gt;sort!&lt;/code&gt; routines.&lt;/p&gt;

&lt;h2 id="Passing+closures+in+data+structures"&gt;Passing closures in data structures&lt;/h2&gt;

&lt;p&gt;As another example that is oriented more towards numerical
computations, we&amp;rsquo;ll examine how we might call the numerical integration
routines in the &lt;a href="http://www.gnu.org/software/gsl/"&gt;GNU Scientific Library
(GSL)&lt;/a&gt;.  There is already a &lt;a href="https://github.com/jiahao/GSL.jl"&gt;GSL
package&lt;/a&gt; that handles the wrapper
work below for you, but it is instructive to look at how this is
implemented because GSL simulates closures in a slightly different
way, with data structures.&lt;/p&gt;

&lt;p&gt;Like most modern C libraries accepting callbacks, GSL uses a &lt;code&gt;void*&lt;/code&gt; pass-through
parameter to allow arbitrary data to be passed through to the callback routine,
and we can use that to support arbitrary closures in Julia.   Unlike &lt;code&gt;qsort_r&lt;/code&gt;,
however, GSL wraps both the C function pointer and the pass-through pointer in
a data structure called &lt;code&gt;gsl_function&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;struct {
    double (*function)(double x, void *params);
    void *params;
} gsl_function;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Using the techniques above, we can easily declare a &lt;code&gt;GSL_Function&lt;/code&gt; type in Julia
that mirrors this C type, and with a constructor &lt;code&gt;GSL_Function(f::Function)&lt;/code&gt; that
creates a wrapper around an arbitrary Julia function &lt;code&gt;f&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function gsl_function_wrap(x::Cdouble, params::Ptr{Void})
    f = unsafe_pointer_to_objref(params)::Function
    convert(Cdouble, f(x))::Cdouble
end
const gsl_function_wrap_c = cfunction(gsl_function_wrap,
                                      Cdouble, (Cdouble, Ptr{Void}))

type GSL_Function
    func::Ptr{Void}
    params::Any
    GSL_Function(f::Function) = new(gsl_function_wrap_c, f)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One subtlety with the above code is that we need to explicitly
&lt;code&gt;convert&lt;/code&gt; the return value of &lt;code&gt;f&lt;/code&gt; to a &lt;code&gt;Cdouble&lt;/code&gt; (in case the caller&amp;rsquo;s
code returns some other numeric type for some &lt;code&gt;x&lt;/code&gt;, such as an &lt;code&gt;Int&lt;/code&gt;).
Moreover, we need to explicitly assert (&lt;code&gt;::Cdouble&lt;/code&gt;) that the result
of the &lt;code&gt;convert&lt;/code&gt; was a &lt;code&gt;Cdouble&lt;/code&gt;.  As with the &lt;code&gt;qsort&lt;/code&gt; example, this
is because &lt;code&gt;cfunction&lt;/code&gt; only works if Julia can guarantee that
&lt;code&gt;gsl_function_wrap&lt;/code&gt; returns the specified &lt;code&gt;Cdouble&lt;/code&gt; type, and
Julia cannot infer the return type of &lt;code&gt;convert&lt;/code&gt; since it does not
know the return type of &lt;code&gt;f(x)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Given the above definitions, it is a simple matter to pass this to the
&lt;a href="http://www.gnu.org/software/gsl/manual/html_node/QAG-adaptive-integration.html"&gt;GSL
adaptive-integration&lt;/a&gt;
routines in a wrapper function &lt;code&gt;gsl_integration_qag&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function gsl_integration_qag(f::Function, a::Real, b::Real, epsrel::Real=1e-12,
                             maxintervals::Integer=10^7)
    s = ccall((:gsl_integration_workspace_alloc,:libgsl), Ptr{Void}, (Csize_t,),
              maxintervals)
    result = Array(Cdouble,1)
    abserr = Array(Cdouble,1)
    ccall((:gsl_integration_qag,:libgsl), Cint,
          (Ptr{GSL_Function}, Cdouble,Cdouble, Cdouble, Csize_t, Cint, Ptr{Void}, 
           Ptr{Cdouble}, Ptr{Cdouble}),
          &amp;amp;GSL_Function(f), a, b, epsrel, maxintervals, 1, s, result, abserr)
    ccall((:gsl_integration_workspace_free,:libgsl), Void, (Ptr{Void},), s)
    return (result[1], abserr[1])
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that &lt;code&gt;&amp;amp;GSL_Function(f)&lt;/code&gt; passes a pointer to a &lt;code&gt;GSL_Function&lt;/code&gt;
&amp;ldquo;struct&amp;rdquo; containing a pointer to &lt;code&gt;gsl_function_wrap_c&lt;/code&gt; and &lt;code&gt;f&lt;/code&gt;, corresponding
to the &lt;code&gt;gsl_function*&lt;/code&gt; argument in C.  The return value is a tuple of the estimated
integral and an estimated error.&lt;/p&gt;

&lt;p&gt;For example, &lt;code&gt;gsl_integration_qag(cos, 0, 1)&lt;/code&gt; returns
&lt;code&gt;(0.8414709848078965,9.34220461887732e-15)&lt;/code&gt;, which computes the
correct integral &lt;code&gt;sin(1)&lt;/code&gt; to machine precision.&lt;/p&gt;

&lt;h2 id="Taking+out+the+trash+(or+not)"&gt;Taking out the trash (or not)&lt;/h2&gt;

&lt;p&gt;In the above examples, we pass an opaque pointer (object reference) to a
Julia &lt;code&gt;Function&lt;/code&gt; into C.  Whenever one passes pointers to Julia data into C
code, one has to ensure that the Julia data is not garbage-collected until
the C code is done with it, and functions are no exception to this rule.
An anonymous function that is no longer referred to by any Julia variable
may be garbage collected, at which point any C pointers to it become invalid.&lt;/p&gt;

&lt;p&gt;This sounds scary, but in practice you don&amp;rsquo;t need to worry about it very often,
because Julia guarantees that &lt;code&gt;ccall&lt;/code&gt; arguments won&amp;rsquo;t be garbage-collected until
the &lt;code&gt;ccall&lt;/code&gt; exits.  So, in all of the above examples, we are safe: the &lt;code&gt;Function&lt;/code&gt;
only needs to live as long as the &lt;code&gt;ccall&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The only danger arises when you pass a function pointer to C and the C code
&lt;em&gt;saves the pointer&lt;/em&gt; in some data structure which it will use in a &lt;em&gt;later&lt;/em&gt; &lt;code&gt;ccall&lt;/code&gt;.
In that case, you are responsible for ensuring that the &lt;code&gt;Function&lt;/code&gt; variable lives
(is referred to by some Julia variable) as long as the C code might need it.&lt;/p&gt;

&lt;p&gt;For example, in the GSL &lt;a href="http://www.gnu.org/software/gsl/manual/html_node/One-dimensional-Minimization.html"&gt;one-dimensional minimization
interface&lt;/a&gt;,
you don&amp;rsquo;t simply pass your objective function to a minimization
routine and wait until it is minimized.  Instead, you call a GSL
routine to create a &amp;ldquo;minimizer object&amp;rdquo;, store your function pointer in this
object, call routines to iterate the minimization, and then deallocate the
minimizer when you are done.  The Julia function must not be garbage-collected
until this process is complete.  The easiest way to ensure this is to create
a Julia wrapper type around the minimizer object that stores an &lt;em&gt;explicit&lt;/em&gt;
reference to the Julia function, like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type GSL_Minimizer
    m::Ptr{Void} # the gsl_min_fminimizer pointer
    f::Any  # explicit reference to objective, to prevent garbage-collection
    function GSL_Minimizer(t)
       m = ccall((:gsl_min_fminimizer_alloc,:libgsl), Ptr{Void}, (Ptr{Void},), t)
       p = new(m, nothing)
       finalizer(p, p -&amp;gt; ccall((:gsl_min_fminimizer_free,:libgsl),
                               Void, (Ptr{Void},), p.m))
       p
    end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This wraps around a &lt;code&gt;gsl_min_fminimizer&lt;/code&gt; object of type &lt;code&gt;t&lt;/code&gt;, with a
placeholder &lt;code&gt;f&lt;/code&gt; to store a reference to the objective function (once
it is set below), including a &lt;code&gt;finalizer&lt;/code&gt; to deallocate the GSL object
when the &lt;code&gt;GSL_Minimizer&lt;/code&gt; is garbage-collected.  The parameter &lt;code&gt;t&lt;/code&gt; is
used to specify the minimization algorithm, which could default to
Brent&amp;rsquo;s algorithm via:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;const gsl_brent = unsafe_load(cglobal((:gsl_min_fminimizer_brent,:libgsl), Ptr{Void}))
GSL_Minimizer() = GSL_Minimizer(gsl_brent)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(The call to &lt;code&gt;cglobal&lt;/code&gt; yields a pointer to the
&lt;code&gt;gsl_min_fminimizer_brent&lt;/code&gt; global variable in GSL, which we then
dereference to get the &lt;em&gt;actual&lt;/em&gt; pointer via &lt;code&gt;unsafe_load&lt;/code&gt;.)&lt;/p&gt;

&lt;p&gt;Then, when we set the function to minimize (the &amp;ldquo;objective&amp;rdquo;), we store
an extra reference to it in the &lt;code&gt;GSL_Minimizer&lt;/code&gt; to prevent
garbage-collection for the lifetime of the &lt;code&gt;GSL_Minimizer&lt;/code&gt;, again
using the &lt;code&gt;GSL_Function&lt;/code&gt; type defined above to wrap the callback:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function gsl_minimizer_set!(m::GSL_Minimizer, f, x0, xmin, xmax)
    ccall((:gsl_min_fminimizer_set,:libgsl), Cint,
          (Ptr{Void}, Ptr{GSL_Function}, Cdouble, Cdouble, Cdouble),
          m.m, &amp;amp;GSL_Function(f), x0, xmin, xmax)
    m.f = f
    m
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are then various GSL routines to iterate the minimizer and to check the
current &lt;code&gt;x&lt;/code&gt;, objective value, or bounds on the minimum, which are convenient to wrap:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gsl_minimizer_iterate!(m::GSL_Minimizer) =
    ccall((:gsl_min_fminimizer_iterate,:libgsl), Cint, (Ptr{Void},), m.m)

gsl_minimizer_x(m::GSL_Minimizer) =
    ccall((:gsl_min_fminimizer_x_minimum,:libgsl), Cdouble, (Ptr{Void},), m.m)

gsl_minimizer_f(m::GSL_Minimizer) =
    ccall((:gsl_min_fminimizer_f_minimum,:libgsl), Cdouble, (Ptr{Void},), m.m)

gsl_minimizer_xmin(m::GSL_Minimizer) =
    ccall((:gsl_min_fminimizer_x_lower,:libgsl), Cdouble, (Ptr{Void},), m.m)
gsl_minimizer_xmax(m::GSL_Minimizer) =
    ccall((:gsl_min_fminimizer_x_upper,:libgsl), Cdouble, (Ptr{Void},), m.m)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Putting all of these together, we can minimize a simple function &lt;code&gt;sin(x)&lt;/code&gt; in
the interval [-3,1], with a starting guess -1, via:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;m = GSL_Minimizer()
gsl_minimizer_set!(m, sin, -1, -3, 1)
while gsl_minimizer_xmax(m) - gsl_minimizer_xmin(m) &amp;gt; 1e-6
    println("iterating at x = $(gsl_minimizer_x(m))")
    gsl_minimizer_iterate!(m)
end
println("found minimum $(gsl_minimizer_f(m)) at x = $(gsl_minimizer_x(m))")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;After a few iterations, it prints &lt;code&gt;found minimum -1.0 at x =
-1.5707963269964016&lt;/code&gt;, which is the correct minimum (&amp;minus;&amp;pi;/2) to
about 10 digits.&lt;/p&gt;

&lt;p&gt;At this point, I will shamelessly plug my own &lt;a href="https://github.com/stevengj/NLopt.jl"&gt;NLopt
package&lt;/a&gt; for Julia, which wraps
around my free/open-source &lt;a href="http://ab-initio.mit.edu/nlopt"&gt;NLopt&lt;/a&gt; library
to provide many more optimization algorithms than GSL, with perhaps a nicer
interface.   However, the techniques used to pass callback functions to
NLopt are actually quite similar to those used for GSL.&lt;/p&gt;

&lt;p&gt;An even more complicated version of these techniques can be found in
the &lt;a href="https://github.com/stevengj/PyCall.jl"&gt;PyCall package&lt;/a&gt; to call
Python from Julia.  In order to pass a Julia function to Python, we
again use &lt;code&gt;cfunction&lt;/code&gt; on a wrapper function that handles the type
conversions and so on, and pass the actual Julia closure through via a
pass-through pointer.  But in that case, the pass-through pointer
consists of a Python object that has been created with a new type that
allows it to wrap a Julia object, and garbage-collection is deferred
by storing the Julia object in a global dictionary of saved objects
(removing it via the Python destructor of the new type).  That is all
somewhat tricky stuff and beyond the scope of this blog post; I only
mention it to illustrate the fact that it is possible to implement
quite complex inter-language calling behaviors purely in Julia by
building on the above techniques.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/JuliaLang/~4/37Rn8aFhrvY" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://julialang.org/blog/2013/05/callback</feedburner:origLink></entry>
 
 <entry>
   <title>Put This In Your Pipe</title>
   <link href="http://feedproxy.google.com/~r/JuliaLang/~3/VsxSnYTyTTM/put-this-in-your-pipe" />
   <updated>2013-04-08T00:00:00-07:00</updated>
   <id>http://julialang.org/blog/2013/04/put-this-in-your-pipe</id>
   <content type="html">&lt;p&gt;In a &lt;a href="/blog/2012/03/shelling-out-sucks/"&gt;previous post&lt;/a&gt;, I talked about why &amp;ldquo;shelling out&amp;rdquo; to spawn a pipeline of external programs via an intermediate shell is a common cause of bugs, security holes, unnecessary overhead, and silent failures.
But it&amp;rsquo;s so convenient!
Why can&amp;rsquo;t running pipelines of external programs be convenient &lt;em&gt;and&lt;/em&gt; safe?
Well, there&amp;rsquo;s no real reason, actually.
The shell itself manages to construct and execute pipelines quite well.
In principle, there&amp;rsquo;s nothing stopping high-level languages from doing it at least as well as shells do – the common ones just don&amp;rsquo;t by default, instead requiring users to make the extra effort to use external programs safely and correctly.
There are two major impediments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some moderately tricky low-level UNIX plumbing using the &lt;a href="https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/pipe.2.html"&gt;&lt;code&gt;pipe&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/dup2.2.html"&gt;&lt;code&gt;dup2&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/fork.2.html"&gt;&lt;code&gt;fork&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/close.2.html"&gt;&lt;code&gt;close&lt;/code&gt;&lt;/a&gt;, and &lt;a href="https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/execve.2.html"&gt;&lt;code&gt;exec&lt;/code&gt;&lt;/a&gt; system calls;&lt;/li&gt;
&lt;li&gt;The UX problem of designing an easy, flexible programming interface for commands and pipelines.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;This post describes the system we designed and implemented for Julia, and how it avoids the major flaws of shelling out in other languages.
First, I&amp;rsquo;ll present the Julia version of the previous post&amp;rsquo;s example – counting the number of lines in a given directory containing the string &amp;ldquo;foo&amp;rdquo;.
The fact that Julia provides complete, specific diagnostic error messages when pipelines fail turns out to reveal a surprising and subtle bug, lurking in what appears to be a perfectly innocuous UNIX pipeline.
After fixing this bug, we go into details of how Julia&amp;rsquo;s external command execution and pipeline construction system actually works, and why it provides greater flexibility and safety than the traditional approach of using an intermediate shell to do all the heavy lifting.&lt;/p&gt;

&lt;h2 id="Simple+Pipeline,+Subtle+Bug"&gt;Simple Pipeline, Subtle Bug&lt;/h2&gt;

&lt;p&gt;Here&amp;rsquo;s how you write the example of counting the number of lines in a directory containing the string &amp;ldquo;foo&amp;rdquo; in Julia
(you can follow along at home if you have Julia installed from source by changing directories into the Julia source directory and doing &lt;code&gt;cp -a src "source code"; mkdir tmp&lt;/code&gt; and then firing up the Julia repl):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; dir = "src";

julia&amp;gt; int(readchomp(`find $dir -type f -print0` | `xargs -0 grep foo` | `wc -l`))
5
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This Julia command looks suspiciously similar to the naïve Ruby version we started with in the previous post:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;`find #{dir} -type f -print0 | xargs -0 grep foo | wc -l`.to_i
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;However, it isn&amp;rsquo;t susceptible to the same problems:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; dir = "source code";

julia&amp;gt; int(readchomp(`find $dir -type f -print0` | `xargs -0 grep foo` | `wc -l`))
5

julia&amp;gt; dir = "nonexistent";

julia&amp;gt; int(readchomp(`find $dir -type f -print0` | `xargs -0 grep foo` | `wc -l`))
find: `nonexistent': No such file or directory
ERROR: failed processes:
  Process(`find nonexistent -type f -print0`, ProcessExited(1)) [1]
  Process(`xargs -0 grep foo`, ProcessExited(123)) [123]
 in pipeline_error at process.jl:412
 in readall at process.jl:365
 in readchomp at io.jl:172

julia&amp;gt; dir = "foo'; echo MALICIOUS ATTACK; echo '";

julia&amp;gt; int(readchomp(`find $dir -type f -print0` | `xargs -0 grep foo` | `wc -l`))
find: `foo\'; echo MALICIOUS ATTACK; echo \'': No such file or directory
ERROR: failed processes:
  Process(`find "foo'; echo MALICIOUS ATTACK; echo '" -type f -print0`, ProcessExited(1)) [1]
  Process(`xargs -0 grep foo`, ProcessExited(123)) [123]
 in pipeline_error at process.jl:412
 in readall at process.jl:365
 in readchomp at io.jl:172
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The default, simplest-to-achieve behavior in Julia is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;not susceptible to any kind of metacharacter breakage,&lt;/li&gt;
&lt;li&gt;reliably detects all subprocess failures,&lt;/li&gt;
&lt;li&gt;automatically raises an exception if any subprocess fails,&lt;/li&gt;
&lt;li&gt;prints error messages including exactly which commands failed.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;In the above examples, we can see that even when &lt;code&gt;dir&lt;/code&gt; contains spaces or quotes, the expression still behaves exactly as intended – the value of &lt;code&gt;dir&lt;/code&gt; is interpolated as a single argument to the &lt;code&gt;find&lt;/code&gt; command.
When &lt;code&gt;dir&lt;/code&gt; is not the name of a directory that exists, &lt;code&gt;find&lt;/code&gt; fails – as it should – and this failure is detected and automatically converted into an informative exception, including the fully expanded command-lines that failed.&lt;/p&gt;

&lt;p&gt;In the previous post, we observed that using the &lt;code&gt;pipefail&lt;/code&gt; option for Bash allows detection of pipeline failures, like this one, occurring before the last process in the pipeline.
However, it only allows us to detect that at least one thing in the pipeline failed.
We still have to guess at what parts of the pipeline actually failed.
In the Julia example, on the other hand, there is no guessing required:
when a non-existent directory is given, we can see that both &lt;code&gt;find&lt;/code&gt; and &lt;code&gt;xargs&lt;/code&gt; fail.
While it is unsurprising that &lt;code&gt;find&lt;/code&gt; fails in this case, it is unexpected that &lt;code&gt;xargs&lt;/code&gt; also fails.
Why &lt;em&gt;does&lt;/em&gt; &lt;code&gt;xargs&lt;/code&gt; fail?&lt;/p&gt;

&lt;p&gt;One possibility to check for is that the &lt;code&gt;xargs&lt;/code&gt; program fails with no input.
We can use Julia&amp;rsquo;s &lt;code&gt;success&lt;/code&gt; predicate to try it out:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; success(`cat /dev/null` | `xargs true`)
true
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ok, so &lt;code&gt;xargs&lt;/code&gt; seems perfectly happy with no input.
Maybe grep doesn&amp;rsquo;t like not getting any input?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; success(`cat /dev/null` | `grep foo`)
false
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Aha! &lt;code&gt;grep&lt;/code&gt; returns a non-zero status when it doesn&amp;rsquo;t get any input.
Good to know.
It turns out that &lt;code&gt;grep&lt;/code&gt; indicates whether it matched anything or not with its return status.
Most programs use their return status to indicate success or failure, but some, like &lt;code&gt;grep&lt;/code&gt;, use it to indicate some other boolean condition – in this case &amp;ldquo;found something&amp;rdquo; versus &amp;ldquo;didn&amp;rsquo;t find anything&amp;rdquo;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; success(`echo foo` | `grep foo`)
true

julia&amp;gt; success(`echo bar` | `grep foo`)
false
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we know why &lt;code&gt;grep&lt;/code&gt; is &amp;ldquo;failing&amp;rdquo; – and &lt;code&gt;xargs&lt;/code&gt; too, since it returns a non-zero status if the program it runs returns non-zero.
This means that our Julia pipeline and the &amp;ldquo;responsible&amp;rdquo; Ruby version are both susceptible to bogus failures when we search an existing directory that happens not to contain the string &amp;ldquo;foo&amp;rdquo; anywhere:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; dir = "tmp";

julia&amp;gt; int(readchomp(`find $dir -type f -print0` | `xargs -0 grep foo` | `wc -l`))
ERROR: failed process: Process(`xargs -0 grep foo`, ProcessExited(123)) [123]
 in error at error.jl:22
 in pipeline_error at process.jl:394
 in pipeline_error at process.jl:407
 in readall at process.jl:365
 in readchomp at io.jl:172
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Since &lt;code&gt;grep&lt;/code&gt; indicates not finding anything using a non-zero return status, the &lt;code&gt;readall&lt;/code&gt; function concludes that its pipeline failed and raises an error to that effect.
In this case, this default behavior is undesirable:
we want the expression to just return &lt;code&gt;0&lt;/code&gt; without raising an error.
The simple fix in Julia is this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; dir = "tmp";

julia&amp;gt; int(readchomp(`find $dir -type f -print0` | ignorestatus(`xargs -0 grep foo`) | `wc -l`))
0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This works correctly in all cases.
Next I&amp;rsquo;ll explain &lt;em&gt;how&lt;/em&gt; all of this works, but for now it&amp;rsquo;s enough to note that the detailed error message provided when our pipeline failed exposed a rather subtle bug that would eventually cause subtle and hard-to-debug problems when used in production.
Without such detailed error reporting, this bug would be pretty difficult to track down.&lt;/p&gt;

&lt;h2 id="Do-Nothing+Backticks"&gt;Do-Nothing Backticks&lt;/h2&gt;

&lt;p&gt;Julia borrows the backtick syntax for external commands form Perl and Ruby, both of which in turn got it from the shell.
Unlike in these predecessors, however, in Julia backticks don&amp;rsquo;t immediately run commands, nor do they necessarily indicate that you want to capture the output of the command.
Instead, backticks just construct an object representing a command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; `echo Hello`
`echo Hello`

julia&amp;gt; typeof(ans)
Cmd
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(In the Julia repl, &lt;code&gt;ans&lt;/code&gt; is automatically bound to the value of the last evaluated input.)
In order to actually run a command, you have to &lt;em&gt;do&lt;/em&gt; something with a command object.
To run a command and capture its output into a string – what other languages do with backticks automatically – you can apply the &lt;code&gt;readall&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; readall(`echo Hello`)
"Hello\n"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Since it&amp;rsquo;s very common to want to discard the trailing line break at the end of a command&amp;rsquo;s output, Julia provides the &lt;code&gt;readchomp(x)&lt;/code&gt; command which is equivalent to writing &lt;code&gt;chomp(readall(x))&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; readchomp(`echo Hello`)
"Hello"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To run a command without capturing its output, letting it just print to the same &lt;code&gt;stdout&lt;/code&gt; stream as the main process – i.e. what the &lt;code&gt;system&lt;/code&gt; function does when given a command as a string in other languages – use the &lt;code&gt;run&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; run(`echo Hello`)
Hello
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;"Hello\n"&lt;/code&gt; after the &lt;code&gt;readall&lt;/code&gt; command is a returned value, whereas the &lt;code&gt;Hello&lt;/code&gt; after the &lt;code&gt;run&lt;/code&gt; command is printed output.
(If your terminal supports color, these are colored differently so that you can easily distinguish them visually.)
Nothing is returned by the &lt;code&gt;run&lt;/code&gt; command, but if something goes wrong, an exception is raised:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; run(`false`)
ERROR: failed process: Process(`false`, ProcessExited(1)) [1]
 in error at error.jl:22
 in pipeline_error at process.jl:394
 in run at process.jl:384

julia&amp;gt; run(`notaprogram`)
execvp(): No such file or directory
ERROR: failed process: Process(`notaprogram`, ProcessExited(-1)) [-1]
 in error at error.jl:22
 in pipeline_error at process.jl:394
 in run at process.jl:384
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As with &lt;code&gt;xargs&lt;/code&gt; and &lt;code&gt;grep&lt;/code&gt; above, this may not always be desirable.
In such cases, you can use &lt;code&gt;ignorestatus&lt;/code&gt; to indicate that the command returning a non-zero value should not be considered an error:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; run(ignorestatus(`false`))
&lt;/code&gt;&lt;/pre&gt;

&lt;!--     julia&gt; run(ignorestatus(`notaprogram`))
    execvp(): No such file or directory
    ERROR: failed process: Process(`notaprogram`, ProcessExited(-1)) [-1]
     in error at error.jl:22
     in pipeline_error at process.jl:394
     in run at process.jl:384

In the latter case, an error is still raised in the parent process since the problem is that the executable doesn't even exist, rather than merely that it ran and returned a non-zero status.
 --&gt;


&lt;p&gt;Although Julia&amp;rsquo;s backtick syntax intentionally mimics the shell as closely as possible, there is an important distinction:
the command string is never passed to a shell to be interpreted and executed;
instead it is parsed in Julia code, using the same rules the shell uses to determine what the command and arguments are.
Command objects allow you to see what the program and arguments were determined to be by accessing the &lt;code&gt;.exec&lt;/code&gt; field:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; cmd = `perl -e 'print "Hello\n"'`
`perl -e 'print "Hello\n"'`

julia&amp;gt; cmd.exec
3-element Union(UTF8String,ASCIIString) Array:
 "perl"
 "-e"
 "print \"Hello\\n\""
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This field is a plain old array of strings that can be manipulated like any other Julia array.&lt;/p&gt;

&lt;h2 id="Constructing+Commands"&gt;Constructing Commands&lt;/h2&gt;

&lt;p&gt;The purpose of the backtick notation in Julia is to provide a familiar, shell-like syntax for making objects representing commands with arguments.
To that end, quotes and spaces work just as they do in the shell.
The real power of backtick syntax doesn&amp;rsquo;t emerge, however, until we begin constructing commands programmatically.
Just as in the shell (and in Julia strings), you can interpolate values into commands using the dollar sign (&lt;code&gt;$&lt;/code&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; dir = "src";

julia&amp;gt; `find $dir -type f`.exec
4-element Union(UTF8String,ASCIIString) Array:
 "find"
 "src"
 "-type"
 "f"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unlike in the shell, however, Julia values interpolated into commands are interpolated as a single verbatim argument – no characters inside the value are interpreted as special after the value has been interpolated:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; dir = "two words";

julia&amp;gt; `find $dir -type f`.exec
4-element Union(UTF8String,ASCIIString) Array:
 "find"
 "two words"
 "-type"
 "f"

julia&amp;gt; dir = "foo'bar";

julia&amp;gt; `find $dir -type f`.exec
4-element Union(UTF8String,ASCIIString) Array:
 "find"
 "foo'bar"
 "-type"
 "f"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This works no matter what the contents of the interpolated value is, allowing simple interpolation of characters that are quite difficult to pass as parts of command-line arguments even in the shell (for the following examples, &lt;code&gt;tmp/a.tsv&lt;/code&gt; and &lt;code&gt;tmp/b.tsv&lt;/code&gt; can be created in the shell with &lt;code&gt;echo -e "foo\tbar\nbaz\tqux" &amp;gt; tmp/a.tsv; echo -e "foo\t1\nbaz\t2" &amp;gt; tmp/b.tsv&lt;/code&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; tab = "\t";

julia&amp;gt; cmd = `join -t$tab tmp/a.tsv tmp/b.tsv`;

julia&amp;gt; cmd.exec
4-element Union(UTF8String,ASCIIString) Array:
 "join"
 "-t\t"
 "tmp/a.tsv"
 "tmp/b.tsv"

julia&amp;gt; run(cmd)
foo     bar     1
baz     qux     2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Moreover, what comes after the &lt;code&gt;$&lt;/code&gt; can actually be any valid Julia expression, not just a variable name:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; `join -t$"\t" tmp/a.tsv tmp/b.tsv`.exec
4-element Union(UTF8String,ASCIIString) Array:
 "join"
 "-t\t"
 "a.tsv"
 "b.tsv"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A tab character is somewhat harder to pass in the shell, requiring command interpolation and some tricky quoting:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;bash-3.2$ join -t"$(printf '\t')" tmp/a.tsv tmp/b.tsv
foo     bar     1
baz     qux     2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;While interpolating values with spaces and other strange characters is great for non-brittle construction of commands, there was a reason why the shell split values on spaces in the first place:
to allow interpolation of multiple arguments.
Most modern shells have first-class array types, but older shells used space-separation to simulate arrays.
Thus, if you interpolate a value like &amp;ldquo;foo bar&amp;rdquo; into a command in the shell, it&amp;rsquo;s treated as two separate words by default.
In languages with first-class array types, however, there&amp;rsquo;s a much better option:
consistently interpolate single values as single arguments and interpolate arrays as multiple values.
This is precisely what Julia&amp;rsquo;s backtick interpolation does:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; dirs = ["foo", "bar", "baz"];

julia&amp;gt; `find $dirs -type f`.exec
6-element Union(UTF8String,ASCIIString) Array:
 "find"
 "foo"
 "bar"
 "baz"
 "-type"
 "f"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And of course, no matter how strange the strings contained in an interpolated array are, they become verbatim arguments, without any shell interpretation.
Julia&amp;rsquo;s backticks have one more fancy trick up their sleeve.
We saw earlier (without really remarking on it) that you could interpolate single values into a larger argument:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; x = "bar";

julia&amp;gt; `echo foo$x`
`echo foobar`
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What happens if &lt;code&gt;x&lt;/code&gt; is an array?
Only one way to find out:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; x = ["bar", "baz"];

julia&amp;gt; `echo foo$x`
`echo foobar foobaz`
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Julia does what the shell would do if you wrote &lt;code&gt;echo foo{bar,baz}&lt;/code&gt;.
This even works correctly for multiple values interpolated into the same shell word:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia&amp;gt; dir = "/data"; names = ["foo","bar"]; exts=["csv","tsv"];

julia&amp;gt; `cat $dir/$names.$exts`
`cat /data/foo.csv /data/foo.tsv /data/bar.csv /data/bar.tsv`
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is the same Cartesian product expansion that the shell does if multiple &lt;code&gt;{...}&lt;/code&gt; expressions are used in the same word.&lt;/p&gt;

&lt;h2 id="Further+Reading"&gt;Further Reading&lt;/h2&gt;

&lt;p&gt;You can read more in Julia&amp;rsquo;s &lt;a href="http://docs.julialang.org/en/release-0.1/manual/running-external-programs/"&gt;online manual&lt;/a&gt;, including how to construct complex pipelines, and how shell-compatible quoting and interpolation rules in Julia&amp;rsquo;s backtick syntax make it both simple and safe to cut-and-paste shell commands into Julia code.
The whole system is designed on the principle that the easiest thing to do should also be the right thing.
The end result is that starting and interacting with external processes in Julia is both convenient and safe.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/JuliaLang/~4/VsxSnYTyTTM" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://julialang.org/blog/2013/04/put-this-in-your-pipe</feedburner:origLink></entry>
 
 <entry>
   <title>Distributed Numerical Optimization</title>
   <link href="http://feedproxy.google.com/~r/JuliaLang/~3/Zz1vqAz1dIw/distributed-numerical-optimization" />
   <updated>2013-04-05T00:00:00-07:00</updated>
   <id>http://julialang.org/blog/2013/04/distributed-numerical-optimization</id>
   <content type="html">&lt;script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"&gt;
&lt;/script&gt;


&lt;p&gt;This post walks through the parallel computing functionality of Julia
to implement an asynchronous parallel version of the classical
&lt;em&gt;cutting-plane&lt;/em&gt; algorithm for convex (nonsmooth) optimization,
demonstrating the complete workflow including running on both Amazon
EC2 and a large multicore server. I will quickly review the
cutting-plane algorithm and will be focusing primarily on parallel
computation patterns, so don&amp;rsquo;t worry if you&amp;rsquo;re not familiar with the
optimization side of things.&lt;/p&gt;

&lt;h3 id="Cutting-plane+algorithm"&gt;Cutting-plane algorithm&lt;/h3&gt;

&lt;p&gt;The cutting-plane algorithm is a method for solving the optimization problem&lt;/p&gt;

&lt;p&gt;&lt;span&gt;$$\text{minimize} _ {x \in \mathbb{R}&amp;#94;d} \sum_{i=1}&amp;#94;n f_i(x)
$$&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;where the functions \( f_i \) are convex but not necessarily differentiable.
The absolute value function \( |x| \) and the 1-norm \( ||x|| _ 1 \) are
typical examples. Important applications also arise from &lt;a
href="http://en.wikipedia.org/wiki/Lagrangian_relaxation"&gt;Lagrangian
relaxation&lt;/a&gt;. The idea of the algorithm is to approximate the functions \(
f_i \) with piecewise linear models \( m_i \) which are built up from
information obtained by evaluating \( f_i \) at different points. We
iteratively minimize over the models to generate candidate solution points.&lt;/p&gt;

&lt;p&gt;We can state the algorithm as&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose starting point \( x \).&lt;/li&gt;
&lt;li&gt;For \(i = 1,\ldots,n\), evaluate \(
f_i(x) \) and update corresponding model \( m_i \).&lt;/li&gt;
&lt;li&gt;Let the next
candidate \( x \) be the minimizer of \( \sum_{i=1}&amp;#94;n m_i(x) \).&lt;/li&gt;
&lt;li&gt;If not converged, goto step 2.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;If it is costly to evaluate \( f_i(x) \), then the algorithm is naturally
parallelizable at step 2. The minimization in step 3 can be computed by solving
a linear optimization problem, which is usually very fast. (Let me point out
here that Julia has interfaces to the linear programming solvers &lt;a
href="https://github.com/mlubin/Clp.jl"&gt;Clp&lt;/a&gt;, &lt;a
href="https://github.com/lindahua/Gurobi.jl"&gt;Gurobi&lt;/a&gt;, and &lt;a
href="https://github.com/carlobaldassi/GLPK.jl"&gt;GLPK&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;Abstracting the math, we can write the algorithm using the following Julia code.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# functions initialize, isconverged, solvesubproblem, and process implemented elsewhere
state, subproblems = initialize()
while !isconverged(state)
    results = map(solvesubproblem,subproblems)
    state, subproblems = process(state, results)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The function &lt;code&gt;solvesubproblem&lt;/code&gt; corresponds to evaluating \( f_i(x) \) for a
given \( i \) and \( x \) (the elements of &lt;code&gt;subproblems&lt;/code&gt; could be tuples
&lt;code&gt;(i,x)&lt;/code&gt;). The function &lt;code&gt;process&lt;/code&gt; corresponds to minimizing the model in step
3, and it produces a new state and a new set of subproblems to solve.&lt;/p&gt;

&lt;p&gt;Note that the algorithm looks much like a map-reduce that would be easy to
parallelize using many existing frameworks. Indeed, in Julia we can simply
replace &lt;code&gt;map&lt;/code&gt; with &lt;code&gt;pmap&lt;/code&gt; (parallel map). Let&amp;rsquo;s consider a twist that makes
the parallelism not so straightforward.&lt;/p&gt;

&lt;h3 id="Asynchronous+variant"&gt;Asynchronous variant&lt;/h3&gt;

&lt;p&gt;Variability in the time taken by the &lt;code&gt;solvesubproblem&lt;/code&gt; function can lead to
load imbalance and limit parallel efficiency as workers sit idle waiting for new
tasks. Such variability arises naturally if &lt;code&gt;solvesubproblem&lt;/code&gt; itself requires
solving a optimization problem, or if the workers and network are shared, as is
often the case with cloud computing.&lt;/p&gt;

&lt;p&gt;We can consider a new variant of the cutting-plane algorithm to address this
issue. The key point is&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When proportion \(0 &amp;lt; \alpha \le 1 \) of subproblems for a given candidate
have been solved, generate a new candidate and corresponding set of
subproblems by using whatever information is presently available.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;In other words, we generate new tasks to feed to workers without needing to wait
for all current tasks to complete, making the algorithm asynchronous. The
algorithm remains convergent, although the total number of iterations may
increase. For more details, see this &lt;a
href="http://dx.doi.org/10.1023/A:1021858008222"&gt;paper&lt;/a&gt; by Jeff Linderoth and
Stephen Wright.&lt;/p&gt;

&lt;p&gt;By introducing asynchronicity we can no longer use a nice black-box &lt;code&gt;pmap&lt;/code&gt;
function and have to dig deeper into the parallel implementation. Fortunately,
this is easy to do in Julia.&lt;/p&gt;

&lt;h3 id="Parallel+implementation+in+Julia"&gt;Parallel implementation in Julia&lt;/h3&gt;

&lt;p&gt;Julia implements distributed-memory parallelism based on one-sided message
passing, where process push work onto others (via &lt;code&gt;remotecall&lt;/code&gt;) and the
results are retrieved (via &lt;code&gt;fetch&lt;/code&gt;) by the process which requires them. Macros
such as &lt;code&gt;@spawn&lt;/code&gt; and &lt;code&gt;@parallel&lt;/code&gt; provide pretty syntax around this low-level
functionality.  This model of parallelism is very different from the typical
SIMD style of MPI. Both approaches are useful in different contexts, and I
expect an MPI wrapper for Julia will appear in the future (see also &lt;a
href="https://github.com/lcw/julia-mpi"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Reading the &lt;a
href="http://docs.julialang.org/en/release-0.1/manual/parallel-computing/"&gt;manual&lt;/a&gt;
on parallel computing is highly recommended, and I won&amp;rsquo;t try to reproduce it in
this post. Instead, we&amp;rsquo;ll dig into and extend one of the examples it presents.&lt;/p&gt;

&lt;p&gt;The implementation of &lt;code&gt;pmap&lt;/code&gt; in Julia is&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function pmap(f, lst)
    np = nprocs()  # determine the number of processors available
    n = length(lst)
    results = cell(n)
    i = 1
    # function to produce the next work item from the queue.
    # in this case it's just an index.
    next_idx() = (idx=i; i+=1; idx)
    @sync begin
        for p=1:np
            if p != myid() || np == 1
                @spawnlocal begin
                    while true
                        idx = next_idx()
                        if idx &amp;gt; n
                            break
                        end
                        results[idx] = remotecall_fetch(p, f, lst[idx])
                    end
                end
            end
        end
    end
    results
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;On first sight, this code is not particularly intuitive. The &lt;code&gt;@spawnlocal&lt;/code&gt;
macro creates a &lt;em&gt;&lt;a
href="http://docs.julialang.org/en/latest/manual/control-flow/#man-tasks"&gt;task&lt;/a&gt;&lt;/em&gt;
on the &lt;em&gt;master process&lt;/em&gt; (e.g. process 1). Each task feeds work to a
corresponding worker; the call &lt;code&gt;remotecall_fetch(p, f, lst[idx])&lt;/code&gt; function
calls &lt;code&gt;f&lt;/code&gt; on process &lt;code&gt;p&lt;/code&gt; and returns the result when finished. Tasks are
uninterruptable and only surrender control at specific points such as
&lt;code&gt;remotecall_fetch&lt;/code&gt;. Tasks cannot directly modify variables from the enclosing
scope, but the same effect can be achieved by using the &lt;code&gt;next_idx&lt;/code&gt; function to
access and mutate &lt;code&gt;i&lt;/code&gt;. &lt;em&gt;The task idiom functions in place of using a loop to
poll for results from each worker process.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Implementing our asynchronous algorithm is not much more than a modification of
the above code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# given constants n and 0 &amp;lt; alpha &amp;lt;= 1
# functions initialize and solvesubproblem defined elsewhere
np = nprocs() 
state, subproblems = initialize()
converged = false
isconverged() = converged
function updatemodel(mysubproblem, result)
    # store result
    ...
    # decide whether to generate new subproblems
    state.numback[mysubproblem.parent] += 1
    if state.numback[mysubproblem.parent] &amp;gt;= alpha*n &amp;amp;&amp;amp; !state.didtrigger[mysubproblem.parent]
        state.didtrigger[mysubproblem.parent] = true
        # generate newsubproblems by solving linear optimization problem
        ...
        if ... # convergence test
            converged = true
        else
            append!(subproblems, newsubproblems)
            push!(state.didtrigger, false)
            push!(state.numback, 0)
            # ensure that for s in newsubproblems, s.parent == length(state.numback)
        end
    end
end

@sync begin
    for p=1:np
        if p != myid() || np == 1
            @spawnlocal begin
                while !isconverged()
                    if length(subproblems) == 0
                        # no more subproblems but haven't converged yet
                        yield()
                        continue
                    end
                    mysubproblem = shift!(subproblems) # pop subproblem from queue
                    result = remotecall_fetch(p, solvesubproblem, mysubproblem)
                    updatemodel(mysubproblem, result)
                end
            end
        end
    end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;where &lt;code&gt;state&lt;/code&gt; is an instance of a type defined as&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type State
    didtrigger::Vector{Bool}
    numback::Vector{Int}
    ...
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There is little difference in the structure of the code inside the &lt;code&gt;@sync&lt;/code&gt;
blocks, and the asynchronous logic is encapsulated in the local &lt;code&gt;updatemodel&lt;/code&gt;
function which conditionally generates new subproblems. A strength of Julia is
that functions like &lt;code&gt;pmap&lt;/code&gt; are implemented in Julia itself, so that it is
particularly straightforward to make modifications like this.&lt;/p&gt;

&lt;h3 id="Running+it"&gt;Running it&lt;/h3&gt;

&lt;p&gt;Now for the fun part. The complete cutting-plane algorithm (along with
additional variants) is implemented in &lt;a
href="https://github.com/mlubin/JuliaBenders"&gt;JuliaBenders&lt;/a&gt;. The code is
specialized for &lt;a
href="http://en.wikipedia.org/wiki/Stochastic_programming"&gt;stochastic
programming&lt;/a&gt; where the cutting-plane algorithm is known as the &lt;a
href="http://www.springerreference.com/docs/html/chapterdbid/72429.html"&gt;L-shaped
method&lt;/a&gt; or Benders decomposition and is used to decompose the solution of
large linear optimization problems. Here, &lt;code&gt;solvesubproblem&lt;/code&gt; entails solving a
relatively small linear optimization problem. Test instances are taken from the
previously mentioned &lt;a
href="http://dx.doi.org/10.1023/A:1021858008222"&gt;paper&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We&amp;rsquo;ll first run on a large multicore server. The
&lt;code&gt;runals.jl&lt;/code&gt; (asynchronous L-shaped) file contains the algorithm we&amp;rsquo;ll use. Its
usage is&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia runals.jl [data source] [num subproblems] [async param] [block size]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;where &lt;code&gt;[num subproblems]&lt;/code&gt; is the \(n\) as above and &lt;code&gt;[async param]&lt;/code&gt; is
the proportion \(\alpha\). By setting \(\alpha = 1\) we obtain the
synchronous algorithm. For the asynchronous version we will take \(\alpha =
0.6\). The &lt;code&gt;[block size]&lt;/code&gt; parameter controls how many subproblems are sent to
a worker at once (in the previous code, this value was always 1). We will use
4000 subproblems in our experiments.&lt;/p&gt;

&lt;p&gt;To run multiple Julia processes on a shared-memory machine, we pass the &lt;code&gt;-p N&lt;/code&gt;
option to the &lt;code&gt;julia&lt;/code&gt; executable, which will start up &lt;code&gt;N&lt;/code&gt; system processes.
To execute the asynchronous version with 10 workers, we run&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia -p 12 runals.jl Data/storm 4000 0.6 30
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that we start 12 processes. These are the 10 workers, the master (which
distributes tasks), and another process to perform the master&amp;rsquo;s computations (an
additional refinement which was not described above). Results from various runs
are presented in the table below.&lt;/p&gt;

&lt;table style="text-align:right;margin-left:auto;margin-right:auto" cellspacing="5"&gt;
&lt;tr style="text-align:center"&gt;
    &lt;td&gt; &lt;/td&gt;
    &lt;td colspan="2" style="border-bottom-style:solid;border-bottom-width:2px"&gt;Synchronous&lt;/td&gt;
    &lt;td colspan="2" style="border-bottom-style:solid;border-bottom-width:2px"&gt;Asynchronous&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="text-align:center"&gt;
    &lt;td style="border-bottom-style:solid;border-bottom-width:2px"&gt;No. Workers&lt;/td&gt;
    &lt;td style="border-bottom-style:solid;border-bottom-width:2px"&gt;Speed&lt;/td&gt;
    &lt;td style="border-bottom-style:solid;border-bottom-width:2px"&gt;Efficiency
    &lt;td style="border-bottom-style:solid;border-bottom-width:2px"&gt;Speed&lt;/td&gt;
    &lt;td style="border-bottom-style:solid;border-bottom-width:2px"&gt;Efficiency
&lt;/td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
    &lt;td style="text-align:center"&gt;10&lt;/td&gt;
    &lt;td&gt;154&lt;/td&gt;
    &lt;td&gt;Baseline&lt;/td&gt;
    &lt;td&gt;166&lt;/td&gt;
    &lt;td&gt;Baseline&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td style="text-align:center"&gt;20&lt;/td&gt;
    &lt;td&gt;309&lt;/td&gt;
    &lt;td&gt;100.3%&lt;/td&gt;
    &lt;td&gt;348&lt;/td&gt;
    &lt;td&gt;105%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td style="text-align:center"&gt;40&lt;/td&gt;
    &lt;td&gt;517&lt;/td&gt;
    &lt;td&gt;84%&lt;/td&gt;
    &lt;td&gt;654&lt;/td&gt;
    &lt;td&gt;98%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td style="text-align:center"&gt;60&lt;/td&gt;
    &lt;td&gt;674&lt;/td&gt;
    &lt;td&gt;73%&lt;/td&gt;
    &lt;td&gt;918&lt;/td&gt;
    &lt;td&gt;92%&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;




&lt;p class="caption" style="text-align:center"&gt;&lt;b&gt;Table:&lt;/b&gt;
Results on a shared-memory 8x Xeon E7-8850 server. Workers correspond to
individual cores. Speed is the rate of subproblems solved per second. Efficiency
is calculated as the percent of ideal parallel speedup obtained. The superlinear
scaling observed with 20 workers is likely a system artifact. 
&lt;/p&gt;


&lt;p&gt;There are a few more hoops to jump through in order to run on EC2. First we must
build a system image (AMI) with Julia installed. Julia connects to workers over
ssh, so I found it useful to put my EC2 ssh key on the AMI and also set
&lt;code&gt;StrictHostKeyChecking no&lt;/code&gt; in &lt;code&gt;/etc/ssh/ssh_config&lt;/code&gt; to disable the
authenticity prompt when connecting to new workers. Someone will likely correct
me on if this is the right approach.&lt;/p&gt;

&lt;p&gt;Assuming we have an AMI in place, we can fire up the instances. I used an
m3.xlarge instance for the master and m1.medium instances for the workers.
(Note: you can save a lot of money by using the spot market.)&lt;/p&gt;

&lt;p&gt;To add remote workers on startup, Julia accepts a file with a list of host names
through the &lt;code&gt;--machinefile&lt;/code&gt; option. We can generate this easily enough by
using the EC2 API Tools (Ubuntu package &lt;code&gt;ec2-api-tools&lt;/code&gt;) with the command&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ec2-describe-instances | grep running | awk '{ print $5; }' &amp;gt; mfile
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;On the master instance we can then run&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;julia --machinefile mfile runatr.jl Data/storm 4000 0.6 30
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Results from various runs are presented in the table below.&lt;/p&gt;

&lt;table style="text-align:right;margin-left:auto;margin-right:auto" cellspacing="5"&gt;
&lt;tr style="text-align:center"&gt;
    &lt;td&gt; &lt;/td&gt;
    &lt;td colspan="2" style="border-bottom-style:solid;border-bottom-width:2px"&gt;Synchronous&lt;/td&gt;
    &lt;td colspan="2" style="border-bottom-style:solid;border-bottom-width:2px"&gt;Asynchronous&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="text-align:center"&gt;
    &lt;td style="border-bottom-style:solid;border-bottom-width:2px"&gt;No. Workers&lt;/td&gt;
    &lt;td style="border-bottom-style:solid;border-bottom-width:2px"&gt;Speed&lt;/td&gt;
    &lt;td style="border-bottom-style:solid;border-bottom-width:2px"&gt;Efficiency
    &lt;td style="border-bottom-style:solid;border-bottom-width:2px"&gt;Speed&lt;/td&gt;
    &lt;td style="border-bottom-style:solid;border-bottom-width:2px"&gt;Efficiency
&lt;/td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
    &lt;td style="text-align:center"&gt;10&lt;/td&gt;
    &lt;td&gt;149&lt;/td&gt;
    &lt;td&gt;Baseline&lt;/td&gt;
    &lt;td&gt;151&lt;/td&gt;
    &lt;td&gt;Baseline&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td style="text-align:center"&gt;20&lt;/td&gt;
    &lt;td&gt;289&lt;/td&gt;
    &lt;td&gt;97%&lt;/td&gt;
    &lt;td&gt;301&lt;/td&gt;
    &lt;td&gt;99.7%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td style="text-align:center"&gt;40&lt;/td&gt;
    &lt;td&gt;532&lt;/td&gt;
    &lt;td&gt;89%&lt;/td&gt;
    &lt;td&gt;602&lt;/td&gt;
    &lt;td&gt;99.5%&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;




&lt;p class="caption" style="text-align:center"&gt;&lt;b&gt;Table:&lt;/b&gt;
Results on Amazon EC2. Workers correspond to individual m1.medium instances. The
master process is run on an m3.xlarge instance. 
&lt;/p&gt;


&lt;p&gt;On both architectures the asynchronous version solves subproblems at a higher
rate and has significantly better parallel efficiency. Scaling is better on EC2
than on the shared-memory server likely because the subproblem calculation is
memory bound, and so performance is better on the distributed-memory
architecture. Anyway, with Julia we can easily experiment on both.&lt;/p&gt;

&lt;h3 id="Further+reading"&gt;Further reading&lt;/h3&gt;

&lt;p&gt;A more detailed &lt;a
href="https://github.com/JuliaLang/julia-tutorial/blob/master/NumericalOptimization/tutorial.pdf?raw=true"&gt;tutorial&lt;/a&gt;
was prepared for the Julia &lt;a href="https://github.com/JuliaLang/julia-tutorial"&gt;IAP session&lt;/a&gt; at MIT in January 2013.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/JuliaLang/~4/Zz1vqAz1dIw" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://julialang.org/blog/2013/04/distributed-numerical-optimization</feedburner:origLink></entry>
 
 <entry>
   <title>Videos from the Julia tutorial at MIT</title>
   <link href="http://feedproxy.google.com/~r/JuliaLang/~3/mHKnjWXt3KA/julia-tutorial-MIT" />
   <updated>2013-03-30T00:00:00-07:00</updated>
   <id>http://julialang.org/blog/2013/03/julia-tutorial-MIT</id>
   <content type="html">&lt;p&gt;We held a two day Julia tutorial at MIT in January 2013, which included 10 sessions. &lt;a href="http://ocw.mit.edu/"&gt;MIT Open Courseware&lt;/a&gt; and &lt;a href="http://www.mitx.org/"&gt;MIT-X&lt;/a&gt; graciously provided support for recording of these lectures, so that the wider Julia community can benefit from these sessions.&lt;/p&gt;

&lt;h2 id="Julia+Lightning+Round+([slides](https://raw.github.com/JuliaLang/julia-tutorial/master/LightningRound/IAP_2013_Lightning.pdf))"&gt;Julia Lightning Round (&lt;a href="https://raw.github.com/JuliaLang/julia-tutorial/master/LightningRound/IAP_2013_Lightning.pdf"&gt;slides&lt;/a&gt;)&lt;/h2&gt;

&lt;p&gt;This session is a rapid introduction to julia, using a number of lightning rounds. It uses a number of short examples to demonstrate syntax and features, and gives a quick feel for the language.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/37L1OMk_3FU" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;


&lt;h2 id="Rationale+behind+Julia+and+the+Vision+([slides](https://github.com/JuliaLang/julia-tutorial/raw/master/Vision/vision.pdf))"&gt;Rationale behind Julia and the Vision (&lt;a href="https://github.com/JuliaLang/julia-tutorial/raw/master/Vision/vision.pdf"&gt;slides&lt;/a&gt;)&lt;/h2&gt;

&lt;p&gt;The rationale and vision behind julia, and its design principles are discussed in this session.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/02U9AJMEWx0" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;


&lt;h2 id="Data+Analysis+with+DataFrames+([slides](https://github.com/JuliaLang/julia-tutorial/raw/master/DataFrames/slides.pdf))"&gt;Data Analysis with DataFrames (&lt;a href="https://github.com/JuliaLang/julia-tutorial/raw/master/DataFrames/slides.pdf"&gt;slides&lt;/a&gt;)&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/HarlanH/DataFrames.jl"&gt;DataFrames&lt;/a&gt; is one of the most widely used Julia packages. This session is an introduction to data analysis with Julia using DataFrames.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/XRClA5YLiIc" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;


&lt;h2 id="Statistical+Models+in+Julia+([slides](https://github.com/JuliaLang/julia-tutorial/raw/master/Stats/slides.pdf))"&gt;Statistical Models in Julia (&lt;a href="https://github.com/JuliaLang/julia-tutorial/raw/master/Stats/slides.pdf"&gt;slides&lt;/a&gt;)&lt;/h2&gt;

&lt;p&gt;This session demonstrates Julia&amp;rsquo;s statistics capabilities, which are provided by these packages: &lt;a href="https://github.com/JuliaStats/Distributions.jl"&gt;Distributions&lt;/a&gt;, &lt;a href="https://github.com/JuliaStats/GLM.jl"&gt;GLM&lt;/a&gt;, and &lt;a href="https://github.com/JuliaStats/LM.jl"&gt;LM&lt;/a&gt;.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/v9Io-p_iymI" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;


&lt;h2 id="Fast+Fourier+Transforms"&gt;Fast Fourier Transforms&lt;/h2&gt;

&lt;p&gt;Julia provides a built-in interface to the &lt;a href="http://www.fftw.org/"&gt;FFTW&lt;/a&gt; library. This session demonstrates the Julia&amp;rsquo;s &lt;a href="http://docs.julialang.org/en/release-0.1/stdlib/base/#signal-processing"&gt;signal processing&lt;/a&gt; capabilities, such as FFTs and DCTs. Also see the &lt;a href="https://github.com/stevengj/Hadamard.jl"&gt;Hadamard&lt;/a&gt; package.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/1iBLaHGL1AM" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;


&lt;h2 id="Optimization+([slides](https://github.com/JuliaLang/julia-tutorial/raw/master/NumericalOptimization/presentation.pdf))"&gt;Optimization (&lt;a href="https://github.com/JuliaLang/julia-tutorial/raw/master/NumericalOptimization/presentation.pdf"&gt;slides&lt;/a&gt;)&lt;/h2&gt;

&lt;p&gt;This session focuses largely on using Julia for solving linear programming problems. The algebraic modeling language discussed was later released as &lt;a href="https://github.com/IainNZ/MathProg.jl"&gt;MathProg&lt;/a&gt;. Benchmarks are shown evaluating the performance of Julia for implementing low-level optimization code. Linear programming solvers are available through the &lt;a href="https://github.com/mlubin/Clp.jl"&gt;CLP&lt;/a&gt;, &lt;a href="https://github.com/carlobaldassi/GLPK.jl"&gt;GLPK&lt;/a&gt;, and &lt;a href="https://github.com/lindahua/Gurobi.jl"&gt;Gurobi&lt;/a&gt; packages. The &lt;a href="https://github.com/johnmyleswhite/Optim.jl"&gt;Optim&lt;/a&gt; and &lt;a href="https://github.com/stevengj/NLopt.jl"&gt;NLopt&lt;/a&gt; packages provide more general optimization algorithms.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/O1icUP6sajU" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;


&lt;h2 id="Metaprogramming+and+Macros"&gt;Metaprogramming and Macros&lt;/h2&gt;

&lt;p&gt;Julia is homoiconic: it represents its own code as a data structure of the language itself. Since code is represented by objects that can be created and manipulated from within the language, it is possible for a program to transform and generate its own code. &lt;a href="http://docs.julialang.org/en/release-0.1/manual/metaprogramming/"&gt;Metaprogramming&lt;/a&gt; is described in detail in the Julia manual.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/EpNeNCGmyZE" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;


&lt;h2 id="Parallel+and+Distributed+Computing+([Lab](https://github.com/JuliaLang/julia-tutorial/raw/master/NumericalOptimization/tutorial.pdf),+[Solution](https://github.com/JuliaLang/julia-tutorial/blob/master/NumericalOptimization/Tutorial.jl))"&gt;Parallel and Distributed Computing (&lt;a href="https://github.com/JuliaLang/julia-tutorial/raw/master/NumericalOptimization/tutorial.pdf"&gt;Lab&lt;/a&gt;, &lt;a href="https://github.com/JuliaLang/julia-tutorial/blob/master/NumericalOptimization/Tutorial.jl"&gt;Solution&lt;/a&gt;)&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://docs.julialang.org/en/release-0.1/manual/parallel-computing/"&gt;Parallel and distributed computing&lt;/a&gt; have been an integral part of Julia&amp;rsquo;s capabilities from an early stage. This session describes existing basic capabilities, which can be used as building blocks for higher level parallel libraries.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/JoRn4ryMclc" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;


&lt;h2 id="Networking"&gt;Networking&lt;/h2&gt;

&lt;p&gt;Julia provides asynchronous networking I/O using the &lt;a href="https://github.com/joyent/libuv"&gt;libuv&lt;/a&gt; library. Libuv is a portable networking library created as part of the &lt;a href="http://www.nodejs.org/"&gt;Node.js&lt;/a&gt; project.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/qYjHYTn7r2w" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;


&lt;h2 id="Grid+of+Resistors+([Lab](https://github.com/JuliaLang/julia-tutorial/blob/master/GridOfResistors/GridOfResistors.md),+[Solution](https://github.com/JuliaLang/julia-tutorial/tree/master/GridOfResistors))"&gt;Grid of Resistors (&lt;a href="https://github.com/JuliaLang/julia-tutorial/blob/master/GridOfResistors/GridOfResistors.md"&gt;Lab&lt;/a&gt;, &lt;a href="https://github.com/JuliaLang/julia-tutorial/tree/master/GridOfResistors"&gt;Solution&lt;/a&gt;)&lt;/h2&gt;

&lt;p&gt;The Grid of Resistors is a classic numerical problem to compute the voltages and the effective resistance of a 2n+1 by 2n+2 grid of 1 ohm resistors if a battery is connected to the two center points. As part of this lab, the problem is solved in Julia in a number of different ways such as a vectorized implementation, a devectorized implementation, and using comprehensions, in order to study the performance characteristics of various methods.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/OFWYPqwVtHU" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;img src="http://feeds.feedburner.com/~r/JuliaLang/~4/mHKnjWXt3KA" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://julialang.org/blog/2013/03/julia-tutorial-MIT</feedburner:origLink></entry>
 
 <entry>
   <title>Efficient Aggregates in Julia</title>
   <link href="http://feedproxy.google.com/~r/JuliaLang/~3/2w-1CaVPNHc/efficient-aggregates" />
   <updated>2013-03-05T00:00:00-08:00</updated>
   <id>http://julialang.org/blog/2013/03/efficient-aggregates</id>
   <content type="html">&lt;p&gt;We recently introduced an exciting feature that has been in planning for some
time: immutable aggregate types. In fact, we have been planning to do this
for so long that this feature is the subject of our issue #13 on GitHub,
out of more than 2400 total issues so far.&lt;/p&gt;

&lt;p&gt;Essentially, this feature drastically reduces the overhead of user-defined
types that represent small number-like values, or that wrap a small number
of other objects. Consider an RGB pixel type:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;immutable Pixel
    r::Uint8
    g::Uint8
    b::Uint8
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Instances of this type can now be packed efficiently into arrays, using
exactly 3 bytes per object. In all other respects, these objects continue
to act like normal first-class objects. To see how we might use
this, here is a function that converts an RGB image in standard 24-bit
framebuffer format to grayscale:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function rgb2gray!(img::Array{Pixel})
    for i=1:length(img)
        p = img[i]
        v = uint8(0.30*p.r + 0.59*p.g + 0.11*p.b)
        img[i] = Pixel(v,v,v)
    end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code will run blazing fast, performing no memory allocation. We
have not done thorough benchmarking, but this is in fact likely to be the
fastest way to write this function in Julia from now on.&lt;/p&gt;

&lt;p&gt;The key to this behavior is the new &lt;code&gt;immutable&lt;/code&gt; keyword, which means
instances of the type cannot be modified. At first this sounds like
a mere restriction — how come I&amp;rsquo;m not allowed to modify one? — but
what it really means is that the object is identified with its contents,
rather than its memory address. A mutable object has &amp;ldquo;behavior&amp;rdquo;; it changes
over time, and there may be many references to the object, all of which
can observe those changes. An immutable object, on the other hand, has only
a value, and no time-varying behavior. Its location does not matter. It is
&amp;ldquo;just some bits&amp;rdquo;.&lt;/p&gt;

&lt;p&gt;Julia has always had some immutable values, in the form of bits types,
which are used to represent fixed-bit-width numbers. It is highly intuitive
that numbers are immutable. If &lt;code&gt;x&lt;/code&gt; equals 2, you might later change the value
of &lt;code&gt;x&lt;/code&gt;, but it is understood that the value of 2 itself does not change.
The &lt;code&gt;immutable&lt;/code&gt; keyword generalizes this idea to structured data types with
named fields. Julia variables and containers, including arrays, are all
still mutable. While a &lt;code&gt;Pixel&lt;/code&gt; object itself can&amp;rsquo;t change, a new &lt;code&gt;Pixel&lt;/code&gt;
can be written over an old one within an array, since the array is mutable.&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s take a look at the benefits of this feature.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The compiler and GC have a lot of freedom to move and copy these objects
around. This flexibility can be used to store data more efficiently,
for example keeping the real and imaginary parts of a complex number in
separate registers, or keeping only one part in a register.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Immutable objects are easy to reason about. Some languages, such as C++
and C#, provide &amp;ldquo;value types&amp;rdquo;, which have many of the benefits of immutable
objects. However, their behavior can be confusing. Consider code like
the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;item = lookup(collection, index)
modify!(item)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The question here is whether we have modified the same &lt;code&gt;item&lt;/code&gt; that is in
the collection, or if we have modified a local copy. In Julia there are
only two possibilities: either &lt;code&gt;item&lt;/code&gt; is mutable, in which case we modified the
one and only copy of it, or it is immutable, in which case modifying it is
not allowed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No-overhead data abstractions become possible. It is often useful to
define a new type that simply wraps a single value, and modifies its
behavior in some way. Our favorite modular integer example type fits this
description:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;immutable ModInt{n} &amp;lt;: Integer
    k::Int
    ModInt(k) = new(mod(k,n))
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Since a given &lt;code&gt;ModInt&lt;/code&gt; doesn&amp;rsquo;t need to exist at a particular address, it
can be passed to functions, stored in arrays, and so on, as efficiently as
a single &lt;code&gt;Int&lt;/code&gt;, with no wrapping overhead. But, in Julia, the overhead will not
&lt;em&gt;always&lt;/em&gt; be zero. The &lt;code&gt;ModInt&lt;/code&gt; type information will &amp;ldquo;follow the data around&amp;rdquo;
at compile time to the extent possible, but heap-allocated wrappers will be
added as needed at run time. Typically these wrappers will be short-lived;
if the final destination of a &lt;code&gt;ModInt&lt;/code&gt; is in a &lt;code&gt;ModInt&lt;/code&gt; array, for example,
the wrapper can be discarded when the value is assigned. But if the value is
only used locally inside a function, there will most likely be no wrappers
at all.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Abstractions are fully enforced. If a custom constructor is written for
an immutable type, then all instances will be created by it. Since the
constructed objects are never modified, the invariants provided by the
constructor cannot be violated. At this time, uninitialized arrays are an
exception to this rule. New arrays of &amp;ldquo;plain data&amp;rdquo; immutable types have
unspecified contents, so it is possible to obtain an invalid value from one.
This is usually harmless in practice, since arrays must be initialized anyway,
and are often created through functions like &lt;code&gt;zeros&lt;/code&gt; that do so.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can automatically type-specialize fields. Since field values at
construction time are final, their types are too, so we learn everything
about the type of an immutable object when it is constructed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;There are many potential optimizations here, and we have not implemented
all of them yet. But having this feature in place provides another lever to
help us improve performance over time.&lt;/p&gt;

&lt;p&gt;For now though, we at least have a much simpler implementation of complex
numbers, and will be able to take advantage of efficient rational matrices
and other similar niceties.&lt;/p&gt;

&lt;h2 id="Addendum:+Under+the+hood"&gt;Addendum: Under the hood&lt;/h2&gt;

&lt;p&gt;For purposes of calling C and writing reflective code, it helps to know a
bit about how immutable types are implemented. Before this change, we had
types &lt;code&gt;AbstractKind&lt;/code&gt;, &lt;code&gt;BitsKind&lt;/code&gt;, and &lt;code&gt;CompositeKind&lt;/code&gt;, for separating which
types are abstract, which are represented by immutable bit strings, and which
are mutable aggregates. It was sometimes convenient that the type system
reflected these differences, but also a bit unwarranted since all these
types participate in the same hierarchy and follow the same subtyping rules.&lt;/p&gt;

&lt;p&gt;Now, the type landscape is both simpler and more complex. The three Kinds
have been merged into a single kind called &lt;code&gt;DataType&lt;/code&gt;. The type of every
value in Julia is now either a &lt;code&gt;DataType&lt;/code&gt;, or else a tuple type (union types
still exist, but of course are always abstract). To find out the details
of a &lt;code&gt;DataType&lt;/code&gt;&amp;rsquo;s physical representation, you must query its properties.
&lt;code&gt;DataType&lt;/code&gt;s have three boolean properties &lt;code&gt;abstract&lt;/code&gt;, &lt;code&gt;mutable&lt;/code&gt;, and
&lt;code&gt;pointerfree&lt;/code&gt;, and an integer property &lt;code&gt;size&lt;/code&gt;. The &lt;code&gt;CompositeKind&lt;/code&gt; properties
&lt;code&gt;names&lt;/code&gt; and &lt;code&gt;types&lt;/code&gt; are still there to describe fields.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;abstract&lt;/code&gt; property indicates that the type was declared with the
&lt;code&gt;abstract&lt;/code&gt; keyword and has no direct instances. &lt;code&gt;mutable&lt;/code&gt; indicates, for
concrete types, whether instances are mutable. &lt;code&gt;pointerfree&lt;/code&gt; means that
instances contain &amp;ldquo;just data&amp;rdquo; and no references to other Julia values.
&lt;code&gt;size&lt;/code&gt; gives the size of an instance in bytes.&lt;/p&gt;

&lt;p&gt;What used to be &lt;code&gt;BitsKind&lt;/code&gt;s are now &lt;code&gt;DataType&lt;/code&gt;s that are immutable, concrete,
have no fields, and have non-zero size. The former &lt;code&gt;CompositeKind&lt;/code&gt;s are
mutable and concrete, and either have fields or are zero size if they
have zero fields. Clearly, new combinations are now possible. We have
already mentioned immutable types with fields. We could have the equivalent
of mutable &lt;code&gt;BitsKind&lt;/code&gt;s, but this combination is not exposed in the language,
since it is easily emulated using mutable fields. Another new combination
is abstract types with fields, which would allow you to declare that all
subtypes of some abstract type should have certain fields. That one is
definitely useful, and we plan to provide syntax for it.&lt;/p&gt;

&lt;p&gt;Typically, the only time you need to worry about these things
is when calling native code, when you want to know whether some array
or struct has C-compatible data layout. This is handled by the type
predicate &lt;code&gt;isbits(T)&lt;/code&gt;.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/JuliaLang/~4/2w-1CaVPNHc" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://julialang.org/blog/2013/03/efficient-aggregates</feedburner:origLink></entry>
 
 <entry>
   <title>Design and implementation of Julia</title>
   <link href="http://feedproxy.google.com/~r/JuliaLang/~3/z7Y2GrO9DB4/design-and-implementation-of-julia" />
   <updated>2012-08-16T00:00:00-07:00</updated>
   <id>http://julialang.org/blog/2012/08/design-and-implementation-of-julia</id>
   <content type="html">&lt;p&gt;We describe the design and implementation of Julia in our first paper &amp;ndash; &lt;a href="/images/julia-dynamic-2012-tr.pdf"&gt;Julia: A Fast Dynamic Language for Technical Computing&lt;/a&gt;. This is work in progress and comments are appreciated.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/JuliaLang/~4/z7Y2GrO9DB4" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://julialang.org/blog/2012/08/design-and-implementation-of-julia</feedburner:origLink></entry>
 
 <entry>
   <title>New York Open Stats Meetup</title>
   <link href="http://feedproxy.google.com/~r/JuliaLang/~3/nGWSNSEoA3A/nyc-open-stats-meetup-announcement" />
   <updated>2012-04-18T00:00:00-07:00</updated>
   <id>http://julialang.org/blog/2012/04/nyc-open-stats-meetup-announcement</id>
   <content type="html">&lt;p&gt;I&amp;rsquo;ll be giving a talk on Julia at the &lt;a href="http://www.meetup.com/nyhackr/events/60839932/"&gt;New York Open Statistical Programming Meetup on May 1st&lt;/a&gt;. After my presentation, &lt;a href="http://www.johnmyleswhite.com/"&gt;John Myles White&lt;/a&gt; and &lt;a href="http://www.statalgo.com/"&gt;Shane Conway&lt;/a&gt; are going to give followup demos of statistical applications using Julia. Then we&amp;rsquo;re going to hang out and grab drinks nearby. Thanks to &lt;a href="http://www.harlan.harris.name/"&gt;Harlan Harris&lt;/a&gt; and &lt;a href="http://www.drewconway.com/"&gt;Drew Conway&lt;/a&gt; for setting the whole thing up!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Announcement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After a brief hiatus, we are very excited to announce our May meetup will feature one of the hottest new languages in statistical computing: Julia.  We are delighted to welcome Stefan Karpinski, one of the creators of Julia, to give an introduction to the language and his perspective on statistical computing.&lt;/p&gt;

&lt;p&gt;Julia is a general-purpose, high-level, dynamic language in the tradition of Lisp, Perl, Python and Ruby. It is designed to take advantage of modern techniques for executing dynamic languages with statically-compiled performance. As part of this design, the language has an expressive type system, which programmers may leverage for dispatch and error checking — incidentally providing the compiler with useful type information. Using types is entirely optional, however: &amp;ldquo;typeless Julia&amp;rdquo; is a valid and useful subset of the language, similar to traditional dynamic languages, which nevertheless runs at statically compiled speeds.\&lt;/p&gt;

&lt;p&gt;Julia is especially good at running Matlab and R-style programs. Given its level of performance, we envision a new era of technical computing where libraries can be developed in a high-level language instead of C or Fortran. We have also experimented with cloud API integration, and begun to develop a web-based interactive computing environment. The ultimate goal is to make cloud-based supercomputing as easy and accessible as Google Docs.&lt;/p&gt;

&lt;p&gt;We will also hear from a mix of people who have already started developing in Julia and see some examples of what they have developed.&lt;/p&gt;

&lt;p&gt;The meetup will follow our typical schedule: pizza will begin at 6:15pm, Stefan will begin promptly at 7pm, and we will head to The Central Bar around 8:30pm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; You can see the slides for the talk &lt;a href="/images/nyhackr.pdf"&gt;here&lt;/a&gt;. There was no video of the talk, but hopefully the slides are informative — there are, among other things, a lot of code examples that should just work if pasted into the Julia repl.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/JuliaLang/~4/nGWSNSEoA3A" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://julialang.org/blog/2012/04/nyc-open-stats-meetup-announcement</feedburner:origLink></entry>
 
 <entry>
   <title>Lang.NEXT Announcement</title>
   <link href="http://feedproxy.google.com/~r/JuliaLang/~3/PigIYrg6rjw/lang-next-talk-announcement" />
   <updated>2012-03-24T00:00:00-07:00</updated>
   <id>http://julialang.org/blog/2012/03/lang-next-talk-announcement</id>
   <content type="html">&lt;p&gt;Jeff and I will be giving a &lt;a href="http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2012/Julia"&gt;presentation on Julia&lt;/a&gt; at the upcoming &lt;a href="http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2012"&gt;Lang.NEXT conference&lt;/a&gt;, a gathering of &amp;ldquo;programming language design experts and enthusiasts&amp;rdquo; featuring &amp;ldquo;talks, panels and discussion on leading programming language work from industry and research.&amp;rdquo;
We are honored and excited to have been invited to speak at an event alongside so many programming language luminaries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Abstract:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Julia is a dynamic language in the tradition of Lisp, Perl, Python and Ruby. It aims to advance  expressiveness and convenience for scientific and technical computing beyond that of environments like Matlab and NumPy, while simultaneously closing the performance gap with compiled languages like C, C++, Fortran and Java.&lt;/p&gt;

&lt;p&gt;Most high-performance dynamic language implementations have taken an existing interpreted language and worked to accelerate its execution. In creating Julia, we have reconsidered the basic language design, taking into account the capabilities of modern JIT compilers and the specific needs of technical computing. Our design includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple dispatch as the core language paradigm.&lt;/li&gt;
&lt;li&gt;Exposing a sophisticated type system including parametric dependent types.&lt;/li&gt;
&lt;li&gt;Dynamic type inference to generate fast code from programs with no declarations.&lt;/li&gt;
&lt;li&gt;Aggressive specialization of generated code for types encountered at run-time.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Julia feels light and natural for data exploration and algorithm prototyping, but has performance that lets you deploy your prototypes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; You can see the slides for our talk &lt;a href="/images/lang.next.pdf"&gt;here&lt;/a&gt;. Video of the presentation is available &lt;a href="http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2012/Julia"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/JuliaLang/~4/PigIYrg6rjw" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://julialang.org/blog/2012/03/lang-next-talk-announcement</feedburner:origLink></entry>
 
 <entry>
   <title>Shelling Out Sucks</title>
   <link href="http://feedproxy.google.com/~r/JuliaLang/~3/0H6a-4z6Wno/shelling-out-sucks" />
   <updated>2012-03-11T00:00:00-08:00</updated>
   <id>http://julialang.org/blog/2012/03/shelling-out-sucks</id>
   <content type="html">&lt;p&gt;Spawning a pipeline of connected programs via an intermediate shell — a.k.a. &amp;ldquo;shelling out&amp;rdquo; — is a really convenient and effective way to get things done.
It&amp;rsquo;s so handy that some &amp;ldquo;&lt;a href="http://en.wikipedia.org/wiki/Glue_language"&gt;glue languages&lt;/a&gt;,&amp;rdquo; like &lt;a href="http://www.perl.org/"&gt;Perl&lt;/a&gt; and &lt;a href="http://www.ruby-lang.org/"&gt;Ruby&lt;/a&gt;, even have special syntax for it (backticks).
However, shelling out is also a common source of bugs, security holes, unnecessary overhead, and silent failures.
Here are the three reasons why shelling out is problematic:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="#Metacharacter+Brittleness"&gt;Metacharacter brittleness.&lt;/a&gt;&lt;/em&gt;
When commands are constructed programmatically, the resulting code is almost always brittle:
if a variable used to construct the command contains any shell metacharacters, including spaces, the command will likely break and do something very different than what was intended — potentially something quite dangerous.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="#Indirection+and+Inefficiency"&gt;Indirection and inefficiency.&lt;/a&gt;&lt;/em&gt;
When shelling out, the main program forks and execs a shell process just so that the shell can in turn fork and exec a series of commands with their inputs and outputs appropriately connected.
Not only is starting a shell an unnecessary step, but since the main program is not the parent of the pipeline commands, it cannot be notified when they terminate — it can only wait for the pipeline to finish and hope the shell indicates what happened.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="#Silent+Failures+by+Default"&gt;Silent failures by default.&lt;/a&gt;&lt;/em&gt;
Errors in shelled out commands don&amp;rsquo;t automatically become exceptions in most languages.
This default leniency leads to code that fails silently when shelled out commands don&amp;rsquo;t work.
Worse still, because of the indirection problem, there are many cases where the failure of a process in a spawned pipeline &lt;em&gt;cannot&lt;/em&gt; be detected by the parent process, even if errors are fastidiously checked for.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;In the rest of this post, I&amp;rsquo;ll go over examples demonstrating each of these problems.
At &lt;a href="#Summary+and+Remedy"&gt;the end&lt;/a&gt;, I&amp;rsquo;ll talk about better alternatives to shelling out, and in a &lt;a href="/blog/2013/04/put-this-in-your-pipe/"&gt;followup post&lt;/a&gt;, I&amp;rsquo;ll demonstrate how Julia makes these better alternatives dead simple to use.
Examples below are given in Ruby which shells out to &lt;a href="http://www.gnu.org/software/bash/"&gt;Bash&lt;/a&gt;, but the same problems exist no matter what language one shells out from:
it&amp;rsquo;s the technique of using an intermediate shell process to spawn external commands that&amp;rsquo;s at fault, not the language.&lt;/p&gt;

&lt;h2 id="Metacharacter+Brittleness"&gt;Metacharacter Brittleness&lt;/h2&gt;

&lt;p&gt;Let&amp;rsquo;s start with a simple example of shelling out from Ruby.
Suppose you want to count the number of lines containing the string &amp;ldquo;foo&amp;rdquo; in all the files under a directory given as an argument.
One option is to write Ruby code that reads the contents of the given directory, finds all the files, opens them and iterates through them looking for the string &amp;ldquo;foo&amp;rdquo;.
However, that&amp;rsquo;s a lot of work and it&amp;rsquo;s going to be much slower than using a pipeline of standard UNIX commands, which are written in C and heavily optimized.
The most natural and convenient thing to do in Ruby is to shell out, using backticks to capture output:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;`find #{dir} -type f -print0 | xargs -0 grep foo | wc -l`.to_i
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This expression interpolates the &lt;code&gt;dir&lt;/code&gt; variable into a command, spawns a Bash shell to execute the resulting command, captures the output into a string, and then converts that string to an integer.
The command uses the &lt;code&gt;-print0&lt;/code&gt; and &lt;code&gt;-0&lt;/code&gt; options to correctly handle strange characters in file names piped from &lt;code&gt;find&lt;/code&gt; to &lt;code&gt;xargs&lt;/code&gt; (these options cause file names to be delimited by &lt;a href="http://en.wikipedia.org/wiki/Null_character"&gt;NULs&lt;/a&gt; instead of whitespace).
Even with extra-careful options, this code for shelling out is simple and clear.
Here it is in action:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;irb(main):001:0&amp;gt; dir="src"
=&amp;gt; "src"
irb(main):002:0&amp;gt; `find #{dir} -type f -print0 | xargs -0 grep foo | wc -l`.to_i
=&amp;gt; 5    
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Great.
However, this only works as expected if the directory name &lt;code&gt;dir&lt;/code&gt; doesn&amp;rsquo;t contain any characters that the shell considers special.
For example, the shell decides what constitutes a single argument to a command using whitespace.
Thus, if the value of &lt;code&gt;dir&lt;/code&gt; is a directory name containing a space, this will fail:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;irb(main):003:0&amp;gt; dir="source code"
=&amp;gt; "source code"
irb(main):004:0&amp;gt; `find #{dir} -type f -print0 | xargs -0 grep foo | wc -l`.to_i
find: `source': No such file or directory
find: `code': No such file or directory
=&amp;gt; 0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The simple solution to the problem of spaces is to surround the interpolated directory name in quotes, telling the shell to treat spaces inside as normal characters:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;irb(main):005:0&amp;gt; `find '#{dir}' -type f -print0 | xargs -0 grep foo | wc -l`.to_i
=&amp;gt; 5
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Excellent.
So what&amp;rsquo;s the problem?
While this solution addresses the issue of file names with spaces in them, it is still brittle with respect to other shell metacharacters.
What if a file name has a quote character in it?
Let&amp;rsquo;s try it.
First, let&amp;rsquo;s create a very weirdly named directory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;bash-3.2$ mkdir "foo'bar"
bash-3.2$ echo foo &amp;gt; "foo'bar"/test.txt
bash-3.2$ ls -ld foo*bar
drwxr-xr-x 3 stefan staff 102 Feb  3 16:17 foo'bar/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;rsquo;s an admittedly strange directory name, but it&amp;rsquo;s perfectly legal in UNIXes of all flavors.
Now back to Ruby:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;irb(main):006:0&amp;gt; dir="foo'bar"
=&amp;gt; "foo'bar"
irb(main):007:0&amp;gt; `find '#{dir}' -type f -print0  | xargs -0 grep foo | wc -l`.to_i
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
=&amp;gt; 0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Doh.
Although this may seem like an unlikely corner case that one needn&amp;rsquo;t realistically worry about, there are serious security ramifications.
Suppose the name of the directory came from an untrusted source — like a web submission, or an argument to a setuid program from an untrusted user.
Suppose an attacker could arrange for any value of &lt;code&gt;dir&lt;/code&gt; they wanted:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;irb(main):008:0&amp;gt; dir="foo'; echo MALICIOUS ATTACK 1&amp;gt;&amp;amp;2; echo '"
=&amp;gt; "foo'; echo MALICIOUS ATTACK 1&amp;gt;&amp;amp;2; echo '"
irb(main):009:0&amp;gt; `find '#{dir}' -type f -print0  | xargs -0 grep foo | wc -l`.to_i
find: `foo': No such file or directory
MALICIOUS ATTACK
grep:  -type f -print0
: No such file or directory
=&amp;gt; 0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Your box is now owned.
Of course, you could sanitize the value of the &lt;code&gt;dir&lt;/code&gt; variable, but there&amp;rsquo;s a fundamental tug-of-war between security (as limited as possible) and flexibility (as unlimited as possible).
The ideal behavior is to allow any directory name, no matter how bizarre, as long as it actually exists, but &amp;ldquo;defang&amp;rdquo; all shell metacharacters.&lt;/p&gt;

&lt;p&gt;The only two way to fully protect against these sorts of metacharacter attacks — whether malicious or accidental — while still using an external shell to construct the pipeline, is to do full shell metacharacter escaping:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;irb(main):010:0&amp;gt; require 'shellwords'
=&amp;gt; true
irb(main):011:0&amp;gt; `find #{Shellwords.shellescape(dir)} -type f -print0  | xargs -0 grep foo | wc -l`.to_i
find: `foo\'; echo MALICIOUS ATTACK 1&amp;gt;&amp;amp;2; echo \'': No such file or directory
=&amp;gt; 0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With shell escaping, this safely attempts to search a very oddly named directory instead of executing the malicious attack.
Although shell escaping does work (assuming that there aren&amp;rsquo;t any mistakes in the shell escaping implementation), realistically, no one actually bothers — it&amp;rsquo;s too much trouble.
Instead, code that shells out with programmatically constructed commands is typically riddled with potential bugs in the best case and massive security holes in the worst case.&lt;/p&gt;

&lt;h2 id="Indirection+and+Inefficiency"&gt;Indirection and Inefficiency&lt;/h2&gt;

&lt;p&gt;If we were using the above code to count the number of lines with the string &amp;ldquo;foo&amp;rdquo; in a directory, we would want to check to see if everything worked and respond appropriately if something went wrong.
In Ruby, you can check if a shelled out command was successful using the bizarrely named &lt;code&gt;$?.success?&lt;/code&gt; indicator:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;irb(main):012:0&amp;gt; dir="src"                                                              
=&amp;gt; "src"
irb(main):013:0&amp;gt; `find #{Shellwords.shellescape(dir)} -type f -print0  | xargs -0 grep foo | wc -l`.to_i
=&amp;gt; 5
irb(main):014:0&amp;gt; $?.success?                                                                
=&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ok, that correctly indicates success.
Let&amp;rsquo;s make sure that it can detect failure:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;irb(main):015:0&amp;gt; dir="nonexistent"                                                              
=&amp;gt; "nonexistent"
irb(main):016:0&amp;gt; `find #{Shellwords.shellescape(dir)} -type f -print0  | xargs -0 grep foo | wc -l`.to_i
find: `nonexistent': No such file or directory
=&amp;gt; 0
irb(main):017:0&amp;gt; $?.success?
=&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Wait. What?!
That wasn&amp;rsquo;t successful.
What&amp;rsquo;s going on?&lt;/p&gt;

&lt;p&gt;The heart of the problem is that when you shell out, the commands in the pipeline are not immediate children of the main program, but rather its grandchildren:
the program spawns a shell, which makes a bunch of UNIX pipes, forks child processes, connects inputs and outputs to pipes using the &lt;a href="https://developer.apple.com/library/IOs/#documentation/System/Conceptual/ManPages_iPhoneOS/man2/dup2.2.html"&gt;&lt;code&gt;dup2&lt;/code&gt; system call&lt;/a&gt;, and then execs the appropriate commands.
As a result, your main program is not the parent of the commands in the pipeline, but rather, their grandparent.
Therefore, it doesn&amp;rsquo;t know their process IDs, nor can it wait on them or get their exit statuses when they terminate.
The shell process, which is their parent, has to do all of that.
Your program can only wait for the shell to finish and see if &lt;em&gt;that&lt;/em&gt; was successful.
If the shell is only executing a single command, this is fine:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;irb(main):018:0&amp;gt; `cat /dev/null`
=&amp;gt; ""
irb(main):019:0&amp;gt; $?.success?
=&amp;gt; true
irb(main):020:0&amp;gt; `cat /dev/nada`
cat: /dev/nada: No such file or directory
=&amp;gt; ""
irb(main):021:0&amp;gt; $?.success?
=&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unfortunately, by default the shell is quite lenient about what it considers to be a successful pipeline:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;irb(main):022:0&amp;gt; `cat /dev/nada | sort`
cat: /dev/nada: No such file or directory
=&amp;gt; ""
irb(main):023:0&amp;gt; $?.success?
=&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As long as the last command in a pipeline succeeds — in this case &lt;code&gt;sort&lt;/code&gt; — the entire pipeline is considered a success.
Thus, even when one or more of the earlier programs in a pipeline fails spectacularly, the last command may not, leading the shell to consider the entire pipeline to be successful.
This is probably not what you meant by success.&lt;/p&gt;

&lt;p&gt;Bash&amp;rsquo;s notion of pipeline success can fortunately be made stricter with the &lt;code&gt;pipefail&lt;/code&gt; option.
This option causes the shell to consider a pipeline successful only if all of its commands are successful:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;irb(main):024:0&amp;gt; `set -o pipefail; cat /dev/nada | sort`
cat: /dev/nada: No such file or directory
=&amp;gt; ""
irb(main):025:0&amp;gt; $?.success?
=&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Since shelling out spawns a new shell every time, this option has to be set for every multi-command pipeline in order to be able to determine its true success status.
Of course, just like shell-escaping every interpolated variable, setting &lt;code&gt;pipefail&lt;/code&gt; at the start of every command is simply something that no one actually does.
Moreover, even with the &lt;code&gt;pipefail&lt;/code&gt; option, your program has no way of determining &lt;em&gt;which&lt;/em&gt; commands in a pipeline were unsuccessful — it just knows that something somewhere went wrong.
While that&amp;rsquo;s better than silently failing and continuing as if there were no problem, its not very helpful for postmortem debugging:
many programs are not as well-behaved as &lt;code&gt;cat&lt;/code&gt; and don&amp;rsquo;t actually identify themselves or the specific problem when printing error messages before going belly up.&lt;/p&gt;

&lt;p&gt;Given the other problems caused by the indirection of shelling out, it seems like a barely relevant afterthought to mention that execing a shell process just to spawn a bunch of other processes is inefficient.
However, it is a real source of unnecessary overhead:
the main process could just do the work the shell does itself.
Asking the kernel to fork a process and exec a new program is a non-trivial amount of work.
The only reason to have the shell do this work for you is that it&amp;rsquo;s complicated and hard to get right.
The shell makes it easy.
So programming languages have traditionally relied on the shell to setup pipelines for them, regardless of the additional overhead and problems caused by indirection.&lt;/p&gt;

&lt;h2 id="Silent+Failures+by+Default"&gt;Silent Failures by Default&lt;/h2&gt;

&lt;p&gt;Let&amp;rsquo;s return to our example of shelling out to count &amp;ldquo;foo&amp;rdquo; lines.
Here&amp;rsquo;s the total expression we need to use in order to shell out without being susceptible to metacharacter breakage and so we can actually tell whether the entire pipeline succeeded:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;`set -o pipefail; find #{Shellwords.shellescape(dir)} -type f -print0  | xargs -0 grep foo | wc -l`.to_i
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;However, an error isn&amp;rsquo;t raised by default when a shelled out command fails.
To avoid silent errors, we need to explicitly check &lt;code&gt;$?.success?&lt;/code&gt; after every time we shell out and raise an exception if it indicates failure.
Of course, doing this manually is tedious, and as a result, it largely isn&amp;rsquo;t done.
The default behavior — and therefore the easiest and most common behavior — is to assume that shelled out commands worked and completely ignore failures.
To make our &amp;ldquo;foo&amp;rdquo; counting example well-behaved, we would have to wrap it in a function like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def foo_count(dir)
  n = `set -o pipefail;
       find #{Shellwords.shellescape(dir)} -type f -print0  | xargs -0 grep foo | wc -l`.to_i
  raise("pipeline failed") unless $?.success?
  return n
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function behaves the way we would like it to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;irb(main):026:0&amp;gt; foo_count("src")
=&amp;gt; 5
irb(main):027:0&amp;gt; foo_count("source code")
=&amp;gt; 5
irb(main):028:0&amp;gt; foo_count("nonexistent")
find: `nonexistent': No such file or directory
RuntimeError: pipeline failed
    from (irb):5:in `foo_count'
    from (irb):13
    from :0
irb(main):029:0&amp;gt; foo_count("foo'; echo MALICIOUS ATTACK; echo '")
find: `foo\'; echo MALICIOUS ATTACK; echo \'': No such file or directory
RuntimeError: pipeline failed
    from (irb):5:in `foo_count'
    from (irb):14
    from :0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;However, this 6-line, 200-character function is a far cry from the clarity and brevity we started with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;`find #{dir} -type f -print0 | xargs -0 grep foo | wc -l`.to_i
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If most programmers saw the longer, safer version of this in a program, they&amp;rsquo;d probably wonder why someone was writing such verbose, cryptic code to get something so simple and straightforward done.&lt;/p&gt;

&lt;h2 id="Summary+and+Remedy"&gt;Summary and Remedy&lt;/h2&gt;

&lt;p&gt;To sum it up, shelling out is great, but making code that shells out bug-free, secure, and not prone to silent failures requires three things that typically aren&amp;rsquo;t done:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Shell-escaping all values used to construct commands&lt;/li&gt;
&lt;li&gt;Prefixing each multi-command pipeline with &amp;ldquo;&lt;code&gt;set -o pipefail;&lt;/code&gt;&amp;rdquo;&lt;/li&gt;
&lt;li&gt;Explicitly checking for failure after each shelled out command.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;The trouble is that after doing all of these things, shelling out is no longer terribly convenient, and the code becomes annoyingly verbose.
In short, shelling out responsibly kind of sucks.&lt;/p&gt;

&lt;p&gt;As is so often the case, the root of all of these problems is relying on a middleman rather than doing things yourself.
If a program constructs and executes pipelines itself, it remains in control of all the subprocesses, can determine their individual exit conditions, automatically handle errors appropriately, and give accurate, comprehensive diagnostic messages when things go wrong.
Moreover, without a shell to interpret commands, there is also no shell to treat metacharacters specially, and therefore no danger of metacharacter brittleness.
&lt;a href="http://python.org/"&gt;Python&lt;/a&gt; gets this right:
using &lt;a href="http://docs.python.org/library/os.html#os.popen"&gt;&lt;code&gt;os.popen&lt;/code&gt;&lt;/a&gt; to shell out is officially deprecated, and the recommended way to call external programs is to use the &lt;a href="http://docs.python.org/library/subprocess.html"&gt;&lt;code&gt;subprocess&lt;/code&gt;&lt;/a&gt; module, which spawns external programs without using a shell.
Constructing pipelines using &lt;code&gt;subprocess&lt;/code&gt; &lt;a href="http://docs.python.org/library/subprocess.html#replacing-shell-pipeline"&gt;can be a little verbose&lt;/a&gt;, but it is safe and avoids all the problems that shelling out is prone to.
In my &lt;a href="/blog/2013/04/put-this-in-your-pipe/"&gt;followup post&lt;/a&gt;, I will describe how Julia makes constructing and executing pipelines of external commands as safe as Python&amp;rsquo;s &lt;code&gt;subprocess&lt;/code&gt; and as convenient as shelling out.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/JuliaLang/~4/0H6a-4z6Wno" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://julialang.org/blog/2012/03/shelling-out-sucks</feedburner:origLink></entry>
 
 <entry>
   <title>Stanford Talk Video</title>
   <link href="http://feedproxy.google.com/~r/JuliaLang/~3/tetgEe_JhbE/stanford-talk-video" />
   <updated>2012-03-01T00:00:00-08:00</updated>
   <id>http://julialang.org/blog/2012/03/stanford-talk-video</id>
   <content type="html">&lt;p&gt;Jeff gave his &lt;a href="/blog/2012/02/talk-announcement/"&gt;previously announced&lt;/a&gt;, invited talk at Stanford yesterday and the video is &lt;a href="http://ee380.stanford.edu/cgi-bin/videologger.php?target=120229-ee380-300.asx"&gt;available here&lt;/a&gt;.
Congrats, Jeff!&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/JuliaLang/~4/tetgEe_JhbE" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://julialang.org/blog/2012/03/stanford-talk-video</feedburner:origLink></entry>
 
 <entry>
   <title>Stanford Talk Announcement</title>
   <link href="http://feedproxy.google.com/~r/JuliaLang/~3/vjpljHmVJy8/talk-announcement" />
   <updated>2012-02-27T00:00:00-08:00</updated>
   <id>http://julialang.org/blog/2012/02/talk-announcement</id>
   <content type="html">&lt;p&gt;I will be speaking about Julia at the
&lt;a href="http://www.stanford.edu/class/ee380/"&gt;Stanford EE Computer Systems Colloquium&lt;/a&gt;
on Wednesday, February 29 at 4:15PM PST.
The title of the talk is &lt;em&gt;Julia: A Fast Dynamic Language For Technical Computing&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Abstract:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Julia is a general-purpose, high-level, dynamic language, designed from the start to take advantage of techniques for executing dynamic languages at statically-compiled language speeds. As a result the language has a more powerful type system, and generally provides better type information to the compiler.&lt;/p&gt;

&lt;p&gt;Julia is especially good at running MATLAB and R-style programs. Given its level of performance, we envision a new era of technical computing where libraries can be developed in a high-level language instead of C or FORTRAN. We have also experimented with cloud API integration, and begun to develop a web-based, language-neutral platform for visualization and collaboration. The ultimate goal is to make cloud-based supercomputing as easy and accessible as Google Docs.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Speaker Bio:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Jeff Bezanson has been developing the Julia language for two and a half years with a small distributed team of collaborators. Previously, he worked as a software engineer at Interactive Supercomputing, which developed the Star-P parallel extension to MATLAB. At the company, Jeff was a principal developer of &amp;ldquo;M#&amp;rdquo;, an implementation of the MATLAB language running on .NET. He is now a second-year graduate student at MIT. Jeff received an A.B. in Computer Science from Harvard University in 2004, and has experience with applications of technical computing in medical imaging.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;The talk will be webcast live.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edit:&lt;/strong&gt; the video of the talk can be &lt;a href="http://ee380.stanford.edu/cgi-bin/videologger.php?target=120229-ee380-300.asx"&gt;found here&lt;/a&gt;.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/JuliaLang/~4/vjpljHmVJy8" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://julialang.org/blog/2012/02/talk-announcement</feedburner:origLink></entry>
 
 <entry>
   <title>Why We Created Julia</title>
   <link href="http://feedproxy.google.com/~r/JuliaLang/~3/E7TOUPv4r60/why-we-created-julia" />
   <updated>2012-02-14T00:00:00-08:00</updated>
   <id>http://julialang.org/blog/2012/02/why-we-created-julia</id>
   <content type="html">&lt;p&gt;In short, because we are greedy.&lt;/p&gt;

&lt;p&gt;We are power Matlab users.
Some of us are Lisp hackers.
Some are Pythonistas, others Rubyists, still others Perl hackers.
There are those of us who used Mathematica before we could grow facial hair.
There are those who still can&amp;rsquo;t grow facial hair.
We&amp;rsquo;ve generated more R plots than any sane person should.
C is our desert island programming language.&lt;/p&gt;

&lt;p&gt;We love all of these languages;
they are wonderful and powerful.
For the work we do — scientific computing, machine learning, data mining, large-scale linear algebra, distributed and parallel computing — each one is perfect for some aspects of the work and terrible for others.
Each one is a trade-off.&lt;/p&gt;

&lt;p&gt;We are greedy: we want more.&lt;/p&gt;

&lt;p&gt;We want a language that&amp;rsquo;s open source, with a liberal license.
We want the speed of C with the dynamism of Ruby.
We want a language that&amp;rsquo;s homoiconic, with true macros like Lisp, but with obvious, familiar mathematical notation like Matlab.
We want something as usable for general programming as Python,
as easy for statistics as R,
as natural for string processing as Perl,
as powerful for linear algebra as Matlab,
as good at gluing programs together as the shell.
Something that is dirt simple to learn, yet keeps the most serious hackers happy.
We want it interactive and we want it compiled.&lt;/p&gt;

&lt;p&gt;(Did we mention it should be as fast as C?)&lt;/p&gt;

&lt;p&gt;While we&amp;rsquo;re being demanding, we want something that provides the distributed power of Hadoop — without the kilobytes of boilerplate Java and XML;
without being forced to sift through gigabytes of log files on hundreds of machines to find our bugs.
We want the power without the layers of impenetrable complexity.
We want to write simple scalar loops that compile down to tight machine code using just the registers on a single CPU.
We want to write &lt;code&gt;A*B&lt;/code&gt; and launch a thousand computations on a thousand machines, calculating a vast matrix product together.&lt;/p&gt;

&lt;p&gt;We never want to mention types when we don&amp;rsquo;t feel like it.
But when we need polymorphic functions, we want to use generic programming to write an algorithm just once and apply it to an infinite lattice of types;
we want to use multiple dispatch to efficiently pick the best method for all of a function&amp;rsquo;s arguments, from dozens of method definitions, providing common functionality across drastically different types.
Despite all this power, we want the language to be simple and clean.&lt;/p&gt;

&lt;p&gt;All this doesn&amp;rsquo;t seem like too much to ask for, does it?&lt;/p&gt;

&lt;p&gt;Even though we recognize that we are inexcusably greedy, we still want to have it all.
About two and a half years ago, we set out to create the language of our greed.
It&amp;rsquo;s not complete, but it&amp;rsquo;s time for a 1.0 release — the language we&amp;rsquo;ve created is called &lt;a href="/"&gt;Julia&lt;/a&gt;.
It already delivers on 90% of our ungracious demands, and now it needs the ungracious demands of others to shape it further.
So, if you are also a greedy, unreasonable, demanding programmer, we want you to give it a try.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/JuliaLang/~4/E7TOUPv4r60" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://julialang.org/blog/2012/02/why-we-created-julia</feedburner:origLink></entry>
 
 
</feed>
