如何在.NET中向进程添加本地环境? [英] How to add a local environment to a process in .NET?

查看:53
本文介绍了如何在.NET中向进程添加本地环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要从命令行编译Cuda内核,需要做两件事.第一种是像这样运行vsvars批处理文件:

To compile a Cuda kernel from the command line, two things are necessary. The first is to run the vsvars batch file like so:

"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64\vcvarsx86_amd64.bat"

第二个是实际调用编译器:

The second is to actually invoke the compiler:

"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin\nvcc.exe" -gencode=arch=compute_30,code=\"sm_30,compute_30\" --use-local-env --cl-version 2015 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64"  -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include"     --keep-dir "C:\Users\Marko\Documents\Visual Studio 2015\Projects\Multi-armed Bandit Experiments\NVCC Experiments\x64\Release" -maxrregcount=0  --machine 64 -ptx -cudart static  -o "C:\Users\Marko\Documents\Visual Studio 2015\Projects\Multi-armed Bandit Experiments\NVCC Experiments\x64\Release\kernel.cu.obj" "C:\Users\Marko\Documents\Visual Studio 2015\Projects\Multi-armed Bandit Experiments\NVCC Experiments\kernel.cu"

我不想通过命令行,而是通过F#脚本或可执行文件来执行上述操作,但不确定如何执行第一步.第二个很好记录在MSDN 上;可以使用Process类来完成.

I want to do the above not from the command line, but from an F# script or an executable, but am not sure how to do the first step. The second is well documented on MSDN though; it can be done using the Process class.

顺便说一句,Cuda库的运行时确实与我到目前为止一直在使用的称为NVRTC的NVCC编译器等效,但是它缺少一些关键功能,例如lambda,元组和对其他Cuda库的访问.我是如此想要lambda和tuple,以至于我甚至对Cuda编译器都做了自己的F#报价,但是这样做比比在自我施加的约束下进行操作更好.

As an aside, the Cuda library does have the runtime equivalent of the NVCC compiler called NVRTC which I've been using up to now, but it lacks some crucial features such as lambdas, tuples and access to other Cuda libraries. I wanted lambdas and tuples so much that I even made my own F# quotations to Cuda compiler, but doing it like this would be better than operating under self imposed constraints.

有什么想法可以在致电NVCC之前添加该本地环境吗?另外,如果该过程可以重用,那将很有趣.

Any ideas how I can add that local environment before calling NVCC? Also, it would be interesting if the process could be reused.

推荐答案

open System
open System.Diagnostics

let procStartInfo = 
    ProcessStartInfo(
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        RedirectStandardInput = true,
        UseShellExecute = false,
        FileName = "cmd"
    )

let outputHandler f (_sender:obj) (args:DataReceivedEventArgs) = f args.Data
let p = new Process(StartInfo = procStartInfo)
let print_to_standard_output = outputHandler <| fun x -> printfn "%s" x
p.OutputDataReceived.AddHandler(DataReceivedEventHandler (print_to_standard_output))
p.ErrorDataReceived.AddHandler(DataReceivedEventHandler (print_to_standard_output))
p.Start()
p.BeginOutputReadLine()
p.BeginErrorReadLine()
p.StandardInput.WriteLine """ "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64\vcvarsx86_amd64.bat" """
p.StandardInput.WriteLine """ "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin\nvcc.exe" -gencode=arch=compute_30,code=\"sm_30,compute_30\" --use-local-env --cl-version 2015 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64"  -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include"     --keep-dir "C:\Users\Marko\Documents\Visual Studio 2015\Projects\Multi-armed Bandit Experiments\NVCC Experiments\x64\Release" -maxrregcount=0  --machine 64 -ptx -cudart static  -o "C:\Users\Marko\Documents\Visual Studio 2015\Projects\Multi-armed Bandit Experiments\NVCC Experiments\x64\Release\kernel.cu.obj" "C:\Users\Marko\Documents\Visual Studio 2015\Projects\Multi-armed Bandit Experiments\NVCC Experiments\kernel.cu" """

事实证明,可以重定向标准输入并以这种方式从程序内部控制Shell.上面的代码片段是从MSDN页面上的代码片段改编而成的,可以执行我想要的-运行批处理文件以引入环境,然后运行NVCC编译器.

As it turns out, it is possible to redirect the standard input and control the shell from within the program that way. The above code fragment is adapted from the one on the MSDN page to do what I wanted - run the batch file to bring in the environment and then run the NVCC compiler.

这篇关于如何在.NET中向进程添加本地环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆