Hello World
From charlesreid1
Contents
Bash
A simple hello world example using Bash.
HelloWorld.sh:
#!bin/sh echo "Hello world!"
To run it:
$ chmod +x HelloWorld.sh $ ./HelloWorld.sh
C
C with MPI
#include <stdio.h>
#include <mpi.h>
int main (argc, argv)
int argc;
char *argv[];
{
int rank, size;
MPI_Init (&argc, &argv); /* starts MPI */
MPI_Comm_rank (MPI_COMM_WORLD, &rank); /* get current process id */
MPI_Comm_size (MPI_COMM_WORLD, &size); /* get number of processes */
printf( "Hello world from process %d of %d\n", rank, size );
MPI_Finalize();
return 0;
}
</source>
And to compile:
<source lang="bash">
$ mpicc hello.c -o hello.o
$ mpirun -np 4 ./hello.o # or whatever your MPI's command line cup of tea is
C++
A simple hello world example in C++.
HelloWorld.cc:
#include <iostream>
int main()
{
std::cout << "Hello, world! This is C++.\n";
}
To run it:
$ g++ HelloWorld.cc -o hello.x $ ./hello.x
C++ with MPI
A simple hello world example in C++ with MPI:
#include <iostream>
#include <cstdlib> // has exit(), etc.
#include <mpi++.h> // MPI header file
int main(int argc, char *argv[]) {
// initialize for MPI (should come before any other calls to
// MPI routines)
MPI::Init(argc, argv);
// get number of processes
int nprocs = MPI::COMM_WORLD.Get_size();
// get this process's number (ranges from 0 to nprocs - 1)
int myid = MPI::COMM_WORLD.Get_rank();
// print a greeting
cout << "hello from process " << myid << " of " << nprocs << endl;
// clean up for MPI
MPI::Finalize();
return EXIT_SUCCESS;
}
To compile and run it:
$ mpic++ hello.cpp -o hello.o $ mpirun -np 4 ./hello.o
PHP
A simple hello world example using PHP.
HelloWorld.php:
<html> <body> <?php echo "Hello world!"; ?> </body> </html>
To run HelloWorld.php:
$ php HelloWorld.php
Alternatively, if you are running a local apache server and have PHP enabled, you can put HelloWorld.php into your web documents directory, and then point you web browser to http://localhost/HelloWorld.php.
If you need help understanding how to do that, check out my page on building and running Apache. If you need help translating that into human-speak, Lifehacker has a really great article explaining how to set up a web server on your machine.
Python
A simple hello world example using Python.
HelloWorld.py:
print "Hello, World!"
To run it:
$ python HelloWorld.py
Alternatively:
$ python -c "print 'Hello World! \n'"