Skip to content

Commit a6247e6

Browse files
committed
Implement the previously discussed changes
When registering an array now, you pass in the number of elements and a name as description. This gets stored for each passed in array. Later the index can be reconstructed by taking a look at the current memory address and the size of a single element which should be in that memory region.
1 parent 2e5c53a commit a6247e6

2 files changed

Lines changed: 44 additions & 27 deletions

File tree

examples/basic/normal.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ int main(){
3737
CudaMemAccessStorage<int> memAccessStorage(10000);
3838

3939

40-
CudaMemAccessLogger<int> input(d_input, &memAccessStorage);
41-
CudaMemAccessLogger<int> output(d_output, &memAccessStorage);
40+
CudaMemAccessLogger<int> input(d_input, prob_size, "Input Datastructure", &memAccessStorage);
41+
CudaMemAccessLogger<int> output(d_output, prob_size, "Output Datastructure", &memAccessStorage);
4242

4343
constexpr int threads = 32;
4444
constexpr int blocks = (prob_size/threads)+1;

include/cuda_mav.cuh

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ private:
8686
// Have a pointer to a list of memory access logs on the host
8787
MemoryAccessLog *h_memoryAccessLog = nullptr;
8888

89+
// Store the memory regions by storing the starting address, the amount of elements, the size of a single element and a name
90+
std::vector<std::tuple<T*, size_t, size_t, std::string>> memoryRegions;
8991

9092
// Store if memory was fetched from the device
9193
bool fetchedFromDevice = false;
@@ -125,8 +127,17 @@ private:
125127
htmlStream << "<h3>Warp Size</h3>" << std::endl;
126128
htmlStream << "<p>Warp Size: " << settingsStruct.warpSize << "</p>" << std::endl;
127129

130+
// Add a section for each memory region and its name
131+
htmlStream << "<h1>Memory Regions</h1>" << std::endl;
128132

129-
// Add the section for the read logs
133+
for (auto region : memoryRegions) {
134+
htmlStream << "<h2>Region: " << std::get<3>(region) << "</h2>" << std::endl;
135+
htmlStream << "<p>Start Address: " << std::get<0>(region) << " (End Address:) " << std::get<0>(region)+ std::get<1>(region) * std::get<2>(region) << "</p>" << std::endl;
136+
htmlStream << "<p>Number of Elements: " << std::get<1>(region) << "</p>" << std::endl;
137+
htmlStream << "<p>Size of Single Element: " << std::get<2>(region) << "</p>" << std::endl;
138+
}
139+
140+
// Add the section for the memory access logs
130141
htmlStream << "<h2>Memory Accesses</h2>" << std::endl;
131142

132143
// Add the table for the read logs
@@ -187,6 +198,25 @@ private:
187198
fetchedFromDevice = true;
188199
}
189200

201+
__device__ int getStorageIndex() {// Atomically increase the currentSize by 1
202+
int current_index = atomicAdd(&d_constantData->currentSize, 1);
203+
204+
// First check if the currentSize is zero, if so we need to initialize the additional data variables, needed later to restore the data
205+
if (current_index == 0) {
206+
// Store the grid dimensions
207+
d_constantData->gridDimX = gridDim.x;
208+
d_constantData->gridDimY = gridDim.y;
209+
d_constantData->gridDimZ = gridDim.z;
210+
// Store the block dimensions
211+
d_constantData->blockDimX = blockDim.x;
212+
d_constantData->blockDimY = blockDim.y;
213+
d_constantData->blockDimZ = blockDim.z;
214+
// Store the warp size
215+
d_constantData->warpSize = warpSize;
216+
}
217+
return current_index;
218+
}
219+
190220
CudaMemAccessStorage<T> *d_this;
191221

192222
public:
@@ -228,7 +258,11 @@ public:
228258

229259
}
230260

231-
__host__ void free() {
261+
__host__ void registerArray(T *array, size_t size, std::string name = "") {
262+
memoryRegions.push_back(std::make_tuple(array, size, sizeof(array[0]), name));
263+
}
264+
265+
__host__ ~CudaMemAccessStorage() {
232266
// Free the memory on the device
233267
checkCudaError(cudaFree(d_memoryAccessLog), "Could not free memory access logs on device.");
234268
checkCudaError(cudaFree(d_constantData), "Could not free constant data on device.");
@@ -239,26 +273,7 @@ public:
239273
delete h_constantData;
240274
}
241275

242-
__device__ int getStorageIndex() {// Atomically increase the currentSize by 1
243-
int current_index = atomicAdd(&d_constantData->currentSize, 1);
244-
245-
// First check if the currentSize is zero, if so we need to initialize the additional data variables, needed later to restore the data
246-
if (current_index == 0) {
247-
// Store the grid dimensions
248-
d_constantData->gridDimX = gridDim.x;
249-
d_constantData->gridDimY = gridDim.y;
250-
d_constantData->gridDimZ = gridDim.z;
251-
// Store the block dimensions
252-
d_constantData->blockDimX = blockDim.x;
253-
d_constantData->blockDimY = blockDim.y;
254-
d_constantData->blockDimZ = blockDim.z;
255-
// Store the warp size
256-
d_constantData->warpSize = warpSize;
257-
}
258-
return current_index;
259-
}
260-
261-
__host__ __device__ CudaMemAccessStorage<T> *getDevicePointer() const {
276+
__host__ __device__ CudaMemAccessStorage<T> *getDevicePointer() {
262277
return d_this;
263278
}
264279

@@ -439,7 +454,7 @@ public:
439454
}
440455

441456
// Constructor which allocates the memory on the device
442-
__host__ CudaMemAccessLogger(T *array_data, CudaMemAccessStorage<T> *storage = nullptr) {
457+
__host__ CudaMemAccessLogger(T *array_data, size_t array_length, std::string description_name, CudaMemAccessStorage<T> *storage = nullptr) {
443458

444459
// Store the passed data pointer within the class
445460
d_data = array_data;
@@ -458,6 +473,8 @@ public:
458473
// Also store the device pointer
459474
d_storage = h_storage->getDevicePointer();
460475

476+
h_storage->registerArray(array_data, array_length, description_name);
477+
461478

462479

463480
// We need to create the copy of this class on the device
@@ -527,7 +544,7 @@ public:
527544
return d_this;
528545
}
529546

530-
CudaMemAccessStorage<T> getStorage() {
531-
return *h_storage;
547+
CudaMemAccessStorage<T>* getStorage() {
548+
return h_storage;
532549
}
533550
};

0 commit comments

Comments
 (0)