Machine Learning With GPU (1): CUDA

ade sueb
ade sueb
Jul 29, 2020 · 3 min read

If you working with Machine Learning using GPU this story is the answer.

The story behind this is when I try to run a tensorflow python script, it comes with this warning message :

2020-07-29 17:11:59.393693: W tensorflow/stream_executor/platform/default/] Could not load dynamic library ''; dlerror: cannot open shared object file: No such file or directory

So i search on google what is And I found that libcudart is CUDA library from NVIDIA that can use GPU to run.

wow.. that is something for me, because so far i only used GPU for gaming, and now i can code with GPU.

OK.. let’s get started…


We need bridge from our code to access GPU. If you used NVIDIA for your GPU then CUDA is the answer

The following NVIDIA® software must be installed on your system:

Look at the table, before you install CUDA toolkit, make sure the backward compatibility between CUDA Toolkit and NVIDIA driver

Easiest way to installCUDA execute the following commands:

$ sudo apt update 
$ sudo apt install nvidia-cuda-toolkit

All should be ready now. Check your CUDA version:

$ nvcc — version 
nvcc: NVIDIA (R) Cuda compiler driver Copyright © 2005–2019 NVIDIA Corporation Built on Sun_Jul_28_19:07:16_PDT_2019 Cuda compilation tools, release 10.1, V10.1.243

noted : If you want to get the latest version or specific version of CUDA you can follow this link

Confirm the installation by compiling an example CUDA C code. Save the following code into a file named eg.

#include <stdio.h>__global__
void saxpy(int n, float a, float *x, float *y)
int i = blockIdx.x*blockDim.x + threadIdx.x;
if (i < n) y[i] = a*x[i] + y[i];
int main(void)
int N = 1<<20;
float *x, *y, *d_x, *d_y;
x = (float*)malloc(N*sizeof(float));
y = (float*)malloc(N*sizeof(float));
cudaMalloc(&d_x, N*sizeof(float));
cudaMalloc(&d_y, N*sizeof(float));
for (int i = 0; i < N; i++) {
x[i] = 1.0f;
y[i] = 2.0f;
cudaMemcpy(d_x, x, N*sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_y, y, N*sizeof(float), cudaMemcpyHostToDevice);
// Perform SAXPY on 1M elements
saxpy<<<(N+255)/256, 256>>>(N, 2.0f, d_x, d_y);
cudaMemcpy(y, d_y, N*sizeof(float), cudaMemcpyDeviceToHost);float maxError = 0.0f;
for (int i = 0; i < N; i++)
maxError = max(maxError, abs(y[i]-4.0f));
printf("Max error: %f\n", maxError);

Use nvcc the Nvidia CUDA compiler to compile the code and run the newly compiled binary:

$ nvcc -o hello 
$ ./hello
Max error: 0.000000

Max error supposed to 0.00000, if you got 2.0000 then probably there are not matching between your NVIDIA driver and CUDA toolkit.

So now after succeed install CUDA, i can running the script very well without warning message.

Sometimes when you got CUDA 10.2, but Tensorflow requires CUDA 10.1 you can do symlink and change the version like this: ln -s /usr/local/cuda-10.2/targets/x86_64-linux/lib/ /usr/lib/x86_64-linux-gnu/


Are you working with Machine Learning?? CUDA is not enough for this.. prepare cuDNN, cuDNN is a GPU-accelerated library of primitives for deep neural networks. don’t worry you can follow this link.

The Startup

Get smarter at building your thing. Join The Startup’s +794K followers.

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

ade sueb

Written by

ade sueb

Still believe, can change the world with code..

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +794K followers.

ade sueb

Written by

ade sueb

Still believe, can change the world with code..

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +794K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app