Guest User

Untitled

a guest
Nov 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3. #include <cstdlib>
  4. #define SMALLER runs[aux].buffer[runs[aux].count]
  5.  
  6. using namespace std;
  7.  
  8. typedef unsigned long long int ll;
  9.  
  10. const int numberRuns = 20;
  11. const long long totalSize = (long long)20*(1<<27); //20GB
  12. const long bufferSize = (1<<26); //512MB para cada run
  13. const long runSize = totalSize/numberRuns; //1GB
  14. const int numberBuffers = runSize/bufferSize;
  15. int bufferPlace = 0;
  16. long size;
  17. struct Run {
  18. ifstream file;
  19. int buffCount;
  20. ll buffer[bufferSize];
  21. bool valid;
  22. long count;
  23. };
  24.  
  25. Run runs[numberBuffers];
  26.  
  27. void loadBuffer (int run) {
  28. if(runs[run].file.tellg()<totalSize){
  29. long aux = totalSize - (runs[run].file.tellg() + size);
  30. if (aux < size) aux = size - aux;
  31. runs[run].file.read((char*)&runs[run].buffer, (size - aux)*sizeof(ll));
  32. }
  33. }
  34.  
  35. int main () {
  36. bool time = 1;
  37. ofstream output;
  38. size=bufferSize;
  39. while(size<totalSize){
  40. bufferPlace=0;
  41. if(time){
  42. output.open("file2.bin", ios::out | ios::binary);
  43. for (long i = 0; i < numberBuffers; ++i) {
  44.  
  45. runs[i].file.open("file1.bin", ios::in | ios::binary);
  46. runs[i].file.seekg(((i+bufferPlace)*runSize)*sizeof(ll));
  47. runs[i].buffCount = 0;
  48. runs[i].valid = true;
  49. runs[i].count = 0;
  50. }
  51. time = 0;
  52. }
  53. else{
  54. output.open("file1.bin", ios::out | ios::binary);
  55. for (long i = 0; i < numberBuffers; ++i) {
  56.  
  57. runs[i].file.open("file2.bin", ios::in | ios::binary);
  58. runs[i].file.seekg(((i+bufferPlace)*runSize)*sizeof(ll));
  59. runs[i].buffCount = 0;
  60. runs[i].valid = true;
  61. }
  62. time = 1;
  63. }
  64. for (int i = 0; i < numberBuffers; ++i) {
  65. loadBuffer(i);
  66. }
  67. for (long p = 0; p < totalSize;){
  68. for (long k = 0; k < (runSize*numberBuffers); ++k, ++p) {
  69. int aux;
  70. for(aux = 0; !runs[aux].valid; ++aux);
  71. printf("teste");
  72. for (int j = 0; j < numberBuffers; ++j) {
  73. if (runs[j].valid) {
  74. if (runs[j].buffer[runs[j].count] < SMALLER) {
  75. aux = j;
  76. }
  77. }
  78. }
  79. printf("%d\n", SMALLER);
  80. output.write((char*)&SMALLER, sizeof(ll));
  81. ++runs[aux].count;
  82. if (runs[aux].count == bufferSize) {
  83. if (++runs[aux].buffCount == (numberBuffers)) runs[aux].valid = false;
  84. if (runs[aux].valid) loadBuffer(aux);
  85. }
  86. }
  87. bufferPlace+=numberBuffers;
  88. for (int i = 0; i < numberBuffers; ++i) runs[i].file.close();
  89. output.close();
  90. }
  91. size<<=1;
  92. }
  93. return 0;
  94. }
Add Comment
Please, Sign In to add comment