Repository navigation
Expand file tree
/
Copy pathMemoryBridge.cpp
More file actions
97 lines (81 loc) · 3.23 KB
/
Copy pathMemoryBridge.cpp
File metadata and controls
97 lines (81 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "MemoryBridge.h" // Include the MemoryBridge header
#include "Metadata.h" // Include the Metadata structure definition
#include <cuda_runtime.h> // Include CUDA runtime API for GPU operations
#include <chrono> // For time-related operations
#include <iostream> // For console output
#include <cstring> // For memcpy
#include <mutex> // For thread-safe access
MemoryBridge::MemoryBridge(size_t bufferSize)
: bufferSize(bufferSize), gpuBuffer(nullptr) {
}
MemoryBridge::~MemoryBridge() {
ReleaseResources();
}
bool MemoryBridge::Initialize() {
// Allocate GPU memory
cudaError_t err = cudaMalloc(&gpuBuffer, bufferSize);
if (err != cudaSuccess) {
std::cerr << "Error: Failed to allocate GPU memory. CUDA error: " << cudaGetErrorString(err) << std::endl;
return false;
}
std::cout << "GPU memory allocated successfully. Size: " << bufferSize << " bytes\n";
return true;
}
void MemoryBridge::ReleaseResources() {
if (gpuBuffer) {
cudaFree(gpuBuffer);
gpuBuffer = nullptr;
std::cout << "GPU memory released.\n";
}
}
bool MemoryBridge::CopyDataToGPU(const std::vector<float>& data) {
if (!gpuBuffer) {
std::cerr << "Error: GPU buffer not initialized.\n";
return false;
}
if (data.size() * sizeof(float) > bufferSize) {
std::cerr << "Error: Data size exceeds buffer size.\n";
return false;
}
cudaError_t err = cudaMemcpy(gpuBuffer, data.data(), data.size() * sizeof(float), cudaMemcpyHostToDevice);
if (err != cudaSuccess) {
std::cerr << "Error: Failed to copy data to GPU. CUDA error: " << cudaGetErrorString(err) << std::endl;
return false;
}
std::cout << "Data successfully copied to GPU.\n";
return true;
}
bool MemoryBridge::RetrieveDataFromGPU(std::vector<float>& data) {
if (!gpuBuffer) {
std::cerr << "Error: GPU buffer not initialized.\n";
return false;
}
data.resize(bufferSize / sizeof(float));
cudaError_t err = cudaMemcpy(data.data(), gpuBuffer, bufferSize, cudaMemcpyDeviceToHost);
if (err != cudaSuccess) {
std::cerr << "Error: Failed to retrieve data from GPU. CUDA error: " << cudaGetErrorString(err) << std::endl;
return false;
}
std::cout << "Data successfully retrieved from GPU.\n";
return true;
}
void MemoryBridge::CacheMetadata(int threadID, const Metadata& metadata) {
std::lock_guard<std::mutex> lock(cacheMutex);
metadataCache[threadID] = metadata;
std::cout << "Metadata cached for thread ID: " << threadID << "\n";
}
bool MemoryBridge::RetrieveMetadata(int threadID, Metadata& metadata) {
std::lock_guard<std::mutex> lock(cacheMutex);
auto it = metadataCache.find(threadID);
if (it != metadataCache.end()) {
metadata = it->second;
return true;
}
std::cerr << "Error: Metadata not found for thread ID: " << threadID << "\n";
return false;
}
double MemoryBridge::GetCurrentTimestamp() {
auto now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
return std::chrono::duration<double>(duration).count();
}