|
| 1 | +#include "test.h" |
| 2 | +#include <cstring> |
| 3 | +#include <infinirt.h> |
| 4 | +#include <iostream> |
| 5 | + |
| 6 | +bool testMemcpy(infiniDevice_t device, int deviceId, size_t dataSize) { |
| 7 | + |
| 8 | + std::cout << "==============================================\n" |
| 9 | + << "Testing memcpy on Device ID: " << deviceId << "\n" |
| 10 | + << "==============================================" << std::endl; |
| 11 | + |
| 12 | + // 分配主机内存 |
| 13 | + std::cout << "[Device " << deviceId << "] Allocating host memory: " << dataSize * sizeof(float) << " bytes" << std::endl; |
| 14 | + std::vector<float> hostData(dataSize, 1.23f); |
| 15 | + std::vector<float> hostCopy(dataSize, 0.0f); |
| 16 | + |
| 17 | + // 分配设备内存 |
| 18 | + void *deviceSrc = nullptr, *deviceDst = nullptr; |
| 19 | + size_t dataSizeInBytes = dataSize * sizeof(float); |
| 20 | + |
| 21 | + std::cout << "[Device " << deviceId << "] Allocating device memory: " << dataSizeInBytes << " bytes" << std::endl; |
| 22 | + if (infinirtMalloc(&deviceSrc, dataSizeInBytes) != INFINI_STATUS_SUCCESS) { |
| 23 | + std::cerr << "[Device " << deviceId << "] Failed to allocate device memory for deviceSrc." << std::endl; |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + if (infinirtMalloc(&deviceDst, dataSizeInBytes) != INFINI_STATUS_SUCCESS) { |
| 28 | + std::cerr << "[Device " << deviceId << "] Failed to allocate device memory for deviceDst." << std::endl; |
| 29 | + infinirtFree(deviceSrc); |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + // 复制数据到设备 |
| 34 | + std::cout << "[Device " << deviceId << "] Copying data from host to device..." << std::endl; |
| 35 | + if (infinirtMemcpy(deviceSrc, hostData.data(), dataSizeInBytes, INFINIRT_MEMCPY_H2D) != INFINI_STATUS_SUCCESS) { |
| 36 | + std::cerr << "[Device " << deviceId << "] Failed to copy data from host to device." << std::endl; |
| 37 | + infinirtFree(deviceSrc); |
| 38 | + infinirtFree(deviceDst); |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + // 设备内存间复制 |
| 43 | + std::cout << "[Device " << deviceId << "] Copying data between device memory (D2D)..." << std::endl; |
| 44 | + if (infinirtMemcpy(deviceDst, deviceSrc, dataSizeInBytes, INFINIRT_MEMCPY_D2D) != INFINI_STATUS_SUCCESS) { |
| 45 | + std::cerr << "[Device " << deviceId << "] Failed to copy data from device to device." << std::endl; |
| 46 | + infinirtFree(deviceSrc); |
| 47 | + infinirtFree(deviceDst); |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + // 设备数据复制回主机 |
| 52 | + std::cout << "[Device " << deviceId << "] Copying data from device back to host..." << std::endl; |
| 53 | + if (infinirtMemcpy(hostCopy.data(), deviceDst, dataSizeInBytes, INFINIRT_MEMCPY_D2H) != INFINI_STATUS_SUCCESS) { |
| 54 | + std::cerr << "[Device " << deviceId << "] Failed to copy data from device to host." << std::endl; |
| 55 | + infinirtFree(deviceSrc); |
| 56 | + infinirtFree(deviceDst); |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + // 数据验证 |
| 61 | + std::cout << "[Device " << deviceId << "] Validating copied data..." << std::endl; |
| 62 | + if (std::memcmp(hostData.data(), hostCopy.data(), dataSizeInBytes) != 0) { |
| 63 | + std::cerr << "[Device " << deviceId << "] Data mismatch between hostData and hostCopy." << std::endl; |
| 64 | + infinirtFree(deviceSrc); |
| 65 | + infinirtFree(deviceDst); |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + std::cout << "[Device " << deviceId << "] Data copied correctly!" << std::endl; |
| 70 | + |
| 71 | + // 释放设备内存 |
| 72 | + std::cout << "[Device " << deviceId << "] Freeing device memory..." << std::endl; |
| 73 | + infinirtFree(deviceSrc); |
| 74 | + infinirtFree(deviceDst); |
| 75 | + |
| 76 | + std::cout << "[Device " << deviceId << "] Memory copy test PASSED!" << std::endl; |
| 77 | + |
| 78 | + return true; |
| 79 | +} |
| 80 | + |
| 81 | +bool testSetDevice(infiniDevice_t device, int deviceId) { |
| 82 | + |
| 83 | + std::cout << "Setting device " << device << " with ID: " << deviceId << std::endl; |
| 84 | + |
| 85 | + infiniStatus_t status = infinirtSetDevice(device, deviceId); |
| 86 | + |
| 87 | + if (status != INFINI_STATUS_SUCCESS) { |
| 88 | + std::cerr << "Failed to set device " << device << " with ID " << deviceId << std::endl; |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + return true; |
| 93 | +} |
0 commit comments