Thanks to visit codestin.com
Credit goes to gist.github.com

Skip to content

Instantly share code, notes, and snippets.

View alifarazz's full-sized avatar
🐊

Ali Farazdaghi alifarazz

🐊
View GitHub Profile
@alifarazz
alifarazz / script-path.bash
Last active October 7, 2025 21:37
Get path of the currently running script in BASH. Also supports SLURM
#!/usr/bin/env bash
# Get path to current script,
if [ -n "${SLURM_JOB_ID:-}" ] ; then
SCRIPT_PATH="$(scontrol show job "$SLURM_JOB_ID" | awk -F= '/Command=/{print $2}')"
else
SCRIPT_PATH="$(readlink -f "${BASH_SOURCE[0]}")"
fi
SCRIPT_DIR="$(dirname "${SCRIPT_PATH}")"
@alifarazz
alifarazz / umonitor-umwait-semaphore-output.md
Last active November 5, 2025 18:53
A semaphore implementation using umonitor and umwait x86 instructions, available on AlderLake and newer.

On an Intel i7-13620H, running Linux 6.12.52-lts and gcc 15.2.1 without sanitizers:

$ ./umonitor-umwait-semaphore 16 3
Semaphore initialized with count = 3.
Spawning 16 worker threads...

Main thread: Releasing an additional permit after some time.

All workers finished.
@alifarazz
alifarazz / dijkstra-README.md
Last active November 5, 2025 16:42
Beating Shortsy's A-star with Dijkstra
  • How to compile:

    • $ g++ -DBIG_INPUT -Ofast -mtune=native -march=native -pipe -funroll-loops -Wall -Wextra -Wcast-align -Wchar-subscripts -Wshadow -Wunused dijkstra.cc -o dijkstra -flto
      or
    • $ clang++ -stdlib=libc++ -DBIG_INPUT -Ofast -mtune=native -march=native -funroll-loops -Wall -Wextra -Wcast-align -Wchar-subscripts -Wshadow -Wunused dijkstra.cc -o dijkstra -flto
  • Results on a Intel Core i7-13620H, compiled with gcc, with 1024x1024 input:

    $ numactl --physcpubind=2 hyperfine -N -i -w 30 -r 100 './dijkstra'
    Benchmark 1: ./dijkstra

Time (mean ± σ): 3.2 ms ± 0.0 ms [User: 2.9 ms, System: 0.3 ms]

@alifarazz
alifarazz / tmux.conf
Last active March 31, 2025 16:55
Personal tmux config
# in $HOME/.config/tmux/tmux.conf
# set-option -g default-shell "path to fish"
# I copied most of this off someone's blog.
# # remap prefix from 'C-b' to 'C-a'
# unbind C-b
# set-option -g prefix C-a
# bind-key C-a send-prefix
# split panes using | and -
@alifarazz
alifarazz / sync-with-remote.bash
Last active November 5, 2025 16:42
Basic script to sync a local project with a remote server
#!/usr/bin/env bash
# SPDX-License-Identifier: AGPL-3.0-or-later
######### NOTE: HOW TO USE #########################################
# $ bash sync-with-remote.bash <path to .(git)ignore file> <local path to watch for>
set -euEo pipefail
# set -x # show every command being executed, used for debug
@alifarazz
alifarazz / cpu_burn.cc
Last active October 6, 2024 23:02
Stress test CPU with int ops + ld + st
// g++ -std=c++20 cpu_burn.cc -o cpu_burn
#include <bits/stdc++.h>
constexpr size_t maxn = 192;
constexpr int iter = 1024 * 1024;
std::array<std::jthread, maxn> threads;
std::array<unsigned int, (1024ull * 1024 * 512)> data;
@alifarazz
alifarazz / syscall_user_dispatch.c
Last active October 7, 2025 21:41
Capture syscalls made in the current thread in a signal handler using `prctl(PR_SET_SYSCALL_USER_DISPATCH, ...)`
/// SPDX-License-Identifier: GPL-2.0-or-later
#define _GNU_SOURCE
#include <linux/prctl.h> /* Definition of PR_* constants */
#include <sys/prctl.h>
#include <sys/syscall.h> /* for __NR_write */
#include <ucontext.h> /* REG_RBP, REG_RSP */
#include <unistd.h> /* for write(2) */
@alifarazz
alifarazz / umwait.cc
Last active July 30, 2024 03:47
An example for Intel umonitor umwait tpause instructions introduced in Tremont, Alder Lake, Sapphire Rapids architectures
// g++ -O3 -Wall -Wextra -Wconversion -Wshadow -Wpedantic -std=c++20 -march=alderlake umwait.cc -o umwait.elf
// ./umwait.elf or ./umwait.elf w
#include <atomic>
#include <iostream>
#include <latch>
#include <thread>
#include <immintrin.h>
#include <x86intrin.h>
@alifarazz
alifarazz / example1.cc
Last active July 16, 2024 21:47
Strict Aliasing and restrict key word examples
#include <cstdio>
int f(int *ap, float *bp) {
*ap = 1;
*bp = 2.0f;
return *ap;
}
int main() {
int a = 0; float b = 0.0f;
@alifarazz
alifarazz / variable_dump.md
Last active November 5, 2025 16:43
CMake variable dump

CMake All Variable Dump

Put this wherever you'd want to dump the variables from.

message("=============BEGIN VARIABLE DUMP ========================")
get_cmake_property(_variableNames VARIABLES)
foreach (_variableName ${_variableNames})
    message("${_variableName}=${${_variableName}}")
endforeach()
message("=============END VARIABLE DUMP ==========================")