OpenMP堆内存管理

问题描述 投票:1回答:1

我有一个代码结构:

#pragma omp parallel
  #pragma omp task
    ...malloc(...);
    ...realloc(...);
    ...free(...);

保护所有共享变量访问,但仍然出现Segmentation Fault错误。这就是为什么我对失败原因的猜测是动态内存分配。

因此,当使用OpenMP时,我们是否有办法安全地allocatereallocatefree堆上的内存?

麻烦的代码在下面,但是如果您想了解更多...(づ ̄  ̄)づ这是回购:https://github.com/Nickpofig/min-graph-cut

void do_depth_first_search
(
    const struct ProblemInstance* instance,
    struct ProblemSolution* best_solution, 
    struct ProblemSolution* current_solution,
    int graph_capacity,
    int depth
)
{
    struct ProblemSolution include_solution;
    struct ProblemSolution exclude_solution;

    int thread =
    #if defined(_OPENMP)
        omp_get_thread_num(); 
    #else
        0;
    #endif

    printf("[%d] start.\n", thread);

    if (graph_capacity == 0 || depth == instance->n) 
    {
        printf("[%d] try calculate cost.\n", thread);

        calculate_cut_cost(current_solution, instance);

        printf("[%d] cost calculated.\n", thread);

        if (best_solution->is_valid == false || 
            best_solution->cost > current_solution->cost) 
        {
            printf("[%d] try validate\n", thread);

            #pragma omp critical
            {
                // temporary allocates memory on the heap 
                validate_solution(current_solution, instance);
            }

            printf("[%d] valiadted\n", thread);

            if (current_solution->is_valid)
            {
                printf("[%d] write best\n", thread);

                #pragma omp critical 
                {
                    best_solution->is_valid = true;
                    best_solution->cost = current_solution->cost;                

                    for (int i = 0; i < instance->n; i++) 
                    {
                        best_solution->array[i] = current_solution->array[i];
                    }
                }                
            }
        }

        printf("[%d] try free\n", thread);

        #pragma omp critical 
        {
            free(current_solution->array);
        }

        printf("[%d] free complete\n", thread);
        return;
    }

    printf("[%d] malloc sol.1\n", thread);
    #pragma omp critical 
    {
        include_solution = (struct ProblemSolution)
        {
            .array = malloc(sizeof(int) * instance->n),
            .size = instance->n,
            .cost = 0,
            .is_valid = false
        };
    }

    printf("[%d] malloc sol.2\n", thread);
    #pragma omp critical 
    {
        exclude_solution = (struct ProblemSolution)
        {
            .array = malloc(sizeof(int) * instance->n),
            .size = instance->n,
            .cost = 0,
            .is_valid = false
        };
    }

    printf("[%d] write sols\n", thread);
    for (int i = 0; i < instance->n; i++) 
    {
        include_solution.array[i] = current_solution->array[i];
        exclude_solution.array[i] = current_solution->array[i];
    }

    printf("[%d] free parent sol\n", thread);
    #pragma omp critical 
    {
        free(current_solution->array);
    }

    include_solution.array[depth] = 1;
    exclude_solution.array[depth] = 0;

    depth++;

    printf("[%d] search left\n", thread);
    #pragma omp task
    {
        do_depth_first_search(instance, best_solution, &include_solution, graph_capacity - 1, depth);
    }
    printf("[%d] search right\n", thread);
    #pragma omp task 
    {
        do_depth_first_search(instance, best_solution, &exclude_solution, graph_capacity    , depth);
    }
}
c segmentation-fault openmp heap-memory
1个回答
0
投票

我的结构必须是:

#pragma omp parallel
  #pragma omp single
    #pragma omp task

都不是

#pragma omp parallel
    #pragma omp task

#pragma omp parallel区域本身仅在任何可用线程上运行其内部的所有内容(实际上是愚蠢的,为什么要这样做openmp-san ??)因此,为了保护您避免随机执行输入点,您必须每次放置#pragma omp single

© www.soinside.com 2019 - 2024. All rights reserved.