Skip to content

Instantly share code, notes, and snippets.

@pchng
pchng / js_single_threadedness.html
Last active August 29, 2015 13:57
Demonstration of JavaScript single-threadedness
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test of JavaScript single-threadedness</title>
</head>
<body>
<h1>Test of JavaScript single-threadedness</h1>
<button id="check">Check</button>
<script src="https://code.jquery.com/jquery-2.1.0.min.js"></script>
@pchng
pchng / Simple Java 8 Stream API performance test
Last active August 29, 2015 14:13
Simple Java 8 Stream API performance test
// Test whether Stream API operations are efficiently re-ordered.
// LOGGER is instance of org.slf4j.Logger
@Test
public void testStreamApiOrdering() {
final Random rnd = new Random();
final int nItems = 10_000;
final List<Integer> values = new ArrayList<>(nItems);
for (int i = 0; i < nItems; ++i) {
values.add(rnd.nextInt(1000)); // Values between 0 and 999, inclusive.
}
@pchng
pchng / cbc_radio_broadcast_logs.py
Created March 9, 2015 02:56
CBC Radio 2 broadcast log history download Python script
#!/usr/bin/env python
# Python 2.7.x
import argparse
import csv
import datetime
import sys
from collections import defaultdict
# Requires requests and beautifulsoup4: pip install beautifulsoup4 requests
import requests

Keybase proof

I hereby claim:

  • I am pchng on github.
  • I am pchng (https://keybase.io/pchng) on keybase.
  • I have a public key whose fingerprint is F722 6230 DB84 09B3 FFB2 737B B1EB 75BC 98B5 23A8

To claim this, I am signing this object:

# Fish prompt to show username, host, CWD, Python VirtualEnv, VCS (git, svn, hg) info.
function fish_prompt --description 'Write out the prompt'
set -l last_status $status
if not set -q __fish_git_prompt_show_informative_status
set -g __fish_git_prompt_show_informative_status 1
end
if not set -q __fish_git_prompt_hide_untrackedfiles
set -g __fish_git_prompt_hide_untrackedfiles 1
end
@pchng
pchng / matmul_test.cu
Created March 6, 2024 19:38
CUDA matrix multiplication: Warp-thread linearization test
#include <stdio.h>
#define A 3000
#define B 4000
#define C 3000
// Computes out = left @ right, where `@` is matrix muliplication
// Dimensions:
// left: a x b
// right: b x c
@pchng
pchng / matmul_good.cu
Created March 6, 2024 19:41
CUDA: matmul good
// Computes out = left @ right, where `@` is matrix muliplication
// Dimensions:
// left: a x b
// right: b x c
// out: a x c
// monolithic kernel: One thread per output element in matrix C.
// (Each thread computes the dot product between a row in `left` and a col in `right`)
__global__ void matMul(float *left, float *right, float *out, int a, int b, int c) {
// Use y to index to rows, x to index to cols (just to match typical visualization)
// row indexes into left, col indexes into right.
@pchng
pchng / matmul_bad.cu
Created March 6, 2024 19:44
CUDA: matmul bad
// Same as above, but row/col set to x/y instead.
__global__ void matMulBad(float *left, float *right, float *out, int a, int b, int c) {
int row = blockIdx.x * blockDim.x + threadIdx.x;
int col = blockIdx.y * blockDim.y + threadIdx.y;
if (row < a && col < c) {
float sum = 0.0;
for (int i = 0; i < b; i++) {
// 1. If row/threadIdx.x is changing within the warp, then on each iteration the threads do a strided access:
// They will access elements separated by a stride of b. This results in non-coalesced accesses (multiple memory reads)