它可以在GPU上运行,并输出识别显卡信息和电脑配置信息这个代码将使用蒙特卡罗方法来计算π的值。该方法基于随机数生成器来生成点,然后计算这些点落在单位圆内还是外部的概率。该算法在每个线程中计算一次,最终将所有线程的结果加起来得到π的估计值。
请注意,这个算法只是一种近似方法,它不能保证得到精确的π的值。它只是一种计算π的方法,可以用来验证其他计算π的方法的正确性
import torch
import cupy as cp
import numpy as np
# 检查是否使用GPU
if torch.cuda.is_available():
print("Using GPU")
else:
print("Using CPU")
# 获取GPU信息
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Device:", device)
print("CUDA Version:", torch.version.cuda)
print("CuPy Version:", cp.__version__)
# 获取显卡信息
print("\nGPU Info:")
if torch.cuda.is_available():
print("Number of GPUs:", torch.cuda.device_count())
for i in range(torch.cuda.device_count()):
print("GPU {}:".format(i))
print(" Name:", torch.cuda.get_device_name(i))
print(" Total Memory:", torch.cuda.get_device_properties(i).total_memory / 1024 / 1024, "MB")
print(" Free Memory:", torch.cuda.memory_allocated(i) / 1024 / 1024, "MB")
print(" Used Memory:", torch.cuda.memory_reserved(i) / 1024 / 1024, "MB")
print(" CUDA Version:", torch.version.cuda)
print(" CuPy Version:", cp.__version__)
print(" Python Version:", torch.version.pytorch)
print(" CPU Info:")
print(" Vendor:", torch.cuda.get_device_properties(i).name)
print(" Cores:", torch.cuda.get_device_properties(i).multi_processor_count)
print(" Clock Rate:", torch.cuda.get_device_properties(i).clock_rate / 1000, "MHz")
print(" Memory Bus Width:", torch.cuda.get_device_properties(i).memory_bandwidth / 1024 / 1024 / 1024, "GB/s")
print(" L2 Cache Size:", torch.cuda.get_device_properties(i).l2_cache_size / 1024 / 1024, "KB")
print(" Shared Memory per Block:", torch.cuda.get_device_properties(i).max_threads_per_block / 1024, "KB")
print(" Shared Memory per Multiprocessor:", torch.cuda.get_device_properties(i).shared_memory_per_block / 1024, "KB")
print(" Max Threads per Multiprocessor:", torch.cuda.get_device_properties(i).max_threads_per_multi_processor)
print(" Max Grid Size:", torch.cuda.get_device_properties(i).max_grid_dimension_x, "x", torch.cuda.get_device_properties(i).max_grid_dimension_y, "x", torch.cuda.get_device_properties(i).max_grid_dimension_z)
print(" Max Block Size:", torch.cuda.get_device_properties(i).max_threads_per_block_x, "x", torch.cuda.get_device_properties(i).max_threads_per_block_y, "x", torch.cuda.get_device_properties(i).max_threads_per_block_z)
print(" Max Memory Pitch:", torch.cuda.get_device_properties(i).max_memory_accessed_per_block / 1024 / 1024, "KB")
print(" Compute Capability:", torch.cuda.get_device_properties(i).compute_capability)
print(" Total Constant Memory:", torch.cuda.get_device_properties(i).total_global_mem / 1024 / 1024, "MB")
print(" Max Texture Size:", torch.cuda.get_device_properties(i).max_texture1d_linear_width / 1024, "KB")
print(" Max Texture Size Layered:", torch.cuda.get_device_properties(i).max_texture2d_linear_width / 1024, "KB")
print(" Max Texture Size 3D:", torch.cuda.get_device_properties(i).max_texture3d_width / 1024, "KB")
print(" Max Texture Pitch:", torch.cuda.get_device_properties(i).max_texture1d_linear_width / 1024, "KB")
print(" Texture Alignment:", torch.cuda.get_device_properties(i).texture_alignment)
print(" Concurrent Kernels:", torch.cuda.get_device_properties(i).concurrent_kernels)
print(" Cooperative Launch:", torch.cuda.get_device_properties(i).cooperative_launch)
print(" Cooperative Multi Device Launch:", torch.cuda.get_device_properties(i).cooperative_multi_device_launch)