Posted by : Sushanth Wednesday 5 January 2022

Below code snippet compares the computation time of using tensor in place of numpy


CPU:

Numpy Code:

%%time
for i in range(10):
  a = np.random.randn(10000,10000)
  b = np.random.randn(10000,10000)
  c = a + b

CPU times: user 1min 20s, sys: 813 ms, total: 1min 20s Wall time: 1min 20s

Tensor

%%time
for i in range(10):
  a = torch.randn([1000010000])
  b = torch.randn([1000010000])
  c = a + b
CPU times: user 17.1 s, sys: 81.1 ms, total: 17.2 s Wall time: 17.1 s


Based on computation time, tensor is nearly 7x times faster than numpy

GPU:

import torch
print(torch.cuda.device_count())
print(torch.cuda.device(0))
print(torch.cuda.get_device_name(0))
cuda0 = torch.device('cuda:0')

%%time
for i in range(10):
  a = torch.randn([1000010000], device=cuda0)
  b = torch.randn([1000010000], device=cuda0)
  b.add_(a)

CPU times: user 1.39 ms, sys: 0 ns, total: 1.39 ms Wall time: 1.75 ms

Computation wise a lot better compared to CPU

===========================
CUDA (Compute Unified Device Architecture) is a parallel computing platform and application programming interface (API) model created by Nvidia.It allows software developers and software engineers to use a CUDA-enabled graphics processing unit (GPU) for general purpose processing – an approach termed GPGPU (General-Purpose computing on Graphics Processing Units). The CUDA platform is a software layer that gives direct access to the GPU's virtual instruction set and parallel computational elements, for the execution of compute kernels

The CUDA platform is designed to work with programming languages such as C, C++, and Fortran. This accessibility makes it easier for specialists in parallel programming to use GPU resources, in contrast to prior APIs like Direct3D and OpenGL, which required advanced skills in graphics programming.[3] CUDA-powered GPUs also support programming frameworks such as OpenACC and OpenCL;and HIP by compiling such code to CUDA.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Technical Articles - Skyblue - Powered by Blogger - Designed by Johanes Djogan -