Xmipp  v3.23.11-Nereus
mpi_write_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: J.M. de la Rosa Trevin (jmdelarosa@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  *
25  ****************************************************************************/
26 
27 /****************************************************************************
28  * This program is a simple example of using the MPI parallel library
29  * in which some needed MPI calls are encapsulated in the MpiNode class.
30  * In the constructor the MPI initialization are done and the finalize
31  * in destructor.
32  *
33  * In this example PI is calculated by sampling discrete
34  * points of a quarter of a circle with radius R and aproximating
35  * the area by the number of points inside the circle.
36  *
37  * Also the points to be process(parallel tasks) are dynamically distrubuted
38  * between the MPI nodes. Two distributors are available, one base on
39  * filesystem locking and another in which one of the MPI nodes will be
40  * distributing the work.
41  ***************************************************************************/
42 
43 #include "parallel/xmipp_mpi.h"
44 #include "core/xmipp_image.h"
46 
47 //Some useful macros
48 #define CREATE_LOG() FILE * _logML = fopen(formatString("nodo%02d.log", node->rank).c_str(), "w+")
49 #define LOG(msg) do{fprintf(_logML, "%s\t%s\n", getCurrentTimeString(), msg); fflush(_logML); }while(0)
50 #define CLOSE_LOG() fclose(_logML)
51 
52 #define IS_MASTER (node->rank == 0)
53 
56 //#define stackSize 1024
57 //#define stackSize 65536
58 //#define Xdim 51
59 //#define Ydim 77
60 //#define Xdim 256
61 //#define Ydim 128
62 
63 int main(int argc, char **argv)
64 {
65  FileName fnIN="test_delete_me.mrcs";
66  int xDim = 64;
67  int yDim = 64;
68  int nDim = 1024;
69  for (int i = 1; i < argc; i++) /* Skip argv[0] (program name). */
70  {
71  if (strcmp(argv[i], "-i") == 0) /* Process optional arguments. */
72  {
73  i++;
74  fnIN =argv[i];
75  }
76  else if (strcmp(argv[i], "--xdim") == 0) /* Process optional arguments. */
77  {
78  i++;
79  xDim = atoi(argv[i]);
80  }
81  else if (strcmp(argv[i], "--ydim") == 0) /* Process optional arguments. */
82  {
83  i++;
84  yDim = atoi(argv[i]);
85  }
86  else if (strcmp(argv[i], "--ndim") == 0) /* Process optional arguments. */
87  {
88  i++;
89  nDim = atoi(argv[i]);
90  }
91 
92  }
93 
94  //First argument should be the md filename
95  //FileName fn(argv[1]);
96  MetaDataVec md;
97  //MPI Initialization
98  node = new MpiNode(argc, argv);
99  int rank = node->rank;
100  int size = node->size;
102  //create blank file
103  if(rank==0)
104  {
105  unlink(fnIN.c_str());
106  createEmptyFile(fnIN, xDim, yDim, 1, nDim);
107  }
108  Image<double> Iaux(xDim,yDim);
109  //Be sure all sync here
111  node->barrierWait();
112 
113  // if (IS_MASTER)
114  for (int var = 1; var <= nDim; var++)
115  {
116  if(var%size==rank)
117  {
118  String ss = formatString("%03d@%s", var,fnIN.c_str());
119  std::cerr << "ssIN: value" << ss << " " << (double)rank << std::endl;
120  Iaux().initConstant((double)rank);
121  node->barrierWait();
122  Iaux.write(ss);
123  }
124  }
125  node->barrierWait();
126  //check results:
127  if(rank==0)
128  {
129  int errors=0;
130  for (int var = 1; var <= nDim; var++)
131  {
132  double value = (double) (var%size);
133  String ss = formatString("%03d@%s", var,fnIN.c_str());
134  Iaux.read(ss.c_str());
135  double min,max,std, avg=0.;
136  Iaux().computeStats(avg,std,min,max);
137  if (
138  ABS(avg-value)> 0.00001 ||
139  ABS(std-0)> 0.00001 ||
140  ABS(min-value)> 0.00001 ||
141  ABS(max-value)> 0.00001
142  ){
143  errors++;
144  std::cerr << "Error in image :" << var
145  << " avg " << avg
146  << " std " << std
147  << " min " << min
148  << " max " << max
149  << std::endl;}
150  }
151  std::cerr << "errors:" << errors << std::endl;
152  }
153 
156  delete node;
157  return 0;
158 }
size_t size
Definition: xmipp_mpi.h:52
void min(Image< double > &op1, const Image< double > &op2)
#define yDim
Definition: projection.cpp:42
#define xDim
Definition: projection.cpp:41
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
ParallelTaskDistributor * distributor
void barrierWait()
Definition: xmipp_mpi.cpp:171
#define i
MpiNode * node
void max(Image< double > &op1, const Image< double > &op2)
#define ABS(x)
Definition: xmipp_macros.h:142
void createEmptyFile(const FileName &filename, int xdim, int ydim, int Zdim, size_t select_img, bool isStack, int mode, int _swapWrite, const MDRowVec *md)
size_t rank
Definition: xmipp_mpi.h:52
std::string String
Definition: xmipp_strings.h:34
String formatString(const char *format,...)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
int main(int argc, char **argv)