// called by init() func initGPUProf(OD string) { if *Flag_gpuprof { util.PanicErr(os.Setenv("CUDA_PROFILE", "1")) util.PanicErr(os.Setenv("CUDA_PROFILE_CSV", "1")) out := OD + "gpuprofile.csv" log.Println("writing GPU profile to", out) util.PanicErr(os.Setenv("CUDA_PROFILE_LOG", out)) cfgfile := OD + "cudaprof.cfg" util.PanicErr(os.Setenv("CUDA_PROFILE_CONFIG", cfgfile)) util.FatalErr(ioutil.WriteFile(cfgfile, []byte(CUDA_PROFILE_CONFIG), 0666), "gpuprof") //AtExit(cuda.DeviceReset) } }
// generate wrapper code from template func wrapgen(filename, funcname string, argt, argn []string) { kernel := &Kernel{funcname, argt, argn, make(map[int]string)} for _, s := range cc { kernel.PTX[s] = filterptx(fmt.Sprint(util.NoExt(filename), "_", s, ".ptx")) } wrapfname := util.NoExt(filename) + "_wrapper.go" wrapout, err := os.OpenFile(wrapfname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) util.PanicErr(err) defer wrapout.Close() util.PanicErr(templ.Execute(wrapout, kernel)) }
// Filter comments and ".file" entries from ptx code. // They spoil the git history func filterptx(fname string) string { f, err := os.Open(fname) util.PanicErr(err) defer f.Close() in := bufio.NewReader(f) var out bytes.Buffer out.Write(([]byte)("`")) line, err := in.ReadBytes('\n') for err != io.EOF { util.PanicErr(err) if !bytes.HasPrefix(line, []byte("//")) && !bytes.HasPrefix(line, []byte(" .file")) { out.Write(line) } line, err = in.ReadBytes('\n') } out.Write(([]byte)("`")) return out.String() }
// generate cuda wrapper for file. func cuda2go(fname string) { // open cuda file f, err := os.Open(fname) util.PanicErr(err) defer f.Close() // read tokens var token []string var s scanner.Scanner s.Init(f) tok := s.Scan() for tok != scanner.EOF { if !filter(s.TokenText()) { token = append(token, s.TokenText()) } tok = s.Scan() } // find function name and arguments funcname := "" argstart, argstop := -1, -1 for i := 0; i < len(token); i++ { if token[i] == "__global__" { funcname = token[i+2] argstart = i + 4 } if argstart > 0 && token[i] == ")" { argstop = i + 1 break } } argl := token[argstart:argstop] // isolate individual arguments var args [][]string start := 0 for i, a := range argl { if a == "," || a == ")" { args = append(args, argl[start:i]) start = i + 1 } } // separate arg names/types and make pointers Go-style argn := make([]string, len(args)) argt := make([]string, len(args)) for i := range args { if args[i][1] == "*" { args[i] = []string{args[i][0] + "*", args[i][2]} } argt[i] = typemap(args[i][0]) argn[i] = args[i][1] } wrapgen(fname, funcname, argt, argn) }
// path to the executable. func procSelfExe() string { me, err := os.Readlink("/proc/self/exe") util.PanicErr(err) return me }