1

我有一个执行以下功能的for循环:

取一个 M x 8 矩阵和:

  1. 将其拆分为大小为 512 个元素的块(表示 X 乘以 8 的矩阵 == 512,元素个数可以是 128,256,512,1024,2048)
  2. 将块重塑为 1 x 512(元素数)矩阵。
  3. 取矩阵的最后 1/4 放在前面,
    例如Data = [Data(1,385:512),Data(1,1:384)];

以下是我的代码:

for i = 1 : NumOfBlock  
    if i == 1  
        Header = tempHeader(1:RowNeeded,:);  
        Header = reshape(Header,1,BlockSize); %BS  
        Header = [Header(1,385:512),Header(1,1:384)]; %CP  
        Data = tempData(1:RowNeeded,:);  
        Data = reshape(Data,1,BlockSize); %BS  
        Data = [Data(1,385:512),Data(1,1:384)]; %CP  
        start = RowNeeded + 1;  
        end1 = RowNeeded * 2;  
    else  
        temp = tempData(start:end1,:);  
        temp = reshape(temp,1,BlockSize); %BS  
        temp = [temp(1,385:512),temp(1,1:384)]; %CP  
        Data = [Data, temp];  
    end

    if i <= 127 & i > 1
        temp = tempHeader(start:end1,:);
        temp = reshape(temp,1,BlockSize); %BS
        temp = [temp(1,385:512),temp(1,1:384)]; %CP
        Header = [Header, temp];
    end

    start = end1 + 1;
    end1=end1 + RowNeeded;  
end

使用 500 万个元素运行此循环将需要 1 个多小时。我需要它尽可能快(以秒为单位)。这个循环可以被矢量化吗?

4

4 回答 4

4

根据您的功能描述,这是我想出的:

M = 320;           %# M must be divisble by (numberOfElements/8)
A = rand(M,8);     %# input matrix

num = 512;         %# numberOfElements
rows = num/8;      %# rows needed

%# equivalent to taking the last 1/4 and putting it in front
A = [A(:,7:8) A(:,1:6)];

%# break the matrix in blocks of size (x-by-8==512) into the third dimension
B = permute(reshape(A',[8 rows M/rows]),[2 1 3]);

%'# linearize everything
B = B(:);

该图可能有助于理解上述内容:

图表

于 2010-02-22T19:56:52.207 回答
3

矢量化可能有帮助,也可能没有帮助。知道瓶颈在哪里会有所帮助。使用此处概述的分析器:

http://blogs.mathworks.com/videos/2006/10/19/profiler-to-find-code-bottlenecks/

于 2010-02-22T16:16:22.927 回答
0

如果您能说出您要做什么,那就太好了(我的猜测是动态系统中的一些模拟,但很难说)。

是的,当然可以矢量化:每个块实际上是四个子块;使用您的(非常非标准的)索引:

1...128、129...256、257...384、385...512

向量化的每个内核/线程/无论你怎么称呼它都应该执行以下操作:

i = threadIdx 在 0 到 127 之间 temp = data[1 + i] data[1 + i] = data[385+i] data[385 + i] = data[257+i] data[257 + i] = data [129+i] 数据[129 + i] = 温度

您当然还应该对块进行并行化,而不仅仅是矢量化。

于 2010-02-22T14:04:14.610 回答
0

再次感谢 Amro 给了我关于如何解决我的问题的想法。很抱歉没有在这个问题上说清楚。

这是我对我的问题的解决方案:

%#BS CDMA, Block size 128,512,1024,2048  
  BlockSize = 512;  
  RowNeeded = BlockSize / 8;  
  TotalRows = size(tempData);  
  TotalRows = TotalRows(1,1);  
  NumOfBlock = TotalRows / RowNeeded;  
  CPSize = BlockSize / 4;  

%#spilt into blocks  
  Header = reshape(tempHeader',[RowNeeded,8, 128]);  
  Data = reshape(tempData',[RowNeeded,8, NumOfBlock]);  
  clear tempData tempHeader;  

%#block spread & cyclic prefix  
    K = zeros([1,BlockSize,128],'single');  
    L = zeros([1,BlockSize,NumOfBlock],'single');  
       for i = 1:NumOfBlock  
           if i <= 128  
              K(:,:,i) = reshape(Header(:,:,i),[1,BlockSize]);  
              K(:,:,i) = [K((CPSize*3)+1:BlockSize),K(1:CPSize*3)];
           end  
           L(:,:,i) = reshape(Data(:,:,i),[1,BlockSize]);  
           L(:,:,i) = [L((CPSize*3)+1:BlockSize),L(1:CPSize*3)];
        end
于 2010-02-23T12:12:41.180 回答