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.
Code Modifications
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:
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!