Hello, I have some code, which works perfectly:
DensityMap* map;
COMPUTE_SAFE(cudaMalloc(reinterpret_cast<void**>(&map), sizeof(DensityMap)))
COMPUTE_SAFE(cudaMemcpy(map, host->Map, sizeof(DensityMap), cudaMemcpyHostToDevice))
// Copy the nodes over to the device
const unsigned long long int nodesSize = static_cast<unsigned long long>(host->Map->GetNodeCount()) * host->Map->GetNodeElementCount() * sizeof(double);
double* nodes;
COMPUTE_SAFE(cudaMalloc(&nodes, nodesSize))
COMPUTE_SAFE(cudaMemcpy(nodes, host->Map->GetNodes(), nodesSize, cudaMemcpyHostToDevice))
COMPUTE_SAFE(cudaMemcpy(&map->m_Nodes, &nodes, sizeof(double*), cudaMemcpyHostToDevice))
// Copy the cell map over to the device
const unsigned long long int cellMapSize = static_cast<unsigned long long>(host->Map->GetCellMapCount()) * host->Map->GetCellMapElementCount() * sizeof(unsigned int);
unsigned int* cellMap;
COMPUTE_SAFE(cudaMalloc(&cellMap, cellMapSize))
COMPUTE_SAFE(cudaMemcpy(cellMap, host->Map->GetCellMap(), cellMapSize, cudaMemcpyHostToDevice))
COMPUTE_SAFE(cudaMemcpy(&map->m_CellMap, &cellMap, sizeof(unsigned int*), cudaMemcpyHostToDevice))
// Copy the cells over to the device
const unsigned long long int cellsSize = host->Map->GetCellCount() * host->Map->GetCellElementCount() * 32u * sizeof(unsigned int);
unsigned int* cells;
COMPUTE_SAFE(cudaMalloc(&cells, cellsSize))
COMPUTE_SAFE(cudaMemcpy(cells, host->Map->GetCells(), cellsSize, cudaMemcpyHostToDevice))
COMPUTE_SAFE(cudaMemcpy(&map->m_Cells, &cells, sizeof(unsigned int*), cudaMemcpyHostToDevice))
// Finally, move the copy the density map itself to the device
COMPUTE_SAFE(cudaMemcpy(&d_RigidBody->Map, &map, sizeof(DensityMap*), cudaMemcpyHostToDevice))
And I’m trying to move it into the DensityMap
struct, but I’m having some issues, this is what I’ve tried so far:
// Inside the DensityMap struct (this = DensityMap*)
__host__ void CopyToDevice(DensityMap* device)
{
DensityMap* map;
COMPUTE_SAFE(cudaMalloc(reinterpret_cast<void**>(&map), sizeof(DensityMap)))
COMPUTE_SAFE(cudaMemcpy(map, this, sizeof(DensityMap), cudaMemcpyHostToDevice))
// Copy the nodes over to the device
const unsigned long long int nodesSize = static_cast<unsigned long long>(m_NodeCount) * m_NodeElementCount * sizeof(double);
double* nodes;
COMPUTE_SAFE(cudaMalloc(&nodes, nodesSize))
COMPUTE_SAFE(cudaMemcpy(nodes, m_Nodes, nodesSize, cudaMemcpyHostToDevice))
COMPUTE_SAFE(cudaMemcpy(&map->m_Nodes, &nodes, sizeof(double*), cudaMemcpyHostToDevice))
// Copy the cell map over to the device
const unsigned long long int cellMapSize = static_cast<unsigned long long>(m_CellMapCount) * m_CellMapElementCount * sizeof(unsigned int);
unsigned int* cellMap;
COMPUTE_SAFE(cudaMalloc(&cellMap, cellMapSize))
COMPUTE_SAFE(cudaMemcpy(cellMap, m_CellMap, cellMapSize, cudaMemcpyHostToDevice))
COMPUTE_SAFE(cudaMemcpy(&map->m_CellMap, &cellMap, sizeof(unsigned int*), cudaMemcpyHostToDevice))
// Copy the cells over to the device
const unsigned long long int cellsSize = m_CellCount * m_CellElementCount * 32u * sizeof(unsigned int);
unsigned int* cells;
COMPUTE_SAFE(cudaMalloc(&cells, cellsSize))
COMPUTE_SAFE(cudaMemcpy(cells, m_Cells, cellsSize, cudaMemcpyHostToDevice))
COMPUTE_SAFE(cudaMemcpy(&map->m_Cells, &cells, sizeof(unsigned int*), cudaMemcpyHostToDevice))
// Finally, move the copy the density map itself to the device
COMPUTE_SAFE(cudaMemcpy(&device, &map, sizeof(DensityMap*), cudaMemcpyHostToDevice))
}
I call the new implementation like this:
// d_RigidBody->Map = DensityMap*
host->Map->CopyToDevice(d_RigidBody->Map);
But the moment it reaches this line I get an access violation reading location error:
COMPUTE_SAFE(cudaMemcpy(&map->m_Nodes, &nodes, sizeof(double*), cudaMemcpyHostToDevice))
Any help is very much appreciated :D