Here are some examples of how to read command line arguments with your program:


In a C/C++ program the declaration of the main function looks like this:

int main ( int argc, char *argv[] )

The integer argc is the argument count. It is the number of arguments passed into the program from the command line, including the name of the program.  The array of character pointers argv is the listing of all the arguments. argv[0] is the name of the program.  After that, every element number less than argc is a command line argument. You use each argv element just like a string.  For example if I pass my program a single command line argument that is the GPU number, the C code to read the command line argument, convert it to an integer and then pass it to the CUDA function that set the GPU number would look like this:

 int main ( int argc, char *argv[] ) {
            int gpuNum;



In a Fortran program, call the subroutine get_command_argument with the number of the command line argument you want and it will return the argument as a string  in arg. Convert that the string to an integer and call the CUDA Fortran function that sets the GPU device.

program test_get_command_argument
    integer :: gpuNum
   character(len=32) :: arg    
   call get_command_argument(1, arg)
   read(arg,'(i2)') gpuNum
end program



To pass a command line argument to your matlab program, turn it into a function and pass the command line argument as an argument of the function. For example, to pass the GPU number to use to your matlab program, the function would look like this:

function my_matlab_function(gpuNum)
   gpuDevice(gpuNum) % tell matlab which GPU to use

In your job submission script, you can tell matlab to run the function and pass the GPU number as a function argument using this command:

matlab -r “my_matlab_function(gpuNum)”