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 / gist:ea9a2e88f20278d2cb53
Created June 17, 2014 13:49
golang https server
package main
import (
"net/http"
"fmt"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Println("Inside handler")
fmt.Fprintf(w, "Hello world from my Go program!")
@helinwang
helinwang / tf_idf.go
Last active January 27, 2016 08:14
calculate max tfidf of each word in text corpus (given directory) in golang
package main
import (
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"sort"
"strings"
@helinwang
helinwang / gist.go
Last active December 11, 2016 16:27
save and load tensorflow tensors using tensorflow golang api
// Save:
outTensor, err := tf.NewTensor(filename)
if err != nil {
return err
}
_, err = session.Run(
map[tf.Output]*tf.Tensor{
graph.Operation("save/Const").Output(0): outTensor,
},
nil,
@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.

@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 / 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 / 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 / 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 / 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 / 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)