Skip to content

Instantly share code, notes, and snippets.

View simonbyrne's full-sized avatar
🦘

Simon Byrne simonbyrne

🦘
View GitHub Profile
@simonbyrne
simonbyrne / thread_sparse.jl
Last active January 17, 2018 23:35
Sparse matrix - vector multiplication with threads.
# Gives roughly a 2x speedup using JULIA_NUM_THREADS=2
# requires 0.7, due to closure type inference bug in 0.6
using Random, SparseArrays
srand(1); X = sprand(20_000,100_000,0.1); v = ones(100_000);
function mul(X,v)
m,n = size(X)
U = zeros(size(X,1), Threads.nthreads())
# To ensure correct alignment (e.g. for an 80-bit type)
struct StrWrap{T}
value::T
end
function unsafe_reinterpret(T, a::A) where {A}
if sizeof(T) <= sizeof(A)
r = Ref(a)
Base.@gc_preserve r begin
u = convert(Ptr{T}, Base.unsafe_convert(Ptr{A}, r))
@simonbyrne
simonbyrne / normalize.jl
Last active December 8, 2017 19:24
column/row normalization of sparse matrices
"""
normalizecols!(X, p)
Normalize the non-empty columns of `X` using a `p`-norm.
Overwrites the contents of `X`.
"""
function normalizecols!(X::SparseMatrixCSC, p=2)
for col = 1:X.n
rr = X.colptr[col]:X.colptr[col+1]-1
module Intrinsics
tname(::Type{Float64}) = "double"
tname(::Type{Float32}) = "float"
fname(::Type{Float64}) = "f64"
fname(::Type{Float32}) = "f32"
for f in [:add, :sub, :mul, :div, :rem]
n = 2_500_000
p = 1.9e-5
X = sprand(n,n,p)
v = sprand(n,8/n)
function fast_A_mul_B(X::SparseMatrixCSC{TX,IX},v::SparseVector{TV,IV}) where {TX,IX,TV,IV}
X.n == v.n || throw(DimensionMismatch())
ind_out = promote_type(IX,IV)[]
val_out = promote_type(TX,TV)[]
@simonbyrne
simonbyrne / gitprob.sh
Created October 4, 2017 20:46
Create a bad git repository
git init --bare gitprob
git clone gitprob gitprobwork
cd gitprobwork
echo "good" > aaaa
GOOD=$(git hash-object -w aaaa)
git add aaaa
git commit -m "1"
rm aaaa
@simonbyrne
simonbyrne / fisher.jl
Created October 4, 2017 05:25
Computing Fisher information via forward-mode automatic differentiation
using Distributions
import ForwardDiff: Dual, value, partials
@generated function get_values(a::NTuple{N}) where {N}
return ForwardDiff.tupexpr(i -> :(value(a[$i])),N)
end
ForwardDiff.value(p::ForwardDiff.Partials) =
ForwardDiff.Partials(get_values(p.values))
#include <stdio.h>
#include <gmp.h>
#include <mpfr.h>
int main (void)
{
unsigned int i;
mpfr_t u;
using SenseHat
function newblob(snake)
while true
x = rand(1:8)
y = rand(1:8)
if (x,y) ∉ snake
return (x,y)
end
end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.