1
In short: How can I split matrix operations and pool threads with a fixed amount of threads using the amount of user cores? I’m cracking my head, looking at the gringo SOF, but nothing that clears my mind. I added the libraries java.util.concurrent.ExecutorService
and java.util.concurrent.Executors
. That’s what I’ve done so far. Main class:
public static void main(String[] args) throws IOException, InterruptedException
{
MatrixesManipulations matrixes = new MatrixesManipulations();
Scanner in = new Scanner(System.in);
System.out.print("De o valor para dimensão das duas matrizes quadradas: ");
int n = in.nextInt();
int[][] matrix1 = matrixes.matrixConstructor(n);
int[][] matrix2 = matrixes.matrixConstructor(n);
//Processadores do usuario para fazer a pool
int cores = Runtime.getRuntime().availableProcessors();
//Ainda não utilizado
ExecutorService pool = Executors.newFixedThreadPool(cores);
Thread matrix3 = new Thread(new MatrixMultiplicationThread(matrix1, matrix2, n));
matrix3.start();
matrix3.join();
////Outputs das matrizes
matrixes.printConsole(matrix1, n, 1);
matrixes.printConsole(matrix2, n, 2);
matrixes.matrixesToFile(matrix1, matrix2, matrix3, n);
}
Class MatrixManipulations
:
public class MatrixesManipulations{
public int[][] matrixConstructor(int n)
{
int[][] matrix = new int[n][n];
Random rand = new Random();
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
{
Integer r = rand.nextInt()% 1000;
matrix[i][j] = Math.abs(r);
}
}
return matrix;
}
public void printConsole(int[][] matrix, int n, int x)
{
synchronized (this)
{
if(x == 1)
{
System.out.println("A primeira matriz gerada de tamanho " + n + " foi:");
for (int i = 0; i < n; i++)
{
System.out.print("[");
for (int j = 0; j < n; j++)
{
System.out.print(matrix[i][j] + " ");
}
System.out.println("]");
}
System.out.println();
}
if(x == 2)
{
System.out.println("A segunda matriz gerada de tamanho " + n + " foi:");
for (int i = 0; i < n; i++)
{
System.out.print("[");
for (int j = 0; j < n; j++)
{
System.out.print(matrix[i][j] + " ");
}
System.out.println("]");
}
System.out.print("\n");
}
if(x == 3)
{
System.out.println("A matriz resultante gerada da multiplicação das 2 matrizes quadradas foi:");
for (int i = 0; i < n; i++)
{
System.out.print("[");
for (int j = 0; j < n; j++)
{
System.out.print(matrix[i][j] + " ");
}
System.out.println("]");
}
System.out.print("\n");
}
}
}
public void matrixesToFile(int[][] matrix1, int[][] matrix2, int[][] matrix3, int n) throws IOException
{
synchronized(this)
{
FileWriter arqMatrix = new FileWriter("Matrizes.txt");
PrintWriter gravarArq = new PrintWriter(arqMatrix);
gravarArq.println("Primeira matriz gerada:");
for (int i = 0; i < n; i++)
{
gravarArq.print("[");
for (int j = 0; j < n; j++)
{
gravarArq.print(matrix1[i][j] + " ");
}
gravarArq.print("]\n");
}
gravarArq.println();
gravarArq.println("Segunda matriz gerada:");
for (int i = 0; i < n; i++)
{
gravarArq.print("[");
for (int j = 0; j < n; j++)
{
gravarArq.print(matrix2[i][j] + " ");
}
gravarArq.print("]\n");
}
gravarArq.println();
//int[][] matrix3 = matrixMultiplication(matrix1, matrix2, n);
gravarArq.println("Matriz resultante da multiplicação:");
for (int i = 0; i < n; i++)
{
gravarArq.print("[");
for (int j = 0; j < n; j++)
{
gravarArq.print(matrix3[i][j] + " ");
}
gravarArq.print("]\n");
}
gravarArq.println();
arqMatrix.close();
System.out.println("Foi gerado um arquivo 'Matrizes.txt' no mesmo local do projeto.\n");
}
}
public int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2, int n)
{
synchronized(this)
{
int[][] matrix3 = new int[n][n];
for(int i = 0 ; i < n ; i++)
{
for(int j = 0 ; j < n ; j++)
{
for(int k = 0 ; k < n ; k++)
{
matrix3[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return matrix3;
}
}
}
And finally, the thread:
public class MatrixMultiplicationThread implements Runnable {
MatrixesManipulations matrixThread = new MatrixesManipulations();
int[][] matrix1;
int[][] matrix2;
int n;
public MatrixMultiplicationThread(int[][] matrix1, int[][] matrix2, int n)
{
this.matrix1 = matrix1;
this.matrix2 = matrix2;
this.n = n;
}
@Override
public void run()
{
int[][] matrix3 = matrixThread.matrixMultiplication(matrix1, matrix2, n);
matrixThread.printConsole(matrix3, n, 3);
}
}
The intention is that the thread, at first, performs the operation of the matrix. But beyond that, I want it to divide for each thread to generate the user line of the outputs. Or at least (which I think is easier) that they divide and perform sum and multiplication operation, it doesn’t even have to be the pool. Another problem I’m having (which I’ll possibly also do is generating a thread) is to generate the file. Since the thread does not give a "return" but generates the matrix, I would like to know a way to pass the method public void matrixesToFile(int[][] matrix1, int[][] matrix2, int[][] matrix3, int n) throws IOException
in main
(which is when I use this method to unify all outputs and record in a .txt
.
Let me get this straight, because your explanation is a little fuzzy. What you want is to run the traditional matrix multiplication algorithm in parallel and at the end save both the original and the resulting matrices to a file. That’s it?
– Victor Stafusa
That’s right! I’m sorry if I messed around a little bit, it’s just that I’m so stressed because I’ve been trying to see something for a week and I’m not getting anywhere haha
– Vinnicius Stein