c++ - 来自 cudaGetErrorString(err) 的未知错误

标签 c++ linux pointers memory cuda

当我在 cudaT.h 中调用 cudaMemcpy 时,出现未知错误。 当我编译时,我收到此警告,在第 46、50、52 行重复多次:

./gameoflife.cu(46): Warning: Cannot tell what pointer points to, assuming global memory space

main.cu

#include "gameoflife.cu"

#include <iostream>

using namespace std;

#define DIM 20

__global__ void Func(CudaMem<GameOfLife<DIM, DIM> > * golDevice)
{
    golDevice->m_t->Step();
}

int main()
{
    GameOfLife<DIM, DIM> golHost;

    golHost.CreateCell(3,3);
    golHost.CreateCell(4,3);
    golHost.CreateCell(5,3);
    golHost.CreateCell(5,2);
    golHost.CreateCell(4,1);

    golHost.CreateCell(1,7);
    golHost.CreateCell(2,7);
    golHost.CreateCell(3,7);

    cout << golHost << endl;

    CudaMem<GameOfLife<DIM, DIM> > golDevice;
//  GameOfLife<DIM, DIM>* golDevice;
//  cudaMalloc((void **)&golDevice, sizeof(GameOfLife<DIM, DIM>));

    CudaMem<GameOfLife<DIM, DIM> >::CudaMemcpyHostToDevice(golDevice, &golHost);
//  cudaMemcpy(golDevice, &golHost, sizeof(GameOfLife<DIM, DIM>), cudaMemcpyHostToDevice);

    for(int i = 0 ; i < 1 ; ++i)
    {

//      cout << "Press anykey to exit.";
//      cin.ignore();
//      cin.get();

        Func<<<DIM, DIM>>>(&golDevice);

        CudaMem<GameOfLife<DIM, DIM> >::CudaMemcpyDeviceToHost(golDevice, &golHost);
//      cudaMemcpy(&golHost, golDevice, sizeof(GameOfLife<DIM, DIM>), cudaMemcpyDeviceToHost);

        cout << golHost << endl;

    }

//  cudaFree(golDevice);

    cudaDeviceReset();


}

gameoflife.cu

#include <cuda_runtime.h>
#include "cudaT.h"
#include <cstddef> 
#include <iostream>

using namespace std;

template<size_t ROWS, size_t COLUMNS>
class GameOfLife
{
public:
    __host__ GameOfLife()
    {
        memset(m_dots, 0, sizeof(m_dots));
        memset(m_dots, 0, sizeof(m_temp));
    }


    __host__ __device__ ~GameOfLife()
    {

    }

    __host__ void CreateCell(size_t _row, size_t _column)
    {
        // need to check overflow
        m_dots[_row + 1][_column + 1] = 1;
    }

    __host__ void KillCell(size_t _row, size_t _column)
    {
        // need to check overflow
        m_dots[_row + 1][_column + 1] = 0;
    }

    __device__ void Step()
    {
        int liveNeighbours = 0;

        if(threadIdx.x > ROWS || blockIdx.x > COLUMNS )
        {
            return;
        }

        m_temp[threadIdx.x + 1][blockIdx.x + 1] = m_dots[threadIdx.x + 1][blockIdx.x + 1];

        __syncthreads();

        liveNeighbours = CalcLiveNeighbours() % 9;

        m_dots[threadIdx.x + 1][blockIdx.x + 1] = ( m_temp[threadIdx.x + 1][blockIdx.x + 1] && (liveNeighbours > 1 && liveNeighbours < 4) ) 
        || ( !m_temp[threadIdx.x + 1][blockIdx.x + 1] && liveNeighbours == 3) ; 

    }

    template<size_t R, size_t C>
    __host__ friend ostream& operator<<(ostream& os, GameOfLife<R, C>& gol);

private:
    __device__ __host__ GameOfLife(const GameOfLife& other);

    __device__ int CalcLiveNeighbours()
    {
        return  m_temp[threadIdx.x + 0][blockIdx.x + 0] + 
                m_temp[threadIdx.x + 0][blockIdx.x + 1] + 
                m_temp[threadIdx.x + 0][blockIdx.x + 2] + 
                m_temp[threadIdx.x + 1][blockIdx.x + 0] + 
                m_temp[threadIdx.x + 1][blockIdx.x + 2] + 
                m_temp[threadIdx.x + 2][blockIdx.x + 0] + 
                m_temp[threadIdx.x + 2][blockIdx.x + 1] + 
                m_temp[threadIdx.x + 2][blockIdx.x + 2]
        ;
    }   

    int m_temp[ROWS + 2][COLUMNS + 2];
    int m_dots[ROWS + 2][COLUMNS + 2];
};

template<size_t R, size_t C>
__host__ ostream& operator<<(ostream& os, GameOfLife<R, C>& gol)
{
    for(int i = 1 ; i < (R+1) ; ++i)
    {
        for(int j = 1 ; j < (C+1) ; ++j)
        {
            os << gol.m_dots[i][j] << " ";
        }
        os << endl;
    }

    return os;
}

cudaT.h

#pragma once

#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <assert.h>

#include "cudaException.h"

#include <iostream>
using namespace std;

template<typename T>
class CudaMem
{
public:

    CudaMem():m_t(0)
    {
        cudaError_t err = cudaSuccess;
        err = cudaMalloc((void **)&m_t, sizeof(T));

        if (err != cudaSuccess)
        {
            throw CudaException(err);
        }

    }

    // CudaMem(T& copyFrom):m_t(0)

    explicit CudaMem(const CudaMem<T>& other):m_t(0)
    {
        cout << "CudaMem copy ctor" << endl;
        cudaError_t err = cudaSuccess;

        err = cudaMalloc((void **)&m_t, sizeof(T));

        if (err != cudaSuccess)
        {
            throw CudaException(err);
        }
        // need to copy the memory 
    }

    static void CudaMemcpyHostToDevice(CudaMem<T>& _deviceMem, T* _hostMem)
    {
        cudaError_t err = cudaSuccess;

        err = cudaMemcpy(_deviceMem.m_t, _hostMem, sizeof(T), cudaMemcpyHostToDevice);

        if(err != cudaSuccess)
        {
            throw CudaException(err);
        }
    }

    static void CudaMemcpyDeviceToHost(CudaMem<T>& _deviceMem, T* _hostMem)
    {
        cudaError_t err = cudaSuccess;
        err = cudaMemcpy(_hostMem, _deviceMem.m_t, sizeof(T), cudaMemcpyDeviceToHost);

        if (err != cudaSuccess)
        {
            throw CudaException(err);
        }
    }


    ~CudaMem()
    {
        cout << "CudaMem dtor" << endl;
        cudaFree(m_t);
    }

    const T* Get()
    {
        return m_t;
    }

    operator void*()
    {
        return m_t;
    }

    operator T*()
    {
        return m_t;
    }

    T* operator->()
    {
        return m_t;
    }

    T* const m_t;
private:

};

cudaException.h

#pragma once

#include <string>
#include <sstream>

#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

using std::string;
using std::stringstream;

class CudaException : public std::exception
{
public:
    CudaException(cudaError_t _err):m_str(cudaGetErrorString(_err))
    {

    }

    CudaException(cudaError_t _err, string _file, int _line):m_str(cudaGetErrorString(_err))
    {
        string s;
        stringstream out;
        out << _line;
        s = out.str();
        m_str += " At file: " + _file + " At line: " + s;
    }

    virtual const char* what() const throw ()
    {
        return m_str.c_str();
    }

    virtual ~CudaException() throw (){}

private:
    string m_str;
};

通常我想做的是将 cudaMalloc\cudaFree 包装在一个类中。 当我不使用此类 CudaMem 时,一切工作正常。

最佳答案

问题出在这个函数调用上:

Func<<<DIM, DIM>>>(&golDevice);

我发送的是主机内存指针,而不是设备内存指针。

关于c++ - 来自 cudaGetErrorString(err) 的未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30291844/

相关文章:

c++ - Xcode 和 C++ [初学者] - 为什么 Hello World 程序打印所有这些额外的东西?

C++运算符查找误区

c++ - 关于旅行商问题的编译器优化

c++ - DeviceIoControl 输出缓冲区为空

c++ - 使用C获取文件夹中的文件列表

linux - 编写一个 txt 文件的脚本,询问您想要在文件中包含什么内容

linux - 系统启动后直接从linux内核运行程序

php - 从 apache 服务器中的 php 访问 shell 可执行文件

c++ - 指针指向 LSB 还是 MSB?

c - 使用链表时的空指针