我有一个并行程序,有时运行,有时只是给出分段错误。强制使用 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 536
是int 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 中生成线程的关键字。