gpu_check.py (997B)
1 import torch 2 3 # 1. CUDAが利用可能か(CUDA版PyTorchがインストールされているか) 4 is_available = torch.cuda.is_available() 5 print(f"torch.cuda.is_available(): {is_available}") 6 7 if is_available: 8 # 2. PCに認識されているGPUの数 9 device_count = torch.cuda.device_count() 10 print(f"torch.cuda.device_count(): {device_count}") 11 12 # 3. 現在使用中のGPUの名前(通常0番目のGPU) 13 device_name = torch.cuda.get_device_name(0) 14 print(f"GPU Name: {device_name}") 15 16 # 4. GPU上でテンソル演算を実行できるか 17 try: 18 # GPU (CUDA) デバイスを指定 19 device = torch.device("cuda") 20 # テンソルをGPUメモリに作成し、演算を実行 21 tensor_on_gpu = torch.rand(2, 3, device=device) 22 print("\nSuccessfully created and calculated a tensor on GPU:") 23 print(tensor_on_gpu) 24 except Exception as e: 25 print(f"\nError accessing GPU for calculation: {e}")