From Documentation
Jump to: navigation, search

Objective

The purpose of this guide is to give a quick step-by-step introduction to compiling and running code on SHARCNET clusters. Specifically, we will go through the steps necessary to compile and run the example program below (in either C or Fortran) which instructs each process to output a message and then terminate. An introduction to MPI programming is covered in a separate tutorial, Getting Started with MPI.

C CODE: hello.c FORTRAN CODE: hello.f
 #include <stdio.h>
 #include <mpi.h>
 
 int main(int argc, char *argv[]) {
     int rank, size;
 
     MPI_Init(&argc, &argv);
     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
     MPI_Comm_size(MPI_COMM_WORLD, &size);
 
     printf("Hello from process %d of %d\n", rank, size);
 
     MPI_Finalize();
 
     return(0);
 }
 program hello
 include "mpif.h"
 integer rank, size, ierror
 
 call MPI_INIT(ierror)
 call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierror)
 call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierror)
 
 print *, 'Hello from process ', rank, ' of ', size
 
 call MPI_FINALIZE(ierror)
 
 end

Logging on to a SHARCNET cluster

To login to any system, a Secure Shell (SSH) connection must be used. (For a complete list of all SHARCNET systems, please refer to the list of system facilities on the web portal.) A command-line SSH client is typically available on Linux and Mac-based machines by default, however if you are logging in from a Windows machine, you may need to install a client (see our SSH page). Under UNIX (including Linux/Mac), the command to log in to the cluster orca.sharcnet.ca as the user username is

ssh -Y username@orca.sharcnet.ca

where the -Y delimeter is used to establish an X window connection so that graphics applications such as the debugger or paraview may be used.

Compiling-step1.gif

Transferring the source files to the cluster

Before compiling the above program you must first transfer it to the cluster. To transfer files to and from a SHARCNET cluster on a UNIX machine, you must use secure FTP (sftp) or secure copy (scp). On Windows or Mac, these commands must first be installed (a commonly used implementation is PuTTY, for example).

Firstly, create the file hello.c on your local machine by pasting source code above into the text editor of your choice and saving it. You will now upload it from your local machine myhost to the orca cluster (orca.sharcnet.ca) by issuing the following command:

myhost$ scp hello.c sn_username@orca.sharcnet.ca:

Do not forget to include the colon (:) at the end of that command!

Alternatively, it is often most convenient to transfer files to SHARCNET using a graphical FTP client that supports sftp. For a more detailed discussion of transferring files on SHARCNET systems, please consult the SHARCNET FAQ.

Compiling the source file

To make it easier to compile programs across all SHARCNET clusters, a generic set of compiler commands has been provided which automatically optimize compiling to the apropriate configuration:

cc, c++, f77, f90, f95

and for MPI,

mpicc, mpic++, mpiCC, mpif77, mpif90

Since you have already logged in and transferred the files hello.c and/or hello.f to the orca cluster, you will now use the mpicc and/or mpif90 compilers to create the executable files helloc.x and hellof.x for this cluster:

mpicc hello.c -o helloc.x
mpif90 -free hello.f -o hellof.x
Compiling-step3.gif

For more detail about compiling on SHARCNET systems please consult the appropriate section of the FAQ.

Running the program

Now that we have compiled our program we may submit it to a batch queue for execution. This is done using the sqsub command, which is additionally used to specify the number of processors to use and where output from the program should be directed. There are three main queues on SHARCNET:

  • serial - for serial jobs
  • mpi - for parallel jobs using MPI library
  • threaded - for threaded jobs using OpenMP or POSIX threads

Since our program uses MPI, we will run our program by submitting it to the mpi queue using, in this example, 4 processors and directing the output to the file helloc.log and/or hellof.log. We issue the following commands:

sqsub -q mpi -r 10m -n 4 -o helloc.log ./helloc.x
sqsub -q mpi -r 10m -n 4 -o hellof.log ./hellof.x

The status of the jobs you submitted can be checked with the sqjobs command. When the job is completed you should verify that the output is as expected.

Congratulations! You have now compiled and run your first program on SHARCNET.

Additional information on the batch queues and execution of programs is available in the FAQ.