从全局内核调用主机函数

时间:2016-01-02 01:43:51

标签: c++ templates cuda

我正在尝试从内核调用函数,并且我一直在收到错误。我的函数是一个模板参数。 这是错误:

 calling a __host__ function("mul<int> ") from a __global__ function("multiply<int, &mul<int> > ") is not allowed. 

这是我的代码:

    template<typename T> T mul(T a, T b)
{
    return a * b;
}
template <typename T, T (*myop)(T, T)>
static __global__ void multiply(int *in, int *out)
{...
 a = myop(b, c);
 ...
}
int main()
{
multiply<T, mul<T> ><<<Blocks, 1024>>>(...);
}

有人知道发生了什么事吗?

1 个答案:

答案 0 :(得分:3)

要在设备代码中使用的功能必须包含int decision(struct CardDeck *deck) { char choice; printf("Value is %d\n", deck->ValueOfSecondPlayer); while (deck->ValueOfSecondPlayer <= 21) { // ------> NOTE you should clear choice in each iteration choice = 0; // ------> NOTE probably need to break out of while here if (deck->ValueOfSecondPlayer == 21) { printf("Blackjack!"); } // ------> NOTE this will never execute // you should move this to the hit block if (deck->ValueOfSecondPlayer > 21) { printf("Sorry, you lose"); } if (deck->ValueOfSecondPlayer < 21) { printf("Would you like to hit or stand?\n"); scanf("%c", &choice); } // ------> NOTE you have a ';' after the closing parenthesis if (choice == 'H' || choice == 'h'); { printf("You wish to hit; here's your card\n"); deck->ValueOfSecondPlayer += printCards(deck); printf("Your total is now %d\n", deck->ValueOfSecondPlayer); } if (choice == 'S' || choice == 's') { printf("You wished to stay\n"); break; } } } qualifier

__device__