Line data Source code
1 : #include "../inc/header.h"
2 : #include "../inc/param_manager.h"
3 : #include "../inc/save_manager.h"
4 :
5 : #include "../inc/global_param.h"
6 :
7 : #include <stdlib.h>
8 : #include <algorithm>
9 :
10 :
11 1 : void loadBinary(std::vector<uint32_t> & ids_data, std::vector<uint32_t> & ids_index, std::vector<uint32_t> & type_data, std::vector<types_datas_t> & ReadCacheCPU,
12 : int nb_data_in_file, std::string rootDirectory_aux, int verbose)
13 : {
14 2 : for (int i = 0; i < GlobalParam::Instance().nbtype_; ++i) // should be ReadCacheCPU.size()
15 1 : ReadCacheCPU[i].cpt = 0;
16 1 : puts("loadBinary");
17 1 : int nb_data_treated = 0;
18 1 : int id_to_treat = 0;
19 1 : int prec_id_data_file = 0;
20 1 : int32_t tmpindex;
21 1 : tmpindex = FindDescIndex(ReadCacheCPU, type_data[0]);
22 1 : data tmp;
23 1 : char data_id_index_name[1000];
24 1 : if (verbose)
25 0 : printf(" Begining loop !\n");
26 2 : while (nb_data_treated < nb_data_in_file)
27 : {
28 1 : int current_id_data_file = ids_index[nb_data_treated];
29 : // sanity check
30 1 : if (prec_id_data_file > current_id_data_file)
31 : {
32 0 : printf("ERROR : index file should be in increasing order ! \n");
33 0 : return;
34 : }
35 1 : sprintf(data_id_index_name, "%s%d.dat", rootDirectory_aux.c_str(), current_id_data_file);
36 : if (verbose)
37 0 : printf("data_id_index = %s\n", data_id_index_name);
38 1 : FILE * fileopen = fopen(data_id_index_name, "rb");
39 1 : if (fileopen == NULL)
40 : {
41 0 : printf("Problem in loadBinary with filename %s !\n", data_id_index_name);
42 0 : return;
43 : }
44 1 : if (verbose)
45 0 : puts("About to find index\n");
46 1 : tmpindex = FindDescIndex(ReadCacheCPU, type_data[nb_data_treated]);
47 1 : if (tmpindex == -1)
48 : {
49 0 : printf("Unexpected data type !");
50 0 : return;
51 : }
52 1 : if (verbose)
53 0 : printf(" While we are taking care of the same index tmpindex : %d \n", tmpindex);
54 11001 : while (nb_data_treated < nb_data_in_file && current_id_data_file == ids_index[nb_data_treated])
55 : {
56 : int to_remove = 0;
57 : int precData = 0;
58 : int nb_same_data = 0;
59 : int arg_max = 0;
60 : // reading one data
61 : int j = 0;
62 55000 : while (j < ReadCacheCPU[tmpindex].size)
63 : {
64 44000 : if(fread(&tmp, sizeof(tmp), 1, fileopen))
65 : ;
66 44000 : size_t idx = (size_t) ReadCacheCPU[tmpindex].cpt * (size_t) ReadCacheCPU[tmpindex].size + (size_t) j;
67 44000 : ReadCacheCPU[tmpindex].datas_vec[idx] = tmp;
68 44000 : j++;
69 : // test de corruption des donnees
70 44000 : if (precData == tmp && tmp != 0 && tmp != 255)
71 161 : nb_same_data++;
72 : else
73 : nb_same_data = 0;
74 44000 : if (nb_same_data > 20 && nb_same_data > to_remove)
75 : {
76 0 : to_remove = nb_same_data;
77 0 : arg_max = tmp;
78 : }
79 : precData = tmp;
80 : }
81 : // VR 13-4-23 : completion modulo 8 pour les données int lorsque leur nombre n'est pas un multiple de 8 (provoquait un crash dans le gpu)
82 11000 : while (j < ReadCacheCPU[tmpindex].size)
83 : {
84 : size_t idx = (size_t) ReadCacheCPU[tmpindex].cpt * (size_t) ReadCacheCPU[tmpindex].size + (size_t) j;
85 : ReadCacheCPU[tmpindex].datas_vec[idx] = 0;
86 : j++;
87 : }
88 11000 : ReadCacheCPU[tmpindex].cpt ++;
89 11000 : if (to_remove)
90 : {
91 0 : printf("%d : %d, arg_max : %d\n", ids_data[nb_data_treated], to_remove, arg_max);
92 : }
93 11000 : nb_data_treated++;
94 : }
95 1 : prec_id_data_file = current_id_data_file;
96 1 : if (verbose)
97 0 : printf("nb data already read : %d\n", nb_data_treated);
98 1 : id_to_treat++;
99 1 : fclose(fileopen);
100 : }
101 : }
102 :
103 : static int saveBinary(std::vector<to_insert_t> &to_insert, std::string filename_aux, std::string rootDirectory_unused, int max, int append, int verbose)
104 : {
105 : std::string completeFileNameAux = std::string(to_insert[0].dire) + "/" + filename_aux + ".dat";
106 :
107 : if (verbose)
108 : {
109 : std::cout << " completeFileNameAux : " << completeFileNameAux << std::endl;
110 : puts("");
111 : }
112 : FILE *file;
113 :
114 : // For debugging
115 : std::cout << "\n\nWriting binary file with size of data: " << sizeof(data) << "\n\n" << std::endl;
116 : // std::cout << "\n\nExample of saved coordinate" << std::endl;
117 :
118 : if (append)
119 : file = fopen(completeFileNameAux.c_str(), "ab");
120 : else
121 : file = fopen(completeFileNameAux.c_str(), "wb");
122 : if (file)
123 : {
124 : for (int i = 0; i < max; i++)
125 : {
126 : if (verbose)
127 : {
128 : if (i < 2)
129 : std::cout << "storing data %dth record, length " << i << " in " << to_insert[i].length << " " << completeFileNameAux << std::endl;
130 : }
131 : if(write(1, ".", 1))
132 : ;
133 : fwrite(&(to_insert[i].datas_vec[0]) , sizeof(data) * to_insert[i].length, 1, file);
134 : }
135 : if (max == 0)
136 : if(write(1, "/", 1))
137 : ;
138 : if(write(1, "\n", 1))
139 : ;
140 : if (verbose)
141 : puts("closing file.\n");
142 : fclose(file);
143 : if (verbose)
144 : puts("file closed.\n");
145 : return 0;
146 : }
147 : else
148 : {
149 : std::cout << "error opening " << completeFileNameAux << std::endl;
150 : return 1;
151 : }
152 : }
153 :
154 0 : static void addListIndexToFile(std::vector<to_insert_t> & to_insert, std::string id_index_ids, std::string filename_list_index_file_aux, int index, int verbose)
155 : {
156 : /**
157 : * addListIndexToFile : ajoute des photo_id et type au photo_list.index
158 : */
159 0 : int i;
160 0 : if (verbose)
161 0 : printf("in addListIndexToFile : %s\n", filename_list_index_file_aux.c_str());
162 0 : FILE * fd = fopen(filename_list_index_file_aux.c_str(), "a");
163 0 : if (verbose)
164 0 : printf("fd opened : %p\n", fd);
165 0 : if (fd == NULL)
166 : {
167 0 : if (verbose)
168 0 : printf("%s doesn't exists in addListIndexToFile creating one!\n", filename_list_index_file_aux.c_str());
169 0 : fd = fopen(filename_list_index_file_aux.c_str(), "w");
170 : }
171 0 : for (i = 0; i < index; i++)
172 : {
173 0 : if (verbose)
174 0 : printf("%d, ", to_insert[i].id);
175 0 : printf("%d, ", to_insert[i].id);
176 0 : fprintf(fd, "%d,%s,%d\n", to_insert[i].id, id_index_ids.c_str(), to_insert[i].type_data);
177 : }
178 0 : fclose(fd);
179 0 : }
180 :
181 0 : void save(std::string rootDirectory_loc, std::string completeFileName_aux, std::vector<to_insert_t> &to_insert,
182 : std::string velours_root, int max, int verbose, bool dontCheckDuplicateIdObject)
183 : {
184 : /**
185 : * save : enregistre les données de descripteur dans le dossier de descripteur cible ici rootDirectory_loc
186 : */
187 0 : clock_t start, stop;
188 0 : start = clock();
189 :
190 0 : clock_t debutp, finp;
191 0 : debutp = clock();
192 :
193 0 : if (verbose)
194 0 : std::cout << "Now inserting !\n";
195 0 : int append = 0;
196 0 : if (max > INSERT)
197 : {
198 0 : std::cout << "ERROR : Data to insert will be forgotten since there are too much data to insert in save() data\n";
199 : max = INSERT;
200 : }
201 :
202 0 : uint32_t file_count = 0;
203 : // OS specific part
204 : #ifdef _WIN64
205 : WIN32_FIND_DATA findFileData; // ok even if 64-bit system
206 : HANDLE hFind;
207 :
208 : std::string temp = std::string(to_insert[0].dire);
209 : temp += L"\\*"
210 : hFind = FindFirstFile(temp.c_str(), &findFileData);
211 : if (hFind == INVALID_HANDLE_VALUE) {
212 : std::cout << L"Error opening folder: " << temp << std::endl;
213 : return;
214 : }
215 :
216 : do {
217 : if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) {
218 : if (_tcsstr(findFileData.cFileName, _T(".dat"))) {
219 : file_count++;
220 : }
221 : if (verbose)
222 : std::cout << "file_count : " << file_count << ",";
223 : }
224 : } while (FindNextFile(hFind, &findFileData) != 0);
225 : FindClose(hFind);
226 : #else
227 0 : DIR * dirp;
228 0 : struct dirent * entry;
229 :
230 0 : std::string temp = std::string(to_insert[0].dire);
231 0 : dirp = opendir(temp.c_str());
232 0 : if (!dirp)
233 : {
234 0 : std::cout << "error opening folder " << to_insert[0].dire << "\n";
235 0 : return;
236 : }
237 0 : while ((entry = readdir(dirp)) != NULL)
238 : {
239 0 : if (entry->d_type == DT_REG)
240 : { /* If the entry is a regular file */
241 : // printf("Found regular file : %s \n", entry->d_name);
242 0 : if (strstr(entry->d_name, ".dat"))
243 0 : file_count++;
244 0 : if (verbose)
245 0 : std::cout << "file_count : " << file_count << ",";
246 : }
247 : }
248 0 : closedir(dirp);
249 : #endif
250 :
251 0 : if (file_count >= 1)
252 0 : file_count --;
253 0 : std::string tmp_str = std::string(to_insert[0].dire) + boost::lexical_cast<std::string>(file_count) + ".dat";
254 :
255 0 : char filename[50];
256 0 : struct stat st;
257 0 : int ret = stat(tmp_str.c_str(), &st); // return information about file tmp_str.c_str() ! it allows to have the size of the file in bytes
258 0 : if (ret != -1)
259 : {
260 0 : size_t size = st.st_size;
261 : // MG 11-5-16 : je verifie le type 2 c'est un peu debile, on a dit qu'on inserait tout les 1000
262 : // VR 11-5-16 : what the fuck ? python doit envoyer un commit insert tous les mille ? NON
263 : // Le premier [0] pour le premier type ...
264 0 : if (size < INSERT * to_insert[0].length)
265 : append = 1;
266 : else
267 0 : file_count ++;
268 : }
269 0 : if (verbose)
270 0 : std::cout << "file_count : " << file_count << ",\n";
271 0 : sprintf(filename, "%u", file_count);
272 0 : if (verbose)
273 0 : std::cout << "Now : remove duplicate\n";
274 0 : std::string list_id;
275 0 : for (int i = 0; i < max; i++)
276 : {
277 0 : if (i > 0)
278 0 : list_id += ",";
279 0 : list_id += boost::lexical_cast<std::string>(to_insert[i].id);
280 : }
281 :
282 : // VR 11-5-16 : last two arguments would be to delete loaded cache
283 0 : completeFileName_aux = std::string(to_insert[0].dire) + "/photo_list.index";
284 0 : std::vector<uint32_t> empty(0);
285 :
286 0 : finp = clock();
287 0 : long double elaps_time = (double) (finp - debutp) / CLOCKS_PER_SEC;
288 0 : std::cout << "detailed temps duree écriture debut : " << elaps_time << std::endl;
289 0 : debutp = clock();
290 :
291 0 : std::cout<<"checkDuplicate "<<dontCheckDuplicateIdObject<<std::endl;
292 :
293 0 : if (!dontCheckDuplicateIdObject)
294 : {
295 0 : deleteTokenFree(completeFileName_aux, list_id, 0, empty, to_insert[0].type_data, verbose);
296 : }
297 :
298 0 : finp = clock();
299 0 : elaps_time = (double) (finp - debutp) / CLOCKS_PER_SEC;
300 0 : std::cout << "detailed temps duree écriture deleteTokenFree : " << elaps_time << std::endl;
301 0 : debutp = clock();
302 :
303 0 : if (verbose)
304 0 : std::cout << " Now : addListIndexToFile\n";
305 0 : addListIndexToFile(to_insert, std::string(filename), completeFileName_aux, max, verbose);
306 :
307 0 : finp = clock();
308 0 : elaps_time = (double) (finp - debutp) / CLOCKS_PER_SEC;
309 0 : std::cout << "detailed temps duree écriture addListIndexToFile : " << elaps_time << std::endl;
310 0 : debutp = clock();
311 :
312 0 : int save = saveBinary(to_insert, std::string(filename), to_insert[0].dire, max, append, verbose);
313 0 : if (verbose)
314 : {
315 0 : if (save)
316 0 : std::cout << "we should do something here cause we had an error\n";
317 0 : std::cout << "End of save for binary data ?\n";
318 : }
319 :
320 0 : finp = clock();
321 0 : elaps_time = (double) (finp - debutp) / CLOCKS_PER_SEC;
322 0 : std::cout << "detailed temps duree écriture total saveBinary : " << elaps_time << std::endl;
323 :
324 0 : stop = clock();
325 0 : elaps_time = (double) (stop - start) / CLOCKS_PER_SEC;
326 0 : std::cout << "detailed temps duree écriture total save : " << elaps_time << std::endl;
327 : }
328 :
329 : #ifndef ONLY_CPU
330 : void testCUDA(cudaError_t error, const char *file, uint32_t line )
331 : {
332 : if (error != cudaSuccess) {
333 : printf("there is an error in file %s at line %d\n", file, line);
334 : printf("%s\n", cudaGetErrorString(error));
335 : exit(EXIT_FAILURE);
336 : }
337 : }
338 : #endif
339 :
340 0 : void cleanDataBinary(std::vector<uint32_t> & ids_data,
341 : std::vector<data> & data_to_load,
342 : int nb_data_in_file, std::string rootDirectory_aux,
343 : std::string completeFileName_aux, int dimension)
344 : {
345 0 : int verbose = 1;
346 :
347 0 : std::vector<uint32_t> ids_index, type_data, ids_data_tmp_vec;
348 : // c'est le type par defaut
349 0 : int descriptor_type = -1;
350 0 : std::cout << " before loadListIndexFile nb_data_in_file " << nb_data_in_file << std::endl;
351 :
352 0 : int nb_data_in_file_2 = loadListIndexFile(ids_data_tmp_vec, ids_index, type_data, completeFileName_aux, 0, descriptor_type, 0);
353 :
354 0 : std::cout << " ids_index.size() : " << ids_index.size() << " nb_data_in_file_2 " << nb_data_in_file_2 << std::endl;
355 :
356 : // I first loop over the data to have the greatest ids of data files
357 : int max_id_files = 0;
358 0 : for (int i = 0; i < std::min((int) nb_data_in_file_2, (int) ids_index.size()); i++)
359 : {
360 0 : if (ids_index[i] > max_id_files)
361 0 : max_id_files = ids_index[i];
362 : }
363 :
364 0 : printf("We have max_id_files : %d\n", max_id_files);
365 0 : printf("We have nb_data_in_file : %d\n", nb_data_in_file);
366 :
367 :
368 0 : std::vector<int> nb_data_per_file_before_cleaning_vec((max_id_files + 1));
369 0 : std::vector<int> nb_data_per_file_after_cleaning_vec((max_id_files + 1));
370 : // VR to MG ca c'est ce que fais la fonction bzero ?
371 0 : for (int id_treated = 0; id_treated <= max_id_files; id_treated++)
372 : {
373 0 : nb_data_per_file_before_cleaning_vec[id_treated] = 0;
374 0 : nb_data_per_file_after_cleaning_vec[id_treated] = 0;
375 : }
376 :
377 0 : printf("I will make a first loop to know how many data there are to be cleaned !\n");
378 0 : for (int i = 0; i < nb_data_in_file ; i++)
379 : {
380 0 : if (verbose > 100)
381 0 : printf("Parse one data : %d, %d, %d, %d\n", i, ids_data[i], ids_index[i], type_data[i]);
382 :
383 0 : nb_data_per_file_before_cleaning_vec[ids_index[i]]++;
384 0 : if (ids_data[i] != 0)
385 : {
386 0 : nb_data_per_file_after_cleaning_vec[ids_index[i]]++;
387 : }
388 : }
389 :
390 0 : for (int id_treated = 0; id_treated <= max_id_files; id_treated++)
391 : {
392 0 : printf(" id %d, had %d data and will have %d after cleaning !\n", id_treated, nb_data_per_file_before_cleaning_vec[id_treated], nb_data_per_file_after_cleaning_vec[id_treated]);
393 : }
394 :
395 0 : int nb_data_in_file_aux = nb_data_in_file;
396 0 : std::vector<to_insert_t> to_insert_vec;
397 : // loop over ids_data files, this enable to save one by one the data
398 0 : for (int id_treated = 0; id_treated <= max_id_files; id_treated++)
399 : {
400 0 : printf(" id_treated : %d\n", id_treated);
401 :
402 0 : int remove_this_amount_of_data = nb_data_per_file_before_cleaning_vec[id_treated] - nb_data_per_file_after_cleaning_vec[id_treated];
403 :
404 0 : nb_data_in_file_aux -= remove_this_amount_of_data;
405 :
406 : // for each data file set in to_insert only the data with non null id
407 0 : if (/* DISABLES CODE */ (true))
408 : {
409 0 : printf("Size to m alloc : %zu\n", nb_data_per_file_after_cleaning_vec[id_treated] * sizeof(to_insert_t));
410 0 : to_insert_vec.resize(nb_data_per_file_after_cleaning_vec[id_treated] + 1);
411 0 : to_insert_vec[0].dire = rootDirectory_aux;
412 0 : std::vector<uint32_t> new_ids_data_vec(nb_data_in_file_aux), new_ids_index_vec(nb_data_in_file_aux), new_type_data_vec(nb_data_in_file_aux);
413 :
414 0 : int id_all_photos = 0;
415 0 : int id_photo_data_binary = 0;
416 0 : for (int i = 0; i < nb_data_in_file ; i++)
417 : {
418 :
419 0 : if (ids_index[i] == id_treated && ids_data[i] != 0)
420 : {
421 0 : int length_data = dimension;
422 0 : to_insert_vec[id_photo_data_binary].id = ids_data[i];
423 0 : to_insert_vec[id_photo_data_binary].type_data = type_data[i];
424 0 : to_insert_vec[id_photo_data_binary].length = length_data;
425 :
426 : // TODO VR 13-2-16 to MG il faut que la taille soit une variable au moins du serveur si ce n'est pas possible d'avoir un serveur avec plusieurs type de données a la fois, VR 15-5-17 : je ne vois pas bien pourquoi
427 0 : to_insert_vec[id_photo_data_binary].datas_vec.resize(length_data);
428 0 : if (to_insert_vec[id_photo_data_binary].datas_vec.size() == 0)
429 : {
430 0 : puts("error m alloc in cleandatabinary");
431 0 : return;
432 : }
433 0 : std::vector<data> & data_to_roll = to_insert_vec[id_photo_data_binary].datas_vec;
434 0 : for (int k = 0; k < length_data; k++)
435 : {
436 0 : size_t len = (size_t)i * (size_t)length_data + k;
437 0 : data number = data_to_load[len];
438 0 : data_to_roll[k] = number;// data_to_load
439 : }
440 0 : id_photo_data_binary++;
441 0 : new_ids_data_vec[id_all_photos] = ids_data[i];
442 0 : new_ids_index_vec[id_all_photos] = ids_index[i];
443 0 : new_type_data_vec[id_all_photos] = type_data[i];
444 0 : id_all_photos++;
445 : }
446 0 : else if (ids_index[i] < id_treated && ids_data[i] != 0)
447 : {
448 0 : new_ids_data_vec[id_all_photos] = ids_data[i];
449 0 : new_ids_index_vec[id_all_photos] = ids_index[i];
450 0 : new_type_data_vec[id_all_photos] = type_data[i];
451 0 : id_all_photos++;
452 : }
453 0 : else if (ids_index[i] > id_treated)
454 : {
455 0 : new_ids_data_vec[id_all_photos] = ids_data[i];
456 0 : new_ids_index_vec[id_all_photos] = ids_index[i];
457 0 : new_type_data_vec[id_all_photos] = type_data[i];
458 0 : id_all_photos++;
459 : }
460 : }
461 : // save index file each time
462 0 : char filename[10];
463 0 : bzero(filename, 10);
464 0 : sprintf(filename, "%d", id_treated);
465 0 : printf("About to print in %s, id_all_photos : %d, id_photo_data_binary : %d nb_data_per_file_after_cleaning_vec[id_treated] : %d \n", filename, id_all_photos, id_photo_data_binary, nb_data_per_file_after_cleaning_vec[id_treated]);
466 0 : saveListIndexToFile(new_ids_data_vec, nb_data_in_file_aux, new_type_data_vec, std::string(completeFileName_aux), new_ids_index_vec, 1);
467 0 : std::string filename_aux = filename;
468 0 : int save = saveBinary(to_insert_vec, filename_aux, rootDirectory_aux, id_photo_data_binary, 0, 1);
469 0 : if (save)
470 0 : printf("we should do something here cause we had an error\n");
471 0 : printf("End of cleanDataBinary for %d ?\n", id_treated);
472 :
473 : } else {
474 : // on pourrait peut etre supprimer le fichier? quid si autre serveur deja loader ( delete, insert?)
475 : // VR 2-8-16 : ca c'est une remarque de MG ? je pense qu'on ne peut pas donc, mais chacun doit prendre un token avant modif
476 : // VR 6-11-16 : je pense en fait qu'on peut en fait
477 : std::string filename;
478 : filename += rootDirectory_aux.c_str();
479 : filename += "/";
480 : filename += boost::lexical_cast<std::string>(id_treated);
481 : FILE *fptw = fopen(&(filename.at(0)), "w");
482 : fprintf(fptw, "0");
483 : fclose(fptw);
484 : }
485 : }
486 : }
487 :
|