1 //
2 // Copyright (C) 2013 Quantum ESPRESSO group
3 // This file is distributed under the terms of the
4 // GNU General Public License. See the file `License'
5 // in the root directory of the present distribution,
6 // or http://www.gnu.org/copyleft/gpl.txt .
7 //
8 
9 #include <mpi.h>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include "libqecouple.h"
14 
15 // ... Test program for Q-E library interface
main(int argc,char ** argv)16 int main(int argc, char **argv)
17 {
18     int retval, pw_comm, ncpu, key, me;
19     char input[81] = { ' ', '\0' };
20     MPI_Comm new_comm;
21 
22     MPI_Init(&argc, &argv);
23     MPI_Comm_size(MPI_COMM_WORLD,&ncpu);
24     MPI_Comm_rank(MPI_COMM_WORLD,&me);
25 
26     // parse command line flags.
27     int i=1;
28     int nimage=1, npots=1, npools=1, ntg=1, nband=1, ndiag=1, nres=0;
29     while (i < argc-1) {
30         if (strncmp("-i",argv[i],2) == 0) {
31             ++i;
32             strncpy(input, argv[i], 80);
33             input[80] = '\0';
34             ++i;
35             continue;
36         }
37         if (strncmp("-ni",argv[i],3) == 0) {
38             ++i;
39             nimage=std::atoi(argv[i]);
40             ++i;
41             continue;
42         }
43         if (strncmp("-npot",argv[i],5) == 0) {
44             ++i;
45             npots=std::atoi(argv[i]);
46             ++i;
47             continue;
48         }
49         if ((strncmp("-nk",argv[i],3) == 0)
50             || (strncmp("-npoo",argv[i],5) == 0)) {
51             ++i;
52             ndiag=std::atoi(argv[i]);
53             ++i;
54             continue;
55         }
56         if (strncmp("-nt",argv[i],3) == 0) {
57             ++i;
58             ntg=std::atoi(argv[i]);
59             ++i;
60             continue;
61         }
62         if (strncmp("-nb",argv[i],3) == 0) {
63             ++i;
64             nband=std::atoi(argv[i]);
65             ++i;
66             continue;
67         }
68         if ((strncmp("-nd",argv[i],3) == 0)
69             || (strncmp("-no",argv[i],3) == 0)
70             || (strcmp("-nproc_diag",argv[i]) == 0)
71             || (strcmp("-nproc_ortho",argv[i]) == 0)) {
72             ++i;
73             ndiag=std::atoi(argv[i]);
74             ++i;
75             continue;
76         }
77         if (strncmp("-nr",argv[i],3) == 0) {
78             ++i;
79             nres=std::atoi(argv[i]);
80             ++i;
81             continue;
82         }
83         std::cerr << "usage: " << argv[0] << " -flag1 <value1> -flag2 <value2>\n"
84                   << std::endl;
85         return -1;
86     }
87 
88     if (i != argc) {
89         std::cerr << "usage: " << argv[0] << " -flag1 <value1> -flag2 <value2>\n"
90                   << std::endl;
91         return -1;
92     }
93 
94     // Create new C-style communicator and convert to Fortran
95     key = MPI_UNDEFINED;
96     if (me < (ncpu - nres))
97         key = 1;
98 
99     MPI_Comm_split(MPI_COMM_WORLD, key, me, &new_comm);
100 
101     if (new_comm != MPI_COMM_NULL) {
102         pw_comm = MPI_Comm_c2f(new_comm);
103         // call Q-E
104         c2libcpv(pw_comm,nimage,npots,npools,ntg,nband,ndiag,&retval,input);
105 
106         std::cout << " rank " << me << " return value is: " << retval << std::endl;
107     } else {
108         std::cout << " rank " << me << " of " << ncpu -1 << " is reserved" << std::endl;
109         retval = 0;
110     }
111 
112 
113     MPI_Finalize();
114     return retval;
115 }
116