您的位置:首页 > 房产 > 建筑 > C# 计算两两坐标之间的距离(SIMD加速)

C# 计算两两坐标之间的距离(SIMD加速)

2024/12/23 10:35:42 来源:https://blog.csdn.net/ftfmatlab/article/details/140988902  浏览:    关键词:C# 计算两两坐标之间的距离(SIMD加速)

问题描述:对于Point[] points坐标数组(Point是坐标点,包含X与Y),请计算两两坐标点之间的距离。

思路:既然是算距离,没有别的要求,只需要最简单的曼哈顿距离就行。

测试数据:

Random random = new Random();
int n = 3000;
Point[] points = new Point[n];
for (int i = 0; i < n; i++)
{points[i] = new Point(random.Next(10), random.Next(10));
}

方法一:采用普通遍历法

Stopwatch sw = Stopwatch.StartNew();
int m = (n * n - n) / 2;
List<int> list2 = new List<int>(m);
for (int i = 0; i < n; i++)
{Point p= points[i];for (int j = i + 1; j < n; j++){Point p2 = points[j];int dif = Math.Abs(p.X - p2.X) + Math.Abs(p.Y - p2.Y);list2.Add(dif);}
}
sw.Stop();
Console.WriteLine($"普通耗时:{sw.ElapsedMilliseconds}ms");

方法二:采用SIMD加速

 public static int[] CalculateDistance(Point[] points){int n = points.Length;int m = (n * n - n) >> 1;int[] ints = new int[m];int k = 0;int vsize = Vector128<int>.Count;if (n <= vsize)// 如果数据量小于等于vsize采用指针处理{fixed (Point* ptr = points){for (int i = 0; i < n; i++){Point* p1 = ptr + i;for (int j = i + 1; j < n; j++){Point* p2 = ptr + j;int dif = Math.Abs(p1->X - p2->X) + Math.Abs(p1->Y - p2->Y);ints[k++] = dif;}}return ints;}}int t = 0;int[] xs = new int[n];int[] ys = new int[n];fixed (Point* ptr = points){fixed (int* xptr = xs, yptr = ys, rptr = ints){// 分离x与yfor (int i = 0; i < n; i++){Point* p = ptr + i;*(xptr + i) = p->X;*(yptr + i) += p->Y;}// 开始向量计算for (int i = 0; i < n; i += vsize){Vector128<int> vx1 = *(Vector128<int>*)(xptr + i);Vector128<int> vy1 = *(Vector128<int>*)(yptr + i);for (int j = i + 1; j < n; j++){Vector128<int> vx2 = *(Vector128<int>*)(xptr + j);Vector128<int> vy2 = *(Vector128<int>*)(yptr + j);// 向量运算var vDifference = Sse2.Add(Vector128.Abs(Sse2.Subtract(vx1, vx2)), Vector128.Abs(Sse2.Subtract(vy1, vy2)));int* maskPtr = (int*)&vDifference;int h = n - j;for (int a = 0; a < vsize; a++){if (a >= h) break;*(rptr + t++) = *(maskPtr + a);}}}}}return ints;}

注:根据CPU选择合适的Vector,比如Vector256、Vector512等等。本文只用Vector128.

测试结果(Release环境):

数据量3000500070001000030000
普通耗时(ms)431272115383872
向量耗时(ms)1956681901092

结论:明显向量运算性能优于普通计算。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com