Hello World: Difference between revisions
From charlesreid1
No edit summary |
(→Python) |
||
| (12 intermediate revisions by the same user not shown) | |||
| Line 4: | Line 4: | ||
HelloWorld.sh: | HelloWorld.sh: | ||
< | |||
<pre> | |||
#!bin/sh | #!bin/sh | ||
echo "Hello world!" | echo "Hello world!" | ||
</ | </pre> | ||
To run it: | To run it: | ||
< | |||
<pre> | |||
$ chmod +x HelloWorld.sh | $ chmod +x HelloWorld.sh | ||
$ ./HelloWorld.sh | $ ./HelloWorld.sh | ||
</ | </pre> | ||
=C= | |||
=C with MPI= | |||
<pre> | |||
#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 | |||
</pre> | |||
= C++ = | = C++ = | ||
| Line 21: | Line 56: | ||
HelloWorld.cc: | HelloWorld.cc: | ||
< | |||
<pre> | |||
#include <iostream> | #include <iostream> | ||
| Line 28: | 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= | |||
A simple hello world example in [[C++]] with [[MPI]]: | |||
<pre> | |||
#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; | |||
} | |||
</pre> | |||
To compile and run it: | |||
<pre> | |||
$ mpic++ hello.cpp -o hello.o | |||
$ mpirun -np 4 ./hello.o | |||
</pre> | |||
* http://www.cs.trinity.edu/~bmassing/Classes/CS3366_2002spring/SamplePrograms/index.html | |||
= PHP = | = PHP = | ||
| Line 41: | Line 120: | ||
HelloWorld.php: | HelloWorld.php: | ||
< | |||
<pre> | |||
<html> | <html> | ||
<body> | <body> | ||
| Line 49: | 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>. | ||
If you need help understanding how to do that, check out [[Apache|my page on building and running Apache]]. If you need help translating that into human-speak, [http://lifehacker.com/124212/geek-to-live--how-to-set-up-a-personal-home-web-server Lifehacker has a really great article explaining how to set up a web server on your machine]. | |||
= Python = | = Python = | ||
| Line 63: | 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> | ||
| Line 81: | Line 167: | ||
{{Languages}} | {{Languages}} | ||
[[Category:MPI]] | |||
[[Category:C++]] | [[Category:C++]] | ||
[[Category:Python]] | [[Category:Python]] | ||
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'"