Skip to content

Commit

Permalink
Unify memory block API.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehpor committed Dec 23, 2024
1 parent cb496f8 commit af420b2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
6 changes: 4 additions & 2 deletions catkit_core/CudaSharedMemory.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef CUDA_SHARED_MEMORY_H
#define CUDA_SHARED_MEMORY_H

#include "Memory.h"

#include <memory>

#ifdef HAVE_CUDA
Expand All @@ -11,7 +13,7 @@ typedef cudaIpcMemHandle_t CudaIpcHandle;
typedef char CudaIpcHandle[64];
#endif

class CudaSharedMemory
class CudaSharedMemory : public Memory
{
private:
CudaSharedMemory(const CudaIpcHandle &ipc_handle, void *device_pointer=nullptr);
Expand All @@ -22,7 +24,7 @@ class CudaSharedMemory
static std::shared_ptr<CudaSharedMemory> Create(size_t num_bytes_in_buffer);
static std::shared_ptr<CudaSharedMemory> Open(const CudaIpcHandle &ipc_handle);

void *GetAddress();
void *GetAddress(std::size_t offset = 0) override;
};

#endif // CUDA_SHARED_MEMORY_H
16 changes: 16 additions & 0 deletions catkit_core/Memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef MEMORY_H
#define MEMORY_H

#include <cstddef>

class Memory
{
public:
virtual ~Memory()
{
}

virtual void *GetAddress(std::size_t offset = 0) = 0;
};

#endif // MEMORY_H
4 changes: 2 additions & 2 deletions catkit_core/SharedMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ SharedMemory::SharedMemory(const std::string &id, FileObject file, bool is_owner
throw std::runtime_error("Something went wrong while mapping shared memory file.");
}

void *SharedMemory::GetAddress()
void *SharedMemory::GetAddress(std::size_t offset)
{
return m_Buffer;
return static_cast<char *>(m_Buffer) + offset;
}
6 changes: 4 additions & 2 deletions catkit_core/SharedMemory.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef SHARED_MEMORY_H
#define SHARED_MEMORY_H

#include "Memory.h"

#include <memory>
#include <string>

Expand All @@ -21,7 +23,7 @@
typedef int FileObject;
#endif

class SharedMemory
class SharedMemory : public Memory
{
private:
SharedMemory(const std::string &id, FileObject file, bool is_owner);
Expand All @@ -32,7 +34,7 @@ class SharedMemory
static std::shared_ptr<SharedMemory> Create(const std::string &id, size_t num_bytes_in_buffer);
static std::shared_ptr<SharedMemory> Open(const std::string &id);

void *GetAddress();
void *GetAddress(std::size_t offset = 0) override;

private:
std::string m_Id;
Expand Down

0 comments on commit af420b2

Please sign in to comment.