I'm having trouble tracking down the source of an invalid argument to a cudaMemcpy call, here is the relevant code:
In gpu_memory.cu I declare and allocate memory for device pointers:
#define cudaErrorCheck(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
...
__device__ double* conc;
...
__global__ void pointer_set_kernel(..., double* conc_in...) {
...
conc = conc_in;
...
}
double* d_conc;
...
//memory initialization
void initialize_gpu_memory(int NUM, int block_size, int grid_size) {
...
cudaErrorCheck(cudaMalloc((void**)&d_conc, NUM * 53 * sizeof(double)));
...
pointer_set_kernel<<<1, 1>>>(...d_conc...);
cudaErrorCheck( cudaPeekAtLastError() ); // Checks for launch error
cudaErrorCheck( cudaThreadSynchronize() ); // Checks for execution error
}
Next in a different file (mechanism.cu), I declare the device pointer as an extern to copy data to it:
extern __device__ double* conc;
void write_jacobian_and_rates_output(int NUM, int block_size, int grid_size) {
...
initialize_gpu_memory(NUM, block_size, grid_size);
...
//get address of conc
double* d_conc;
cudaErrorCheck(cudaGetSymbolAddress((void **)&d_conc, conc));
//populate the concentrations on the host
double conc_host[NSP];
double* conc_host_full = (double*)malloc(NUM * NSP * sizeof(double));
//populate the concentrations
get_concentrations(1.01325e6, y_host, conc_host);
for (int i = 0; i < NUM; ++i) {
for (int j = 0; j < NSP; ++j) {
conc_host_full[i + j * NUM] = conc_host[j];
}
}
//check for errors, and copy over
cudaErrorCheck( cudaPeekAtLastError() ); // Checks for launch error
cudaErrorCheck( cudaThreadSynchronize() ); // Checks for execution error
cudaErrorCheck(cudaMemcpy(d_conc, conc_host_full, NUM * 53 * sizeof(double), cudaMemcpyHostToDevice));
...
}
I get the error on the last line, (the Memcpy). It appears that the initialize_gpu_memory function works correctly, this being the cuda-gdb inspection after the malloc and pointer_set_kernel:
p d_conc
$1 = (double *) 0x1b03236000
p conc
$2 = (@generic double * @global) 0x1b03236000
and in the write_jacobian_and_rates function:
p d_conc
$3 = (double *) 0x1b02e20600
p conc
$4 = (@generic double * @global) 0x1b03236000
I don't know why d_conc in the write function points to a different memory location after the cudaGetSymbolAddress call, or why I'm getting an invalid argument on the memcpy. I'm sure I'm doing something stupid, but for the life of me I can't see it. Would appreciate any help in tracking down the source of this, thanks!