并行编程中的一个奇怪案例

时间:2012-07-02 00:23:06

标签: c parallel-processing cilk cilk-plus

我有一个并行程序,有时会运行,有时只会给出分段错误。强制运行3个线程时的可执行文件运行正常(基本上它也运行单线程只是串行)但是当强制与任何其他线程值一起运行时它会给出分段错误。这是场景:

来自主要功能内的main.c

cilk_for ( line_count = 0; line_count != no_of_lines ; ++line_count )
{
     //some stuff here
     for ( j=line_count+1; j<no_of_lines; ++j )
     {
         //some stuff here
         final_result[line_count][j] = bf_dup_eleminate ( table_bloom[line_count], file_names[j], j );
         //some stuff here
     }
     //some stuff here
}
来自bf_dup_eleminate文件的

bloom-filter.c函数:

int bf_dup_eleminate ( const bloom_filter *bf, const char *file_name, int j )
{
    int count=-1;
    FILE *fp = fopen (file_name, "rb" );
    if (fp)
    {
        count = bf_dup_eleminate_read ( bf, fp, j);
        fclose ( fp );
    }
    else
    {
        printf ( "Could not open file\n" );
    }
    return count;
}
来自bf_dup_eleminate_read档案的

bloom-filter.c

int bf_dup_eleminate_read ( const bloom_filter *bf, FILE *fp, int j )
{
    //some stuff here
    printf ( "before while loop. j is %d ** workder id: **********%d***********\n", j, __cilkrts_get_worker_number());
    while (/*somecondition*/)
    {/*some stuff*/}
    //some stuff
}

我从intel inspector报告此错误是:

ID | Problem                         |  Sources       
P1 | Unhandled application exception | bloom-filter.c

并且调用堆栈是:

exec!bf_dup_eleminate_read - bloom-filter.c:550
exec!bf_dup_eleminate - bloom-filter.c:653
exec!__cilk_for_001.10209 - main.c:341

同样,gdb也会在同一位置报告错误,它是:

现在gdb告诉我您有以下错误

0x0000000000406fc4 in bf_dup_eleminate_read (bf=<error reading variable: Cannot access memory at address 0x7ffff7edba58>, fp=<error reading variable: Cannot access memory at address 0x7ffff7edba50>, j=<error reading variable: Cannot access memory at address 0x7ffff7edba4c>) at bloom-filter.c:536

Line 536int bf_dup_eleminate_read ( const bloom_filter *bf, FILE *fp, int j )

其他详情:

现在我的bloomfilter是一个定义为

的结构
struct bloom_filter
{
    int64_t m;      //size of bloom filter.
    int32_t k;      //number of hash functions.
    uint8_t *array;
    int64_t no_of_elements_added;
    int64_t expected_no_of_elements;
};

和它的内存分配如下:

    bloom_filter *bf = (bloom_filter *)malloc( sizeof(bloom_filter));
    if ( bf != NULL )
    {
        bf->m = filter_size*8;      /* Size of bloom filter */
        bf->k = num_hashes;
        bf->expected_no_of_elements = expected_no_of_elements;
        bf->no_of_elements_added = (int64_t)0;
        bf->array = (uint8_t *)malloc(filter_size);
        if ( bf->array == NULL )
        {
            free(bf);
            return NULL;
        }
    }  

bloom_filter只有一个副本,每个线程都应该访问相同的内容(因为我没有修改任何只读的内容)。

有人可以帮助我,因为我被困在这里过去4天,我只是想不出办法。最糟糕的是它运行3个线程!!!

注意:cilk_for只是在cilk中生成线程的关键字。

1 个答案:

答案 0 :(得分:8)

当调试器发出如下错误时:

0x0000000000406fc4 in bf_dup_eleminate_read (
    bf=<error reading variable: Cannot access memory at address 0x7ffff7edba58>,
    fp=<error reading variable: Cannot access memory at address 0x7ffff7edba50>,
    j=<error reading variable: Cannot access memory at address 0x7ffff7edba4c>
) at bloom-filter.c:536

536: int bf_dup_eleminate_read ( const bloom_filter *bf, FILE *fp, int j )

它通常表示函数入口代码(称为函数“prologue”)崩溃。简而言之,当计算三个局部变量的地址并在堆栈上为它们分配空间时,堆栈已经损坏并且CPU崩溃。

我会检查或尝试修复此错误的事情(其中没有一个可以保证有效,其中一些您可能已经尝试过了):

  1. 确保您没有超出您在程序其他部分声明的任何局部变量使用的任何空间。

  2. 确保您没有写入已声明为局部变量的指针,然后从程序其他部分的函数返回。

  3. 确保每个线程都有足够的堆栈空间来处理您声明的所有局部变量。您是否声明任何大型基于堆栈的缓冲区?默认的每线程堆栈大小取决于编译器设置,或者在本例中是 cilk 库。尝试在编译时增加每线程堆栈大小,看看崩溃是否消失。

  4. 运气好的话,上面的一个应该可以让你缩小问题的根源。