我遇到了一个非常奇怪的错误,即在运行特定大小的 Heat 2D 模拟时出现“非法内存访问”错误,但如果我运行完全相同的模拟,则模拟运行良好,只是元素更少。
增加数组大小会导致此异常是否有原因?我使用的是 Titan Black GPU(6 GB 内存),但我运行的模拟远不及那个大小。我计算出我可以运行 4000 x 4000 的模拟,但如果超过 250 x 250,我会得到错误。
在我在设备上实例化模拟对象数组后立即发生错误。实例化代码如下:
template<typename PlaceType, typename StateType>
__global__ void instantiatePlacesKernel(Place** places, StateType *state,
void *arg, int *dims, int nDims, int qty) {
unsigned idx = blockDim.x * blockIdx.x + threadIdx.x;
if (idx < qty) {
// set pointer to corresponding state object
places[idx] = new PlaceType(&(state[idx]), arg);
places[idx]->setIndex(idx);
places[idx]->setSize(dims, nDims);
}
}
template<typename PlaceType, typename StateType>
Place** DeviceConfig::instantiatePlaces(int handle, void *argument, int argSize,
int dimensions, int size[], int qty) {
// add global constants to the GPU
memcpy(glob.globalDims,size, sizeof(int) * dimensions);
updateConstants(glob);
// create places tracking
PlaceArray p; // a struct to track qty,
p.qty = qty;
// create state array on device
StateType* d_state = NULL;
int Sbytes = sizeof(StateType);
CATCH(cudaMalloc((void** ) &d_state, qty * Sbytes));
p.devState = d_state; // save device pointer
// allocate device pointers
Place** tmpPlaces = NULL;
int ptrbytes = sizeof(Place*);
CATCH(cudaMalloc((void** ) &tmpPlaces, qty * ptrbytes));
p.devPtr = tmpPlaces; // save device pointer
// handle arg if necessary
void *d_arg = NULL;
if (NULL != argument) {
CATCH(cudaMalloc((void** ) &d_arg, argSize));
CATCH(cudaMemcpy(d_arg, argument, argSize, H2D));
}
// load places dimensions
int *d_dims;
int dimBytes = sizeof(int) * dimensions;
CATCH(cudaMalloc((void** ) &d_dims, dimBytes));
CATCH(cudaMemcpy(d_dims, size, dimBytes, H2D));
// launch instantiation kernel
int blockDim = (qty - 1) / BLOCK_SIZE + 1;
int threadDim = (qty - 1) / blockDim + 1;
Logger::debug("Launching instantiation kernel");
instantiatePlacesKernel<PlaceType, StateType> <<<blockDim, threadDim>>>(tmpPlaces, d_state,
d_arg, d_dims, dimensions, qty);
CHECK();
CATCH(cudaDeviceSynchronize()); // ERROR OCCURS HERE
// clean up memory
if (NULL != argument) {
CATCH(cudaFree(d_arg));
}
CATCH(cudaFree(d_dims));
CATCH(cudaMemGetInfo(&freeMem, &allMem));
return p.devPtr;
}
请假设您看到的任何自定义类型都在工作,因为此代码在足够小的模拟上执行而不会出错。当大小超过 250 x 250 个元素时,内核函数的位置和状态数组中的元素数量似乎会导致错误,这让我感到很沮丧。任何见解都会很棒。
谢谢!