ALL 0.9.3
A Loadbalacing Library
Loading...
Searching...
No Matches
ascii2mpibin.cpp
Go to the documentation of this file.
1#include <stdlib.h>
2#include <iostream>
3#include <fstream>
4#include <sstream>
5#include <string>
6#include <vector>
7#include <numeric>
8#include <mpi.h>
9
10int main (int argc, char** argv)
11{
12 MPI_Init(&argc, &argv);
13 int dim[3];
14
15 if (argc < 6)
16 {
17 std::cout << "usage: " << argv[0] << " <in_file (ASCII)> <out_file (binary)> <n_x> <n_y> <n_z>" << std::endl;
18 MPI_Finalize();
19 exit(-1);
20 }
21
22 // read dimension of process grid from command line
23 dim[0] = atoi(argv[3]);
24 dim[1] = atoi(argv[4]);
25 dim[2] = atoi(argv[5]);
26
27 int err;
28
29 MPI_File outfile;
30
31 std::ifstream infile;
32 infile.open(argv[1]);
33
34 err = MPI_File_open(MPI_COMM_WORLD, argv[2], MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &outfile);
35
36 char line[256];
37
38 long blockID;
39 double values[4];
40 int counter = 0;
41
42 double max[3];
43
44 // get maximum system size
45 while(infile.getline(line,256))
46 {
47 if(infile.eof()) break;
48 std::string str(line);
49 std::istringstream istr(str);
50 //std::cout << str << std::endl;
51 istr >> blockID >> values[0] >> values[1] >> values[2] >> values[3];
52 for (int i = 0; i < 3; ++i)
53 if (max[i] < values[i]) max[i] = values[i];
54 }
55
56 // go back to start of file
57 infile.clear();
58 infile.seekg(0,infile.beg);
59
60 double cell_size[3];
61
62 for (int i = 0; i < 3; ++i)
63 {
64 // adjust the system size to the next multiple of 10
65 max[i] = (double)((int)max[i] - ((int)max[i] % 10) + 10);
66 cell_size[i] = max[i] / (double)dim[i];
67 }
68
69 std::cout << "domain layout: " << dim[0] << " " << dim[1] << " " << dim[2] << std::endl;
70 std::cout << "system size: " << max[0] << " " << max[1] << " " << max[2] << std::endl;
71 std::cout << "domain size: " << cell_size[0] << " " << cell_size[1] << " " << cell_size[2] << std::endl;
72
73 int offset = 0;
74 const int n_procs = dim[0] * dim[1] * dim[2];
75
76 std::vector<long> loc_parts(dim[0]*dim[1]*dim[2]);
77 std::vector<long> offset_domain(dim[0]*dim[1]*dim[2]);
78 std::vector<int> curr_index(dim[0]*dim[1]*dim[2]);
79
80 for (int i = 0; i < dim[0] * dim[1] * dim[2]; ++i)
81 {
82 loc_parts.at(i) = 0;
83 offset_domain.at(i) = 0;
84 curr_index.at(i) = 0;
85 }
86
87 // count particles per domain
88 while(infile.getline(line,256))
89 {
90 if(infile.eof()) break;
91 std::string str(line);
92 std::istringstream istr(str);
93 istr >> blockID >> values[0] >> values[1] >> values[2] >> values[3];
94
95 int cell[3];
96
97 for (int i = 0; i < 3; ++i)
98 cell[i] = (int)(values[i] / cell_size[i]);
99
100 loc_parts.at(cell[0]*dim[1]*dim[2]+cell[1]*dim[2]+cell[2])++;
101 }
102
103 std::partial_sum(loc_parts.begin(),loc_parts.end(),offset_domain.begin());
104
105 // subtract the particles on own domain (partial sum includes local contribution)
106 for (int i = 0; i < dim[0] * dim[1] * dim[2]; ++i)
107 {
108 offset_domain.at(i) -= loc_parts.at(i);
109 int n_loc = (int)loc_parts.at(i);
110 MPI_File_write_at_all(outfile,(MPI_Offset)(i*sizeof(long)),&offset_domain.at(i),1,MPI_LONG,MPI_STATUS_IGNORE);
111 MPI_File_write_at_all(outfile,(MPI_Offset)(n_procs *sizeof(long) + i * sizeof(int)),&n_loc,1,MPI_INT,MPI_STATUS_IGNORE);
112 }
113
114 // go back to start of file
115 infile.clear();
116 infile.seekg(0,infile.beg);
117
118 // copy point data to MPI I/O file
119 while(infile.getline(line,256))
120 {
121 if(infile.eof()) break;
122 std::string str(line);
123 std::istringstream istr(str);
124 istr >> blockID >> values[0] >> values[1] >> values[2] >> values[3];
125
126 int cell[3];
127
128 for (int i = 0; i < 3; ++i)
129 cell[i] = (int)(values[i] / cell_size[i]);
130
131 int domain = cell[0] * dim[1] * dim[2] + cell[1] * dim[2] + cell[2];
132
133 long block_size = sizeof(long) + 4*sizeof(double);
134
135 MPI_File_write_at_all(
136 outfile,
137 (MPI_Offset)
138 (n_procs*(sizeof(int) + sizeof(long))+(offset_domain.at(domain)+curr_index.at(domain))*block_size),
139 &blockID,
140 1,
141 MPI_LONG,
142 MPI_STATUS_IGNORE
143 );
144
145 MPI_File_write_at_all(
146 outfile,
147 (MPI_Offset)
148 (n_procs*(sizeof(int) + sizeof(long))+(offset_domain.at(domain)+curr_index.at(domain))*block_size+sizeof(long)),
149 values,
150 4,
151 MPI_DOUBLE,
152 MPI_STATUS_IGNORE
153 );
154
155 curr_index.at(domain)++;
156 }
157 /*
158 for (int ix = 0; ix < dim[0]; ++ix)
159 for (int iy = 0; iy < dim[1]; ++iy)
160 for (int iz = 0; iz < dim[2]; ++iz)
161 {
162 int index = iz + iy * dim[2] + ix * dim[1] * dim[2];
163 int loc_count = 0;
164 while(infile.getline(line,256))
165 {
166 if(infile.eof()) break;
167 std::string str(line);
168 std::istringstream istr(str);
169 istr >> values[0] >> values[1] >> values[2] >> values[3] >> values[4];
170
171 // if the point is within the current domain
172 if (
173 ((double)ix * cell_size[0]) <= values[1] && ((double)(ix + 1) * cell_size[0]) > values[1] &&
174 ((double)iy * cell_size[1]) <= values[2] && ((double)(iy + 1) * cell_size[1]) > values[2] &&
175 ((double)iz * cell_size[2]) <= values[3] && ((double)(iz + 1) * cell_size[2]) > values[3]
176 )
177 {
178 MPI_Offset write_location= (MPI_Offset)(5 * (offset + loc_count) * sizeof(double) + 2 * n_procs * sizeof(int));
179 MPI_File_write_at_all(outfile,write_location,values,5,MPI_DOUBLE,MPI_STATUS_IGNORE);
180 loc_count++;
181 }
182 }
183 std::cout << "index " << index << ": " << offset << " | " << loc_count << std::endl;
184 MPI_File_write_at_all(outfile,(MPI_Offset)(index*sizeof(int)),&offset,1,MPI_INT,MPI_STATUS_IGNORE);
185 MPI_File_write_at_all(outfile,(MPI_Offset)((n_procs + index)*sizeof(int)),&loc_count,1,MPI_INT,MPI_STATUS_IGNORE);
186 offset += loc_count;
187 infile.clear();
188 infile.seekg(0,infile.beg);
189 }
190 */
191 infile.close();
192 MPI_File_close(&outfile);
193
194 MPI_Finalize();
195}
int main(int argc, char **argv)