16/5/14

MPICH2 on Code::Blocks

Here is the steps to make Code::Blocks (http://www.codeblocks.org/downloads) recognize and compile when usingMPICH2 (http://www.mcs.anl.gov/research/projects/mpich2/).
I am using Windows 7 and I am assuming that before following these steps that you had already installedCode::Blocks and MPICH2.
Note that when you use Code::Blocks 32-bit you have to use also MPICH2 32-bit, even when your Windows is 64-bit.
So here now we go with the steps:

  1. Add the MPICH2 bin path to you system variable "PATH":
    • Right click "Computer" -> "Properties" -> "Advanced system settings" -> "Environment Variables..." -> click on "Path" and then Edit...
    • Type your MPICH2 bin path at the beginning of the text, in my case "C:\Program Files\MPICH2\bin"
  2. Open Code::Blocks, then click "Settings" -> "Compiler and debugger..."
  3. Click on "Linker settings" tab -> "Add" -> and then from the pop-up go to the MPICH2 installation directory and choose "lib\mpi.lib" -> then click "OK"
  4. Click on "Search directories" tab -> "Add" -> and then from the pop-up go to the MPICH2 installation directory and choose "include" -> then click "OK"

    Now you can use Code::Blocks to write code using MPICH2 implementation, here is an example:

    1. Open Code::Blocks and create new project:
      •  "File" -> "New" -> "Project..."
      • choose "Console application" -> "Go"
      • Follow the wizard to create a new project, you need to choose either "C" or "C++", then where to create your project... etc.
    2. Open the "main.cpp" file and write the following code:
      #include <iostream>
      #include "mpi.h"
      #include <string>
      
      using namespace std;
      
      int main(int argc, char *argv[])
      {
      
          int  my_rank;       /* rank of process */
          int  noProcesses;   /* number of processes */
          int  nameSize;      /* length of name */
      
          char computerName[MPI_MAX_PROCESSOR_NAME];
      
          MPI_Init(&argc, &argv); /*START MPI */
      
          /*Determines the size of the group associated with a communicator */
          MPI_Comm_size(MPI_COMM_WORLD, &noProcesses);
      
          /*Determines the rank of the calling process in the communicator*/
          MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
      
          /*Gets the name of the processor*/
          MPI_Get_processor_name(computerName, &nameSize);
      
          printf("Hello from process %d of %d processor on %s\n", my_rank, noProcesses, computerName);
      
          MPI_Finalize(); /* EXIT MPI */
      
          return 0;
      }
      
    3. Build the code (Ctrl + F9), and then go the the directory where you saved your project, and then to "bin\Debug"
    4. To run the program open a Command Prompt, then type "mpiexec -n 2 file.exe" where:
      • 2 is the number of processors to run the code on.
      • "file.exe" the executable file that was generated in the "Debug" directory.
    5. The output will be something like:
      Hello from process 0 of 2 processor on mzein-l
      Hello from process 1 of 2 processor on mzein-l

Bài đăng phổ biến