【发布时间】:2016-02-22 01:50:16
【问题描述】:
我有一个获取数组和数组索引的全局函数。 该函数需要在某个字典中找到一个单词,并在给定的序列中从哪里开始。
但我看到线程覆盖了结果。所以我猜这是因为记忆竞赛。 我该怎么办?
__global__ void find_words(int* dictionary, int dictionary_size, int* indeces,
int indeces_size, int *sequence, int sequence_size,
int longest_word, int* devWords, int *counter)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
int start = id * (CHUNK_SIZE - longest_word);
int finish = start + CHUNK_SIZE;
int word_index = -1;
if (finish > sequence_size)
{
finish = sequence_size;
}
// search in a closed area
while(start < finish)
{
find_word_in_phoneme_dictionary_kernel(dictionary, dictionary_size,
indeces, indeces_size, sequence, &word_index, start, finish);
if(word_index >= 0 && word_index <= indeces[indeces_size-1])
{
devWords[*counter] = word_index;
devWords[*counter+1] = start; // index in sequence
*counter+=2;
start += dictionary[word_index];
}
else
{
start++;
}
}
__syncthreads();
}
我还尝试为每个线程创建他自己的数组和计数器来存储他的结果 然后收集所有线程结果..但我不明白如何在 CUDA 中实现收集。有什么帮助吗?
【问题讨论】:
标签: c++ cuda race-condition