Skip to content

Instantly share code, notes, and snippets.

View main.cpp
#include <thread>
#include <chrono>
#include <atomic>
int message = 1;
std::atomic<int> sync_dummy{0};
void Write() {
message = 0;
sync_dummy.fetch_add(0, std::memory_order_relaxed);
View bench.cpp
#include <benchmark/benchmark.h>
void LockedAdd(benchmark::State& state) {
unsigned int val;
while (state.KeepRunning()) {
asm volatile("lock addl $1, %[val]" : [val] "=m"(val));
benchmark::DoNotOptimize(val);
}
}
@npetrenko
npetrenko / BaseConcept.hpp
Last active Sep 24, 2019
Using concepts for matching CRTP inheritance
View BaseConcept.hpp
#include <type_traits>
template <class Derived>
class Base {
public:
int Create() const {
return static_cast<const Derived&>(*this).CreateImpl();
}
};
View CloneTrait.cpp
#include <type_traits>
#include <cassert>
template <class T>
struct Clone {
template <class Dummy = void>
auto operator()(const T& val) const {
static_assert(!std::is_same_v<Dummy, void>, "Clone trait not implemented");
}
};
View type_erasure.cpp
#include <array>
#include <type_traits>
#include <cstddef>
template <class ValueType, bool is_const>
class ContiguousContainerView {
using CQualValueType = std::conditional_t<is_const, const ValueType, ValueType>;
public:
using DerefT = CQualValueType&;
using PointerT = CQualValueType*;
View crtp.cpp
#include <type_traits>
#include <iostream>
// Template magic to test if class has .METHOD
#define ENABLEHASMETHOD(METHOD) \
template <class T__> \
struct Has_##METHOD { \
template <class U__, class = decltype(&(U__::METHOD))> \
static const std::true_type Tester(U__*); \
\
View .emacs
(require 'package)
(setq package-enable-at-startup nil)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
(add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/"))
(add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/"))
(package-initialize)
(unless (package-installed-p 'use-package)
(package-refresh-contents)
View test_quantize
def test_shape():
arr = np.random.normal(size=(2,1,2,3,1,4,5))
boundaries, quantized = quantize(arr, 20)
assert np.all(quantized.shape == arr.shape)
boundaries_slow, quantized_slow = quantize_slow(arr.reshape(-1), 20)
boundaries_slow = boundaries_slow.reshape(boundaries.shape)
quantized_slow = quantized_slow.reshape(quantized.shape)
View test_speed
import bisect
import random
import pytest
import numpy as np
from quantize import quantize
from time import time
def quantize_slow(x, n):
View cmake_numpy
cmake_minimum_required(VERSION 3.13)
project(np_test)
execute_process(
COMMAND "python3" -c "import numpy; print(numpy.get_include(), end='')"
OUTPUT_VARIABLE NUMPY_INCLUDE_DIRS
RESULT_VARIABLE NUMPY_NOTFOUND)
find_package(PythonLibs 3 REQUIRED)