Skip to content

Instantly share code, notes, and snippets.

View helinwang's full-sized avatar

Helin Wang helinwang

  • Bay Area
View GitHub Profile
@helinwang
helinwang / log15.diff
Last active March 11, 2018 05:51
Improve golang log15 library
diff --git a/format.go b/format.go
index bce4914..fb40b9d 100644
--- a/format.go
+++ b/format.go
@@ -12,8 +12,8 @@ import (
)
const (
- timeFormat = "2006-01-02T15:04:05-0700"
- termTimeFormat = "01-02|15:04:05"
@helinwang
helinwang / .travis.yml
Created March 2, 2018 22:31
PaddlePaddle EDL .travis.yml file
matrix:
include:
- language: go
go: 1.8.x
sudo: required
install:
- go get -u github.com/golang/lint/golint
- curl https://glide.sh/get | bash
- sudo pip install pre-commit
script:
@helinwang
helinwang / .pre-commit-config.yaml
Created March 2, 2018 22:29
PaddlePaddle EDL .pre-commit-config.yaml
- repo: https://github.com/dnephin/pre-commit-golang
sha: e4693a4c282b4fc878eda172a929f7a6508e7d16
hooks:
- id: go-fmt
files: \.go$
- id: go-lint
files: \.go$
exclude: (.*\/client\/.*\.go|.*\generated\.deepcopy\.go)$
- repo: local
@helinwang
helinwang / program_summary.py
Created February 16, 2018 23:03
Print PaddlePaddle Operators and Blocks
def program_summary(program):
print("--------------------")
for block in program.blocks:
for op in block.ops:
outputs = [[x + ":"] + op.output(x) for x in op.output_names]
inputs = [[x + ":"] + op.input(x) for x in op.input_names]
print(block.idx, op.type, inputs, "|", outputs)
@helinwang
helinwang / run.sh
Created December 20, 2017 23:57
Run docker with nvidia-gpu without nvidia-docker
ls /dev/nvidia* | xargs -I{} echo '--device {}:{}'
docker run -v /var/lib/nvidia-docker/volumes/nvidia_driver/384.98:/driver --device /dev/nvidia0:/dev/nvidia0 --device /dev/nvidiactl:/dev/nvidiactl --device /dev/nvidia-uvm:/dev/nvidia-uvm --device /dev/nvidia-uvm-tools:/dev/nvidia-uvm-tools -it -v `pwd`:/data nvidia/cuda:8.0-runtime-ubuntu16.04 bash
export PATH=$PATH:/driver/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/driver/lib64/
@helinwang
helinwang / byte_slice_to_float32_slice.go
Last active February 6, 2023 02:29
Go byte slice to float32 slice
package main
import (
"fmt"
"unsafe"
)
func byteSliceToFloat32Slice(src []byte) []float32 {
if len(src) == 0 {
return nil
@helinwang
helinwang / main.go
Created July 27, 2017 07:21
go net/rpc graceful shutdown
package main
import (
"context"
"flag"
"log"
"net/http"
"net/rpc"
"os"
"os/signal"
@helinwang
helinwang / init.el
Last active November 29, 2017 20:04
emacs rtags configuration
(menu-bar-mode -1)
(global-set-key "\M-g" 'goto-line)
(global-set-key "\M-." 'rtags-find-symbol-at-point)
(global-set-key "\M-," 'rtags-location-stack-back)
(global-set-key (kbd "M-]") 'next-error) ; Go to next error (or msg)
(global-set-key (kbd "M-[") 'previous-error) ; Go to previous error or msg
(global-set-key (kbd "M-m") 'rtags-find-references-at-point)
(require 'package)
@helinwang
helinwang / string.md
Last active March 7, 2024 01:23
How to call go from c with string (char *) as the parameter without making a copy

Here is the gist for showing how to send string from c to go without a copy, for sending an float32 array from c to go without copy, please see here

Below is the documentation for converting between a c string and a go string:

// From https://golang.org/cmd/cgo/
// A few special functions convert between Go and C types by making copies of the data. 
// Go string to C string
// The C string is allocated in the C heap using malloc.
// It is the caller's responsibility to arrange for it to be
// freed, such as by calling C.free (be sure to include stdlib.h
@helinwang
helinwang / gostring.md
Last active January 3, 2023 07:50
Call go (or c) code from python with string as argument and string as return value

go code (foo.go) compiled into a shared library and a c header (foo.h) is generated.

For calling go from c, please see here

Arguments

The code below shows two ways of passing string parameter to go function:

  1. Using GoString structure as argument, without making a copy in go code: no conversion to go string needed.
  2. Using c_char_p as argument, making a copy in go code when converting to go string.

When using the first method without the copy, I don't know how python will do the memory management with the pointer passed into go. So the second method is preferred.