vectorization

Intel's pragma simd vs OpenMP's pragma omp simd

China☆狼群 提交于 2019-12-13 14:17:00
问题 The Intel compiler allows us to vectorize loops via #pragma simd for ( ... ) However, you also have the option to do this with OpenMP 4's directive: #pragma omp simd for ( ... ) Is there any difference between the two? 回答1: For all intents and purposes they should be identical. The difference is that the OpenMP 4.0 #pragma omp simd directive is portable and should work with other compilers that support OpenMP 4.0 as well as Intel's. Furthemore, there are several clauses in the OpenMP version

Comparing single dataframe value to previous 10 in same column

醉酒当歌 提交于 2019-12-13 14:03:09
问题 In a dataframe, I would like to count how many of the prices from the previous 10 days are greater than today's price. Result would look like this: price ct>prev10 50.00 51.00 52.00 50.50 51.00 50.00 50.50 53.00 52.00 49.00 51.00 3 I have seen this post answered by DSM, but the requirement was different in that the base for comparison was a static number as opposed to the current row: Achieving "countif" with pd.rolling_sum() Of course I would like to do this without looping through 1x1.

Evenly spaced numbers between two sets (Vectorize LINSPACE) - MATLAB

有些话、适合烂在心里 提交于 2019-12-13 12:29:02
问题 How can I define a matrix M according to M=[a:(b-a)/5:b] (from a to b in 5 steps), when a and b are vectors or sets; more specifically, each row i in M should have a first value equal to a(i) and last value b(i) and, in between, 5 equal steps. For example, if I have a = [0; b = [10; 0]; 20]; I'd like to produce a matrix M of the form [0 2 4 6 8 10;... 0 4 8 12 16 20] I know how to do this using loops, but I'm looking for a solution without. How can I do that? 回答1: One vectorized approach with

Why is my Matlab for-loop code faster than my vectorized version

久未见 提交于 2019-12-13 11:48:36
问题 I had always heard that vectorized code runs faster than for loops in MATLAB. However, when I tried vectorizing my MATLAB code it seemed to run slower. I used tic and toc to measure the times. I changed only the implementation of a single function in my program. My vectorized version ran in 47.228801 seconds and my for-loop version ran in 16.962089 seconds. Also in my main program I used a large number for N, N = 1000000 and DataSet's size is 1 301 , and I ran each version several times for

Optimizing MATLAB code

こ雲淡風輕ζ 提交于 2019-12-13 11:34:45
问题 This code takes an extremely long time to run (more than 10 minutes). Is there any way in which I can optimize it so that it finishes in less than one minute? clear all; for i = 1:1000000 harmonicsum = 0; lhs = 0; for j = 1:i % compute harmonic sum harmonicsum = harmonicsum + 1/j; % find sum of factors if (mod(i,j)==0) lhs = lhs + j; end end %define right hand side (rhs) of Riemann Hypothesis rhs = harmonicsum + log(harmonicsum) * exp(harmonicsum); if lhs > rhs disp('Hypothesis violated') end

Vectorized array comparison in Fortran

余生颓废 提交于 2019-12-13 07:47:16
问题 I would like to perform the do-end do bit of the following pseudocode within Fortran using a single line statement: integer, parameter :: N = 1000 integer, dimension(1:N) :: ArrayA, ArrayB logical, dimension(1:N) :: ArrayL ... ... do i = 1, N if( ArrayA(i) <= ArrayB(i) ) then ArrayL(i) = .true. else ArrayL(i) = .false. end if end do Is this possible? If so, how do I do so? 回答1: integer, parameter :: N = 1000 integer, dimension(1:N) :: ArrayA, ArrayB logical, dimension(1:N) :: ArrayL ... ...

How can I cumulatively apply a custom function to a vector in R? In an efficient and idiomatic way?

て烟熏妆下的殇ゞ 提交于 2019-12-13 04:26:45
问题 I know the function cumsum in R which compute a cumulative sum of its vector argument. I need to "cumulatively apply" not the sum function but a generic function, in my specific case, the quantile function. My current solution is based on a loop: set.seed(42) df<-data.frame(measurement=rnorm(1000),upper=0,lower=0) for ( r in seq(1,nrow(df))){ df$upper[r]<-quantile(df[seq(1,r),"measurement"],c(.99)) df$lower[r]<-quantile(df[seq(1,r),"measurement"],c(.01)) } x=seq(1,nrow(df)) plot(df

Octave: How can I vectorize this function?

爱⌒轻易说出口 提交于 2019-12-13 04:00:00
问题 Can these for-loops of this function be vectorized? function [sta]=bootstrap(data,N,p) rand('state', sum(100*clock)); n=length(data); n1=round(prctile(1:n,(100-p)/2)); n2=round(prctile(1:n,p/2+50)); for i=1:N choose=round(((n-1)*rand(1,n))+1); for j=n1:n2 sample(j-n1+1,1)=data(choose(j)); end sta(i)=mean(sample); end 回答1: Yes you can, try to replace your loop with the code below: choose=round(((n-1)*rand(N,n))+1); sample(:,(n1:n2)-n1+1,1)=data(choose(:,n1:n2)); sta=mean(sample'); The point is

How to vectorize custom algorithms in numpy or pytorch?

五迷三道 提交于 2019-12-13 03:45:51
问题 Suppose I have two matrices: A: size k x m B: size m x n Using a custom operation, my output will be k x n. This custom operation is not a dot product between the rows of A and columns of B . Suppose this custom operation is defined as: For the Ith row of A and Jth column of B , the i,j element of the output is: sum( (a[i] + b[j]) ^20 ), i loop over I, j loops over J The only way I can see to implement this is to expand this equation, calculate each term, them sum them. Is there a way in

MATLAB How to fill individual entries of a sparse matrix using vectorised form?

╄→гoц情女王★ 提交于 2019-12-13 00:14:54
问题 I have a sparse matrix and I need to fill certain entries with a specific value, I am using a for loop right now but I know its not the correct way to do it so I was wondering if its possible to vectorise this for loop? K = sparse(N); for i=vectorofrandomintegers K(i,i) = 1; end If I vectorise it normally as so: K(A,A) = 1; then it fills all the entries in each row denoted by A whereas I want individual entries (i.e. K(1,1) = 1 or K(6,6)=1 ). Also, the entries are not diagonally adjacent so I