如何制作这样的作品?
#define Eval(x, y, func) {y = func(x);}
__global__ void Evaluate(double *xs, double *ys, int N, double f(double))
{
int tid = threadIdx.x;
if (tid < N)
Eval(xs[tid], ys[tid], f);
}
然后在我的主函数中
double *xs_d, *ys_d;
double *xs_h, *ys_h;
xs_h = (double *) malloc(sizeof(double) * 256);
ys_h = (double *) malloc(sizeof(double) * 256);
cudaMalloc((void **)&xs_d, sizeof(double) * 256);
cudaMalloc((void **)&ys_d, sizeof(double) * 256);
for (int i = 0; i < 256; i++)
{
xs_h[i] = (double)i;
}
HANDLE_ERROR(cudaMemcpy(xs_d, xs_h, 256*sizeof(double), cudaMemcpyHostToDevice));
Evaluate<<<1,256>>>(xs_d, ys_d, 256, Sin);
cudaDeviceSynchronize();
HANDLE_ERROR(cudaMemcpy(ys_h, ys_d, 256*sizeof(double), cudaMemcpyDeviceToHost));
它在最后一行失败了。到目前为止,我已经看到了像 How to pass device function as an input argument to host-side function? 这样的解决方案,但它们使用了 __device__
函数,主机(例如 main
)函数无法更改或访问这些函数。例如,我不能将 __device__ int *fptrf1 = (int *)f1;
(取自示例)放在 main 中。是否有可能以某种方式拥有这种灵活性?
答案 0 :(得分:2)
例如,我不能将 public function searchProducts(Request $request) {
$product = $request->input('product');
$categories = Category::with('categories')->where(['parent_id' => 0])->get();
$productsAll = Category::query()->join('products', 'products.category_id', '=', 'categories.id')
->where('categories.name', 'LIKE', "%{$product}%")
->orWhere('products.product_name', 'LIKE', "%{$product}%")
->where('products.status', 1)->get();
$breadcrumb = "<a href='/'>Home</a> / ".$product;
return view('pages.results')->with(compact('categories','productsAll','product','breadcrumb'));
}
(取自示例)放入 __device__ int *fptrf1 = (int *)f1;
。是否有可能以某种方式拥有这种灵活性?
一种可能的方法是使用 lambda:
main
(CUDA 11.3)
对于各种设备函数指针的使用,此 answer 链接到许多示例。