[CMake] how to write the following comand into cmake file.

罗勇刚(Yonggang Luo) luoyonggang at gmail.com
Mon Jun 24 21:44:17 EDT 2013


OK, gotcha,

After trying out a recent build of Kdevelop4, I decided to give CMake
a go. I currently have a project running utilizing MPI
(parallellization libraries). This is nice and not that hard to use,
but the linking of the project gets arbitrarily complicated. What
helped me before when using Kdevelop3 and automake was this solution.
This year around the solution is so simple, I figured I’d share with
the world how to write a Hello World program utilizing OpenMPI and
CMake. I will not cover the installation of those tools, that you have
to find out yourself since it depends on what OS you use.

I have to say that Kdevelop4 looks stunning. It has a lot of tools,
automatically checks headers included in the project for functions
etc, has semantic code control, wonderful integration with CMake, and
generally looks like pure heaven for a programmer. At the moment it
does crash every now and then, but then again it is not a finished
product. I’m sure I could write ten pages about how good it is, but
that is for another post when I have tested it a bit more thoroughly..

Now, first the C++ code, which initializes the MPI, and then prints
Hello World from each parallell process + the time it has used to get
there since it was initialized. I have named the file "main.cpp"

#include <iostream>
#include <mpi.h>

static int numprocs;

int main(int argc, char **argv) {
int my_rank;
// MPI initializations
MPI_Status status;
MPI_Init (&argc, &argv);
MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank (MPI_COMM_WORLD, &my_rank);
double time_start = MPI_Wtime();
std::cout << "Hello World, my rank is " << my_rank <<" "<< MPI_Wtime()
- time_start << std::endl;
// End MPI
MPI_Finalize ();
return 0;
}

Now if you find this complicated then have a look at some tutorial for
MPI. You can basically consider the four lines of MPI initializations
a black hole, then you have a timer which starts, and the Hello World
message. At the end of the code it is important to finalize MPI,
otherwise you get an error message. Anyway, this is not the important
part, because it is CMake that makes my life THAT much easier. Have a
look at the CMake file:

PROJECT(test)
ADD_EXECUTABLE(test main.cpp)
SET(CMAKE_C_COMPILER mpicc)
SET(CMAKE_CXX_COMPILER mpicxx)
target_link_libraries(test mpi)

That’s it! The first line defines the name of the project. Next, we
define the name of the executable to be "test", and the files that
executable depends on, "main.cpp". After that we define the C
compilers because MPI code should be compiled with these special
compilers which are basically the gcc with something extra (I consider
them black holes). Finally, we need to link the project called "test"
to the library called "MPI", which is done in the last line. Simple as
that!

Afterwards you create another folder, e.g. "build", inside your
project. Then you generate the makefile with "cmake ../" (where ../
denotes the location of your project), and compile/link with "make".
The code can be ran with the command "mpirun -np 2 ./test", where 2 is
the number of processes. Et voila!

Update 24. of May: So I have noticed that quite a few people have
clicked on this post. I figured I wanted to mention (after learning
quite a bit more CMake), that in principle I did not use much of the
features of CMake in this example. Hence I wanted to add an
alternative CMake script that better exemplifies the power of CMake.
Consider the following code for CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

project(mytest)

add_executable(mytest main.cpp)

# Require MPI for this project:
find_package(MPI REQUIRED)
set(CMAKE_CXX_COMPILE_FLAGS ${CMAKE_CXX_COMPILE_FLAGS} ${MPI_COMPILE_FLAGS})
set(CMAKE_CXX_LINK_FLAGS ${CMAKE_CXX_LINK_FLAGS} ${MPI_LINK_FLAGS})
include_directories(MPI_INCLUDE_PATH)
target_link_libraries(mytest ${MPI_LIBRARIES})

# Add a test:
enable_testing()
add_test(SimpleTest ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} 4
${CMAKE_CURRENT_BINARY_DIR}/mytest)

Instead of relying on using the compiler wrappers (which depends on
you knowing the precise name of these binaries), we here use the
functionality find_package() in CMake. This calls an external function
which will set up the environment variables you need to link to MPI.
We add the word “REQUIRED” so that CMake knows that MPI is … required.
The script might look more complex, but it is much more powerful. The
way we set it up here we actually do it in a way that should work
independent of which MPI installation one has. I also added a test
demo at the bottom that runs the binary with four parallell processes.
The test can be ran after “make” using the command “ctest”.


From:http://eothred.wordpress.com/2010/02/25/hello-world-cmakeopenmpi/

2013/6/24 丁老师 <ztdepyahoo at 163.com>:
> I develop a parallel code with PETSc, it can sucessfully compiled with the
> following mpic++ command, but how to translate it into a cmake file. so i
> can further develop it in kdevelop 4.4.
>
> mpic++ main.cpp -o main -I/home/ztdep/Downloads/petsc-3.3-p6/inc…
> -I/home/ztdep/Downloads/petsc-3.3-p6/arc…
> -Wl,-rpath,/home/ztdep/Downloads/petsc-3…
> -L/home/ztdep/Downloads/petsc-3.3-p6/arc… -lpetsc -lX11
> -Wl,-rpath,/home/ztdep/Downloads/petsc-3… -lHYPRE -lcmumps -ldmumps -lsmumps
> -lzmumps -lmumps_common -lpord -lsuperlu_dist_3.1 -lparmetis -lmetis
> -lscalapack -lblacs -lpthread -llcg -lflapack -lfblas -lm
> -L/usr/lib64/gcc/x86_64-suse-linux/4.7 -L/usr/x86_64-suse-linux/lib
> -lmpichf90 -lgfortran -lm -lgfortran -lm -lquadmath -lm -lmpichcxx -lstdc++
> -ldl -lmpich -lopa -lmpl -lrt -lpthread -lgcc_s -ldl
>
>
>
>
>
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake



-- 
         此致
礼
罗勇刚
Yours
    sincerely,
Yonggang Luo


More information about the CMake mailing list