在CUDA内核中使用Eigen 3.3 [英] Using Eigen 3.3 in a CUDA kernel

查看:580
本文介绍了在CUDA内核中使用Eigen 3.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自2016年11月以来,可以编译引用Eigen3.3的CUDA代码-请参见此答案

Since Nov. 2016 it's possible to compile CUDA code which references Eigen3.3 - see this answer

这个答案不是我想要的,现在可能是 现在已过时,因为以下内容写在文档

This answer is not what I'm looking for and may now be "outdated" in the sense that there might now be an easier way, since the following is written in the docs


从Eigen 3.3开始,现在可以在内部使用Eigen的对象和
算法CUDA内核。但是,仅支持
功能的子集,以确保在
a CUDA内核中不会触发任何动态分配。

Starting from Eigen 3.3, it is now possible to use Eigen's objects and algorithms within CUDA kernels. However, only a subset of features are supported to make sure that no dynamic allocation is triggered within a CUDA kernel.

另请参见此处。不幸的是,我找不到任何示例。

See also here. Unfortunately, I was not able to find any example of how this might look like.

现在是否可以编写如下的内核了,它应该只计算一堆点积?

Is it now possible to write a kernel such as the following, which should simply calculate a bunch of dot products?

__global__ void cu_dot(Eigen::Vector3d *v1, Eigen::Vector3d *v2, double *out, size_t N)
{
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if(idx < N)
    {
        out[idx] = v1[idx].dot(v2[idx]);
    }
    return;
}

我可以编译它,但似乎不起作用。当我尝试将数据复制到主机时,获得非法内存访问。请注意,我最初将Vector3d存储为`std :: vector,然后分别使用

I can compile this, but it does not seem to work. When I try to copy the data to host, I get illegal memory access. Note that I originally store the Vector3d's as `std::vector and then respectively use

cudaMalloc((void **)&p_device_v1, sizeof(Eigen::Vector3d)*n);
cudaMemcpy(p_v1_device, v1.data(), sizeof(Eigen::Vector3d)*n, cudaMemcpyHostToDevice);

我已经在MWE项目 https://github.com/GPMueller/eigen-cuda rel = nofollow noreferrer> https://github.com/GPMueller/eigen-cuda

I have set up an MWE project using CMake at https://github.com/GPMueller/eigen-cuda

推荐答案

在github上的MWE项目中,您写道:

In the MWE project on github, you wrote:

double dot(std::vector<Eigen::Vector3d> v1, std::vector<Eigen::Vector3d> v2)
{   
    ...     
    // Dot product
    cu_dot<<<(n+1023)/1024, 1024>>>(v1.data(), v2.data(), dev_ret, n);

v1.data() v2.data()指针位于CPU内存中。您需要在GPU内存中使用指针,即

The v1.data() and v2.data() pointers are in the CPU memory. You need to use the pointers in the GPU memory, i.e.

// Dot product
cu_dot<<<(n+1023)/1024, 1024>>>(dev_v1, dev_v2, dev_ret, n);

CPU与GPU的结果相同,但这是一个问题代码,即您没有对多个点积进行归约。

The CPU vs GPU results are not identical, but that's an issue with the code, i.e. you didn't perform a reduction on the multiple dot products.

这篇关于在CUDA内核中使用Eigen 3.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆