Skip to content

Instantly share code, notes, and snippets.

@aprell
aprell / omp_tids.c
Last active June 8, 2022 14:27
A small experiment to show that OpenMP implementations reuse threads
// RUN: cc -Wall -Wextra -pthread %s && ./a.out | sort | uniq
// RUN: cc -Wall -Wextra -fopenmp %s && ./a.out | sort | uniq
#include <assert.h>
#include <omp.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <syscall.h>
#include <unistd.h>
@aprell
aprell / unroll.c
Last active October 26, 2021 15:35
Different loop unrolling strategies
// CFLAGS="-Wall -Wextra -Wno-implicit-fallthrough"
#include <assert.h>
#include <stdio.h>
// 1) (n - (n % 4)) / 4 iterations
// 2) n % 4 iterations
void loop1(int n)
{
int i, j;
@aprell
aprell / testrun.sh
Created August 26, 2019 12:44
A simple test runner
#!/usr/bin/env bash
# ShellChecked
set -eu
set -o pipefail
usage=$(cat <<EOF
Usage: $(basename "$0") test.input [repetitions]
If not specified, repetitions is set to 10.
@aprell
aprell / spinning_wheel.sh
Created August 26, 2019 12:37
A simple spinning wheel
#!/usr/bin/env bash
# ShellChecked with no issues detected
set -eu
set -o pipefail
spinning_wheel() {
local spinning=("|" "/" "-" "\\")
local i=0
@aprell
aprell / run.sh
Last active October 6, 2019 08:28
Convenience script for compiling and running C/C++ programs
#!/usr/bin/env bash
# ShellChecked with no issues detected
set -eu
set -o pipefail
compile() {
local args
args=$(head -1 "$src" | sed 's/^\/*//')
# Ignore any Makefiles that might be present
@aprell
aprell / time_machine.sh
Created August 25, 2019 15:32
Speed up Time Machine operation (macOS)
# Speed up Time Machine operation:
sudo sysctl debug.lowpri_throttle_enabled=0
# Revert to default setting:
sudo sysctl debug.lowpri_throttle_enabled=1
@aprell
aprell / Makefile
Last active June 16, 2019 17:54
Target-specific variables
foo: bar
foo:
@echo foo: $(FLAGS)
bar: FLAGS += B
bar: baz
bar:
@echo bar: $(FLAGS)
baz: FLAGS += C
#include <functional>
#include <iostream>
template<typename T>
struct coro {
using fn = std::function<T(T)>;
coro(fn f) : m_lc(0), m_done(false), m_fn(f) {}
bool done() const { return m_done; }
T resume(T arg) { return m_fn(arg); }
#include <stdbool.h>
#include <stdio.h>
struct coro {
unsigned int lc;
bool done;
};
#define CORO_INIT(co) \
(co)->lc = 0; \
@aprell
aprell / Makefile
Created March 7, 2019 19:13
Wrapping library functions using -Wl,--wrap=symbol
CC := gcc
CFLAGS += -Wall -Wextra -std=c99
LDLIBS += -lpthread
all: test_wrap
test_wrap: LDFLAGS += -Wl,--wrap=pthread_create -Wl,--wrap=pthread_join
test_wrap: test_wrap.c wrap.c
clean: