Compiling Programs

C Programs 

A2C2 has a number of different C compiliers made by Intel and GCC. There are advantages and drawbacks to each, but, in general, we recommend using the Intel compilers because they are much better optimized for our systems.

To compile a C-program using the Intel compilers:

[saguaro1:~]$ module load intel
[saguaro1:~]$ icc source.c -o output_filename

Where ‘source.c’ contains your source code. For example:

#include <stdio.h>
int main(int argc, char **argv) {
  printf(“Hello, world!”);
  return(0);
}

Fortran Programs

A2C2 has a number of different Fortran compilers made by Intel and Gnu. There are advantages and drawbacks to each, but, in general, we recommend using the Intel compilers because they are much better optimized for our systems.

To compile a Fortran-program using the Intel compilers:

[saguaro1:~]$ module load intel
[saguaro1:~]$ ifort source.f90 -o output_filename

Where ‘source.c’ contains your source code. For example:

protram MyProgram
implicit none

print *, “Hello World!”

end program MyProgram

You can then run your program with:

[saguaro1:~]$ ./output_filename
Hello World!

 

MPI Programs

MPI (Message Passing Interface) is the de facto standard for writing high speed parallel programs. A simple MPI program in C++ might look something like this:

#include <iostream>
#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);
  int *buffer = new int[1];
  if (rank == 0) {
    buffer[0] = 42;
  }
  std::cout << “hello from “ << rank << “ of “ << size << “ my buffer is: “ << buffer[0] << “ before bcast” << std::endl;
  MPI_Bcast(buffer, 1, MPI_INTEGER, 0, MPI_COMM_WORLD);
  std::cout << “hello from “ << rank << “ of “ << size << “ my buffer is: “ << buffer[0] << “ after bcast” << std::endl;
  delete [] buffer;
  MPI_Finalize();
}
The source code looks like a single program, but when it runs, an MPI program running in parallel is actually an ensemble of programs. Each program in the ensemble has its own rank. It must be initialized with an MPI_Init and finalized with MPI_Finalize. A2C2 has a number of MPI enabled compilers. As a general rule, we recommend using the latest Intel compilers with OpenMPI.
[saguaro1:~]$ module load openmpi/1.4.3-intel
[saguaro1:~]$ mpicc hello_MPI.C -o hello_MPI