Skip to content

Instantly share code, notes, and snippets.

View mratsim's full-sized avatar
:shipit:

Mamy Ratsimbazafy mratsim

:shipit:
  • Paris
View GitHub Profile
## main.nim - uses a loop and a split
import strutils
let inFile = open("input.txt", fmRead)
let outFile = open("output.txt", fmWrite)
var ln: TaintedString = ""
var parts: seq[string]
while inFile.readLine(ln):
@mratsim
mratsim / chaining.nim
Created May 24, 2017 23:26
Nim inline iterator chaining macro
## From https://forum.nim-lang.org/t/2856
import macros
macro chaining(code: untyped): untyped =
const chainIdent = "chain"
const combineIdent = "combine"
proc inspect(depth: int, n: NimNode): NimNode =
case(n.kind)
of nnkIdent, nnkStrLit:
@mratsim
mratsim / whilelet.nim
Created July 4, 2017 19:49
Nim while let
proc filter[T](it: (iterator : T), f: proc(x: T): bool): (iterator: T) =
return iterator(): T =
while (let x = it(); not finished(it)):
if f(x):
yield x
@mratsim
mratsim / find_big_git.sh
Created September 8, 2017 11:07
Git: find big files
git rev-list --objects --all \
| git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' \
| awk '/^blob/ {print substr($0,6)}' \
| sort --numeric-sort --key=2 \
| cut --complement --characters=13-40 \
| numfmt --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest
@mratsim
mratsim / create_labels.sh
Created September 10, 2017 09:37 — forked from omegahm/create_labels.sh
Create Gtihub labels from Bash
#!/usr/bin/env bash
# Colours picked from https://robinpowered.com/blog/best-practice-system-for-organizing-and-tagging-github-issues/
###
# Label definitions
###
declare -A LABELS
# Platform
@mratsim
mratsim / call_cuda.nim
Last active September 16, 2017 09:26
Calling CUDA kernels from Nim
import nimcuda/[cuda_runtime_api, driver_types, nimcuda]
import sequtils, future
type GpuArray[T: SomeReal] = object
data: ref[ptr T]
len: int
{.compile: "./square.cu".}
proc cuda_square(bpg, tpb: cint, y: ptr cfloat, x: ptr cfloat) {.importc, header:"../square.cuh".}
#../square.cuh is a workaround because header is not copied to nimcache
@mratsim
mratsim / bad.cu
Created September 16, 2017 10:49
Cuda - avoid branching in the same warp
__global__
void GPUCode( int* input, int* output, int length)
{
int idx = __umul24( blockDim.x, blockIdx.x) + threadIdx.x;
if ( idx < length )
{
output[ idx ] = input[ idx ] + 2 * input[ idx + 1 ];
}
}
@mratsim
mratsim / grid-stride.cu
Created September 16, 2017 10:50
CUDA - Grid-stride loop
__global__
void saxpy(int n, float a, float *x, float *y)
{
for (int i = blockIdx.x * blockDim.x + threadIdx.x;
i < n;
i += blockDim.x * gridDim.x)
{
y[i] = a * x[i] + y[i];
}
}
@mratsim
mratsim / call_cuda2.nim
Last active September 17, 2017 10:47
Call cuda from Nim (alternative version)
## Even easier, without the cu / cuh
## Note VScode properly syntax highlight the emit part, yeah!
import nimcuda/[cuda_runtime_api, driver_types, nimcuda]
import sequtils, future
type GpuArray[T: SomeReal] = object
data: ref[ptr T]
len: int
@mratsim
mratsim / elementIDToOffset.cpp
Created September 18, 2017 07:36
Convert element ID to Offset in strided array
// Note: this doesn't work (yet)
// Example program
#include <iostream>
#include <string>
int cuda_getIndexOfElementID(
const int rank,
const int * __restrict__ shape,
const int * __restrict__ strides,