简体   繁体   English

C ++并行化:快速“重新初始化”数组

[英]C++ Parallelization: Fast way to “reinitialize” array

I am doing a parallel for loop in OpenMP. 我在OpenMP中进行并行for循环。

Here is my problem: I have a 4D array, where the 4th dimension's depth is a maximum value--I might not use all of the elements of the 4th dimension. 这是我的问题:我有一个4D数组,其中第4维的深度是最大值 - 我可能不会使用第4维的所有元素。 In this array, I store indices of another array (I calculate the indices in a parallel loop). 在这个数组中,我存储另一个数组的索引(我在并行循环中计算索引)。

So, 所以,

int my_index_array[dim_x][dim_y][dim_z][max_count];

At the same time, in the parallel loop, I am recording the number of elements stored in each 4th dimension vector (ie, my_index_array[0][0][0][:] ) in another integer array: 同时,在并行循环中,我在另一个整数数组中记录存储在每个第四维向量中的元素数(即my_index_array[0][0][0][:] ):

int my_index_counts[dim_x][dim_y][dim_z] = {(int) 0}

Such that I can create parallel for loops later using the values from my_index_counts. 这样我以后可以使用my_index_counts中的值创建并行for循环。


So here is my problem: 所以这是我的问题:

I need an optimal, super fast way to "re-initialize" the index counts when I come back around to the top of the outer loop. 当我回到外部循环的顶部时,我需要一种最佳的,超快速的方法来“重新初始化”索引计数。 Ideally, I would not loop through the index counts array and reset the values. 理想情况下,我不会遍历索引计数数组并重置值。 But, in C++, I can't reinitialize the array. 但是,在C ++中,我无法重新初始化数组。 I found "memcpy," and have the space to create another "zeroed" array to copy from, but is that the optimal way to do this? 我找到了“memcpy”,并且有空间来创建另一个“归零”数组来复制,但这是最好的方法吗?

For array initialization you may use memset , it is suitable for continuous memory blocks. 对于数组初始化,您可以使用memset ,它适用于连续内存块。 Your code should be something like this: 你的代码应该是这样的:

memset(&my_index_counts[x][y][z], 0, max_count*sizeof(int));

where x,y,z are indices to all but the last dimension 其中x,y,z是除最后一个维度之外的所有索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM