P.S I later got free GPU credits from Runpod. First to sign up at this link gets some as well :)
Quick intro
LeetArxiv is Leetcode for implementing Arxiv and other research papers.
1.0 Introduction
Two things are true: I never learnt Python, and I can’t afford a GPU. Just a broke math bro* learning CUDA to land a new job. I saw Google Colab offers free GPUs and I could program them in C** so I figured I could try it.
*Trump cut my lab’s funding so I’m in between jobs, your support means the world.
**I’m from the part of tech academia where we only used C lol
2.0 Environment Setup
Here’s the notebook we write in this section.
First things first, you need to open Google Colab and change the runtime type to T4 GPU.
Keep your runtime type as Python 3. Then select a Tesla V4 GPU.
Next, check Colab’s CUDA compiler version and compare it to your GPU’s compiler version (there might be a mismatch). Use the command
!nvcc --version
!nvidia-smi
We observe our CUDA compilation is 12.5 but the driver is 12.4. We’ll fix this in section 2.1
Next we install a jupyter notebook extension for writing C/C++ on Colab
!pip install nvcc4jupyter
%load_ext nvcc4jupyter
2.1 Fixing Driver Mismatch
Before we write our hello world. We fix the driver mismatch by writing to a file that does not exist. Pretty quirky IMO. We do this by affixing to our command a directory. Something akin to
%%cuda -c "-I /does/not/exist -arch sm_75"
So let’s write our hello world code.
%%cuda -c "-I /does/not/exist -arch sm_75"
#include <stdio.h>
// This is a special function that runs on the GPU (device) instead of the CPU (host)
__global__ void kernel() {
printf("Hello world!\n");
}
int main() {
// Invoke the kernel function on the GPU with one block of one thread
kernel<<<1,1>>>();
// Check for error codes (remember to do this for _every_ CUDA function)
if(cudaDeviceSynchronize() != cudaSuccess) {
fprintf(stderr, "CUDA Error: %s\n", cudaGetErrorString(cudaPeekAtLastError()));
}
return 0;
}
It runs! We’re writing in C inside Google Colab!
I found lots of complementary resources like ShashankStack’s1 blog and Cuda on Colab notebook2. Take a gander!
This is Chapter 2 in our upcoming book: A Programmer’s Guide to Hacking Satoshi’s Billion Dollar Wallet.
Available Chapters
Chapter 1 : A Programmer’s Introduction to Elliptic Curves.
Chapter 2 : Hacking Dormant Bitcoin Wallets in C and Elliptic Curves.
Chapter 3 : A Super Friendly Guide to Finite Fields for Programmers.
Chapter 4 : The US Government Hid a Mathematical Backdoor in another Elliptic Curve