How to access gpu_diff()[i] ?
Hello guys,
Today I was trying to modify split_layer.cu to fit my personal application, and all I do is to add these lines to Backward_gpu function:
if (top[0]->count() == 1 && top[1]->count() == 1 && top[1]->gpu_diff()[0] == 1.5) { top[0]->mutable_gpu_diff()[0] = 0; top[1]->mutable_gpu_diff()[0] = 0; } LOG(INFO) << "split 0 gpu_diff is " << top[0]->gpu_diff()[0] << std::endl; LOG(INFO) << "split 1 gpu_diff is " << top[1]->gpu_diff()[0] << std::endl;
make all && make test all finished without error, but when I ran make runtest, it aborted with segmentation fault.
[----------] 3 tests from SplitLayerTest/3, where TypeParam = caffe::DoubleGPU [ RUN ] SplitLayerTest/3.TestGradient *** Aborted at 1420715699 (unix time) try "date -d @1420715699" if you are using GNU date *** PC: @ 0x83ca79 caffe::SplitLayer<>::Backward_gpu() *** SIGSEGV (@0x704e00000) received by PID 15831 (TID 0x2b9987dd0000) from PID 81788928; stack trace: *** @ 0x2b9991a91c30 (unknown) @ 0x83ca79 caffe::SplitLayer<>::Backward_gpu() @ 0x434047 caffe::GradientChecker<>::CheckGradientSingle() @ 0x434bc4 caffe::GradientChecker<>::CheckGradientEltwise() @ 0x43b87a caffe::SplitLayerTest_TestGradient_Test<>::TestBody() @ 0x724f43 testing::internal::HandleExceptionsInMethodIfSupported<>() @ 0x71ba87 testing::Test::Run() @ 0x71bb2e testing::TestInfo::Run() @ 0x71bc35 testing::TestCase::Run() @ 0x71ef78 testing::internal::UnitTestImpl::RunAllTests() @ 0x71f207 testing::UnitTest::Run() @ 0x422aba main @ 0x2b9991a7cec5 (unknown) @ 0x42bd97 (unknown) @ 0x0 (unknown) Segmentation fault (core dumped) make: *** [runtest] Error 139
I personally suspect there is something wrong when I access gpu_diff()[i] in a wrong way? Any help or advice would be appreciated. Thanks a lot!
BTW: I did this in cpu and all are fine (access cpu_diff()[i])
James
You cannot access GPU memory from CPU code.
All questions about usage, installation, code, and applications should be searched for and asked on the caffe-users mailing list.
For GPU code you need to first declare the array and then index:
Dtype* top_data = (*top)[i]->mutable_gpu_data();
top_data[idx] = value;
since the GPU memory can't be directly indexed, unlike the CPU memory.
@shelhamer Thanks a lot. That's really helpful! I also noticed that even in some .cu file, we can also access CPU data, like in euclidean_loss_layer.cu, we can call top[0]->cpu_data()[0], does this mean we could treat CPU and GPU data as the same in value sense? So when I can't access gpu_data()[i], I could simply use cpu_data()[i] instead?
@sguada sorry, I thought this would be some development question :)
@jamesguoxin the blob will communicate the data between the CPU and GPU as-needed while avoiding unnecessary round trips. The CPU and GPU data data are always synchronized in this way, but when computation switches between CPU and GPU there is a communication cost. See the caffe tutorial slides and documentation for more on this.
@sguada I think this might have just been caught in the caffe-users cleanup -- thanks for closing those off-topic issues! -- but to me this is a development question as it's about blob memory and layer code.
@jamesguoxin sorry about the confusion, if you need this issue to be re-opened just let me know
@shelhamer Thanks for your clarification! I understand now :+1:
@sguada No worries, I've solved the problem, it could stay closed :)
I0912 14:49:46.450429 22519 net.cpp:408] conv1_1 -> conv1_1 *** Aborted at 1505198986 (unix time) try "date -d @1505198986" if you are using GNU date *** PC: @ 0x7fdab620261d (unknown) *** SIGSEGV (@0x48) received by PID 22519 (TID 0x7fdab535e700) from PID 72; stack trace: *** @ 0x7fdaf84c34b0 (unknown) @ 0x7fdab620261d (unknown) @ 0x7fdab5c39426 (unknown) @ 0x7fdab5c17170 (unknown) @ 0x7fdab6215f9d (unknown) @ 0x7fdab5c1dfd2 (unknown) @ 0x7fdab5c20391 (unknown) @ 0x7fdab5c16aec __cuda_CallJitEntryPoint @ 0x7fdabdf555c2 fatBinaryCtl_Compile @ 0x7fda27782f92 (unknown) @ 0x7fda27783b13 (unknown) @ 0x7fda277841e3 (unknown) @ 0x7fda2767e277 (unknown) @ 0x7fda2767eb1d (unknown) @ 0x7fda2767da9d (unknown) @ 0x7fdaefc7d8f9 (anonymous namespace)::opencl_fn6<>::switch_fn() @ 0x7fdaefc64aa9 cv::ocl::Context::getDefault() @ 0x7fdaefc662eb cv::ocl::Device::getDefault() @ 0x7fdaefc6634d cv::ocl::useOpenCL() @ 0x7fdaedf6eb2a cv::cvtColor() @ 0x7fdafa459828 caffe::AdjustSaturation() @ 0x7fdafa45e95b caffe::RandomSaturation() @ 0x7fdafa45f11f caffe::ApplyDistort() @ 0x7fdafa61fee2 caffe::DataTransformer<>::DistortImage() @ 0x7fdafa5ae2f6 caffe::AnnotatedDataLayer<>::load_batch() @ 0x7fdafa54c6cf caffe::BasePrefetchingDataLayer<>::InternalThreadEntry() @ 0x7fdafa6308d5 caffe::InternalThread::entry() @ 0x7fdaed5495d5 (unknown) @ 0x7fdae5d166ba start_thread @ 0x7fdaf85953dd clone @ 0x0 (unknown) Segmentation fault (core dumped)
I meet this alike problem,how to solve it? My OS is ubuntun16.04,
@pengyulong I meet this alike problem,have you solve it ?