Skip to content

Instantly share code, notes, and snippets.

@matthieubulte
matthieubulte / c_env.py
Last active February 12, 2025 14:35
cache_experiment.py
class CEnv:
def __init__(self):
self.executable = None
def compile(self, code: str) -> str:
with tempfile.NamedTemporaryFile(mode="w", suffix=".c", delete=False) as temp_c:
temp_c.write(code)
c_file = temp_c.name
try:
# %%
from jupyc import c
# %%
%%c
int x = 5;
int y = 2;
x + y
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
from scipy.optimize import basinhopping
from scipy.ndimage import gaussian_filter1d
from sklearn.model_selection import KFold
def cv_bandwidth(X, y, bandwidths=np.logspace(-1, 1, 10)):
kf = KFold(n_splits=5)
// gcc -O3 -g -I/opt/OpenBLAS/include -L/opt/OpenBLAS/lib -lopenblas -march=native main.c -o main && ./main
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <arm_neon.h>
#include <stdio.h>
#include <stdlib.h>
#include <cblas.h>
@matthieubulte
matthieubulte / matmul.py
Last active January 14, 2025 12:45
matmul.py
######################################################################## PREP
import numpy as np
from scipy.linalg.blas import sgemm
import jax
import jax.numpy as jnp
DIM = 4096
accelerate = ctypes.cdll.LoadLibrary(
import time
import jax
from jax import numpy as jnp
import numpy as np
N=4096
flops_ish = N*N*2*N
for i in range(5):
A = np.random.rand(N,N).astype(np.float64)
B = np.random.rand(N,N).astype(np.float64)
t1 = time.monotonic()
@matthieubulte
matthieubulte / gadfly_animate.jl
Last active January 21, 2022 08:43
Animations in Gadfly
using Gadfly
import Cairo, Fontconfig
using Printf
using FFMPEG
#################################### This is pretty much the same code as in https://github.com/JuliaPlots/Plots.jl/blob/master/src/animation.jl
struct Animation
dir::String
frames::Vector{String}
kwargs::Iterators.Pairs
### A Pluto.jl notebook ###
# v0.12.21
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local el = $(esc(element))
@matthieubulte
matthieubulte / main.jl
Created April 24, 2021 17:31
Symbolic expression from a string
using MacroTools
using Symbolics
function parse(strexpr)
expr = Meta.parse(strexpr)
freevars = Set()
expr = MacroTools.postwalk(x -> begin
if isa(x, Symbol) && !isdefined(Base, x)
push!(freevars, Variable(x))
Expr(:call, Variable, QuoteNode(x))
@matthieubulte
matthieubulte / main.jl
Created April 2, 2021 22:56
Using Symbolics.jl to compile sparse matrix operations
using LinearAlgebra, BenchmarkTools, SparseArrays, SymbolicUtils, Symbolics
## Problem setup
const z = zeros;
wx = Float64[ 0 -1 0;
1 0 0;
0 0 0;];
Ak_1 = [ wx -I zeros(3,12);