在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:lindahua/Devectorize.jl开源软件地址:https://github.com/lindahua/Devectorize.jl开源编程语言:Julia 100.0%开源软件介绍:Devectorize -- A Julia Framework for De-vectorized EvaluationDevectorize is a Julia framework, which provides macros and functions to de-vectorize a vector expression. With Devectorize, users can write computations in high-level vectorized way and at the same time enjoying the high run-time performance of de-vectorized loops. Devectorize automatically translates vectorized expressions into faster tight-loops, which often results in 2x - 8x performance gain. Why DevectorizeIn many programming languages (including Julia), expressions are immediately evaluated upon construction. This simple strategy often results in less than optimal behaviors, which, for example, include creation of unnecessary temporaries and repeated memory round-trips. Consider the following example, r = a .* b + c .* d + a With immediate evaluation, three temporaries, respectively for storing the results of For the formula above, a much more efficient way to evaluate it can be expressed using for-loops as follows n = length(a)
r = zeros(n)
for i = 1 : n
r[i] = a[i] * b[i] + c[i] * d[i] + a[i]
end With this piece of code, you can get all the results in one pass, without creating any temporary arrays. However, low-level for-loops are often much longer and more difficult to read, write, and maintain.
The answer is Yes. Let's look at the examples above, we can hold off the evaluation of all the temporaries until the assignment to The powerful meta-programming framework of Julia makes it possible to achieve this goal using incredibly simple syntax. Taking advantage of this framework, Devectorize provides a macro @devec r = a .* b + c .* d + a This statement is exactly the same as the one we saw above -- except for the macro The remaining part is organized into two section: Basic Usage, which introduces how to use Devectorize to improve the performance of your code, and Design of the Framework, which provides a brief overview of the framework and its structures. Basic UsageYou may install the package using Julia's official package manager: Pkg.add("Devectorize") To keep it always updated, you may have to switch Devectorize to the For ordinary use, you only have to remember one macro -- @devec r = exp(a + b) .* sum(c) To inspect the code generated by Devectorize, you can use the @inspect_devec r = exp(a + b) .* sum(c) This statement will prints the generated codes (prior to evaluating them). BenchmarkHere is a table of benchmark results on some typical cases.
The result was obtained with Julia Here, we use vectorized Julia code as the baseline, and report the performance gains (for example, if the baseline takes 1 sec, and devec takes 0.5sec, then the gain is 2x). We can see that codes tagged with the It is important to note that Devectorize only recognizes a subset of expressions of Julia (but the most commonly used subset), as listed below. Element-wise map of numbers and arrays@devec r = a + b + c
@devec r = sin(a) + exp(a + 1.0) .* log(c) Here is the list of operators and functions currently supported by Devectorize: +, -, .+, .-, .*, ./, .^, max, min, clamp, blend,
.==, .!=, .<, .>, .<=, .>=,
sqrt, cbrt, sqr, rcp, floor, ceil, round, trunc,
exp, log, log10, exp2, log2, expm1, log1p,
sin, cos, tan, asin, acos, atan,
sinh, cosh, tanh, asinh, acosh, atanh,
erf, erfc, gamma, lgamma, digamma Notes:
Simple references@devec r = x[:,1] + y[:,2]
@devec r = a[i,:] .* b
@devec r[:,j] = x + sin(a[:,j]) Simple reference here means the reference expressions in either of the following forms: ```a[:], a[i], a[i, j], a[:, :], a[:, i], a[i, :]``, where i can be either an integer or a symbol that refers to an integer variable. Reference expressions can appear in both left and right hand side of an assignment. Support of more flexible references is planned for future releases. Note that when you write r = a + b .* c Devectorize will emit codes that creates an array to store the results and bound it to
Then, the results will be directly written to Op-assignment@devec r += a
@devec r[:,i] .*= sin(a) Devectorize will automatically translate them into ordinary assignment expressions. Full/Colwise/Rowwise reduction@devec r = sum(a + b)
@devec r = maximum(sin(a), 1)
@devec r[:,j] = mean(a, 2) Devectorize currently recognizes five reduction functions Hybrid expressionsConsider the example below, @devec r = (a - sum(a)) .* b This seemingly simple expression actually requires two loops to evaluate, one for computing @devec tmp1 = sum(a)
@devec r = (a - tmp1) .* b Note that Devectorize only breaks expressions only when it is really necessary to do so, and tries to generate as few memory traversals as possible. Block expressions@devec begin
a = sin(x) - cos(y)
b = sum(a) + exp(z)
c = x .* y - maximum(b)
end In current implementation, Devectorize simply de-vectorizes each assignment expression respectively. In future version, it may use a more sophisticated algorithm to identify opportunities of sharing some computation across expressions. Design of the FrameworkIn Devectorize, the process of translating a Julia expression into de-vectorized codes goes through two stages:
Typed expressionsJulia front-end parses any input expression into an instance of To express the semantics of an expression, Devectorize establishes a type hierarchy in
The function The analysis performed in ContextsTo make the framework extensible, Devectorize introduces the notion of Context, which refers to a specific setting in which the codes are generated (e.g. CPU, SIMD, CUDA, OpenCL, etc) The abstract type EvalContext (in In future, other contexts might be introduced (e.g. SIMD and CUDA), thus providing users options to choose specific ways to emit the evaluation code for their expressions. CompilationThe function To reduce the complexity of back-end implementation, the
After this processing, the back-end can be implemented in a much simpler way, without taking into account such intricacies. The functions to generate codes for Code compositionThe routines in Take the expression For The |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论