我需要以下主机代码的设备版本:
double (**func)(double x);
double func1(double x)
{
return x+1.;
}
double func2(double x)
{
return x+2.;
}
double func3(double x)
{
return x+3.;
}
void test(void)
{
double x;
for(int i=0;i<3;++i){
x=func[i](2.0);
printf("%g\n",x);
}
}
int main(void)
{
func=(double (**)(double))malloc(10*sizeof(double (*)(double)));
test();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
其中func1,func2,func3必须是__device__函数,"test"必须是(适当修改)__global__内核.
我有一台NVIDIA GeForce GTS 450(计算能力2.1),先谢谢Michele
================================================== ======
一个有效的解决方
#define REAL double
typedef REAL (*func)(REAL x);
__host__ __device__ REAL func1(REAL x)
{
return x+1.0f;
}
__host__ __device__ REAL func2(REAL x)
{
return x+2.0f;
}
__host__ …Run Code Online (Sandbox Code Playgroud) 我试图在我的玩具示例中使用cublas函数cublasSgemmBatched.在这个例子中,我首先分配2D数组:h_AA, h_BBsize [ 6] [ 5]和h_CCsize [ 6] [ 1].之后,我将其复制到设备,执行cublasSgemmBatched并尝试将阵列复制d_CC回主机阵列h_CC.但是,我收到了一个错误(cudaErrorLaunchFailure)设备主机复制,我不确定我是否正确地将数组复制到设备中:
int main(){
cublasHandle_t handle;
cudaError_t cudaerr;
cudaEvent_t start, stop;
cublasStatus_t stat;
const float alpha = 1.0f;
const float beta = 0.0f;
float **h_AA, **h_BB, **h_CC;
h_AA = new float*[6];
h_BB = new float*[6];
h_CC = new float*[6];
for (int i = 0; i < 6; i++){
h_AA[i] = new float[5]; …Run Code Online (Sandbox Code Playgroud) 我对CUDA/Thrust很新,并且在代码片段方面存在问题.为了使它更容易,我把它修剪到最低限度.代码如下:
struct functor{
functor(float (*g)(const float&)) : _g{g} {}
__host__ __device__ float operator()(const float& x) const {
return _g(x);
}
private:
float (*_g)(const float&);
};
__host__ __device__ float g(const float& x){return 3*x;}
int main(void){
thrust::device_vector<float> X(4,1);
thrust::transform(X.begin(), X.end(), X.begin(), functor(&g));
}
Run Code Online (Sandbox Code Playgroud)
我的想法是我可以将任何函数传递给仿函数,因此我可以将该函数应用于Vector中的每个元素.不幸的是,我不确定为什么我会得到描述的错误.我编译-w -O3 -shared -arch=sm_20 -std=c++11 -DTHRUST_DEBUG
我很感谢你能给我的任何帮助:)