Understanding a Scientist's Tools Outside the Lab

PhyComm

What this talk is not

  • How to code
  • How to how to code
  • What to use to code

What this talk will cover

  • What is an instruction
  • How do CPUs execute instructions?
  • How does computer memory work?
  • How can I practically use this information?

Let's familiarize ourselves by example

def f():
	print("Hello world")
    
f()
sh-5.1$ python3 example_1.py 
Hello World

Let's familiarize ourselves by example

import dis

def f():
    print("Hello World")

dis.dis(f)
sh-5.1$ python3 example_2.py
  4           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('Hello World')
              4 CALL_FUNCTION            1
              6 POP_TOP
              8 LOAD_CONST               0 (None)
             10 RETURN_VALUE

Let's familiarize ourselves by example

#include <stdio.h>

int main() {
	printf("Hello World!\n");
	return 0;
}
sh-5.1$ gcc -S example_3.c
sh-5.1$ cat example_3.s
	.file	"example_3.c"
	.text
	.section	.rodata
.LC0:
	.string	"Hello World!"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$.LC0, %edi
	call	puts
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 12.1.1 20220507 (Red Hat 12.1.1-1)"
	.section	.note.GNU-stack,"",@progbits

The first step: instructions

Python's Instruction Set: Bytecode

  • An intermediary "computer" - The Python VM
  • Variations - CPython, Jython, PyPy, etc.









     
  • But not the only one
import dis.dis

def main():
    l = [1, 2, 3]
    return l[2]

dis.dis(main)
sh-5.1$ python3 example_4.py
  4           0 BUILD_LIST               0
              2 LOAD_CONST               1 ((1, 2, 3))
              4 LIST_EXTEND              1
              6 STORE_FAST               0 (l)

  5           8 LOAD_FAST                0 (l)
             10 LOAD_CONST               2 (2)
             12 BINARY_SUBSCR
             14 RETURN_VALUE

Your CPU's (likely) Instruction Set: AMD64 (aka x86_64)

  • Much much more complex, and not worth your time
  • What is worth your time: Understanding how your CPU uses them
  1. Load into memory
  2. Read a "word" from memory
  3. Decode what that word means
  4. Run the code

The clock cycle

But how did the code get in the memory?

The next level of abstraction: The Kernel

  • Stands between the hardware and software
  • Handles exchange of data between them
  • Handles memory allocation
  • Lets take a look at mine!

Something actually useful: JIT Compilation

  • Compile when needed and cache
  • Example - Julia and PyPy
  • Here's some benchmarks

Something actually useful: CPU Caching

  • Recently read words from memory are cached in your CPU Cache
  • Fetching from memory is slower than reading from cache
  • Moral of the story: Keep objects in cache while you want to act on them

Something actually useful: Reading this article!

https://viralinstruction.com/posts/hardware/

Something not useful but fun: Listen to your RAM!

$ aplay /dev/mem

Thanks for listening! Questions?

Telegram: t.me/cocoa1231
Email: cocoathepenguin@protonmail.com

Made with Slides.com