strorage.cpp 1.75 KB
Newer Older
thatPepe's avatar
thatPepe committed
1
#include "../allocator.hpp"
PanZezhong's avatar
init  
PanZezhong committed
2
3
4
#include "../tensor.hpp"

std::shared_ptr<Storage> Storage::create(size_t size) {
wooway777's avatar
wooway777 committed
5
    auto storage = std::shared_ptr<Storage>(new Storage());
PanZezhong's avatar
init  
PanZezhong committed
6
7
8
9
10
11
12
    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) {
wooway777's avatar
wooway777 committed
13
    auto storage = std::shared_ptr<Storage>(new Storage());
PanZezhong's avatar
init  
PanZezhong committed
14
15
16
17
18
19
    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
std::shared_ptr<Storage> Storage::createFromPool(size_t size, std::shared_ptr<MemoryPool> pool) {
wooway777's avatar
wooway777 committed
21
    auto storage = std::shared_ptr<Storage>(new Storage());
thatPepe's avatar
thatPepe committed
22
23
24
25
26
27
28
29
30
31
32
    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
std::shared_ptr<Storage> Storage::createHost(size_t size) {
wooway777's avatar
wooway777 committed
34
    auto storage = std::shared_ptr<Storage>(new Storage());
PanZezhong's avatar
init  
PanZezhong committed
35
36
37
38
    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
    return storage;
}

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