Skip to content

Instantly share code, notes, and snippets.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <xmmintrin.h>
#include <mmintrin.h>
#include "matrix.h"
#include "z_order.h"
using namespace nda;
@bjourne
bjourne / bsearch.c
Created August 11, 2023 17:21
Fast and slow (?) bsearch
static uint32_t *
fast_bsearch_u32(uint32_t *base, size_t nmemb, uint32_t key) {
while (nmemb) {
size_t half = nmemb >> 1;
if (base[half] < key) {
base += nmemb - half;
}
nmemb = half;
}
return base;
// Compile and run with:
//
// gcc -O3 -march=native fast-strlen.c -lpthread -o fast-strlen
// && ./fast-strlen
//
// Use gcc because clang is too smart and optimizes away parts of the
// benchmark. Results on Xeon(R) CPU E5-2650 v4 @ 2.20GHz with gcc
// 9.4.0:
//
// Scanning 10 times over 4.00GB...
@bjourne
bjourne / mul5.c
Created September 28, 2022 16:18
// Notes:
//
// * unsigned int vs. int: makes a small difference for clang but
// probably not for gcc.
// * best tiling appears to be 256x256x256.
//
// 12.31 for two 8192 matrices
//
//
#include <assert.h>
@bjourne
bjourne / mul4.c
Created September 11, 2022 12:10
// Notes:
//
// * unsigned int vs. int: makes a small difference for clang but
// probably not for gcc.
// * best tiling appears to be 256x256x256.
//
// 12.31 for two 8192 matrices
#include <assert.h>
#include <math.h>
#include <pthread.h>
@bjourne
bjourne / cas.py
Last active July 26, 2022 20:03
Computer algebra in 99 lines of Python
# Usage:
# python cas.py simplify "(x+y)**30*(2*x)+123*z**8"
# python cas.py solve "3*x**2 == -x**2 + 5*x"
import math, sys
from ast import *
from functools import reduce
from collections import Counter, defaultdict
def parse_mv(mv):
def pow(n, e):
from ast import *
from pygraphviz import AGraph
from re import sub
COLOR_KWD = '#a020f0'
COLOR_VAR = '#6c71c4'
COLOR_STR = '#8b2252'
def colorize(s, col):
return '<font color="%s">%s</font>' % (col, s)
@bjourne
bjourne / index.html
Created October 29, 2021 14:19
layout with bootstrap resizable
<nav class="navbar navbar-default navbar-fixed-top navbar-inverse">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
from observations import ptb
from time import sleep, time
from torch.nn import *
from torch.optim import *
from torch.utils.data import DataLoader
from torch.utils.data.distributed import DistributedSampler
from torch_xla.core.xla_model import (get_ordinal,
is_master_ordinal,
master_print,
xla_device,
"""
Character-based language model in TensorFlow
============================================
A character-based language model written in TensorFlow and trained on
the Penn Treebank dataset. The model can either be trained using TFs
model.fit() method (auto) or using a custom training loop
(manual). Results should not differ.
Usage:
char_lm_tf.py [options] ( manual | auto )