|
$VMName = "ubuntu-vgpu" |
|
|
|
# 停止虚拟机 |
|
Stop-VM -Name $VMName |
|
|
|
# 基础VM配置 |
|
Set-VMFirmware -VMName $VMName -EnableSecureBoot Off |
|
#Set-VMProcessor -VMName $VMName -ExposeVirtualizationExtensions $true |
|
Set-VM -Name $VMName -GuestControlledCacheTypes $true |
|
Set-VM -Name $VMName -LowMemoryMappedIoSpace 3GB |
|
Set-VM -Name $VMName -HighMemoryMappedIoSpace 96GB |
|
|
|
# 设置静态内存 |
|
Set-VMMemory -VMName $VMName -DynamicMemoryEnabled $false -StartupBytes 32GB |
|
|
|
Write-Host "基础VM配置完成" |
|
|
|
# 获取所有GPU适配器 |
|
$hostGPUs = Get-VMHostPartitionableGpu |
|
$currentAdapters = Get-VMGpuPartitionAdapter -VMName $VMName |
|
|
|
Write-Host "检测到 $($hostGPUs.Count) 张主机GPU" |
|
Write-Host "当前虚拟机有 $($currentAdapters.Count) 个GPU适配器" |
|
|
|
# 确保有两个GPU适配器 |
|
if ($currentAdapters.Count -eq 0) { |
|
Write-Host "添加第一张GPU适配器..." |
|
Add-VMGpuPartitionAdapter -VMName $VMName -InstancePath $hostGPUs[0].Name |
|
Write-Host "添加第二张GPU适配器..." |
|
Add-VMGpuPartitionAdapter -VMName $VMName -InstancePath $hostGPUs[1].Name |
|
} elseif ($currentAdapters.Count -eq 1) { |
|
Write-Host "添加第二张GPU适配器..." |
|
$usedGPU = $currentAdapters[0].InstancePath |
|
$unusedGPU = $hostGPUs | Where-Object { $_.Name -ne $usedGPU } | Select-Object -First 1 |
|
Add-VMGpuPartitionAdapter -VMName $VMName -InstancePath $unusedGPU.Name |
|
} |
|
|
|
# 等待适配器添加完成 |
|
Start-Sleep -Seconds 5 |
|
$adapters = Get-VMGpuPartitionAdapter -VMName $VMName | Sort-Object InstancePath |
|
|
|
Write-Host "当前已添加 $($adapters.Count) 个GPU适配器" |
|
Write-Host "配置GPU资源分配..." |
|
|
|
if ($adapters.Count -eq 2) { |
|
# 获取每个适配器的ID |
|
$gpu1 = $adapters[0] |
|
$gpu2 = $adapters[1] |
|
|
|
Write-Host "第一张GPU ID: $($gpu1.Id)" |
|
Write-Host "第二张GPU ID: $($gpu2.Id)" |
|
|
|
# 第一张GPU:80%显存 + 100%计算 |
|
Write-Host "配置第一张GPU (80%显存 + 100%计算)..." |
|
|
|
Set-VMGpuPartitionAdapter -VMName $VMName -AdapterId $gpu1.Id ` |
|
-MinPartitionVRAM 1000000000 ` |
|
-MaxPartitionVRAM 19.2GB ` |
|
-OptimalPartitionVRAM 19.2GB ` |
|
-MinPartitionCompute 80000000 ` |
|
-MaxPartitionCompute 100000000 ` |
|
-OptimalPartitionCompute 100000000 ` |
|
-MinPartitionEncode 50000000 ` |
|
-MaxPartitionEncode 80000000 ` |
|
-OptimalPartitionEncode 100000000 ` |
|
-MinPartitionDecode 50000000 ` |
|
-MaxPartitionDecode 80000000 ` |
|
-OptimalPartitionDecode 100000000 |
|
|
|
Write-Host "第一张GPU配置完成" |
|
|
|
# 第二张GPU:100%显存 + 100%计算 |
|
Write-Host "配置第二张GPU (100%显存 + 100%计算)..." |
|
|
|
Set-VMGpuPartitionAdapter -VMName $VMName -AdapterId $gpu2.Id ` |
|
-MinPartitionVRAM 1000000000 ` |
|
-MaxPartitionVRAM 24GB ` |
|
-OptimalPartitionVRAM 24GB ` |
|
-MinPartitionCompute 80000000 ` |
|
-MaxPartitionCompute 100000000 ` |
|
-OptimalPartitionCompute 100000000 ` |
|
-MinPartitionEncode 50000000 ` |
|
-MaxPartitionEncode 100000000 ` |
|
-OptimalPartitionEncode 100000000 ` |
|
-MinPartitionDecode 50000000 ` |
|
-MaxPartitionDecode 100000000 ` |
|
-OptimalPartitionDecode 100000000 |
|
|
|
Write-Host "第二张GPU配置完成" |
|
|
|
} else { |
|
Write-Host "错误:GPU适配器数量不正确,当前数量: $($adapters.Count)" |
|
Write-Host "请检查GPU适配器添加是否成功" |
|
} |
|
|
|
# 验证配置 |
|
Write-Host "`n=== 验证GPU配置 ===" |
|
$finalAdapters = Get-VMGpuPartitionAdapter -VMName $VMName |
|
|
|
for ($i = 0; $i -lt $finalAdapters.Count; $i++) { |
|
$adapter = $finalAdapters[$i] |
|
Write-Host "GPU $($i+1) (ID: $($adapter.Id)):" |
|
Write-Host " 实例路径: $($adapter.InstancePath.Substring(0,60))..." |
|
Write-Host " 显存配置: Min=$([math]::Round($adapter.MinPartitionVRAM/1GB,1))GB, Max=$([math]::Round($adapter.MaxPartitionVRAM/1GB,1))GB, Optimal=$([math]::Round($adapter.OptimalPartitionVRAM/1GB,1))GB" |
|
Write-Host " 计算资源: Min=$($adapter.MinPartitionCompute/1000000)%, Max=$($adapter.MaxPartitionCompute/1000000)%, Optimal=$($adapter.OptimalPartitionCompute/1000000)%" |
|
Write-Host " 编码资源: Min=$($adapter.MinPartitionEncode/1000000)%, Max=$($adapter.MaxPartitionEncode/1000000)%, Optimal=$($adapter.OptimalPartitionEncode/1000000)%" |
|
Write-Host " 解码资源: Min=$($adapter.MinPartitionDecode/1000000)%, Max=$($adapter.MaxPartitionDecode/1000000)%, Optimal=$($adapter.OptimalPartitionDecode/1000000)%" |
|
Write-Host "" |
|
} |
|
|
|
Write-Host "=== 配置总结 ===" |
|
Write-Host "第一张GPU: 19.2GB显存 (80%) + 100%计算" |
|
Write-Host "第二张GPU: 24.0GB显存 (100%) + 100%计算" |
|
Write-Host "总可用显存: 43.2GB" |
|
Write-Host "双GPU精确资源配置完成!" |
|
|
|
# 启动虚拟机 |
|
#Write-Host "启动虚拟机..." |
|
#Start-VM -Name $VMName |
|
#Write-Host "虚拟机已启动!请在虚拟机内使用 'nvidia-smi' 验证GPU配置" |