Skip to content

Instantly share code, notes, and snippets.

View brenhinkeller's full-sized avatar

C. Brenhin Keller brenhinkeller

View GitHub Profile
# run with e.g. mpiexec -np 8 julia ./mpihello.jl
using MPI
MPI.Init()
comm = MPI.COMM_WORLD
size = MPI.Comm_size(comm)
rank = MPI.Comm_rank(comm)
print("Hello from $rank of $size processors!\n")
## ---
using StatGeochem, Plots
# Calculate a histogram of three variables in ternary space
function ternaryhist(a,b,c; nbins::Int=10)
# Normalize input data
d = a + b + c
a ./= d
b ./= d
c ./= d
We can make this file beautiful and searchable if this error is corrected: It looks like row 6 should actually have 17 columns, instead of 4. in line 5.
Compilation Reference Locality Glacier Region Type Methodology Area (km2) Time interval (yr) Sediment flux (km3/yr) Exhumation Exhumation min Exhumation max Erosion rate (mm/yr) Erosion rate min Erosion rate max Comments
This study Lasabuda et al., 2018 NE Svalbard margin Fennoscandian Icesheet Fennoscandia Continental Volumetric 47500 2700000 530 410 650 0.196296296296296 0.151851851851852 0.240740740740741
This study Laberg et al., 2012 SW Barents / Bjørnøya Fan Fennoscandian Icesheet Fennoscandia Continental Volumetric 576000 2700001 1100 0.407407256515831
This study Laberg et al., 2012 SW Barents / Bjørnøya Fan Fennoscandian Icesheet Fennoscandia Continental Volumetric 576000 800000 375 330 420 0.46875 0.4125 0.525
This study Laberg et al., 2012 SW Barents / Bjørnøya Fan Fennoscandian Icesheet Fennoscandia Continental Volumetric 200000 700000 485 440 530 0.692857142857143 0.628571428571429 0.757142857142857
This study Hjelstuen et al., 1996 NW Barents / Storfjorden Fan Fennoscandian Iceshee
julia> using StaticCompiler, StaticTools
julia> hello() = println(c"Hello from compiled Julia!")
hello (generic function with 1 method)
julia> # Functions to compile into a single shlib
# add more (name, (argtypes..)) tuples to the array to add more functions
funcs = [(hello, ())]
1-element Vector{Tuple{typeof(hello), Tuple{}}}:
(hello, ())
@brenhinkeller
brenhinkeller / mpihello.jl
Created October 19, 2022 16:28
Natively compile and run parallel Hello World with MPICH_jll MPI in Julia!
using StaticCompiler, StaticTools, StaticMPI, MPICH_jll
function mpihello(argc, argv)
MPI_Init(argc, argv)
comm = MPI_COMM_WORLD
world_size, world_rank = MPI_Comm_size(comm), MPI_Comm_rank(comm)
printf((c"Hello from ", world_rank, c" of ", world_size, c" processors!\n"))
MPI_Finalize()
@brenhinkeller
brenhinkeller / heftyboxes.jl
Created August 22, 2022 20:15
Reproduce HEFTY-type t-T paths using _only_ the boxes
## --- Reproduce HEFTY-type t-T paths using _only_ the boxes
using Plots, Colors
struct TtBox
Tmin::Float64
Tmax::Float64
tmin::Float64
tmax::Float64
end
@brenhinkeller
brenhinkeller / printmandel.jl
Created June 8, 2022 03:04
Compile an executable that will print an ascii image of the Mandelbrot Set to the terminal
# Looks best when run in a terminal with the tab stop width set to four spaces.
using StaticTools, StaticCompiler
function mandelbrot(zᵣ, zᵢ)
cᵣ, cᵢ = zᵣ, zᵢ
maxiter = 999
for n = 1:maxiter
zᵣ², zᵢ² = zᵣ^2, zᵢ^2
if zᵣ² + zᵢ² > 4
return n-1
@brenhinkeller
brenhinkeller / verf.jl
Last active January 22, 2021 07:52
verf from SIMDPirates.jl updated to (I think!) work on Julia 1.6
# Copyright (c) 2016, Johan Mabille, Sylvain Corlay, Wolf Vollprecht and Martin Renou
# Copyright (c) 2016, QuantStack
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
@brenhinkeller
brenhinkeller / lispmode.jl
Last active July 4, 2023 22:30
Bare-bones REPL for Julia's secret built-in s-expression syntax, in under 30 lines
to_expr(x) = x
to_expr(t::Tuple) = Expr(to_expr.(t)...) # Recursive to_expr implementation courtesy of Mason Protter
lisparse(x) = to_expr(eval(Meta.parse(x))) # Note that the `eval` in here means that any normal (non-s-expression) Julia syntax gets treated a bit like a preprocessor macro: evaluated _before_ the s-expression syntax is compiled and evaluated
function lispmode()
# READ
printstyled("\nlisp> ", color=:magenta, bold=true)
l = readline()
while l !== "(:exit)"
try # So we don't get thrown out of the mode
# EVAL