Parallelize write_transformation funcs

This commit is contained in:
Cory Balaton 2023-09-26 12:15:56 +02:00
parent b80220f3e7
commit 53b1bfcda7
No known key found for this signature in database
GPG Key ID: 3E5FCEBFD80F432B

View File

@ -4,7 +4,7 @@
* The program performs the Jacobi rotation method. * The program performs the Jacobi rotation method.
* The size of the matrix, and number of transformations * The size of the matrix, and number of transformations
* performed are written to file. * performed are written to file.
* Eigenvector correstonding to the 3 smallest eigenvalues * Eigenvector corresponding to the 3 smallest eigenvalues
* for matrices of size 6x6 and 100x100 are written to file. * for matrices of size 6x6 and 100x100 are written to file.
* *
* @author Cory Alexander Balaton (coryab) * @author Cory Alexander Balaton (coryab)
@ -13,7 +13,10 @@
*/ */
#include <cassert> #include <cassert>
#include <cmath> #include <cmath>
#include <ctime>
#include <iostream> #include <iostream>
#include <omp.h>
#include <ostream>
#include "utils.hpp" #include "utils.hpp"
#include "matrix.hpp" #include "matrix.hpp"
@ -28,6 +31,7 @@ void write_transformation_dense(int N)
ofile << "N,T" << std::endl; ofile << "N,T" << std::endl;
// Increase size of matrix, start at 5 to avoid logic_error of N=4 // Increase size of matrix, start at 5 to avoid logic_error of N=4
#pragma omp parallel for ordered schedule(static, 1)
for (int i = 5; i <= N; i++) { for (int i = 5; i <= N; i++) {
arma::mat A = arma::mat(i, i).randn(); arma::mat A = arma::mat(i, i).randn();
A = arma::symmatu(A); A = arma::symmatu(A);
@ -40,7 +44,10 @@ void write_transformation_dense(int N)
jacobi_eigensolver(A, 10e-14, eigval, eigvec, 100000, iters, converged); jacobi_eigensolver(A, 10e-14, eigval, eigvec, 100000, iters, converged);
// Write size, and number of iterations to file // Write size, and number of iterations to file
ofile << i << "," << iters << std::endl; #pragma omp ordered
{
ofile << i << "," << iters << std::endl;
}
} }
ofile.close(); ofile.close();
} }
@ -58,6 +65,7 @@ void write_transformation_tridiag(int N)
ofile << "N,T" << std::endl; ofile << "N,T" << std::endl;
// Increase size of matrix, start at 5 to avoid logic_error of N=4 // Increase size of matrix, start at 5 to avoid logic_error of N=4
#pragma omp parallel for ordered schedule(static, 1)
for (int i = 5; i <= N; i++) { for (int i = 5; i <= N; i++) {
h = 1. / (double) (i+1); h = 1. / (double) (i+1);
a = -1. / (h*h), d = 2. / (h*h); a = -1. / (h*h), d = 2. / (h*h);
@ -72,7 +80,10 @@ void write_transformation_tridiag(int N)
jacobi_eigensolver(A, 10e-14, eigval, eigvec, 100000, iters, converged); jacobi_eigensolver(A, 10e-14, eigval, eigvec, 100000, iters, converged);
// Write size, and number of iterations to file // Write size, and number of iterations to file
ofile << i << "," << iters << std::endl; #pragma omp ordered
{
ofile << i << "," << iters << std::endl;
}
} }
ofile.close(); ofile.close();
} }
@ -156,8 +167,8 @@ void write_eigenvec(int N)
int main() int main()
{ {
write_transformation_tridiag(100); write_transformation_tridiag(100);
write_transformation_dense(100); write_transformation_dense(100);
write_eigenvec(10); write_eigenvec(10);
write_eigenvec(100); write_eigenvec(100);
return 0; return 0;