Skip to content

Instantly share code, notes, and snippets.

View maedoc's full-sized avatar

marmaduke woodman maedoc

  • now
  • here
View GitHub Profile
@maedoc
maedoc / 2d.jl
Created October 22, 2021 20:38
2D field in Julia
using FFTW
using DiffEqSensitivity, OrdinaryDiffEq, Zygote
nlon = 64
nlat = 64
K = (1:(div(nlat,2)+1)) .* (1:nlon)' / (nlat*nlon)
function f(x,p,t)
k = p[1]
lc = k * irfft(K .* rfft(x), nlat)
@maedoc
maedoc / sht.py
Created October 14, 2021 17:10
Spherical harmonic transform with NumPy/SciPy
import numpy as np
from scipy.special import sph_harm
class ShtDiff:
def __init__(self, lmax=16, nlat=64, D=-1e-2, nlon=None):
self.lmax = lmax
self.nlat = nlat
self.D = D
self.nlon = nlon or (2 * nlat)
@maedoc
maedoc / fftre2n.py
Created October 11, 2021 19:20
Compute 2N-point real FFT w/ a N-point complex FFT
import numpy as np
# Compute 2N-point real FFT w/ a N-point complex FFT, cf. TI SPRA 291
# random 2N-point real sequence
g = np.random.randn(16)
# size of sequence
n = len(g)//2
n2 = 2 * n
@maedoc
maedoc / polyarith.fut
Last active October 1, 2021 19:23
Polymorphic arithmetic in Futhark
-- was not clear to me from the user guide how this is supposed to work
-- when in fact it's straightforward
-- put the needed arithmetic ops in an "interface" / "concept"
module type arith = {
type t
val i32 : i32 -> t
val * : t -> t -> t
}
@maedoc
maedoc / trirecol.fut
Created September 28, 2021 17:06
Futhark kernel for triangular matrix generation with recursion down columns [wip]
-- we want to verify we can do the irregular structure of the sht
-- when mapped on the GPU.
-- if we can't, we have (lmax+1)*lmax/2 wasted space, as follows
let lt1 [nlat] (lmax:i64) (m:i64) (fm:[nlat]f32): [lmax]f32 =
let q = tabulate lmax (\_ -> 0f32)
let (q, _) =
loop (q, x) = (q, 0f32) for i < (lmax - m) do
let m' = m + i
let q[m'] = reduce (+) x fm |> (*x)
@maedoc
maedoc / dif-fft-gen.py
Last active October 5, 2021 15:57
Generating explicit DIF FFT kernels with Sympy
import os, ctypes
import numpy as np
from sympy import exp, pi, I, Symbol, re, im, cse
from sympy.printing.c import ccode
# DIF FT recursion
def ft(x):
N = len(x)
if N == 2:
a, b = x
@maedoc
maedoc / Pmn.py
Last active October 1, 2021 12:50
Associated Legendre polynomials via recursion relations
import numpy as np
from scipy.special import sph_harm, lpmv
import shtns
import os
os.system('futhark c --library pmn.fut')
os.system('build_futhark_ffi pmn')
from futhark_ffi import Futhark
import _pmn
pmn = Futhark(_pmn)
@maedoc
maedoc / mpr-append.fut
Last active September 30, 2021 12:44
Neural mass network simulations in Futhark
-- this variant uses functional approach, appending new states to buffer
-- instead of in-place update
let pi = 3.141592653589793f32
-- some type abbreviations
type mpr_pars = {G: f32, I: f32, Delta: f32, eta: f32, tau: f32, J: f32}
type mpr_node = (f32, f32)
type mpr_net [n] = [n] mpr_node
-- this is tranposed from mpr-pdq to avoid tranposes in history update
@maedoc
maedoc / README.md
Last active June 30, 2021 14:13
TF op with C++ resource

mini-example of a TF op with a custom C++ resource

There's a TF tutorial on creating a op, which covers anything written from scratch, but when you've got existing C++ classes (or anything callable from C++, really) which do the heavy lifting and are stateful, or at least require intialization/instatiation, the tutorial recommends, as a side note that ops need to be reentrant, that a ResourceMgr be used, linking to the corresponding header and no example.

That's sort of a cliff-hanger given how nice the rest of the tutorial. Two related SO questions

@maedoc
maedoc / SHTMatrix2.ipynb
Last active May 1, 2021 20:34
Prototyping spherical harmonic transforms
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.