Advertisement
Guest User

My

a guest
Jan 18th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int batch_size = 100;
  8.  
  9.  
  10. void read_rows(ifstream* input, char* matr_buf, int matr_size, int batch_no, int rows_to_read) {
  11. int start_pos = 8 + matr_size * batch_no * batch_size;
  12. input->seekg (start_pos, input->beg);
  13. input->read(matr_buf, matr_size * rows_to_read);
  14. }
  15.  
  16. void read_columns(ifstream* input, char* matr_buf, int matr_size, int batch_no, int cols_to_read) {
  17. int matr2_start_pos = 16 + matr_size * matr_size;
  18. int start_pos = matr2_start_pos + batch_no * batch_size;
  19. for (int col_no = 0; col_no < matr_size; col_no++) {
  20. input->seekg (start_pos, input->beg);
  21. input->read(matr_buf + col_no * cols_to_read, cols_to_read);
  22. start_pos += matr_size;
  23. }
  24. }
  25.  
  26. void multiply_blocks(char* rows_buf, char* cols_buf, char* res_buf, int matr_size, int rows, int cols) {
  27. for (int i = 0; i < rows; i++) {
  28. for (int j = 0; j < cols; j++) {
  29. char elem = 0;
  30. for (int k = 0; k < matr_size; k++) {
  31. elem += rows_buf[i * matr_size + k] * cols_buf[k * cols + j];
  32. }
  33. res_buf[i * cols + j] = elem;
  34. }
  35. }
  36. }
  37.  
  38. int main() {
  39. ifstream input ("input.bin", ios::in | ios::binary);
  40.  
  41. ofstream output ("output.bin", ios::out | ios::binary);
  42.  
  43. int N;
  44. input.read(reinterpret_cast<char *>(&N), sizeof(N));
  45.  
  46. output.write(reinterpret_cast<char *>(&N), sizeof(N));
  47. output.write(reinterpret_cast<char *>(&N), sizeof(N));
  48.  
  49.  
  50. char* rows1_buf = new char[batch_size * N];
  51. char* cols2_buf = new char[N * batch_size];
  52. char* res_matr_buf = new char[batch_size * batch_size];
  53.  
  54. int last_batch_size = N % batch_size;
  55. int num_of_batches = N / batch_size;
  56. if (last_batch_size > 0) {
  57. num_of_batches++;
  58. }
  59.  
  60. for (int col_batch_no = 0; col_batch_no < num_of_batches; col_batch_no++) {
  61. int cols_to_read = batch_size;
  62. if (col_batch_no == num_of_batches - 1) {
  63. cols_to_read = last_batch_size;
  64. }
  65. read_columns(&input, cols2_buf, N, col_batch_no, cols_to_read);
  66.  
  67. for (int row_batch_no = 0; row_batch_no < num_of_batches; row_batch_no++) {
  68. int rows_to_read = batch_size;
  69. if (row_batch_no == num_of_batches - 1) {
  70. rows_to_read = last_batch_size;
  71. }
  72. read_rows(&input, rows1_buf, N, row_batch_no, rows_to_read);
  73. multiply_blocks(rows1_buf, cols2_buf, res_matr_buf, N, rows_to_read, cols_to_read);
  74.  
  75. int start_pos = 8 + row_batch_no * batch_size * N + col_batch_no * batch_size;
  76. for (int i = 0; i < rows_to_read; i++) {
  77. output.seekp(start_pos, output.beg);
  78. output.write(res_matr_buf + cols_to_read * i, cols_to_read);
  79. start_pos += N;
  80.  
  81. }
  82. }
  83. }
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement