Scripting in Visual Studio: It's Not a Pain in the Ass Actually

2:20 PM

Recently, I ran into a stumbling block with a project and want to share my solution. Even if you're a super-beginner, this will show you how to automate running a program you're working on in Visual Studio. 


This quarter I'm taking a Parallel Programming course. Our 6th project required the use of a GPU to run OpenCL code. The default for our class is to SSH into the professor's Linux server Rabbit, which has a nice beefy graphics card. That stopped being an option when we accidentally killed it. Some of us with a GPU at home were skittish about the idea of running it locally in Visual Studio (me). We were unsure of how to automate running the program hundreds of times in Windows, with the way our skeleton code was set up, so this tutorial is for my CS 475 classmates and for Professor Bailey's poor Rabbit.

Onto the tutorial!

I will be using our Project 6 as an example. We were given some skeleton code to benchmark, so I will show the modifications I made to that, the script I wrote, and how I ran it.

Code Modifications


The code I'm working with is a program to perform benchmark tests on the compute performance of simple calculations using OpenCL with differing values for the global and local work sizes each run. We are supposed to run the program many times with different combinations of these values and analyze the results. The idea is that the GPU is good for running certain things really fast. Understanding OpenCL is not at all needed for this simple tutorial, but if you're curious, here's a little bit more.

Here is the top of the file for the original first.cpp given to us by the professor. In its original form, it looks for variables defined at compile-time: NUM-ELEMENTS, and LOCAL_SIZE. If those are not defined, it sets a default:



I moved the definition of these into main() at the very top. I defined them as command line arguments, with defaults if they do not exist. I shouldn't have them in all-caps, but this was a quick "I want to go to bed" solution.


The basic idea here is that we just need to accept command line arguments!

Build the Solution


Now that the modifications are done, let's compile this thing.
Build->Rebuild in Visual Studio




Now we have an executable. If you want to look at it, open the VS solution from File Explorer, go into the Debug folder, and look for solutionname.exe, in this case mine is first.exe.

This is something we can run from the command line!

The Command Line


Open your command prompt. Hit the windows key and type "cmd". If you want to be lazy, open File Explorer to your solutions folder, and copy the address at the top into your clipboard. Type cd and paste that into cmd.

 cd C:\Users\Sara\Documents\School\Spring18\475\P6\First

This will move us into the directory we need to run our program. In this case, we need a .cl file in the code that sits outside of where the exe file is, so we're not going to cd all the way into the debug folder where the executable is.  To get around that, we're going to run
Debug/first.exe.
This will run the program with the default values. To run it with custom values, run
Debug/first.exe 1024 16
1024 will be assigned to NUM_ELEMENTS, and 16 will be assigned to LOCAL_SIZE

Script

But we want to run this code hundreds of times! On top of that, I'm dyslexic as hell and won't be able to keep track of all the values I want to run the code for. This is where a script comes in! I used bash since I have that installed, but batch files, python or c-shell work just fine.

Here's how it works:
The loops loop through the values that we were asked to run the program with in the assignment. The code will be run for every combination. The command we give it inside the nested loops is to run that executable file, using the current value for t and the current value for s, which are respectively assigned to NUM_ELEMENTS and LOCAL_SIZE.

From this point I started Bash in the command line with bash.exe, which allows me to use the "./" notation. If you don't have Bash on Windows, it's easy to install, or you can use a Batch file. The idea is the same though: run the executable that Visual Studio spits out from your script as many times as you need with whatever variables you need, then use cmd to run your script.

To run the Bash script , simply go back to the command line and run ./scriptname.sh. For example,
./loop.sh
That will handle everything for you! You can get fancy and have output to text handled in your script. I have that sitting in my code itself for now.

Wrapping up

This scripting technique prevents us from having to manually change values, and really cuts down on the time needed to collect our data (and finish the assignment). I hadn't ever played with scripts before this class, and I found them really helpful so I can't wait to poke around with them some more. Big shoutout to my partner for helping me out and showing me some tricks!

Hopefully that was helpful, feel free to ask any questions!