Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- using namespace std;
- int batch_size = 100;
- void read_rows(ifstream* input, char* matr_buf, int matr_size, int batch_no, int rows_to_read) {
- int start_pos = 8 + matr_size * batch_no * batch_size;
- input->seekg (start_pos, input->beg);
- input->read(matr_buf, matr_size * rows_to_read);
- }
- void read_columns(ifstream* input, char* matr_buf, int matr_size, int batch_no, int cols_to_read) {
- int matr2_start_pos = 16 + matr_size * matr_size;
- int start_pos = matr2_start_pos + batch_no * batch_size;
- for (int col_no = 0; col_no < matr_size; col_no++) {
- input->seekg (start_pos, input->beg);
- input->read(matr_buf + col_no * cols_to_read, cols_to_read);
- start_pos += matr_size;
- }
- }
- void multiply_blocks(char* rows_buf, char* cols_buf, char* res_buf, int matr_size, int rows, int cols) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- char elem = 0;
- for (int k = 0; k < matr_size; k++) {
- elem += rows_buf[i * matr_size + k] * cols_buf[k * cols + j];
- }
- res_buf[i * cols + j] = elem;
- }
- }
- }
- int main() {
- ifstream input ("input.bin", ios::in | ios::binary);
- ofstream output ("output.bin", ios::out | ios::binary);
- int N;
- input.read(reinterpret_cast<char *>(&N), sizeof(N));
- output.write(reinterpret_cast<char *>(&N), sizeof(N));
- output.write(reinterpret_cast<char *>(&N), sizeof(N));
- char* rows1_buf = new char[batch_size * N];
- char* cols2_buf = new char[N * batch_size];
- char* res_matr_buf = new char[batch_size * batch_size];
- int last_batch_size = N % batch_size;
- int num_of_batches = N / batch_size;
- if (last_batch_size > 0) {
- num_of_batches++;
- }
- for (int col_batch_no = 0; col_batch_no < num_of_batches; col_batch_no++) {
- int cols_to_read = batch_size;
- if (col_batch_no == num_of_batches - 1) {
- cols_to_read = last_batch_size;
- }
- read_columns(&input, cols2_buf, N, col_batch_no, cols_to_read);
- for (int row_batch_no = 0; row_batch_no < num_of_batches; row_batch_no++) {
- int rows_to_read = batch_size;
- if (row_batch_no == num_of_batches - 1) {
- rows_to_read = last_batch_size;
- }
- read_rows(&input, rows1_buf, N, row_batch_no, rows_to_read);
- multiply_blocks(rows1_buf, cols2_buf, res_matr_buf, N, rows_to_read, cols_to_read);
- int start_pos = 8 + row_batch_no * batch_size * N + col_batch_no * batch_size;
- for (int i = 0; i < rows_to_read; i++) {
- output.seekp(start_pos, output.beg);
- output.write(res_matr_buf + cols_to_read * i, cols_to_read);
- start_pos += N;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement