main.cpp

 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
#include "model/model.h"
#include "memory/memory_utils.h"
#include <thread>
#include "utils/utils.h"

void ModelExecute(uint32_t deviceId, Model &model)
{
    // Initialize the model, and create the required context and stream.
    model.InitResource(deviceId);

    // Create a model graph.
    model.CreateModelGraph();

    // Create a model input and enter a value.
    model.CreateModelInput();

    // Create the output size of the model.
    model.CreateModelOutput();

    // Execute the model.
    model.Execute();

    // Print the values of the output tensors.
    PrintOutTensorValue(model.modelOutTensors_.at(0));

    // Release the resources.
    model.FreeResource();
}

int main()
{
    // Initialize AscendCL.
    auto ret = aclInit(nullptr);
    CHECK_RET(ret, "aclInit failed. ret: " + std::to_string(ret));

    // Create a memory pool.
    size_t poolSize = 104857600; // Allocated memory 100 MB.
    GetMemoryManager().CreateMemoryPool(poolSize);

    // Create a model graph.
    uint32_t deviceCount = 0;
    CHECK_RET(aclrtGetDeviceCount(&deviceCount), "get devicecount fail");
    std::vector<Model> modelArray(deviceCount);

    // Deliver the model graph in multiple threads.
    std::vector<std::thread> threadArray(deviceCount);
    for (size_t i = 0; i < deviceCount; i++) {
        Model &model = modelArray.at(i);
        threadArray.at(i) = std::thread([i, &model]{ModelExecute(i, model);}); // Create the thread and bind the function.
    }
    for (size_t i = 0; i < deviceCount; i++) {
        threadArray.at(i).join(); // Wait for the child thread to end.
    }

    aclFinalize();
    return 0;
}