Skip to content

Instantly share code, notes, and snippets.

View V0XNIHILI's full-sized avatar
🎯
Focusing

Douwe den Blanken V0XNIHILI

🎯
Focusing
View GitHub Profile
@V0XNIHILI
V0XNIHILI / aws-ec2-latex.md
Last active December 24, 2019 11:49 — forked from m4kvn/aws_ec2_latex_setting.md
AWS EC2 LaTeX

AWS EC2 LaTeX

Setup

[ec2-user ~]$ wget http://ftp.jaist.ac.jp/pub/CTAN/systems/texlive/tlnet/install-tl-unx.tar.gz
[ec2-user ~]$ tar xzf install-tl-unx.tar.gz
[ec2-user ~]$ cd install-tl-2017xxxx
[ec2-user ~]$ sudo ./install-tl

Multicore Python

Below you can find some sample template code which you can use to run multicore Python computations.

# =================================================================================================
# Imports
# =================================================================================================

import multiprocessing as mp
@V0XNIHILI
V0XNIHILI / check.py
Created December 17, 2020 00:33
On 16-12-20, Maarten sent me as message asking whether I knew what was special about today's date. I didn't know, but it turned out that 12^2+16^2=20^2. So I made a little Python program to check this for all years from 2000-3000:
for y in range(0, 1000):
for m in range(1,13):
max_day = 32
# Month length correction
if m == 11 or m == 9 or m == 6 or m == 4:
max_day = 31
# Leap year correction
elif m == 2:
y_actual = y + 2000
@V0XNIHILI
V0XNIHILI / run.sh
Last active May 5, 2021 09:56
Check which CPU you have on macOS
sysctl -n machdep.cpu.brand_string
# Example output: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
@V0XNIHILI
V0XNIHILI / Möller-Trumbore algorithm in Python.md
Last active October 25, 2024 12:16
Möller–Trumbore ray-triangle intersection algorithm (for ray tracing) in Python and Numpy, vectorized

On my 2014 MacBook Pro (2.5 GHz), this vectorized version is about 250 times faster when used for a large numbers of triangles compared to the original algorithm. You can also parallelize this code over multiple threads, but due to the large overhead of creating a thread pool, this is only really useful when you have a very large number of rays.

I also tried to vectorize the rays, instead of using a loop. I did this by using:

np.reshape(np.array(np.meshgrid(np.arange(0, len(ray_direction), 1), np.arange(0, len(triangles_vertices), 1))), (2, resulting_array_length))

to create all combinations of indices of the rays and the triangles. However, using this process, combined with using np.take() to actually create these combinations is about twice as slow as the code below.

@V0XNIHILI
V0XNIHILI / division.cpp
Last active March 14, 2022 10:41
PYNQ-Z2 floating point conversions
void divide (float a, float b, float *c)
{
#pragma HLS interface s_axilite port=a
#pragma HLS interface s_axilite port=b
#pragma HLS interface s_axilite port=c
#pragma HLS interface ap_ctrl_none port=return
*c = a / b;
}
@V0XNIHILI
V0XNIHILI / zip_with.py
Last active July 19, 2022 10:38
Python implementation of Haskell's zipWith
from typing import List, Any, Callable
def zip_with (func: Callable[[Any, Any], Any], list1: List[Any], list2: List[Any]) -> Any:
"""
>>> zip_with (lambda x, y: x + y, [1, 2, 3], [4, 5, 6])
[5, 7, 9]
"""
return [func (x, y) for x, y in zip (list1, list2)]
@V0XNIHILI
V0XNIHILI / SequentialMNIST.py
Last active September 28, 2022 19:30
Simple sequential MNIST dataset implementation in Python and PyTorch for usage in recurrent neural networks. See for project using this dataset here: https://github.com/V0XNIHILI/LSTM-Sequential-MNIST
import torch
import torch.nn as nn
import torchvision
from torch.utils.data import Dataset
class SequentialMNIST(Dataset):
def __init__(self, MNIST_dataset: torchvision.datasets.MNIST):
self.MNIST_dataset = MNIST_dataset
@V0XNIHILI
V0XNIHILI / matrix_as_graph.ipynb
Last active January 29, 2024 16:48
Matrix multiplication in graph form
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@V0XNIHILI
V0XNIHILI / on_chip_energy.py
Last active March 16, 2023 10:43
Functions to compute energy per operation on a chip
"""Data taken from paper: Computing's Energy Problem (and what we can do about it) - Mark Horowitz
All output energies are in pJ (pico Joule)
"""
import math
E_INT_ADD_PER_BIT = (0.03 / 8 + 0.1 / 32) / 2