Skip to content

Commit 8e8a7e1

Browse files
committed
Replace the strings with struct for function Append.
1 parent 0f3722d commit 8e8a7e1

File tree

6 files changed

+68
-82
lines changed

6 files changed

+68
-82
lines changed

include/singa/core/common.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ typedef struct _Opencl { } Opencl;
5353
} // namespace lang
5454

5555
class Device;
56+
struct DeviceOptInfoToAppend;
57+
58+
5659
/// Block represent a chunk of memory (on device or host).
5760
class Block {
5861
public:
@@ -97,6 +100,16 @@ class Block {
97100
std::atomic<int> ref_count_;
98101
};
99102

103+
// struct for Append purpose in device class.
104+
struct DeviceOptInfoToAppend{
105+
string operation_type;
106+
string block_ptr;
107+
int size;
108+
long t = (std::chrono::system_clock::now()).time_since_epoch().count();
109+
110+
DeviceOptInfoToAppend(string opt_type, string ptr,int s):operation_type(opt_type),block_ptr(ptr),size(s){}
111+
};
112+
100113
typedef struct _Context {
101114
std::mt19937 random_generator;
102115
#ifdef USE_CUDA

include/singa/core/device.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class Device {
6666
/// Called by Tensor.
6767
void FreeBlock(Block* block);
6868

69-
void AppendInfo(string block_info);
7069
void* UpdateGpuPtrInfo(const Block* block_ptr);
7170

7271
/// Return the size (bytes) of memory in use
@@ -107,7 +106,7 @@ class Device {
107106
int id() const { return id_; }
108107

109108
virtual void* UpdateGpuPtr(const Block* block_ptr) = 0;
110-
109+
virtual void Append(DeviceOptInfoToAppend dev_opt_info) = 0;
111110
private:
112111
Device() {};
113112

@@ -124,7 +123,7 @@ class Device {
124123
/// Free device memory.
125124
virtual void Free(void* ptr) = 0;
126125
virtual void AppendAfterMalloc(Block* block,void* data_ptr,int size) = 0;
127-
virtual void Append(string block_info) = 0;
126+
128127

129128
protected:
130129
int id_ = 0;
@@ -154,6 +153,7 @@ class CppCPU : public Device {
154153

155154
std::shared_ptr<Device> host() const override { return defaultDevice;}
156155
void SetRandSeed(unsigned seed) override;
156+
void Append(DeviceOptInfoToAppend dev_opt_info) override {}
157157

158158
protected:
159159
void DoExec(function<void(Context*)>&& fn, int executor) override;
@@ -167,7 +167,7 @@ class CppCPU : public Device {
167167
/// Free cpu memory.
168168
void Free(void* ptr) override;
169169
void AppendAfterMalloc(Block* block,void* data_ptr,int size) override {}
170-
void Append(string block_info) override {}
170+
171171
void* UpdateGpuPtr(const Block* block_ptr) override {}
172172

173173
};
@@ -188,6 +188,8 @@ class CudaGPU : public Device {
188188

189189
void SetRandSeed(unsigned seed) override;
190190
size_t GetAllocatedMem() override;
191+
void Append(DeviceOptInfoToAppend dev_opt_info) override {}
192+
191193

192194
protected:
193195
void DoExec(function<void(Context*)>&& fn, int executor) override;
@@ -201,7 +203,6 @@ class CudaGPU : public Device {
201203
/// Free cpu memory.
202204
void Free(void* ptr) override;
203205
void AppendAfterMalloc(Block* block,void* data_ptr,int size) override {}
204-
void Append(string block_info) override;
205206
void* UpdateGpuPtr(const Block* block_ptr) override;
206207

207208
private:
@@ -284,6 +285,8 @@ class SwapGPU : public Device {
284285

285286
void SetRandSeed(unsigned seed) override;
286287
size_t GetAllocatedMem() override;
288+
//Append at every index: free, read, mutable
289+
void Append(DeviceOptInfoToAppend dev_opt_info) override;
287290

288291
protected:
289292
void DoExec(function<void(Context*)>&& fn, int executor) override;
@@ -295,10 +298,7 @@ class SwapGPU : public Device {
295298
void* Malloc(int size) override;
296299

297300
/// Free cpu memory.
298-
void Free(void* ptr) override;
299-
300-
//Append at every index: free, read, mutable
301-
void Append(string block_info) override;
301+
void Free(void* ptr) override;
302302

303303
//append info after Malloc, as Block* is not available till Malloc() done.
304304
void AppendAfterMalloc(Block* block,void* data_ptr,int size) override;
@@ -408,7 +408,7 @@ class OpenclDevice : public singa::Device {
408408
virtual void CopyDataToFrom(Block* dst, Block* src, size_t nBytes,
409409
CopyDirection direction, int dst_offset = 0,
410410
int src_offset = 0) override;
411-
411+
void Append(DeviceOptInfoToAppend dev_opt_info) override {}
412412
protected:
413413
/// The OpenCL device that this object represents.
414414
/// Each OpenclDevice contains exactly one cl::Device for the lifetime of the
@@ -439,7 +439,7 @@ class OpenclDevice : public singa::Device {
439439
/// This has the effect of freeing up device memory.
440440
void Free(void* ptr) override;
441441
void AppendAfterMalloc(Block* block,void* data_ptr,int size) override {}
442-
void Append(string block_info) override {}
442+
443443
void* UpdateGpuPtr(const Block* block_ptr) override {}
444444

445445

src/core/common/common.cc

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ void* Block::mutable_data() {
3030

3131
//Append block info: opt_type, ptr, time_stamp
3232
if (ptr_device_!=nullptr){
33-
stringstream strm2;
34-
strm2<<this;
35-
string temp_str2 = strm2.str();
36-
stringstream strm4;
37-
auto t2 = (std::chrono::system_clock::now()).time_since_epoch().count();
38-
strm4<<t2;
39-
string temp_str4 = strm4.str();
40-
string temp = "Mutable "+temp_str2+" "+temp_str4;
41-
ptr_device_->AppendInfo(temp);
33+
stringstream strm;
34+
strm<<this;
35+
string temp_str = strm.str();
36+
DeviceOptInfoToAppend dev_opt_info("Mutable", temp_str,size());
37+
auto t = (std::chrono::system_clock::now()).time_since_epoch().count();
38+
dev_opt_info.t = t;
39+
ptr_device_->Append(dev_opt_info);
4240
}
4341

4442
//update ptr after swap in done, if variable is not swapped back yet as expected.
@@ -56,16 +54,13 @@ const void* Block::data() const {
5654

5755
//Append block info: opt_type, ptr, time_stamp
5856
if (ptr_device_!=nullptr){
59-
//Append info.
60-
stringstream strm2;
61-
strm2<<this;
62-
string temp_str2 = strm2.str();
63-
stringstream strm4;
64-
auto t2 = (std::chrono::system_clock::now()).time_since_epoch().count();
65-
strm4<<t2;
66-
string temp_str4 = strm4.str();
67-
string temp = "Read "+temp_str2+" "+temp_str4;
68-
ptr_device_->AppendInfo(temp);
57+
stringstream strm;
58+
strm<<this;
59+
string temp_str = strm.str();
60+
DeviceOptInfoToAppend dev_opt_info("Read", temp_str,size());
61+
auto t = (std::chrono::system_clock::now()).time_since_epoch().count();
62+
dev_opt_info.t = t;
63+
ptr_device_->Append(dev_opt_info);
6964
}
7065

7166
//update ptr after swap in done, if variable is not swapped back yet as expected.

src/core/device/cuda_gpu.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,6 @@ void CudaGPU::Free(void* ptr) {
123123
}
124124
}
125125

126-
void CudaGPU::Append(string blockInfo){
127-
pool_->Append(blockInfo);
128-
}
129-
130126
void* CudaGPU::UpdateGpuPtr(const Block* block_){
131127
return nullptr;
132128
}

src/core/device/device.cc

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,18 @@ void Device::FreeBlock(Block* block) {
5555
Free(tempPtr);
5656

5757
//append block info for free operation.
58-
stringstream strm1;
59-
strm1<<block;
60-
string temp_str1 = strm1.str();
61-
stringstream strm4;
62-
auto t2 = (std::chrono::system_clock::now()).time_since_epoch().count();
63-
strm4<<t2;
64-
string temp_str4 = strm4.str();
65-
string blockInfo ="Free "+temp_str1+" "+temp_str4;
66-
Append(blockInfo);
58+
stringstream strm;
59+
strm<<block;
60+
string temp_str = strm.str();
61+
DeviceOptInfoToAppend dev_opt_info("Free", temp_str,block->size());
62+
auto t = (std::chrono::system_clock::now()).time_since_epoch().count();
63+
dev_opt_info.t = t;
64+
Append(dev_opt_info);
6765

6866
delete block;
6967
}
7068
}
7169

72-
void Device::AppendInfo(string blockInfo){
73-
Append(blockInfo);
74-
}
7570

7671
void* Device::UpdateGpuPtrInfo(const Block* block_){
7772
return UpdateGpuPtr(block_);

src/core/device/swap_gpu.cc

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -904,18 +904,13 @@ void SwapGPU::AppendAfterMalloc(Block* block_ptr,void* data_ptr,int size){
904904
*/
905905

906906
//append info
907-
stringstream strm1;
908-
strm1<<size;
909-
string temp_str1 = strm1.str();
910-
stringstream strm3;
911-
strm3<<block_ptr;
912-
string temp_str3 = strm3.str();
913-
stringstream strm4;
914-
auto t2 = (std::chrono::system_clock::now()).time_since_epoch().count();
915-
strm4<<t2;
916-
string temp_str4 = strm4.str();
917-
string block_info ="Malloc "+temp_str3+" "+temp_str1+" "+temp_str4;
918-
Append(block_info);
907+
stringstream strm;
908+
strm<<block_ptr;
909+
string temp_str = strm.str();
910+
DeviceOptInfoToAppend dev_opt_info("Malloc", temp_str,size);
911+
auto t = (std::chrono::system_clock::now()).time_since_epoch().count();
912+
dev_opt_info.t = t;
913+
Append(dev_opt_info);
919914

920915
}
921916

@@ -994,49 +989,39 @@ void SwapGPU::DeploySwapExec(int r_global_index){
994989
}
995990
}
996991

997-
void SwapGPU::Append(string block_info){
998-
/*
999-
Append Operation block info after each operation
1000-
Meantime execute following operations:
1001-
insert size for non-malloc operations
1002-
update global memory load
1003-
control swap flag on and off
1004-
update table_meta and table_sched
1005-
deploy swap at every index.
1006-
test moved from start of malloc/free to end of append, only global_index+1 changed
1007-
call PoolOpt to Construct Pool
1008-
*/
992+
void SwapGPU::Append(DeviceOptInfoToAppend dev_opt_info){
1009993

1010-
vector<string> v = SplitOptString(block_info, " ");
994+
//convert block_ptr from string to Block*
1011995
void* temp_ptr;
1012-
stringstream convert(v[1]);
996+
stringstream convert(dev_opt_info.block_ptr);
1013997
convert>>temp_ptr;
1014998
auto block_ptr = static_cast<Block*>(temp_ptr);
1015-
1016-
// insert size, malloc : flag, block_, size, t; others: insert size t.
1017-
if (v.size() != 4) {
1018-
stringstream strm1;
1019-
strm1<<block_ptr->size();
1020-
string temp_str1 = strm1.str();
1021-
block_info = v[0] + ' ' + v[1] + ' ' + temp_str1 + ' ' + v[2];
1022-
}
1023999

10241000
// update global load
10251001
if (iteration_length < iteration_length_threshold){
1026-
if (v[0] == "Malloc"){
1002+
if (dev_opt_info.operation_type == "Malloc"){
10271003
if (global_load.size()>0){
10281004
global_load.push_back(global_load[global_load.size()-1]+block_ptr->size());
10291005
} else {
10301006
global_load.push_back(block_ptr->size());
10311007
}
1032-
} else if (v[0] == "Free"){
1008+
} else if (dev_opt_info.operation_type == "Free"){
10331009
global_load.push_back(global_load[global_load.size()-1]-block_ptr->size());
10341010
} else {
10351011
global_load.push_back(global_load[global_load.size()-1]);
10361012
}
10371013
}
10381014

10391015
//append into vec_block
1016+
stringstream strm1;
1017+
strm1<<dev_opt_info.size;
1018+
string temp_str1 = strm1.str();
1019+
stringstream strm4;
1020+
strm4<<dev_opt_info.t;
1021+
string temp_str4 = strm4.str();
1022+
string block_info = dev_opt_info.operation_type + " " + dev_opt_info.block_ptr + " " +
1023+
temp_str1 + " " + temp_str4;
1024+
//cout<<"1 "<<block_info<<endl;
10401025
vec_block.push_back(block_info);
10411026

10421027
//change swap flag on and off
@@ -1067,6 +1052,8 @@ void SwapGPU::Append(string block_info){
10671052

10681053
}
10691054

1055+
1056+
10701057
void* SwapGPU::UpdateGpuPtr(const Block* block_ptr){
10711058
/*
10721059
in case that block is not at device memory, swapIn ad hoc.

0 commit comments

Comments
 (0)