This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
Running Parallel Programming Data-Acquisition Scripts from a Windows Powershell
powershell.pptx
Copyright By PowCoder代写 加微信 powcoder
Computer Graphics
mjb – March 22, 2021
Change the NUMT and NUMTRIES to Global int Variables
Right now, our code is using defined constants, like this:
#ifndef NUMT
#define NUMT 2 #endif
#ifndef NUMS
#define NUMS 32 #endif
Instead, change it to use global variables, like this:
int NUMT = 2; int NUMS = 32;
Computer Graphics
mjb – March 22, 2021
argc and argv
When you write in C or C++, your main program, which is really a special function call, looks like this:
int main( int argc, char *argv[ ] ) {
These arguments describe what was entered on the command line used to run the program.
The argc is the number of arguments (the arg count)
The argv is a list of argc character strings that were typed (the arg vector).
The name of the program counts as the 0th argv (i.e., argv[0])
So, for example, when you type
in a shell, the ls program sees argc and argv filled like this: argc = 2
argv[0] = “ls” argv[1] = “-l”
Computer Graphics
mjb – March 22, 2021
argc and argv
So, if NUMT and NUMTRIALS are global int variables:
int NUMT = 2; int NUMS = 32;
and you want to set them from the command line, like this:
./prog 1 64
Then, inside your main program, you would say this: if( argc >= 2 )
NUMT = atoi( argv[1] );
if( argc >= 3 )
NUMS = atoi( argv[2] );
The if-statements guarantee that nothing bad happens if you forget to type values on the command line.
The atoi function converts a string into an integer (“ascii-to-integer”). If you ever need it, there is also an atof function for floating-point.
Computer Graphics
mjb – March 22, 2021
5 Also, remember, since NUMTRIALS is a variable, it needs to be declared as shared
shared( ) in the #pragma omp Line in the #pragma omp line:
#pragma omp parallel for default(none) shared(NUMS,xcs,ycs,rs,tn) reduction(+:numHits)
NUMT does not need to be declared in this way because it is not used in the for-loop that has the #pragma omp in front of it.
Computer Graphics
mjb – March 22, 2021
Windows Powershell
Windows comes with a shell program called Powershell. It might not be as familiar to most of us as some of the Linux shells are (csh, bash), but it can still be used to run multiple combinations of your program parameters in one shot.
There are a number of ways to get Powershell running. Either:
• Click on the Microsoft icon. Then scroll down to Windows
Powershell and run Windows Powershell.
• Shift right-click in the directory you want to work in and select Open Powershell
• Hold down the Windows key and hit the ‘x’ key, then select Windows Powershell.
The resulting window should look like this:
Computer Graphics
mjb – March 22, 2021
Windows Powershell
cd (change directory) to your home directory. Then cd to the folder with your project
Then cd to the folder with your executable (*.exe)
The prompt will always tell you where you are in the file system.
Computer Graphics
mjb – March 22, 2021
Windows Powershell
So, if you have cd’ed to where your executable (.exe) file lives, you can run it from the command line like this:
Computer Graphics
mjb – March 22, 2021
Windows Powershell
But, here’s the cool part. Type:
foreach($tin1,2,4 ) {
foreach ( $n in 1024, 2048, 4096) {
followed by Enter:
./MonteCarlo.exe $t $n
Computer Graphics
mjb – March 22, 2021
10 You can also use a text editor like notepad or notepad++ and put these lines into a file
Windows Powershell
called, say, loop.ps1 (ps1 is the Powershell file extension). Then, you can run this script from Powershell just by typing it:
Instead of printing these lines to the screen, you probably want to print them to a text file that can then by imported by Excel.
I had to type this to give myself permission to run scripts. This means don’t run any .ps1 files that you didn’t create yourself!
Computer Graphics
mjb – March 22, 2021
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com