-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fusion of nested f.(args) calls into a single broadcast call #17300
Changes from all commits
85d15b4
1eafcc9
9025f17
47f5e8a
0c4b67e
d8f4b60
4637b3c
8caf3fd
05bc2fa
4367fc6
61d2d39
fb8f1e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -640,6 +640,22 @@ then ``f.(pi,A)`` will return a new array consisting of ``f(pi,a)`` for each | |
consisting of ``f(vector1[i],vector2[i])`` for each index ``i`` | ||
(throwing an exception if the vectors have different length). | ||
|
||
Moreover, *nested* ``f.(args...)`` calls are *fused* into a single ``broadcast`` | ||
loop. For example, ``sin.(cos.(X))`` is equivalent to ``broadcast(x -> sin(cos(x)), X)``, | ||
similar to ``[sin(cos(x)) for x in X]``: there is only a single loop over ``X``, | ||
and a single array is allocated for the result. [In contrast, ``sin(cos(X))`` | ||
in a typical "vectorized" language would first allocate one temporary array for ``tmp=cos(X)``, | ||
and then compute ``sin(tmp)`` in a separate loop, allocating a second array.] | ||
This loop fusion is not a compiler optimization that may or may not occur, it | ||
is a *syntactic guarantee* whenever nested ``f.(args...)`` calls are encountered. Technically, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why "Technically" ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because this is a technical caveat (something that most users won't need to think too much about) to the broad informal statement at the beginning of the paragraph that nested dot calls are fused. |
||
the fusion stops as soon as a "non-dot" function is encountered; for example, | ||
in ``sin.(sort(cos.(X)))`` the ``sin`` and ``cos`` loops cannot be merged | ||
because of the intervening ``sort`` function. | ||
|
||
(In future versions of Julia, operators like ``.*`` will also be handled with | ||
the same mechanism: they will be equivalent to ``broadcast`` calls and | ||
will be fused with other nested "dot" calls.) | ||
|
||
Further Reading | ||
--------------- | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope brackets don't have special meaning in rst
I don't think we use square brackets for parenthetical comments elsewhere in the docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The usual typographical convention is to use square brackets for parenthetical comments that have nested parentheses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, I haven't come across that convention. Do you have a citation for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In math, the usual style is to nest parens as
{[(...)]}
. In text, it is common to recommend square inside round parens if you must nest, but that isn't possible here because the nested parens are code and hence are constrained by Julia syntax.http://blog.apastyle.org/apastyle/2013/05/punctuation-junction-parentheses-and-brackets.html
http://www.chicagomanualofstyle.org/16/ch12/ch12_sec026.html
http://www.chicagomanualofstyle.org/16/ch06/ch06_sec099.html