daily pastebin goal
9%
SHARE
TWEET

Not my

a guest Jan 18th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <fcntl.h>
  2. #include <io.h>
  3. #include <stdio.h>
  4. #include <iostream>
  5. #include <stdlib.h>
  6. #include <sys/stat.h>
  7.  
  8.  
  9. /*
  10.  В общем идея примерно такая: делим левую матрицу на блоки из нескольких строк ("режем по строкам"),
  11.  а вторую - на блоки из нескольких столбцов ("режем по столбцам").
  12.  Так как матрицы записаны построчно, эффективнее прочитать один раз столбцы, а для них
  13.  каждый раз считать строки.
  14.  
  15.  Ну вот так примерно и происходит:
  16.  считываем блок столбцов правой матрицы (read_columns),
  17.  считываем блок строк левой матрицы (read_rows),
  18.  перемножаем (multiply_addition),
  19.  и записываем результат в нужное место (write_block)
  20.  
  21.  Честно говоря не могу сказать, почему block_size = 100)
  22.  Я когда сдавал, просто подбирал
  23.  */
  24. const int block_size = 100;
  25.  
  26. void multiply_addition(char *result_matrix_buffer, char *first_matrix_buffer, char *second_matrix_buffer,
  27.                        int first_matrix_row_size, int second_matrix_column_size, int matrix_size) {
  28.    
  29.     for (int row_cunter = 0; row_cunter < first_matrix_row_size; ++row_cunter) {
  30.         for (int column_counter = 0; column_counter < second_matrix_column_size; ++column_counter) {
  31.             char sum = 0;
  32.             for (int k = 0; k < matrix_size; ++k) {
  33.                 sum += first_matrix_buffer[row_cunter*  matrix_size + k] *
  34.                 second_matrix_buffer[k * second_matrix_column_size + column_counter];
  35.             }
  36.             result_matrix_buffer[row_cunter* second_matrix_column_size + column_counter] = sum;
  37.         }
  38.     }
  39. }
  40.  
  41. void read_rows(int input_file_desctriptor, char* buffer, int matrix_size, int row_block_counter, int row_number) {
  42.    
  43.     int start_position = 8 + row_block_counter * matrix_size *  block_size;
  44.     _lseek(input_file_desctriptor, start_position, 0);
  45.     _read(input_file_desctriptor, buffer, matrix_size * row_number);
  46. }
  47.  
  48. void read_columns(int input_file_desctriptor, char* second_matrix_buffer, int matrix_size, int column_block_counter,
  49.                   int second_matrix_column_number, int offset) {
  50.    
  51.     int start_position = offset + column_block_counter * block_size;
  52.     for (int column_count = 0; column_count < matrix_size; ++column_count) {
  53.         _lseek(input_file_desctriptor, start_position, 0);
  54.         _read(input_file_desctriptor, second_matrix_buffer + column_count * second_matrix_column_number, second_matrix_column_number);
  55.         start_position += matrix_size;
  56.     }
  57. }
  58.  
  59. void write_block(int output_file_desctriptor, char* result_buffer, int matrix_size, int row_block_counter, int column_block_counter,
  60.                  int result_matrix_row_number, int result_matrix_column_number) {
  61.     int start_position = 8 + row_block_counter* block_size * matrix_size + column_block_counter * block_size;
  62.     for (int i = 0; i < result_matrix_row_number; ++i) {
  63.         _lseek(output_file_desctriptor, start_position, 0);
  64.         _write(output_file_desctriptor, result_buffer + i * result_matrix_column_number, result_matrix_column_number);
  65.         start_position += matrix_size;
  66.     }
  67. }
  68. void multiply(int input_file_desctriptor, int output_file_dectriptor) {
  69.    
  70.    
  71.     char size_buf[4];
  72.     _read(input_file_desctriptor, size_buf, 4);
  73.     const int matrix_size = *reinterpret_cast<int*>(size_buf);
  74.    
  75.     _write(output_file_dectriptor, size_buf, 4);
  76.     _write(output_file_dectriptor, size_buf, 4);
  77.    
  78.     char * first_matrix_buffer = new char[block_size * matrix_size];
  79.     char * second_matrix_buffer = new char[matrix_size * block_size];
  80.     char * result_buffer = new char[block_size * block_size];
  81.    
  82.    
  83.     const int block_number = matrix_size / block_size;
  84.     const int last_block_size = matrix_size % block_size;
  85.     const int iterations_number = block_number + (last_block_size > 0 ? 1 : 0);
  86.    
  87.    
  88.    
  89.     for (int column_block_counter = 0; column_block_counter < iterations_number; ++column_block_counter) {
  90.        
  91.         int result_matrix_column_number = (column_block_counter < block_number) ? block_size : last_block_size;
  92.        
  93.         read_columns(input_file_desctriptor, second_matrix_buffer, matrix_size,
  94.                      column_block_counter, result_matrix_column_number, 16 + matrix_size * matrix_size);
  95.        
  96.        
  97.         for (int row_block_counter = 0; row_block_counter < iterations_number; ++row_block_counter) {
  98.            
  99.             int result_matrix_row_number = (row_block_counter < block_number)? block_size: last_block_size;
  100.            
  101.             read_rows(input_file_desctriptor, first_matrix_buffer, matrix_size, row_block_counter, result_matrix_row_number);
  102.            
  103.             multiply_addition(result_buffer, first_matrix_buffer, second_matrix_buffer,
  104.                               result_matrix_row_number, result_matrix_column_number, matrix_size);
  105.            
  106.             write_block(output_file_dectriptor, result_buffer, matrix_size, row_block_counter, column_block_counter,
  107.                         result_matrix_row_number, result_matrix_column_number);
  108.         }
  109.     }
  110.     delete[] first_matrix_buffer;
  111.     delete[] second_matrix_buffer;
  112.     delete[] result_buffer;
  113. }
  114.  
  115. int main() {
  116.    
  117.     const int input_fd = _open("input.bin", _O_RDONLY | _O_BINARY | _O_RANDOM, _S_IREAD);
  118.     const int output_file_dectriptor =
  119.     _open("output.bin", _O_CREAT | _O_TRUNC | _O_WRONLY | _O_BINARY | _O_RANDOM, _S_IWRITE);
  120.    
  121.     multiply(input_fd, output_file_dectriptor);
  122.    
  123.     _close(output_file_dectriptor);
  124.     _close(input_fd);
  125.     return 0;
  126. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top