package sspr2;

import mpi.MPI;
import mpi.MPIException;

import java.io.FileWriter;
import java.io.IOException;

public class CTFirst {
    public static void main(String[] args) throws MPIException, IOException {
        try {
            MPI.Init(args);
            String name = MPI.Get_processor_name();
            int row = Integer.parseInt(args[args.length - 4]);
            int col = Integer.parseInt(args[args.length - 5]);
            if (row % 2 != 0 || col % 2 != 0) {
                System.out.println("Row or col not even");
                MPI.Finalize();
                return;
            }
            System.out.println("Row = " + row);
            System.out.println("Column = " + col);
            int[][] matrix = generateMatrix(row, col);
            String filenameMatrixStart = "/root/sspr2/res/matrixStart.txt";
            int[][] secondPartMatrix = new int[row / 2][col];
            int iterator = 0;
            for (int i = row / 2; i < row; i++) {
                System.arraycopy(matrix[i], 0, secondPartMatrix[iterator], 0, col);
                iterator++;
            }
            System.out.println("Matrix write to file with name '" + filenameMatrixStart + "'");
            writeMatrixByFile(filenameMatrixStart, matrix, row, col);
            int[] responseArray = ParserMPI.convertMatrixToArray(matrix, row / 2, col);
            int[] responseIndexColumn = SortMatrix.getIndexColumn(matrix, col);
            System.out.println(responseIndexColumn.length);
            int[] sizeMatrix = {row / 2, col};
            MPI.COMM_WORLD.Send(sizeMatrix, 0, 2, MPI.INT, 1, 0);
            System.out.println("Началась отправка массива с размерами с " + name + "процессора");
            MPI.COMM_WORLD.Send(responseArray, 0, responseArray.length, MPI.INT, 1, 0);
            System.out.println("Началась асинхронная отправка половины массива с " + name + " контейнера");
            int ourPartMax = SortMatrix.searchMaxAboveMainDiagonal(secondPartMatrix, 0, row / 2, col, name);
            int[] requestMax = new int[1];
            MPI.COMM_WORLD.Recv(requestMax, 0, 1, MPI.INT, 1, 0);
            int secondMax = requestMax[0];
            if(ourPartMax < secondMax) {
                System.out.println("Max element " + secondMax + " is greater than " + ourPartMax);
            }
            else {
                System.out.println("Max element " + ourPartMax + " is greater than " + secondMax);
            }
            MPI.Finalize();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static int[][] generateMatrix(int row, int col) {
        int[][] matrix = new int[row][col];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                matrix[i][j] = (int) (Math.random() * 100);
            }
        }
        return matrix;
    }

    private static void writeMatrixByFile(String filename, int[][] matrix, int row, int col) throws IOException {
        StringBuilder sb = new StringBuilder();
        FileWriter fw = new FileWriter(filename);
        fw.write(row + "\n");
        fw.write(col + "\n");
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                String s = Math.round(matrix[i][j] * 100.0) / 100.0 + "";
                sb.append(s).append(" ");
            }
            sb.append("\n");
        }
        fw.write(sb.toString());
        fw.close();
    }

}
