我有一个数组X,我想将一个函数f应用于所有行X:
# silly example
X = numpy.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 0]], 'i')
def f(row): return sum(row)
y = numpy.vectorize(f, 'i')(rows(X))
Run Code Online (Sandbox Code Playgroud)
现在,y应该是array([15,30], 'i').哪种方法或切片魔术将以rows最有效的方式实施?
是否有矢量化方式来执行以下操作?(以示例显示):
input_lengths = [ 1 1 1 4 3 2 1 ]
result = [ 1 2 3 4 4 4 4 5 5 5 6 6 7 ]
Run Code Online (Sandbox Code Playgroud)
我已经间隔了input_lengths,因此很容易理解如何获得结果
合成矢量的长度为:sum(lengths).我目前result使用以下循环计算:
result = ones(1, sum(input_lengths ));
counter = 1;
for i = 1:length(input_lengths)
start_index = counter;
end_index = counter + input_lengths (i) - 1;
result(start_index:end_index) = i;
counter = end_index + 1;
end
Run Code Online (Sandbox Code Playgroud)
编辑:
我也可以使用arrayfun(虽然这不是一个矢量化函数)
cell_result = arrayfun(@(x) repmat(x, 1, input_lengths(x)), 1:length(input_lengths), 'UniformOutput', false);
cell_result : …Run Code Online (Sandbox Code Playgroud) dummies = matrix(c(0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0), nrow=6, ncol=6)
colnames(dummies) <- c("a","b", "c", "d", "e", "f")
Run Code Online (Sandbox Code Playgroud)
我有一个假人矩阵
> dummies
a b c d e f
[1,] 0 0 0 0 1 0
[2,] 0 0 1 0 0 0
[3,] 1 0 0 0 0 0
[4,] 0 0 …Run Code Online (Sandbox Code Playgroud) 我尝试对 16 位整数 ARGB 通道的 64 位颜色的预乘进行矢量化。
我很快意识到,由于缺乏加速整数除法支持,我需要将我的值转换为float并显式使用一些 SSE2/SSE4.1 内在函数以获得最佳性能。尽管如此,我还是想保留非特定的通用版本作为后备解决方案(我知道它目前比某些普通操作慢,但它将提供未来可能的改进的兼容性)。
但是,在我的机器上结果不正确。
一个非常小的重现:
// Test color with 50% alpha
(ushort A, ushort R, ushort G, ushort B) c = (0x8000, 0xFFFF, 0xFFFF, 0xFFFF);
// Minimal version of the fallback logic if HW intrinsics cannot be used:
Vector128<uint> v = Vector128.Create(c.R, c.G, c.B, 0u);
v = v * c.A / Vector128.Create(0xFFFFu);
var cPre = (c.A, (ushort)v[0], (ushort)v[1], (ushort)v[2]);
// Original color:
Console.WriteLine(c); // prints (32768, 65535, 65535, 65535)
// Expected …Run Code Online (Sandbox Code Playgroud) 我有一堆时间序列,每个时间序列由两个组件描述,一个时间戳向量(以秒为单位),以及一个测量值向量.时间向量是不均匀的(即以非常规间隔采样)
我试图计算每个1分钟间隔值的平均值/ SD(采用X分钟间隔,计算其平均值,采用下一个间隔,......).
我当前的实现使用循环.这是我到目前为止的样本:
t = (100:999)' + rand(900,1); %' non-uniform time
x = 5*rand(900,1) + 10; % x(i) is the value at time t(i)
interval = 1; % 1-min interval
tt = ( floor(t(1)):interval*60:ceil(t(end)) )'; %' stopping points of each interval
N = length(tt)-1;
mu = zeros(N,1);
sd = zeros(N,1);
for i=1:N
indices = ( tt(i) <= t & t < tt(i+1) ); % find t between tt(i) and tt(i+1)
mu(i) = mean( x(indices) );
sd(i) = std( x(indices) …Run Code Online (Sandbox Code Playgroud) 无论如何以SIMD方式"向量化"跨数组的元素添加?
例如,我想转:
var a = new[] { 1, 2, 3, 4 };
var b = new[] { 1, 2, 3, 4 };
var c = new[] { 1, 2, 3, 4 };
var d = new[] { 1, 2, 3, 4 };
var e = new int[4];
for (int i = 0; i < a.Length; i++)
{
e[i] = a[i] + b[i] + c[i] + d[i];
}
// e should equal { 4, 8, 12, 16 }
Run Code Online (Sandbox Code Playgroud)
变成这样的东西:
var e …Run Code Online (Sandbox Code Playgroud) 我理解在MATLAB这样的语言中使用矢量化是如何通过消除维护循环变量的开销来加速代码的,但是矢量化实际上是如何在汇编/机器代码中进行的?我的意思是在某个地方仍然必须有一个循环,对吗?
我有两个函数用于在Julia中以数字方式确定pi.第二个函数(我认为是矢量化的)比第一个函数慢.为什么矢量化速度较慢?是否有规则何时进行矢量化以及何时不进行?
function determine_pi(n)
area = zeros(Float64, n);
sum = 0;
for i=1:n
if ((rand()^2+rand()^2) <=1)
sum = sum + 1;
end
area[i] = sum*1.0/i;
end
return area
end
Run Code Online (Sandbox Code Playgroud)
和另一个功能
function determine_pi_vec(n)
res = cumsum(map(x -> x<=1?1:0, rand(n).^2+rand(n).^2))./[1:n]
return res
end
Run Code Online (Sandbox Code Playgroud)
当运行n = 10 ^ 7时,以下是执行时间(运行几次后)
n=10^7
@time returnArray = determine_pi(n)
#output elapsed time: 0.183211324 seconds (80000128 bytes allocated)
@time returnArray2 = determine_pi_vec(n);
#elapsed time: 2.436501454 seconds (880001336 bytes allocated, 30.71% gc time)
Run Code Online (Sandbox Code Playgroud) 一个可重现的代码示例我正在尝试矢量化.
cutOffs <- seq(1,10,0.2)
plotOutput <- matrix(nrow=length(cutOffs), ncol=2)
colnames(plotOutput) <- c("x","y")
plotOutput[,"y"] <- cutOffs
for(plotPoint in 1:length(cutOffs))
{
plotOutput[plotPoint, "x"] <-
nrow(iris[ which(iris$Sepal.Length > cutOffs[plotPoint] &
iris$Sepal.Width > cutOffs[plotPoint]), ])
}
plotOutput
Run Code Online (Sandbox Code Playgroud)
特别是我想要找到的是,如果有一种方法来矢量化这部分.
nrow(iris[ which(iris$Sepal.Length > cutOffs[plotPoint] &
iris$Sepal.Width > cutOffs[plotPoint]), ])
Run Code Online (Sandbox Code Playgroud)
假设我是使用plyr库或某种形式的应用程序,可能没有太多加速,这正是我正在寻找的.从根本上说,我试图看看是否存在一些我在搜索时忽略或设法错过的矢量化技术.
更新:
Unit: milliseconds
expr min lq mean median uq max neval
op() 33663.39700 33663.39700 33663.39700 33663.39700 33663.39700 33663.39700 1
jr() 3976.53088 3976.53088 3976.53088 3976.53088 3976.53088 3976.53088 1
dd() 4253.21050 4253.21050 4253.21050 4253.21050 4253.21050 4253.21050 1
exp() 5085.45331 …Run Code Online (Sandbox Code Playgroud) Hennessy-Patterson关于计算机体系结构的书(定量方法5ed)表示,在具有多个存储体的矢量体系结构中,如果满足以下条件,则可能发生银行冲突(参见5):
(银行数量)/ LeastCommonMultiple(银行数量,步幅)<银行繁忙时间
但是,我认为它应该是GreatestCommonFactor而不是LCM,因为如果您拥有的有效银行数量少于繁忙时间,则会发生内存冲突.有效数量的银行我的意思是这个 - 假设你有8个银行,并且步幅为2个.那么实际上你有4个银行,因为内存访问只能在四个银行排队(例如,假设您的访问都是偶数,从0开始,然后你的访问将在0,2,4,6银行排队.
实际上,这个公式甚至不适用于它下面给出的例子.假设我们有8个存储器组,其繁忙时间为6个时钟周期,总存储器延迟为12个时钟周期,以1的步幅完成64个元素的向量加载需要多长时间? - 这里他们计算时间为12 + 64 = 76个时钟周期.但是,根据给定的条件会发生存储库冲突,因此我们显然不能在每个周期中进行一次访问(方程式中为64).
我是错了,还是错误的公式能够在本书的5个版本中生存(不太可能)?