【问题标题】:Sorting coordinates of point cloud in accordance with X, Y or Z value根据 X、Y 或 Z 值对点云坐标进行排序
【发布时间】:2014-12-31 21:25:54
【问题描述】:

A 是一系列 3D 坐标点 (X,Y,Z),例如:

>> A = [1 2 0;3 4 7;5 6 9;9 0 5;7 8 4]

A = 1 2 0 3 4 7 5 6 9 9 0 5 7 8 4

我想根据"Y" (second column) 值对矩阵进行排序。

这是我正在使用的代码:

>> tic;[~, loc] = sort(A(:,2)); SortedA = A(loc,:) toc;

SortedA = 9 0 5 1 2 0 3 4 7 5 6 9 7 8 4

Elapsed time is **0.001525** seconds.

但是,对于大量数据,它可能会非常慢。如果有人知道更有效的方法,我将不胜感激。

【问题讨论】:

  • 如果你有一个不错的 GPU,你可以利用它来很好地加速,只需一些信息。
  • @Divakar 是否需要更改我电脑中的任何设置?
  • 您只需要在 PCT 工具箱中使用gpuArray,但您有合适的 GPU 使用吗?
  • @Divakar 是的,伙计,我得到了“NVIDIA GeForce GTX 645”,应该没问题。对吗?
  • 添加了GPU based solution,看看你的结果如何!

标签: arrays performance matlab sorting


【解决方案1】:

介绍性讨论

此答案主要讨论如何利用计算效率高的GPU 来解决所述问题。问题中所述问题的解决方案代码是 -

[~, loc] = sort(A(:,2));
SortedA = A(loc,:);

它基本上有两个部分-

  1. 选择第二列,对它们进行排序并获得排序后的索引。
  2. 使用排序后的索引对输入矩阵的行进行索引。

现在,Part 1 是计算密集型的,可以移植到 GPU,但 Part 2 作为索引工作,可以在 CPU 本身上完成。

建议的解决方案

因此,考虑到所有这些,一个有效的GPU 解决方案将是 -

gA = gpuArray(A(:,2)); %// Port only the second column of input matrix to GPU
[~, gloc] = sort(gA); %// compute sorted indices on GPU
SortedA = A(gather(gloc),:); %// get the sorted indices back to CPU with `gather` 
                             %// and then use them to get sorted A

基准测试

接下来是比较 GPU 版本与原始解决方案的基准代码,但请记住,由于我们在不同硬件上运行 GPU 代码,而不是在 @ 上运行的原始解决方案987654331@,基准测试结果可能因系统而异。

这是基准代码 -

N = 3000000; %// datasize (number of rows in input)
A = rand(N,3); %// generate random large input

disp('------------------ With original solution on CPU')
tic
[~, loc] = sort(A(:,2));
SortedA = A(loc,:);
toc, clear SortedA loc

disp('------------------ With proposed solution on GPU')
tic
gA = gpuArray(A(:,2));
[~, gloc] = sort(gA);
SortedA = A(gather(gloc),:);
toc

这是基准测试结果 -

------------------ With original solution on CPU
Elapsed time is 0.795616 seconds.
------------------ With proposed solution on GPU
Elapsed time is 0.465643 seconds.

所以,如果您有足够好的GPU,那么现在是时候尝试GPU 来解决相关问题,而MATLAB 提供如此简单的GPU 移植解决方案更是如此。


系统配置

MATLAB Version: 8.3.0.532 (R2014a)
Operating System: Windows 7
RAM: 3GB
CPU Model: Intel® Pentium® Processor E5400 (2M Cache, 2.70 GHz)
GPU Model: GTX 750Ti 2GB

【讨论】:

  • 就是这样。你太棒了!
【解决方案2】:

MATLAB 确实有一个名为 sortrows() 的功能可以做到这一点,但根据我的经验,它往往和你对一般非结构化矩阵所做的一样慢。

测试:

N = 1e4;
A = rand(N,N);
tic;[~, loc] = sort(A(:,2));
SortedA = A(loc,:);
toc;

tic; sortrows(A,2); toc;

给予:

Elapsed time is 0.515903 seconds.
Elapsed time is 0.525725 seconds.

【讨论】:

  • 我看到了更大的不同。如果您只考虑对 1 列进行排序,显然 sortrows 并不是那么好。最好按特定顺序考虑多个列。
  • @chappjc,很有趣。我刚刚阅读了它的源代码,事实证明,只有当列维度大于 4 时,sortrows 才会调用 C 包装器。否则,它只会执行 OP 所做的事情。
  • 在 R2014 中它超过 3 (sortrowsc),否则它会调用 sortrows>sort_back_to_front,就像你说的 OP 一样。我怀疑有比他正在做的更快的方法……这是标准方法是有原因的。 :)
【解决方案3】:

试试sortrows,指定第2列:

Asorted = sortrows(A,2)

更简单,但现在我测试它实际上更慢...如果您只考虑对 1 列进行排序,显然sortrows 并不是那么好。按特定顺序考虑多列可能是最好的。

【讨论】:

  • 谢谢,它看起来很有用。但是,我会留下这个问题,希望得到更快的解决方案。
猜你喜欢
  • 1970-01-01
  • 2018-05-15
  • 2022-12-10
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
  • 2021-10-15
  • 1970-01-01
相关资源
最近更新 更多