1
9
运行时
提问帖
有回复
新人贴
调用thrust::device_vector的resize方法直接崩溃
2026-01-12 10:19:00
56
我现在在一个有8块海光BW500SM(gfx936)加速卡的docker中测试程序,docker安装的操作系统是Ubuntu22.04,我的程序是在我本机的Ubuntu22.04docker中用cmake编译的,远程传输到海光BW500SM卡的docker中运行,调用thrust::device_vector的resize方法分配200KB全局内存直接崩溃。为了定位问题,我写了一个测试程序,用hipcc编译运行都正常,但用cmake编译则还是在resize时崩溃。我的cmake除了有一个下面写的结构与HIP有关,其他都是与HIP无关的东西,请问有谁碰上过与我类似的问题吗?怎么解决的? add_compile_options( -arch=gfx906 -arch=gfx926 -arch=gfx928 -arch=gfx936 #-rdc=true # 启用 RDC #这行一开始是开启的,但resize时还是崩溃,我注释掉了就没改回来 )
全部评论(3)
只看楼主
安
安乐
楼主
3楼
刚才测试了一下,这个cmake文件可用,测试程序可以正常编译运行。
1/13
安
安乐
楼主
2楼
下面是我的测试程序:
#include <hip/hip_runtime.h>
#include <thrust/version.h>
#include <thrust/device_vector.h>
#include <cstdint>
#include <iostream>
struct ss {
    uint64_t m;
    uint64_t n;
};
int main() {
    const size_t size_in_bytes = 200 * 1024; // 200 KB
    std::cout << "Thrust version: " << THRUST_VERSION << std::endl;
    try {
        hipStream_t stream;
        hipError_t err = hipStreamCreateWithFlags(&stream, hipStreamNonBlocking);
        if (err != hipSuccess) {
            std::cerr << "Error: " << std::string(hipGetErrorString(err)) << std::endl;
            return -1;
        }
        // 创建一个空的 device_vector
        thrust::device_vector<ss> d_vec;
        // 分配 200 KB 内存(即 200 * 1024 个 uint8_t 元素)
        d_vec.resize(size_in_bytes);
        std::cout << "Successfully allocated " << size_in_bytes << " bytes on GPU." << std::endl;
        std::cout << "Device vector size: " << d_vec.size() << " elements." << std::endl;
        // 可选:验证分配是否成功(例如通过访问第一个和最后一个元素)
        if (!d_vec.empty()) {
            thrust::for_each(
                thrust::hip::par.on(stream),
                d_vec.begin(),
                d_vec.end(),
                [=] __device__ (ss &s) {
                    s.m = 1;
                    s.n = 2;
                }
            );
            std::cout << "Test write successful." << std::endl;
        }
        if (stream) {
             err = hipStreamDestroy(stream);
            if (err != hipSuccess) {
                std::cerr << "Error: " << std::string(hipGetErrorString(err)) << std::endl;
                return -1;
            }
        }
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return -1;
    }
    return 0;
}
下面海光技术支持提供的cmake文件:
cmake_minimum_required(VERSION 3.16)
# Find hipcc executable
find_program(HIPCC_EXECUTABLE hipcc
    PATHS /opt/dtk-25.04.3/bin
    NO_DEFAULT_PATH
)
if(NOT HIPCC_EXECUTABLE)
    find_program(HIPCC_EXECUTABLE hipcc REQUIRED)
endif()
# Find ROCm path
find_path(ROCM_ROOT
    NAMES include/hip/hip_runtime.h
    PATHS /opt/dtk-25.04.3
    DOC "ROCm installation directory"
)
# Set hipcc as the C++ compiler
set(CMAKE_CXX_COMPILER ${HIPCC_EXECUTABLE})
project(resize_test LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17) # Changed to match your original code
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(resize-test resize-test.cpp)
# Set HIP-specific compile options with ROCm path and specific architecture
set_property(TARGET resize-test APPEND_STRING PROPERTY
    COMPILE_FLAGS " --rocm-path=${ROCM_ROOT} --offload-arch=gfx936")
# Only include architectures that exist in your ROCm installation
# Adjust the architecture based on what's available in your system
target_compile_definitions(resize-test PRIVATE
    __HIP_PLATFORM_AMD__=1
    THRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_HIP
    THRUST_HOST_SYSTEM=THRUST_HOST_SYSTEM_CPP
)
这个cmake文件是否可用我还没有测试,估计可用。下面是原来用hipcc直接编译的命令,这个编译运行都没问题:
hipcc -std=c++14 -o ./test ./resize_test.cpp
1/13
C
changhl
1楼
rocthrust 的 使用手册可以从这里找到:https://download.sourcefind.cn:65024/1/main/latest/Document 《rocthrust使用手册》
另外,您可以把最小可复现的测试用例同步给我,我们内部复现定位排查一下。
1/12
没有更多内容了
--
楼主
Level.1
0 帖子
楼主热帖
暂无数据