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

Ama*_*tam 5 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
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

我报告的这个错误intel inspector是:

ID | Problem                         |  Sources       
P1 | Unhandled application exception | bloom-filter.c
Run Code Online (Sandbox Code Playgroud)

并且调用堆栈是:

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
Run Code Online (Sandbox Code Playgroud)

同样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;
};
Run Code Online (Sandbox Code Playgroud)

它的内存分配如下:

    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;
        }
    }  
Run Code Online (Sandbox Code Playgroud)

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

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

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

aps*_*012 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 )
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

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