jkushwaha commited on
Commit
1e4b350
·
verified ·
1 Parent(s): 2aa7125

Update GPU_test.py

Browse files
Files changed (1) hide show
  1. GPU_test.py +14 -30
GPU_test.py CHANGED
@@ -1,36 +1,20 @@
1
  import paddle
2
 
3
- # Check if GPU is available
4
- if paddle.is_compiled_with_cuda():
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
- # Create a random input tensor
23
- input_data = paddle.randn([1, 10])
24
-
25
- # Instantiate the network
26
- model = SimpleNet()
27
 
28
- # Move model to GPU explicitly if not set by default
29
- if paddle.is_compiled_with_cuda():
30
- model = model.cuda()
31
 
32
- # Perform a forward pass
33
- output = model(input_data)
 
34
 
35
- # Print the output to verify GPU usage
36
- print("Output:", output)
 
 
 
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.")