mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-12-22 14:24:22 +00:00
Add simple Tensor class
This commit is contained in:
21
include/backend/backend.hpp
Normal file
21
include/backend/backend.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace CUDANet::Backend
|
||||
{
|
||||
|
||||
class IBackend
|
||||
{
|
||||
public:
|
||||
|
||||
// Memory management
|
||||
virtual void* allocate(size_t bytes) = 0;
|
||||
virtual void deallocate(void* ptr) = 0;
|
||||
|
||||
virtual void copyToDevice(void* devicePtr, const void* hostPtr, size_t bytes) = 0;
|
||||
virtual void copyToHost(void* hostPtr, const void* devicePtr, size_t bytes) = 0;
|
||||
|
||||
};
|
||||
|
||||
} // namespace CUDANet::Backend
|
||||
38
include/backend/tensor.hpp
Normal file
38
include/backend/tensor.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
#include "backend/backend.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace CUDANet::Backend
|
||||
{
|
||||
|
||||
enum class DType
|
||||
{
|
||||
FLOAT32,
|
||||
// FLOAT16, // Not implemented yet
|
||||
// INT32, // Not implemented yet
|
||||
};
|
||||
|
||||
typedef std::vector<size_t> Shape;
|
||||
|
||||
class Tensor
|
||||
{
|
||||
public:
|
||||
Tensor(Shape shape, DType dtype, IBackend* backend);
|
||||
~Tensor();
|
||||
|
||||
void* allocate();
|
||||
void deallocate();
|
||||
|
||||
void toDevice(const void* hostPtr);
|
||||
void toHost(void* hostPtr);
|
||||
|
||||
private:
|
||||
Shape shape;
|
||||
DType dtype;
|
||||
IBackend* backend;
|
||||
void* devicePtr;
|
||||
void* hostPtr;
|
||||
};
|
||||
|
||||
} // namespace CUDANet::Backend
|
||||
11
src/backends/tensor.cpp
Normal file
11
src/backends/tensor.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "backend/tensor.hpp"
|
||||
|
||||
using namespace CUDANet::Backend;
|
||||
|
||||
Tensor::Tensor(Shape shape, DType dtype, IBackend* backend)
|
||||
: shape(shape), dtype(dtype), backend(backend), devicePtr(nullptr), hostPtr(nullptr) {}
|
||||
|
||||
Tensor::~Tensor() {
|
||||
deallocate();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user