@@ -36,49 +36,6 @@ class TBlob;
36
36
*/
37
37
class Blob {
38
38
public:
39
- // This proxy makes sure that we can't rely on cached values while pointer
40
- // to data is being used and data potentially might be changed.
41
- // When pointer is actually given, proxy flushes the cache.
42
- // There are use cases where we "preliminary convert data" but don't change it yet.
43
- // In such cases cache is still valid until we really change data.
44
- // For example, this line doesn't change blob's state:
45
- // Blob::PtrProxy<Ftype> top_data = top[i]->mutable_gpu_data<Ftype>();
46
- // The state will be changed at the moment of passing a raw pointer to,
47
- // let say, CuDNN routine.
48
- template <typename Ptype>
49
- class PtrProxy {
50
- public:
51
- PtrProxy () : tensor_(), is_gpu_(false ), zero_new_mem_(true ) {}
52
-
53
- PtrProxy (shared_ptr<Tensor> tensor, bool is_gpu, bool zero_new_mem = true )
54
- : tensor_(tensor), is_gpu_(is_gpu), zero_new_mem_(zero_new_mem) {}
55
-
56
- operator Ptype*() {
57
- CHECK (tensor_);
58
- return reinterpret_cast <Ptype*>(tensor_->mutable_memory (tp<Ptype>(), is_gpu_, zero_new_mem_));
59
- }
60
-
61
- ~PtrProxy () {}
62
-
63
- PtrProxy (PtrProxy&& other) : tensor_(std::move(other.tensor_)), is_gpu_(other.is_gpu_),
64
- zero_new_mem_ (other.zero_new_mem_) {}
65
-
66
- PtrProxy& operator =(PtrProxy&& other) {
67
- tensor_ = std::move (other.tensor_ );
68
- is_gpu_ = other.is_gpu_ ;
69
- zero_new_mem_ = other.zero_new_mem_ ;
70
- return *this ;
71
- }
72
-
73
- PtrProxy (const PtrProxy&) = delete ;
74
- PtrProxy& operator =(const PtrProxy& other) = delete ;
75
-
76
- private:
77
- shared_ptr<Tensor> tensor_;
78
- bool is_gpu_;
79
- bool zero_new_mem_;
80
- };
81
-
82
39
void Swap (Blob& other) noexcept {
83
40
std::swap (data_tensor_, other.data_tensor_ );
84
41
std::swap (diff_tensor_, other.diff_tensor_ );
@@ -387,26 +344,15 @@ class Blob {
387
344
}
388
345
389
346
template <typename Dtype>
390
- PtrProxy< Dtype> mutable_cpu_data (bool zero_new_mem = true ) {
347
+ Dtype* mutable_cpu_data () {
391
348
convert_data (tp<Dtype>());
392
- return PtrProxy <Dtype>(data_tensor_, false , zero_new_mem );
349
+ return static_cast <Dtype* >(data_tensor_-> mutable_synced_mem ()-> mutable_cpu_data () );
393
350
}
394
351
395
352
template <typename Dtype>
396
- PtrProxy< Dtype> mutable_cpu_diff (bool zero_new_mem = true ) {
353
+ Dtype* mutable_cpu_diff () {
397
354
convert_diff (tp<Dtype>());
398
- return PtrProxy<Dtype>(diff_tensor_, false , zero_new_mem);
399
- }
400
-
401
- // pycaffe needs these two, do NOT use them anywhere else
402
- template <typename Dtype>
403
- Dtype* mutable_cpu_data_raw () {
404
- return (Dtype*) Blob::mutable_cpu_data<Dtype>();
405
- }
406
-
407
- template <typename Dtype>
408
- Dtype* mutable_cpu_diff_raw () {
409
- return (Dtype*) Blob::mutable_cpu_diff<Dtype>();
355
+ return static_cast <Dtype*>(diff_tensor_->mutable_synced_mem ()->mutable_cpu_data ());
410
356
}
411
357
412
358
// Element-wise accessor. Might be slow due to syncing from GPU to CPU.
@@ -572,15 +518,15 @@ class Blob {
572
518
}
573
519
574
520
template <typename Dtype>
575
- PtrProxy< Dtype> mutable_gpu_data (bool zero_new_mem = true ) {
521
+ Dtype* mutable_gpu_data () {
576
522
convert_data (tp<Dtype>());
577
- return PtrProxy <Dtype>(data_tensor_, true , zero_new_mem );
523
+ return static_cast <Dtype* >(data_tensor_-> mutable_synced_mem ()-> mutable_gpu_data () );
578
524
}
579
525
580
526
template <typename Dtype>
581
- PtrProxy< Dtype> mutable_gpu_diff (bool zero_new_mem = true ) {
527
+ Dtype* mutable_gpu_diff () {
582
528
convert_diff (tp<Dtype>());
583
- return PtrProxy <Dtype>(diff_tensor_, true , zero_new_mem );
529
+ return static_cast <Dtype* >(diff_tensor_-> mutable_synced_mem ()-> mutable_gpu_data () );
584
530
}
585
531
586
532
void async_gpu_push () {
@@ -701,19 +647,18 @@ class TBlob : public Blob {
701
647
}
702
648
703
649
template <typename T = Dtype>
704
- PtrProxy <T> mutable_cpu_data (bool zero_new_mem = true ) {
650
+ T* mutable_cpu_data () {
705
651
check_integrity (true , data_type (), tp<T>());
706
- return Blob::mutable_cpu_data<T>(zero_new_mem );
652
+ return Blob::mutable_cpu_data<T>();
707
653
}
708
654
709
655
template <typename T = Dtype>
710
- PtrProxy <T> mutable_cpu_diff (bool zero_new_mem = true ) {
656
+ T* mutable_cpu_diff () {
711
657
check_integrity (false , diff_type (), tp<T>());
712
- return Blob::mutable_cpu_diff<T>(zero_new_mem );
658
+ return Blob::mutable_cpu_diff<T>();
713
659
}
714
660
715
661
#ifndef CPU_ONLY
716
-
717
662
template <typename T = Dtype>
718
663
const T* gpu_data () const {
719
664
check_integrity (true , data_type (), tp<T>());
@@ -727,15 +672,15 @@ class TBlob : public Blob {
727
672
}
728
673
729
674
template <typename T = Dtype>
730
- PtrProxy <T> mutable_gpu_data (bool zero_new_mem = true ) {
675
+ T* mutable_gpu_data () {
731
676
check_integrity (true , data_type (), tp<T>());
732
- return Blob::mutable_gpu_data<T>(zero_new_mem );
677
+ return Blob::mutable_gpu_data<T>();
733
678
}
734
679
735
680
template <typename T = Dtype>
736
- PtrProxy <T> mutable_gpu_diff (bool zero_new_mem = true ) {
681
+ T* mutable_gpu_diff () {
737
682
check_integrity (false , diff_type (), tp<T>());
738
- return Blob::mutable_gpu_diff<T>(zero_new_mem );
683
+ return Blob::mutable_gpu_diff<T>();
739
684
}
740
685
#endif
741
686
0 commit comments