Update GPU_test.py
Browse files- GPU_test.py +14 -30
GPU_test.py
CHANGED
|
@@ -1,36 +1,20 @@
|
|
| 1 |
import paddle
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
| 5 |
-
# Define the device configuration
|
| 6 |
-
paddle.set_device('gpu')
|
| 7 |
-
print("CUDA is available. Using GPU for computations.")
|
| 8 |
-
else:
|
| 9 |
-
paddle.set_device('cpu')
|
| 10 |
-
print("CUDA is not available. Using CPU for computations.")
|
| 11 |
-
|
| 12 |
-
# Test code to verify GPU usage
|
| 13 |
-
# Define a simple neural network example
|
| 14 |
-
class SimpleNet(paddle.nn.Layer):
|
| 15 |
-
def __init__(self):
|
| 16 |
-
super(SimpleNet, self).__init__()
|
| 17 |
-
self.linear = paddle.nn.Linear(10, 1)
|
| 18 |
-
|
| 19 |
-
def forward(self, x):
|
| 20 |
-
return self.linear(x)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# Instantiate the network
|
| 26 |
-
model = SimpleNet()
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
print("
|
|
|
|
|
|
|
|
|
| 1 |
import paddle
|
| 2 |
|
| 3 |
+
# Define an array on the CPU
|
| 4 |
+
array_on_cpu = paddle.to_tensor([1, 2, 3, 4, 5])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Check if GPU is available
|
| 7 |
+
use_gpu = paddle.is_compiled_with_cuda() and paddle.device.is_compiled_with_cuda()
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
if use_gpu:
|
| 10 |
+
# Transfer the array to GPU
|
| 11 |
+
array_on_gpu = array_on_cpu.cuda()
|
| 12 |
|
| 13 |
+
# Print the array on GPU
|
| 14 |
+
print("Array on GPU:")
|
| 15 |
+
print(array_on_gpu)
|
| 16 |
|
| 17 |
+
# Verify the device of the tensor
|
| 18 |
+
print("Device of the tensor:", array_on_gpu.place)
|
| 19 |
+
else:
|
| 20 |
+
print("CUDA is not available. Cannot load array onto GPU.")
|