以下是一个示例代码,用于找出两个设备之间的最佳P2P连接。

#include <stdio.h>
#include <cuda_runtime.h>

#define CUDA_CHECK(call)                                               \
{                                                                       \
    cudaError_t error = call;                                           \
    if (error != cudaSuccess)                                           \
    {                                                                   \
        printf("CUDA error at %s:%d code=%d(%s) \n",                    \
               __FILE__, __LINE__, error, cudaGetErrorString(error));   \
        exit(EXIT_FAILURE);                                             \
    }                                                                   \
}

int main()
{
    int deviceCount = 0;
    cudaGetDeviceCount(&deviceCount);

    int maxP2PValue = 0;
    int bestDevice1 = -1;
    int bestDevice2 = -1;

    for (int i = 0; i < deviceCount; i++)
    {
        for (int j = 0; j < deviceCount; j++)
        {
            if (i != j)
            {
                int p2pValue = 0;
                cudaDeviceGetP2PAttribute(&p2pValue, cudaDevP2PAttrPerformanceRank, i, j);

                if (p2pValue > maxP2PValue)
                {
                    maxP2PValue = p2pValue;
                    bestDevice1 = i;
                    bestDevice2 = j;
                }
            }
        }
    }

    printf("Best P2P connection is between device %d and device %d\n", bestDevice1, bestDevice2);

    return 0;
}

请注意,上述代码仅找到了两个设备之间的最佳P2P连接,并输出了这两个设备的索引。根据具体需求,您可以进一步扩展代码,以获取更多有关P2P连接性能的信息

利用cudaDeviceGetP2PAttribute找出P2P最好的两个device给出代码示例

原文地址: https://www.cveoy.top/t/topic/ijuM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录