strorage.cpp 1.71 KB
Newer Older
thatPepe's avatar
thatPepe committed
1
#include "../allocator.hpp"
PanZezhong's avatar
init  
PanZezhong committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "../tensor.hpp"

std::shared_ptr<Storage> Storage::create(size_t size) {
    auto storage = std::make_shared<Storage>();
    RUN_INFINI(infinirtMalloc(&storage->memory, size));
    storage->size = size;
    RUN_INFINI(infinirtGetDevice(&storage->device_type, &storage->device_id));
    return storage;
}

std::shared_ptr<Storage> Storage::createAsync(size_t size, infinirtStream_t stream) {
    auto storage = std::make_shared<Storage>();
    RUN_INFINI(infinirtMallocAsync(&storage->memory, size, stream));
    storage->size = size;
    RUN_INFINI(infinirtGetDevice(&storage->device_type, &storage->device_id));
    return storage;
}

thatPepe's avatar
thatPepe committed
20
21
22
23
24
25
26
27
28
29
30
31
32
std::shared_ptr<Storage> Storage::createFromPool(size_t size, std::shared_ptr<MemoryPool> pool) {
    auto storage = std::make_shared<Storage>();
    storage->memory_pool = pool;
    if (pool) {
        storage->memory = pool->alloc(size);
    } else {
        RUN_INFINI(infinirtMalloc(&storage->memory, size));
    }
    storage->size = size;
    RUN_INFINI(infinirtGetDevice(&storage->device_type, &storage->device_id));
    return storage;
}

PanZezhong's avatar
init  
PanZezhong committed
33
34
35
36
37
38
std::shared_ptr<Storage> Storage::createHost(size_t size) {
    auto storage = std::make_shared<Storage>();
    RUN_INFINI(infinirtMallocHost(&storage->memory, size));
    storage->size = size;
    storage->device_type = INFINI_DEVICE_CPU;
    storage->device_id = 0;
thatPepe's avatar
thatPepe committed
39
    storage->memory_pool = nullptr; // No pool for host memory
PanZezhong's avatar
init  
PanZezhong committed
40
41
42
43
44
45
46
    return storage;
}

Storage::~Storage() {
    if (device_type == INFINI_DEVICE_CPU) {
        RUN_INFINI(infinirtFreeHost(memory));
    } else {
thatPepe's avatar
thatPepe committed
47
48
49
50
51
        if (memory_pool) {
            memory_pool->release(memory);
        } else {
            RUN_INFINI(infinirtFree(memory));
        }
PanZezhong's avatar
init  
PanZezhong committed
52
53
    }
}