I wrote a customized module with c and cuda. It works fine with GPU 0. But when I switch to GPU 1 (I do have 2 GPUs on my machine), the following error occurs:
THCudaCheck FAIL file=/opt/conda/conda-bld/pytorch_1502006348621/work/torch/lib/THC/THCTensorCopy.cu line=100 error=77 : an illegal memory access was encountered
THCudaCheck FAIL file=/opt/conda/conda-bld/pytorch_1502006348621/work/torch/lib/THC/THCTensorCopy.cu line=100 error=77 : an illegal memory access was encountered
terminate called after throwing an instance of 'std::runtime_error'
  what():  terminate called recursively
cuda runtime error (77) : an illegal memory access was encountered at /opt/conda/conda-bld/pytorch_1502006348621/work/torch/lib/THC/THCTensorCopy.cu:100
Aborted (core dumped)

I selected GPU using the following code:
torch.cuda.set_device(args.gpu_id)

A sample of my customized module is as follows:
void updateOutput_cuda(THCudaTensor *input,  THCudaTensor *output,){

    input = THCudaTensor_newContiguous(state, input);

    THCudaTensor_resize4d(state, output, batchSize, nInputPlane, outputHeight, outputWidth);
    output = THCudaTensor_newContiguous(state, output);
    THCudaTensor_zero(state,output);

    THCudaTensor_free(state, input);
    THCudaTensor_free(state, output);
}

Thanks for your help!