将2-Dim循环拆分为多个线程
Splitting 2-Dim loop into multiple threads
我的算法通过使用在U和V段上迭代的二维循环来计算三维空间中形状的顶点。
for (LONG i=0; i < info.useg + 1; i++) {
// Calculate the u-parameter.
u = info.umin + i * info.udelta;
for (LONG j=0; j < info.vseg + 1; j++) {
// Calculate the v-parameter.
v = info.vmin + j * info.vdelta;
// Compute the point's position.
point = calc_point(op, &info, u, v);
// Set the point to the object and increase the point-index.
points[point_i] = point;
point_i++;
}
}
然而,点的数组是一维数组,这就是为什么point_i
在每个循环中递增的原因。我知道我可以通过point_i = i * info.vseg + j
来计算索引。
我希望这个循环是多线程的。我的目标是创建多个线程,所有线程都处理特定范围的点。在线程中,我会这样做:
for (LONG x=start; x <= end; x++) {
LONG i = // ...
LONG j = // ...
Real u = info.umin + i * info.udelta;
Real v = info.vmin + j * info.vdelta;
points[i] = calc_point(op, &info, u, v);
}
问题是从线性点指数计算i
和j
指数。当(我认为):时,我如何计算i
和j
point_i = i * vsegments + j
我不会解数学题,这里。。
point_i = i * vsegments + j
为您提供:
i = point_i / vsegments
j = point_i % vsegments
当然,您的循环实际上每个都进行segments + 1
次迭代(索引0
到segments
),因此您需要使用vsegments + 1
而不是vsegments
顺便说一句:你真的需要把循环合并成一个多线程吗?我希望外循环通常有足够的迭代来饱和你的可用核心。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- 将执行循环拆分为多个线程 (1-N-1-N-1..)
- 跨线程共享数据集与拆分每个线程的数据
- 如何通过循环将 OpenMP 线程拆分为子组
- 使用哪个线程并行接口来使用堆栈进行任务共享和拆分
- 是否可以将Qt GUI拆分为多个线程用于GUI,仿真和OpenGL
- 将2-Dim循环拆分为多个线程