cilk_for problem with strtok

cilk_for problem with strtok

The following simple test code shows race conditions with cilkscreen, even when all the variables
are local to the loop.

#include
using namespace std;

int cilk_main()
{

cilk_for(int i=0;i<3;i++)
{
string line="hi ashay here";
string res;
char *pch,*str;
str=(char*)line.c_str();
pch = strtok (str," ");
while (pch != NULL)
{
res.append(pch);
pch = strtok (NULL, " ");
}
}
return 0;
}

It would be great if someone could point to the possible problem.
Following is the cilkscreen report:

Race condition on location 0x33065533d0
write access at 0x3306279fb8: (strtok+0xe8)
read access at 0x3306279ee8: (strtok+0x18)
called by 0x2aaabe1ae379: (_Z15cilkscreen_loopIPFvPvmmEmEQbT_S0_T0_+0xa9)
called by 0x2aaabe1ae90e: (_ZN4cilk13cilk_for_loopEQvPFvPvmmES0_mm+0x15e)
called by 0x4010b1: (_Z9cilk_mainQiiPPc+0xd1)
called by 0x401513: (_ZN4cilk9main_wrapEQiPv+0x53)
called by 0x2aaabe1ad5d1: (_Z20cilk_run_wrapper_intQvPv+0x51)
called by 0x2aaabe1af70a: (__cilkrts_init_helper+0x5)

Race condition on location 0x602a6a
write access at 0x3306279f99: (strtok+0xc9)
read access at 0x3306279f7b: (strtok+0xab)
called by 0x2aaabe1ae379: (_Z15cilkscreen_loopIPFvPvmmEmEQbT_S0_T0_+0xa9)
called by 0x2aaabe1ae90e: (_ZN4cilk13cilk_for_loopEQvPFvPvmmES0_mm+0x15e)
called by 0x4010b1: (_Z9cilk_mainQiiPPc+0xd1)
called by 0x401513: (_ZN4cilk9main_wrapEQiPv+0x53)
called by 0x2aaabe1ad5d1: (_Z20cilk_run_wrapper_intQvPv+0x51)
called by 0x2aaabe1af70a: (__cilkrts_init_helper+0x5)

Race condition on location 0x602a6a
write access at 0x3306279f99: (strtok+0xc9)
write access at 0x3306279f99: (strtok+0xc9)
called by 0x2aaabe1ae379: (_Z15cilkscreen_loopIPFvPvmmEmEQbT_S0_T0_+0xa9)
called by 0x2aaabe1ae90e: (_ZN4cilk13cilk_for_loopEQvPFvPvmmES0_mm+0x15e)
called by 0x4010b1: (_Z9cilk_mainQiiPPc+0xd1)
called by 0x401513: (_ZN4cilk9main_wrapEQiPv+0x53)
called by 0x2aaabe1ad5d1: (_Z20cilk_run_wrapper_intQvPv+0x51)
called by 0x2aaabe1af70a: (__cilkrts_init_helper+0x5)

Race condition on location 0x602a6a
read access at 0x33062795c0: (strlen+0x30)
write access at 0x3306279f99: (strtok+0xc9)
called by 0x2aaabe1ae379: (_Z15cilkscreen_loopIPFvPvmmEmEQbT_S0_T0_+0xa9)
called by 0x2aaabe1ae90e: (_ZN4cilk13cilk_for_loopEQvPFvPvmmES0_mm+0x15e)
called by 0x4010b1: (_Z9cilk_mainQiiPPc+0xd1)
called by 0x401513: (_ZN4cilk9main_wrapEQiPv+0x53)
called by 0x2aaabe1ad5d1: (_Z20cilk_run_wrapper_intQvPv+0x51)
called by 0x2aaabe1af70a: (__cilkrts_init_helper+0x5)

Race condition on location 0x33065533d0
write access at 0x3306279fb8: (strtok+0xe8)
write access at 0x3306279fa7: (strtok+0xd7)
called by 0x2aaabe1ae379: (_Z15cilkscreen_loopIPFvPvmmEmEQbT_S0_T0_+0xa9)
called by 0x2aaabe1ae90e: (_ZN4cilk13cilk_for_loopEQvPFvPvmmES0_mm+0x15e)
called by 0x4010b1: (_Z9cilk_mainQiiPPc+0xd1)
called by 0x401513: (_ZN4cilk9main_wrapEQiPv+0x53)
called by 0x2aaabe1ad5d1: (_Z20cilk_run_wrapper_intQvPv+0x51)
called by 0x2aaabe1af70a: (__cilkrts_init_helper+0x5)

Race condition on location 0x33065533d0
read access at 0x3306279ee8: (strtok+0x18)
write access at 0x3306279fa7: (strtok+0xd7)
called by 0x2aaabe1ae379: (_Z15cilkscreen_loopIPFvPvmmEmEQbT_S0_T0_+0xa9)
called by 0x2aaabe1ae90e: (_ZN4cilk13cilk_for_loopEQvPFvPvmmES0_mm+0x15e)
called by 0x4010b1: (_Z9cilk_mainQiiPPc+0xd1)
called by 0x401513: (_ZN4cilk9main_wrapEQiPv+0x53)
called by 0x2aaabe1ad5d1: (_Z20cilk_run_wrapper_intQvPv+0x51)
called by 0x2aaabe1af70a: (__cilkrts_init_helper+0x5)
6 errors found by Cilkscreen
Cilkscreen suppressed 16 duplicate error messages

Note: When the strtok part was put in a function and called from cilk_for, it gave an
internal compiler error.

3 posts / novo 0
Último post
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.

strtok uses a global to maintain state across it's calls. That's how the line

pch = strtok (NULL, " ");

can return you the next token. Cilkscreen is calling this out:

Race condition on location 0x33065533d0
write access at
0x3306279fb8: (strtok+0xe8)
read access at 0x3306279ee8:
(strtok+0x18)

0x33065533d0 is the global variable that strtok is using to maintain it's pointer into your string.
strtok+0xe8 and strtok+0x18 are two of the instructions that are accessing that global variable.

Note that the documentation at http://www.opengroup.org/onlinepubs/000095399/functions/strtok.html explicitly states that strtok is not thread safe, so you shouldn't be using it in a cilk_for loop. It appears that the strtok_r function *is* thread safe, so it should be OK, assuming that your system implements it.

Thanks a lot! It worked with strtok_r.

Deixar um comentário

Faça login para adicionar um comentário. Não é membro? Inscreva-se hoje mesmo!