Skip to content

Instantly share code, notes, and snippets.

View blackball's full-sized avatar
🎯
Focusing

Hui blackball

🎯
Focusing
View GitHub Profile
@blackball
blackball / quad-tree.c
Last active August 29, 2015 14:04
quad-tree
#include "quad-tree.h"
#include <math.h>
#include <stdlib.h>
struct qnode_t {
struct point_t pt;
struct qnode_t *children[4];
};
struct qtree_t {
#define CUDA_KERNEL_GUARD(i, n) for(int i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += blockDim.x * gridDim.x)
__global__
void saxpy(int n, float a, float *x, float *y) {
CUDA_KERNEL_GUARD(i, n) {
y[i] = a * x[i] + y[i];
}
}
@blackball
blackball / .emacs-part
Created November 27, 2013 06:15
set the colors of bracklets/braces/... for C and C++ mode
(defface paren-face
'((((class color) (background dark))
(:foreground "grey80"))
(((class color) (background light))
(:foreground "grey40")))
"Face used to dim parentheses.")
(defface paren-face-2
'((((class color) (background dark))
(:foreground "grey40"))
@blackball
blackball / cuda-exec-model-explained.cu
Last active December 24, 2015 23:29
here is a way I try to self-explain the CUDA execution configuration model. this makes me think flexiblly when writing CUDA codes.
/**
* here is a way I try to self-explain the CUDA execution configuration model.
* this makes me think flexiblly when writing CUDA codes.
*
* @blackball
*/
/*
normally, we write kernel function like this.
note, __global__ means this function will be called from host codes,
@blackball
blackball / str-int.c
Last active December 15, 2015 00:19
convert 32 bit long int or string to string or int.
/* @TODO A simple optimization is to concatenate all sub strings into one big string.
* this will save some memory and probably make accessing more efficient.
*/
/* char value to binary string look-up table, generated by Python */
static const char* _convTab[256] = {
"00000000","00000001","00000010","00000011","00000100","00000101","00000110","00000111",
"00001000","00001001","00001010","00001011","00001100","00001101","00001110","00001111",
"00010000","00010001","00010010","00010011","00010100","00010101","00010110","00010111",
"00011000","00011001","00011010","00011011","00011100","00011101","00011110","00011111",
@blackball
blackball / var.c
Created December 24, 2012 03:28
accurate and stable variance calculation
#include <stdio.h>
#include <assert.h>
typedef double real;
static inline real
variance(const real *vec, const int len) {
/* iteratove variance calculation */
register real oldm, newm, olds, news;
assert( vec && (len > 0));
/**
* Simple Lisp tree implementation.
* Adopted from http://nakkaya.com/2010/08/24/a-micro-manual-for-lisp-implemented-in-c/
*
* @blackball
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
@blackball
blackball / one_nn_merge.c
Created November 2, 2012 15:49
merge 1-NN graph into connected components.
#include <stdio.h>
#include <string.h>
static void
_merge(int *labels, int n, int src, int dst) {
for (int i = 0; i < n; ++i) {
if (labels[i] == src)
labels[i] = dst;
}
}
@blackball
blackball / k-smallest.c
Created October 14, 2012 04:54
k-smallest selection using max-heap
/* Find K-smallest value in array use a simplified max-heap */
/*
0. Build a max-heap(MH) for the first K elements
1. For each element after the Kth element, compare it with the root of MH
0. If the element is smaller than the root, make it root and heapify^1 MH
1. Else ignore it.
2. Finally, MH has K-smallest elements.
Complexity: O(K + (n-K)log(K) + Klog(K)), the last Klog(K) for sorted^2 ouput.
@blackball
blackball / gist:3446925
Created August 24, 2012 07:00
inc-de-crease-fontsize
;; overide the default changing font size bindings
;; to make changing more subtle
(defun local-increase-font-size ()
(interactive)
(set-face-attribute 'default
nil
:height
(ceiling (* 1.03 (face-attribute 'default :height)))))
(defun local-decrease-font-size ()