Skip to content

Instantly share code, notes, and snippets.

@firejox
firejox / CMakeLists.txt
Created May 24, 2023 13:55
LibTorch integrated with CMake on Windows Subsystem Linux
View CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
set(CMAKE_CUDA_ARCHITECTURES "75")
endif()
set(CMAKE_CUDA_COMPILER "/usr/local/cuda/bin/nvcc")
project(example-app)
@firejox
firejox / machine.cr
Last active December 21, 2019 13:18
State machine (pure inline)
View machine.cr
struct Machine
enum State
Before
Run
After
Next
end
def initialize(@state : State, @line : Int32)
end
@firejox
firejox / rotate.cr
Created December 5, 2019 16:39
another array rotaion
View rotate.cr
class Array
def new_rotate!(n = 1)
return self if empty?
sz = @size
n %= sz
return self if n == 0
st = to_unsafe
ed = st + sz
while (sz > 1 && n != 0)
@firejox
firejox / benchmark.cr
Created December 5, 2019 04:04
Array rotation
View benchmark.cr
require "benchmark"
class Array
def new_rotate!(n = 1)
return self if empty?
sz = self.size
n %= sz
return self if n == 0
tmp = Slice.new(to_unsafe, sz)
@firejox
firejox / LL(1)-pg.cr
Created November 8, 2019 12:50
LL(1) parser generator
View LL(1)-pg.cr
annotation ParserAttr
end
annotation TopSymbolAttr
end
annotation SymbolAttr
end
annotation Rule
@firejox
firejox / solution-1.cr
Last active June 8, 2020 05:47
The changes about prevent leaking container_of globaly
View solution-1.cr
# channel.cr
class Channel(T)
struct Sender(T)
@link : Crystal::StaticList
end
Crystal::StaticList.container_of(ptr, Channel::Sender(T), @link)
end
# crystal/static_list.cr
@firejox
firejox / bench.cr
Created October 14, 2019 06:22
Channel ping-pong latency
View bench.cr
ch = Channel(Int32).new
done = Channel(Time::Span).new
count = (ARGV[0]? || 1000).to_i
4.times do
spawn do
delay = Time::Span::ZERO
count.times do |i|
@firejox
firejox / bench.cr
Created October 14, 2019 06:04
Channel close latency
View bench.cr
total_delay = Time::Span::ZERO
done = Channel(Time::Span).new
(ARGV[0]? || 1000).to_i.times do |i|
ch = Channel(Int32).new
501.times do |j|
if j == 250
spawn do
delay = Time.measure do
@firejox
firejox / bench.cr
Created October 14, 2019 05:31
Channel select unwait benchmark
View bench.cr
ch1 = Channel(Int32).new
ch2 = Channel(Int32).new
500.times do
spawn do
r = Random.new
ch2.send(r.next_int)
end
end
@firejox
firejox / static_list.cr
Created October 7, 2019 16:57
Circular Linked List
View static_list.cr
macro container_of(ptr, _type, member)
{% if _type.resolve < Reference %}
(({{ ptr }}).as(Void*) - offsetof({{ _type }}, {{ member }})).as({{ _type }})
{% else %}
(({{ ptr }}).as(Void*) - offsetof({{ _type }}, {{ member }})).as(Pointer({{ _type }}))
{% end %}
end
struct StaticList
@prev = uninitialized Pointer(StaticList)