Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在OpenMP之后连接专用阵列_Arrays_C_Concatenation_Openmp - Fatal编程技术网

Arrays 在OpenMP之后连接专用阵列

Arrays 在OpenMP之后连接专用阵列,arrays,c,concatenation,openmp,Arrays,C,Concatenation,Openmp,我正在使用OpenMP并行化下面的代码 int *neighbors = malloc(num_of_neigh * sizeof(int)); int pos = 0; for(int i=0;i<n;i++){ if(M[n*element+i]==1 && element!=i){ neighbors[pos] = i; pos++; } } 因此,我决定对邻居和位置使用局部变量,并在计算后将私有变量连接到全局变量。但

我正在使用OpenMP并行化下面的代码

int *neighbors = malloc(num_of_neigh * sizeof(int));
int pos = 0;
for(int i=0;i<n;i++){
    if(M[n*element+i]==1 && element!=i){
        neighbors[pos] = i;
        pos++;
    }
}
因此,我决定对
邻居
位置
使用局部变量,并在计算后将私有变量连接到全局变量。但是,我对这些连接有一些问题

int *neighbors = malloc(num_of_neigh * sizeof(int));
int pos = 0;
#pragma omp parallel num_threads(8)
{
    int *temp_neighbors = malloc(num_of_neigh * sizeof(int));
    int temp_pos = 0;
    #pragma omp for
    for(int i=0;i<n;i++){
        if(M[n*element+i]==1 && element!=i){
            temp_neighbors[temp_pos] = i;
            temp_pos++;
        }
    }

    // I want here to concatenate the local variables temp_neighbors to the global one neighbors.
}
因此,问题是: 如何将私有变量(尤其是数组)连接到全局变量?
我找了很多,但没有找到合适的答案。提前感谢,对于这个大问题,我很抱歉。

全局部分应该如下所示

#pragma-omp-critical
{
memcpy(邻居+位置、临时邻居、临时位置*大小(int));
职位+=临时职位;
}

memcpy
temp\u pos
元素(
temp\u pos*sizeof(int)
字节)的数据复制到
邻居中第一个未使用的位置(
邻居+pos
)。然后,
pos
被增加为下一个未使用位置的索引。

从一开始就拥有一个全局数组,让每个线程知道它应该使用该共享数组的哪个部分,不是更容易(也更有效)吗?@Paul如果我们在开始循环之前知道每个线程需要多少元素,一个全局阵列肯定会更容易、更快。
int *neighbors = malloc(num_of_neigh * sizeof(int));
int pos = 0;
#pragma omp parallel num_threads(8)
{
    int *temp_neighbors = malloc(num_of_neigh * sizeof(int));
    int temp_pos = 0;
    #pragma omp for
    for(int i=0;i<n;i++){
        if(M[n*element+i]==1 && element!=i){
            temp_neighbors[temp_pos] = i;
            temp_pos++;
        }
    }

    // I want here to concatenate the local variables temp_neighbors to the global one neighbors.
}
#pragma omp critical
{
    memcpy(neighbors+pos, temp_neighbors, num_of_neigh * sizeof(neighbors));
    pos++;
}