main.cc 9.44 KB
Newer Older
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "memory_test.h"
#include "test_tensor_destructor.h"
#include <iostream>
#include <memory>
#include <spdlog/spdlog.h>
#include <vector>

struct ParsedArgs {
    infiniDevice_t device_type = INFINI_DEVICE_CPU;
    bool run_basic = true;
    bool run_concurrency = true;
    bool run_exception_safety = true;
    bool run_memory_leak = true;
    bool run_performance = true;
    bool run_stress = true;
    int num_threads = 4;
    int iterations = 1000;
};

void printUsage() {
    std::cout << "Usage:" << std::endl
              << "  infinicore-test [--<device>] [--test <test_name>] [--threads <num>] [--iterations <num>]" << std::endl
              << std::endl
              << "Options:" << std::endl
              << "  --<device>        Specify the device type (default: cpu)" << std::endl
              << "  --test <name>     Run specific test (basic|concurrency|exception|leak|performance|stress|all)" << std::endl
              << "  --threads <num>   Number of threads for concurrency tests (default: 4)" << std::endl
              << "  --iterations <num> Number of iterations for stress tests (default: 1000)" << std::endl
              << "  --help            Show this help message" << std::endl
              << std::endl
              << "Available devices:" << std::endl
              << "  cpu         - Default" << std::endl
              << "  nvidia" << std::endl
              << "  cambricon" << std::endl
              << "  ascend" << std::endl
              << "  metax" << std::endl
              << "  moore" << std::endl
              << "  iluvatar" << std::endl
              << "  kunlun" << std::endl
              << "  hygon" << std::endl
              << std::endl
              << "Available tests:" << std::endl
              << "  basic       - Basic memory allocation and deallocation tests" << std::endl
              << "  concurrency - Thread safety and concurrent access tests" << std::endl
              << "  exception   - Exception safety tests" << std::endl
              << "  leak        - Memory leak detection tests" << std::endl
              << "  performance - Performance and benchmark tests" << std::endl
              << "  stress      - Stress tests with high load" << std::endl
              << "  all         - Run all tests (default)" << std::endl
              << std::endl;
    exit(EXIT_SUCCESS);
}

ParsedArgs parseArgs(int argc, char *argv[]) {
    ParsedArgs args;

    for (int i = 1; i < argc; ++i) {
        std::string arg = argv[i];

        if (arg == "--help" || arg == "-h") {
            printUsage();
        } else if (arg == "--cpu") {
            args.device_type = INFINI_DEVICE_CPU;
        } else if (arg == "--nvidia") {
            args.device_type = INFINI_DEVICE_NVIDIA;
        } else if (arg == "--cambricon") {
            args.device_type = INFINI_DEVICE_CAMBRICON;
        } else if (arg == "--ascend") {
            args.device_type = INFINI_DEVICE_ASCEND;
        } else if (arg == "--metax") {
            args.device_type = INFINI_DEVICE_METAX;
        } else if (arg == "--moore") {
            args.device_type = INFINI_DEVICE_MOORE;
        } else if (arg == "--iluvatar") {
            args.device_type = INFINI_DEVICE_ILUVATAR;
        } else if (arg == "--kunlun") {
            args.device_type = INFINI_DEVICE_KUNLUN;
        } else if (arg == "--hygon") {
            args.device_type = INFINI_DEVICE_HYGON;
        } else if (arg == "--test") {
            if (i + 1 >= argc) {
                std::cerr << "Error: --test requires a test name" << std::endl;
                exit(EXIT_FAILURE);
            }

            std::string test_name = argv[++i];
            args.run_basic = args.run_concurrency = args.run_exception_safety = args.run_memory_leak = args.run_performance = args.run_stress = false;

            if (test_name == "basic") {
                args.run_basic = true;
            } else if (test_name == "concurrency") {
                args.run_concurrency = true;
            } else if (test_name == "exception") {
                args.run_exception_safety = true;
            } else if (test_name == "leak") {
                args.run_memory_leak = true;
            } else if (test_name == "performance") {
                args.run_performance = true;
            } else if (test_name == "stress") {
                args.run_stress = true;
            } else if (test_name == "all") {
                args.run_basic = args.run_concurrency = args.run_exception_safety = args.run_memory_leak = args.run_performance = args.run_stress = true;
            } else {
                std::cerr << "Error: Unknown test name: " << test_name << std::endl;
                exit(EXIT_FAILURE);
            }
        } else if (arg == "--threads") {
            if (i + 1 >= argc) {
                std::cerr << "Error: --threads requires a number" << std::endl;
                exit(EXIT_FAILURE);
            }
            args.num_threads = std::stoi(argv[++i]);
            if (args.num_threads <= 0) {
                std::cerr << "Error: Number of threads must be positive" << std::endl;
                exit(EXIT_FAILURE);
            }
        } else if (arg == "--iterations") {
            if (i + 1 >= argc) {
                std::cerr << "Error: --iterations requires a number" << std::endl;
                exit(EXIT_FAILURE);
            }
            args.iterations = std::stoi(argv[++i]);
            if (args.iterations <= 0) {
                std::cerr << "Error: Number of iterations must be positive" << std::endl;
                exit(EXIT_FAILURE);
            }
        } else {
            std::cerr << "Error: Unknown argument: " << arg << std::endl;
            exit(EXIT_FAILURE);
        }
    }

    return args;
}

int main(int argc, char *argv[]) {
    try {
        // Initialize spdlog for debugging
        spdlog::set_level(spdlog::level::debug);
        spdlog::info("Starting InfiniCore Memory Management Test Suite");

        ParsedArgs args = parseArgs(argc, argv);
        spdlog::debug("Arguments parsed successfully");

        std::cout << "==============================================\n"
                  << "InfiniCore Memory Management Test Suite\n"
                  << "==============================================\n"
                  << "Device: " << static_cast<int>(args.device_type) << "\n"
                  << "Threads: " << args.num_threads << "\n"
                  << "Iterations: " << args.iterations << "\n"
                  << "==============================================" << std::endl;

        spdlog::debug("About to initialize InfiniCore context");
        // Initialize InfiniCore context
        infinicore::context::setDevice(infinicore::Device(static_cast<infinicore::Device::Type>(args.device_type), 0));
        spdlog::debug("InfiniCore context initialized successfully");

        spdlog::debug("Creating test runner");
        // Create test runner
        infinicore::test::MemoryTestRunner runner;
        spdlog::debug("Test runner created successfully");

        // Add tests based on arguments
        if (args.run_basic) {
            spdlog::debug("Adding BasicMemoryTest");
            runner.addTest(std::make_unique<infinicore::test::BasicMemoryTest>());
            spdlog::debug("BasicMemoryTest added successfully");

            spdlog::debug("Adding TensorDestructorTest");
            runner.addTest(std::make_unique<infinicore::test::TensorDestructorTest>());
            spdlog::debug("TensorDestructorTest added successfully");
        }

        if (args.run_concurrency) {
            runner.addTest(std::make_unique<infinicore::test::ConcurrencyTest>());
        }

        if (args.run_exception_safety) {
            // runner.addTest(std::make_unique<infinicore::test::ExceptionSafetyTest>());
        }

        if (args.run_memory_leak) {
            runner.addTest(std::make_unique<infinicore::test::MemoryLeakTest>());
        }

        if (args.run_performance) {
            runner.addTest(std::make_unique<infinicore::test::PerformanceTest>());
        }

        if (args.run_stress) {
            runner.addTest(std::make_unique<infinicore::test::StressTest>());
        }

        spdlog::debug("About to run all tests");
        // Run all tests
        auto results = runner.runAllTests();
        spdlog::debug("All tests completed");

        // Count results
        size_t passed = 0, failed = 0;
        for (const auto &result : results) {
            if (result.passed) {
                passed++;
            } else {
                failed++;
            }
        }

        // Print final summary
        std::cout << "\n==============================================\n"
                  << "Final Results\n"
                  << "==============================================\n"
                  << "Total Tests: " << results.size() << "\n"
                  << "Passed: " << passed << "\n"
                  << "Failed: " << failed << "\n"
                  << "==============================================" << std::endl;

        // Exit with appropriate code
        if (failed > 0) {
            std::cout << "\n❌ Some tests failed. Please review the output above." << std::endl;
            return EXIT_FAILURE;
        } else {
            std::cout << "\n✅ All tests passed!" << std::endl;
            return EXIT_SUCCESS;
        }

    } catch (const std::exception &e) {
        std::cerr << "Fatal error: " << e.what() << std::endl;
        return EXIT_FAILURE;
    } catch (...) {
        std::cerr << "Fatal error: Unknown exception" << std::endl;
        return EXIT_FAILURE;
    }
}