Hello World: Difference between revisions
From charlesreid1
(→Bash) |
(→Python) |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 22: | Line 22: | ||
=C with MPI= | =C with MPI= | ||
< | <pre> | ||
#include <stdio.h> | #include <stdio.h> | ||
#include <mpi.h> | #include <mpi.h> | ||
| Line 49: | Line 49: | ||
$ mpirun -np 4 ./hello.o # or whatever your MPI's command line cup of tea is | $ mpirun -np 4 ./hello.o # or whatever your MPI's command line cup of tea is | ||
</ | </pre> | ||
= C++ = | = C++ = | ||
| Line 56: | Line 56: | ||
HelloWorld.cc: | HelloWorld.cc: | ||
< | |||
<pre> | |||
#include <iostream> | #include <iostream> | ||
| Line 63: | Line 64: | ||
std::cout << "Hello, world! This is C++.\n"; | std::cout << "Hello, world! This is C++.\n"; | ||
} | } | ||
</ | </pre> | ||
To run it: | To run it: | ||
< | |||
<pre> | |||
$ g++ HelloWorld.cc -o hello.x | $ g++ HelloWorld.cc -o hello.x | ||
$ ./hello.x | $ ./hello.x | ||
</ | </pre> | ||
=C++ with MPI= | =C++ with MPI= | ||
| Line 75: | Line 77: | ||
A simple hello world example in [[C++]] with [[MPI]]: | A simple hello world example in [[C++]] with [[MPI]]: | ||
< | <pre> | ||
#include <iostream> | #include <iostream> | ||
#include <cstdlib> // has exit(), etc. | #include <cstdlib> // has exit(), etc. | ||
| Line 101: | Line 103: | ||
} | } | ||
</ | </pre> | ||
To compile and run it: | To compile and run it: | ||
< | <pre> | ||
$ mpic++ hello.cpp -o hello.o | $ mpic++ hello.cpp -o hello.o | ||
$ mpirun -np 4 ./hello.o | $ mpirun -np 4 ./hello.o | ||
</ | </pre> | ||
* http://www.cs.trinity.edu/~bmassing/Classes/CS3366_2002spring/SamplePrograms/index.html | * http://www.cs.trinity.edu/~bmassing/Classes/CS3366_2002spring/SamplePrograms/index.html | ||
| Line 118: | Line 120: | ||
HelloWorld.php: | HelloWorld.php: | ||
< | |||
<pre> | |||
<html> | <html> | ||
<body> | <body> | ||
| Line 126: | Line 129: | ||
</body> | </body> | ||
</html> | </html> | ||
</ | </pre> | ||
To run HelloWorld.php: | To run HelloWorld.php: | ||
< | |||
<pre> | |||
$ php HelloWorld.php | $ php HelloWorld.php | ||
</ | </pre> | ||
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 <code>http://localhost/HelloWorld.php</code>. | 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 <code>http://localhost/HelloWorld.php</code>. | ||
| Line 142: | Line 146: | ||
HelloWorld.py: | HelloWorld.py: | ||
< | |||
<pre> | |||
print "Hello, World!" | print "Hello, World!" | ||
</ | </pre> | ||
To run it: | To run it: | ||
< | |||
<pre> | |||
$ python HelloWorld.py | $ python HelloWorld.py | ||
</ | </pre> | ||
Alternatively: | Alternatively: | ||
< | |||
<pre> | |||
$ python -c "print 'Hello World! \n'" | $ python -c "print 'Hello World! \n'" | ||
</ | </pre> | ||
Latest revision as of 01:00, 10 October 2017
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'"