C openMP分段错误:使用if和else的单for循环

C openMP分段错误:使用if和else的单for循环,c,algorithm,matrix,openmp,C,Algorithm,Matrix,Openmp,我对一个函数有一个问题,这个函数把一个矩阵分解成一个矩阵,取决于它是大于还是小于一个中值点。这些函数在没有openMP的情况下可以正常工作,但是有了它,我可以在一段时间后得到分割错误。该函数用于三球算法,因此被多次调用。经过几次迭代后,它可能会崩溃,因为出于某种原因,它在pts矩阵中得到了错误的值。知道为什么吗 /****************************************************************************** * *

我对一个函数有一个问题,这个函数把一个矩阵分解成一个矩阵,取决于它是大于还是小于一个中值点。这些函数在没有openMP的情况下可以正常工作,但是有了它,我可以在一段时间后得到分割错误。该函数用于三球算法,因此被多次调用。经过几次迭代后,它可能会崩溃,因为出于某种原因,它在pts矩阵中得到了错误的值。知道为什么吗


 /******************************************************************************
 *
 *                          create_L_and_R():
 *
 * -    create two sets of points, L and R. The point smaller than the median 
 *      point goes to the left (L) and bigger (and including the point) goes to 
 *      the right (R).
 * 
 *  - input: 
 *      -n_dims: dimension of the points
 *      -n_points: number of points in the pts
 *      - median_point
 *      -pts: pts of the line 
 *      - Two empty arrays:
 *            L_split: empty array which will be filled with points
 *            R_split: empty array which will be filled with points, pluss the center point
 *****************************************************************************/

void create_L_and_R(int n_dims, int n_ponits, double** pts, double** L_split, double** R_split, double *median_point, int *counter_L, int *counter_R){
        int n = 0;
        printf("\nMedian: %f\n", median_point[0]);
        #pragma omp parallel for private(n) \
                shared(L_split, R_split, pts, counter_L, counter_R, median_point)
        for (n=0; n<n_ponits; n++){
                printf("pts[n][dim]: %f\n", pts[n][0]);
                if (pts[n][0] < median_point[0]){
                        L_split[*counter_L] =pts[n]; 
                        printf("L_split: %f\n", L_split[*counter_L][0]);
                        *counter_L = *counter_L + 1;
                }
                else{
                        R_split[*counter_R] = pts[n]; 
                        printf("R_split: %f\n", R_split[*counter_R][0]);
                        *counter_R = *counter_R + 1;
                 }

        }
        printf("\n");
        return;
}

   

/******************************************************************************
*
*创建_L_和_R():
*
*-创建两组点,L和R。该点小于中间值
*点向左(L)移动,较大的(包括点)移动到
*右边(右)。
* 
*-输入:
*-n_dims:点的尺寸
*-n_点数:pts中的点数
*-中间点
*-pts:线路的pts
*-两个空阵列:
*L_split:将用点填充的空数组
*R_split:将填充点的空数组,加上中心点
*****************************************************************************/
无效创建\u L\u和\u R(整数n\u dims、整数n\u ponits、双**点、双**L\u分割、双**R\u分割、双*中间点、整数*计数器、整数*计数器){
int n=0;
printf(“\n中间点:%f\n”,中间点[0]);
#pragma omp并行专用(n)\
共享(左分割、右分割、pts、计数器、计数器、中间点)

对于(n=0;n什么是
R_split[*counter_R]>=pts[n];
应该做什么?它比较两个指针,然后丢弃结果。至于崩溃,请尝试创建一个来向我们展示。您如何调用此函数?使用的索引的初始值是什么?指针的“数组”是如何初始化的?R_split[*counter_R]>=pts[n];不应该出现在代码中。现在更正了
*counter_R
*counter_L
上存在争用条件。但即使解决了这一问题,多个线程仍可以在递增之前写入同一位置。基本上,您必须将整个内容包装到一个关键部分,这将有效地顺序化它。要并行执行此操作,您需要一个不同的算法。您可以首先为每个线程创建一个位置数组,以便开始写入。问题是,这是否真的会给您带来更好的性能。由于您只复制一个指针,您的问题无疑是带宽限制,而不是计算因此,无论如何,通过并行化,您不会获得太多的加速。